Skip to content
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

Implement String.prototype.charCodeAt() #352

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,59 @@ static ecma_completion_value_t
ecma_builtin_string_prototype_object_char_code_at (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
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_OP_TO_NUMBER_TRY_CATCH (index_num,
arg,
ret_value);

/* 4 */
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val);
const ecma_length_t len = ecma_string_get_length (original_string_p);

ecma_number_t *ret_num_p = ecma_alloc_number ();

/* 5 */
// When index_num is NaN, then the first two comparisons are false
if (index_num < 0 || index_num >= len || (ecma_number_is_nan (index_num) && !len))
{
*ret_num_p = ecma_number_make_nan ();
}
else
{
/* 6 */
/*
* String length is currently uit32_t, but index_num may be bigger,
* ToInteger performs floor, while ToUInt32 performs modulo 2^32,
* hence after the check 0 <= index_num < len we assume to_uint32 can be used.
* We assume to_uint32 (NaN) is 0.
*/
JERRY_ASSERT (ecma_number_is_nan (index_num) || ecma_number_to_uint32 (index_num) == ecma_number_trunc (index_num));

ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num));
Copy link
Contributor

Choose a reason for hiding this comment

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

While actual length of strings, currently, can't exceed uint32_t range, value of index_num can be bigger. ToInteger is not equivalent to ToUInt32, as ToUInt32 performs "modulo 2^32" operation on its argument, and ToInteger performs floor.

The code would work correctly, as index_num doesn't exceed len, as checked above.

Could you, please, add an assertion that ecma_number_to_uint32 (index_num) == ecma_number_trunc (index_num) and add comment, describing why ToInteger and ToUInt32 are equivalent in the case?

*ret_num_p = ecma_uint32_to_number (new_ecma_char);
}

ecma_value_t new_value = ecma_make_number_value (ret_num_p);
ret_value = ecma_make_normal_completion_value (new_value);

ECMA_OP_TO_NUMBER_FINALIZE (index_num);

ECMA_FINALIZE (to_string_val);
ECMA_FINALIZE (check_coercible_val);

return ret_value;
} /* ecma_builtin_string_prototype_object_char_code_at */

/**
Expand Down
90 changes: 90 additions & 0 deletions tests/jerry/string-prototype-charcodeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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.

// check properties
assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').configurable === false);

assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').enumerable === false);

assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').writable === false);

assert(String.prototype.charCodeAt.length === 1);

// check empty string
assert(isNaN(String.prototype.charCodeAt.call(new String())));

// check Object with NaN pos
assert(String.prototype.charCodeAt.call({}) === 91);

// simple checks
assert("hello world!".charCodeAt(0) === 104);

assert("hello world!".charCodeAt(1) === 101);

assert("HELLO WORLD".charCodeAt(10) === 68);

// check +-Inf
assert(isNaN("hello world!".charCodeAt(-Infinity)));

assert(isNaN("hello world!".charCodeAt(Infinity)));

assert("hello world!".charCodeAt(11) === 33);

assert(isNaN("hello world!".charCodeAt(12)));

// check negative
assert(isNaN("hello world!".charCodeAt(-1)));

assert(isNaN("hello world!".charCodeAt(-9999999)));

assert("hello world!".charCodeAt(-0) === 104);

// check undefined
assert("hello world!".charCodeAt(undefined) === 104);

// check booleans
assert("hello world!".charCodeAt(true) === 101);

assert("hello world!".charCodeAt(false) === 104);

// check index above uint32_t
assert(isNaN("hello world!".charCodeAt(4294967299)));

// check coercible - undefined
try {
assert(isNaN(String.prototype.charCodeAt.call(undefined)));
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// check coercible - null
try {
assert(isNaN(String.prototype.charCodeAt.call(null, 0)));
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// check coercible - Boolean
assert(String.prototype.charCodeAt.call(true, 1) === 114);
assert(String.prototype.charCodeAt.call(true) === 116);

// check coercible - Object
var test_object = {firstName:"John", lastName:"Doe"};
assert(String.prototype.charCodeAt.call(test_object, 1) === 111);

// check coercible - Number
assert(String.prototype.charCodeAt.call(123, 2) === 51);