Skip to content

Commit cb1d234

Browse files
committed
Implement String.prototype.charCodeAt()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent b414329 commit cb1d234

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,50 @@ 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+
if (index_num < 0 || index_num >= len || !len)
197+
{
198+
*ret_num_p = ecma_number_make_nan ();
199+
}
200+
else
201+
{
202+
/* 6 */
203+
ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num));
204+
*ret_num_p = ecma_uint32_to_number (new_ecma_char);
205+
}
206+
207+
ecma_value_t new_value = ecma_make_number_value (ret_num_p);
208+
ret_value = ecma_make_normal_completion_value (new_value);
209+
210+
ECMA_OP_TO_NUMBER_FINALIZE (index_num);
211+
212+
ECMA_FINALIZE (to_string_val);
213+
ECMA_FINALIZE (check_coercible_val);
214+
215+
return ret_value;
173216
} /* ecma_builtin_string_prototype_object_char_code_at */
174217

175218
/**
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 NaN
29+
assert("hello world!".charCodeAt(0) === 104);
30+
31+
assert("HELLO WORLD".charCodeAt(10) === 68);
32+
33+
// check Object
34+
assert(String.prototype.charCodeAt.call({}) === 91);
35+
36+
37+
// simple checks
38+
assert("hello world!".charCodeAt(0) === 104);
39+
40+
assert("hello world!".charCodeAt(1) === 101);
41+
42+
// check +-Inf
43+
assert(isNaN("hello world!".charCodeAt(-Infinity)));
44+
45+
assert(isNaN("hello world!".charCodeAt(Infinity)));
46+
47+
assert("hello world!".charCodeAt(11) === 33);
48+
49+
assert(isNaN("hello world!".charCodeAt(12)));
50+
51+
// check negative
52+
assert(isNaN("hello world!".charCodeAt(-1)));
53+
54+
assert(isNaN("hello world!".charCodeAt(-9999999)));
55+
56+
assert("hello world!".charCodeAt(-0) === 104);
57+
58+
// check undefined
59+
assert("hello world!".charCodeAt(undefined) === 104);
60+
61+
// check booleans
62+
assert("hello world!".charCodeAt(true) === 101);
63+
64+
assert("hello world!".charCodeAt(false) === 104);
65+
66+
// check this is undefined
67+
try {
68+
String.prototype.charCodeAt.call(undefined);
69+
assert(false);
70+
} catch(e) {
71+
assert(e instanceof TypeError);
72+
}
73+
74+
// check this is null
75+
try {
76+
String.prototype.charCodeAt.call(null);
77+
assert(false);
78+
} catch(e) {
79+
assert(e instanceof TypeError);
80+
}
81+
82+
// check coercible - undefined
83+
try {
84+
assert(true.charCodeAt());
85+
assert(false);
86+
} catch (e) {
87+
assert(e instanceof TypeError);
88+
}
89+
90+
// check coercible - null
91+
try {
92+
assert(isNaN(String.prototype.charCodeAt.call(null, 0)));
93+
assert(false);
94+
} catch (e) {
95+
assert(e instanceof TypeError);
96+
}
97+
98+
// check coercible - Boolean
99+
assert(String.prototype.charCodeAt.call(true, 1) === 114);
100+
101+
// check coercible - Object
102+
var test_object = {firstName:"John", lastName:"Doe"};
103+
assert(String.prototype.charCodeAt.call(test_object, 1) === 111);
104+
105+
// check coercible - Number
106+
assert(String.prototype.charCodeAt.call(123, 2) === 51);

0 commit comments

Comments
 (0)