Skip to content

Implement String.prototype.slice() #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 135 additions & 1 deletion jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -258,7 +259,140 @@ ecma_builtin_string_prototype_object_slice (ecma_value_t this_arg, /**< this arg
ecma_value_t arg1, /**< routine's first argument */
ecma_value_t arg2) /**< routine's second argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

/* 1. */
ECMA_TRY_CATCH (check_coercible_val,
ecma_op_check_object_coercible (this_arg),
ret_value);

/* 2. */
ECMA_TRY_CATCH (to_string_val,
ecma_op_to_string (this_arg),
ret_value);

/* 3. */
ecma_string_t *get_string_val = ecma_get_string_from_value (to_string_val);

JERRY_ASSERT (ecma_string_get_length (get_string_val) >= 0);

const uint32_t len = (uint32_t) ecma_string_get_length (get_string_val);

/* 4. */
uint32_t start = 0, end = len;

ECMA_OP_TO_NUMBER_TRY_CATCH (start_num,
arg1,
ret_value);

if (!ecma_number_is_nan (start_num))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that the case is covered inside of ecma_number_to_int32.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there are some cases, when it is not enough.
This NaN-check is needed for the next Infinity-check, because if the given ecma_number_t is NaN an assert triggered.
We need the Infinity-check, because the ecma_number_to_int32 converts Infinity value to 0, but in case of positive Infinity we should clamp to the string's length.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Initially, I've confused ToInt32 with ToInteger.

{

if (ecma_number_is_infinity (start_num))
{
start = ecma_number_is_negative (start_num) ? 0 : len;
}
else
{
const int int_start = ecma_number_to_int32 (start_num);

if (int_start < 0)
{
const uint32_t start_abs = (uint32_t) - int_start;
start = start_abs > len ? 0 : len - start_abs;
}
else
{
start = (uint32_t) int_start;

if (start > len)
{
start = len;
}
}
}
}
else
{
start = 0;
}

/* 5. */
if (ecma_is_value_undefined (arg2))
{
end = len;
}
else
{
ECMA_OP_TO_NUMBER_TRY_CATCH (end_num,
arg2,
ret_value);

if (!ecma_number_is_nan (end_num))
{

if (ecma_number_is_infinity (end_num))
{
end = ecma_number_is_negative (end_num) ? 0 : len;
}
else
{
const int32_t int_end = ecma_number_to_int32 (end_num);

if (int_end < 0)
{
const uint32_t end_abs = (uint32_t) - int_end;
end = end_abs > len ? 0 : len - end_abs;
}
else
{
end = (uint32_t) int_end;

if (end > len)
{
end = len;
}
}
}
}
else
{
end = 0;
}

ECMA_OP_TO_NUMBER_FINALIZE (end_num);
}

ECMA_OP_TO_NUMBER_FINALIZE (start_num);

JERRY_ASSERT (start <= len && end <= len);

if (ecma_is_completion_value_empty (ret_value))
{
/* 8. */
const uint32_t span = (start > end) ? 0 : end - start;
const uint32_t new_str_size = (uint32_t) sizeof (ecma_char_t) * (span + 1);

MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_str_size, ecma_char_t);

/* 9. */
for (uint32_t idx = 0; idx < span; idx++)
{
new_str_buffer[idx] = ecma_string_get_char_at_pos (get_string_val, start + idx);
}

new_str_buffer[span] = '\0';
ecma_string_t* new_str = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer);

ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str));

MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer);
}

ECMA_FINALIZE (to_string_val);
ECMA_FINALIZE (check_coercible_val);

return ret_value;
} /* ecma_builtin_string_prototype_object_slice */

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/jerry/string_prototype_slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var str = "universe";
var res;

res = str.slice();
assert (res === "universe");

res = str.slice(1, 6);
assert (res === "niver");

res = str.slice("a", "-4");
assert (res === "univ");

res = str.slice(-5);
assert (res === "verse");

res = str.slice(-12, undefined);
assert (res === "universe");

res = str.slice(undefined, -20);
assert (res === "");

res = str.slice(undefined, undefined);
assert (res === "universe");

res = str.slice(Infinity, NaN);
assert (res === "");

res = str.slice(-Infinity, Infinity);
assert (res === "universe");

res = str.slice(NaN, -Infinity);
assert (res === "");

res = str.slice(false, true);
assert (res === "u");

var x;
res = str.slice(x, x);
assert (res === "universe");

var obj = {y: "foo"};
var arr = [x, x];
res = str.slice(obj, arr);
assert (res === "");