diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index d39866b424..1d74966fe0 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -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)); + *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 */ /** diff --git a/tests/jerry/string-prototype-charcodeat.js b/tests/jerry/string-prototype-charcodeat.js new file mode 100644 index 0000000000..97fc80786e --- /dev/null +++ b/tests/jerry/string-prototype-charcodeat.js @@ -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);