Skip to content

Commit 02bf718

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

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,47 @@ static ecma_completion_value_t
112112
ecma_builtin_string_prototype_object_char_at (ecma_value_t this_arg, /**< this argument */
113113
ecma_value_t arg) /**< routine's argument */
114114
{
115-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
115+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
116+
117+
/* 1 */
118+
ECMA_TRY_CATCH (check_coercible_val,
119+
ecma_op_check_object_coercible (this_arg),
120+
ret_value);
121+
122+
/* 2 */
123+
ECMA_TRY_CATCH (to_string_val,
124+
ecma_op_to_string (this_arg),
125+
ret_value);
126+
127+
/* 4 */
128+
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val);
129+
const ecma_length_t len = ecma_string_get_length (original_string_p);
130+
131+
/* 3 */
132+
ECMA_OP_TO_NUMBER_TRY_CATCH (index_num,
133+
arg,
134+
ret_value);
135+
136+
/* 5 */
137+
if (index_num < 0 || index_num >= len || !len)
138+
{
139+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (
140+
ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY)));
141+
}
142+
else
143+
{
144+
/* 6 */
145+
ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num));
146+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (
147+
ecma_new_ecma_string_from_code_unit (new_ecma_char)));
148+
}
149+
150+
ECMA_OP_TO_NUMBER_FINALIZE (index_num);
151+
152+
ECMA_FINALIZE (to_string_val);
153+
ECMA_FINALIZE (check_coercible_val);
154+
155+
return ret_value;
116156
} /* ecma_builtin_string_prototype_object_char_at */
117157

118158
/**
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.charAt, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').writable === false);
22+
23+
assert(String.prototype.charAt.length === 1);
24+
25+
// check empty string
26+
assert(String.prototype.charAt.call(new String()) === "");
27+
28+
// check NaN
29+
assert("hello world!".charAt(NaN) === "h");
30+
31+
// check Object
32+
assert(String.prototype.charAt.call({}) === "[");
33+
34+
// simple checks
35+
assert("hello world!".charAt(0) === "h");
36+
37+
assert("hello world!".charAt(1) === "e");
38+
39+
// check +-Inf
40+
assert("hello world!".charAt(-Infinity) === "");
41+
42+
assert("hello world!".charAt(Infinity) === "");
43+
44+
assert("hello world!".charAt(11) === "!");
45+
46+
assert("hello world!".charAt(12) === "");
47+
48+
// check negative
49+
assert("hello world!".charAt(-1) === "");
50+
51+
assert("hello world!".charAt(-9999999) === "");
52+
53+
assert("hello world!".charAt(-0) === "h");
54+
55+
// check undefined
56+
assert("hello world!".charAt(undefined) === "h");
57+
58+
// check this is undefined
59+
try {
60+
String.prototype.charAt.call(undefined);
61+
assert(false);
62+
} catch(e) {
63+
assert(e instanceof TypeError);
64+
}
65+
66+
// check this is null
67+
try {
68+
String.prototype.charAt.call(null);
69+
assert(false);
70+
} catch(e) {
71+
assert(e instanceof TypeError);
72+
}
73+
74+
// check coercible - undefined
75+
try {
76+
assert(true.charAt() === "");
77+
assert(false);
78+
} catch (e) {
79+
assert(e instanceof TypeError);
80+
}
81+
82+
// check coercible - null
83+
try {
84+
assert(String.prototype.charAt.call(null, 0) === "");
85+
assert(false);
86+
} catch (e) {
87+
assert(e instanceof TypeError);
88+
}
89+
90+
// check coercible - Boolean
91+
assert(String.prototype.charAt.call(true, 1) === "r");
92+
93+
// check coercible - Object
94+
var test_object = {firstName:"John", lastName:"Doe"};
95+
assert(String.prototype.charAt.call(test_object, 1) === "o");
96+
97+
// check coercible - Number
98+
assert(String.prototype.charAt.call(123, 2) === "3");

0 commit comments

Comments
 (0)