Skip to content

Commit c7a47c1

Browse files
committed
Implement String.prototype.charCodeAt()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 7b3042f commit c7a47c1

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,59 @@ static ecma_completion_value_t
169169
ecma_builtin_string_prototype_object_char_code_at (ecma_value_t this_arg, /**< this argument */
170170
ecma_value_t arg) /**< routine's argument */
171171
{
172-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
172+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
173+
174+
/* 1 */
175+
ECMA_TRY_CATCH (check_coercible_val,
176+
ecma_op_check_object_coercible (this_arg),
177+
ret_value);
178+
179+
/* 2 */
180+
ECMA_TRY_CATCH (to_string_val,
181+
ecma_op_to_string (this_arg),
182+
ret_value);
183+
184+
/* 3 */
185+
ECMA_OP_TO_NUMBER_TRY_CATCH (index_num,
186+
arg,
187+
ret_value);
188+
189+
/* 4 */
190+
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val);
191+
const ecma_length_t len = ecma_string_get_length (original_string_p);
192+
193+
ecma_number_t *ret_num_p = ecma_alloc_number ();
194+
195+
/* 5 */
196+
// When index_num is NaN, then the first two comparisons are false
197+
if (index_num < 0 || index_num >= len || (ecma_number_is_nan (index_num) && !len))
198+
{
199+
*ret_num_p = ecma_number_make_nan ();
200+
}
201+
else
202+
{
203+
/* 6 */
204+
/*
205+
* String length is currently uit32_t, but index_num may be bigger,
206+
* ToInteger performs floor, while ToUInt32 performs modulo 2^32,
207+
* hence after the check 0 <= index_num < len we assume to_uint32 can be used.
208+
* We assume to_uint32 (NaN) is 0.
209+
*/
210+
JERRY_ASSERT (ecma_number_is_nan (index_num) || ecma_number_to_uint32 (index_num) == ecma_number_trunc (index_num));
211+
212+
ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num));
213+
*ret_num_p = ecma_uint32_to_number (new_ecma_char);
214+
}
215+
216+
ecma_value_t new_value = ecma_make_number_value (ret_num_p);
217+
ret_value = ecma_make_normal_completion_value (new_value);
218+
219+
ECMA_OP_TO_NUMBER_FINALIZE (index_num);
220+
221+
ECMA_FINALIZE (to_string_val);
222+
ECMA_FINALIZE (check_coercible_val);
223+
224+
return ret_value;
173225
} /* ecma_builtin_string_prototype_object_char_code_at */
174226

175227
/**
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// check properties
17+
assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.charCodeAt, 'length').writable === false);
22+
23+
assert(String.prototype.charCodeAt.length === 1);
24+
25+
// check empty string
26+
assert(isNaN(String.prototype.charCodeAt.call(new String())));
27+
28+
// check Object with NaN pos
29+
assert(String.prototype.charCodeAt.call({}) === 91);
30+
31+
// simple checks
32+
assert("hello world!".charCodeAt(0) === 104);
33+
34+
assert("hello world!".charCodeAt(1) === 101);
35+
36+
assert("HELLO WORLD".charCodeAt(10) === 68);
37+
38+
// check +-Inf
39+
assert(isNaN("hello world!".charCodeAt(-Infinity)));
40+
41+
assert(isNaN("hello world!".charCodeAt(Infinity)));
42+
43+
assert("hello world!".charCodeAt(11) === 33);
44+
45+
assert(isNaN("hello world!".charCodeAt(12)));
46+
47+
// check negative
48+
assert(isNaN("hello world!".charCodeAt(-1)));
49+
50+
assert(isNaN("hello world!".charCodeAt(-9999999)));
51+
52+
assert("hello world!".charCodeAt(-0) === 104);
53+
54+
// check undefined
55+
assert("hello world!".charCodeAt(undefined) === 104);
56+
57+
// check booleans
58+
assert("hello world!".charCodeAt(true) === 101);
59+
60+
assert("hello world!".charCodeAt(false) === 104);
61+
62+
// check index above uint32_t
63+
assert(isNaN("hello world!".charCodeAt(4294967299)));
64+
65+
// check coercible - undefined
66+
try {
67+
assert(isNaN(String.prototype.charCodeAt.call(undefined)));
68+
assert(false);
69+
} catch (e) {
70+
assert(e instanceof TypeError);
71+
}
72+
73+
// check coercible - null
74+
try {
75+
assert(isNaN(String.prototype.charCodeAt.call(null, 0)));
76+
assert(false);
77+
} catch (e) {
78+
assert(e instanceof TypeError);
79+
}
80+
81+
// check coercible - Boolean
82+
assert(String.prototype.charCodeAt.call(true, 1) === 114);
83+
assert(String.prototype.charCodeAt.call(true) === 116);
84+
85+
// check coercible - Object
86+
var test_object = {firstName:"John", lastName:"Doe"};
87+
assert(String.prototype.charCodeAt.call(test_object, 1) === 111);
88+
89+
// check coercible - Number
90+
assert(String.prototype.charCodeAt.call(123, 2) === 51);

0 commit comments

Comments
 (0)