Skip to content

Commit de5d77f

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 de5d77f

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 booleans
59+
assert("hello world!".charAt(true) === "e");
60+
61+
assert("hello world!".charAt(false) === "h");
62+
63+
// check this is undefined
64+
try {
65+
String.prototype.charAt.call(undefined);
66+
assert(false);
67+
} catch(e) {
68+
assert(e instanceof TypeError);
69+
}
70+
71+
// check this is null
72+
try {
73+
String.prototype.charAt.call(null);
74+
assert(false);
75+
} catch(e) {
76+
assert(e instanceof TypeError);
77+
}
78+
79+
// check coercible - undefined
80+
try {
81+
assert(true.charAt() === "");
82+
assert(false);
83+
} catch (e) {
84+
assert(e instanceof TypeError);
85+
}
86+
87+
// check coercible - null
88+
try {
89+
assert(String.prototype.charAt.call(null, 0) === "");
90+
assert(false);
91+
} catch (e) {
92+
assert(e instanceof TypeError);
93+
}
94+
95+
// check coercible - Boolean
96+
assert(String.prototype.charAt.call(true, 1) === "r");
97+
98+
// check coercible - Object
99+
var test_object = {firstName:"John", lastName:"Doe"};
100+
assert(String.prototype.charAt.call(test_object, 1) === "o");
101+
102+
// check coercible - Number
103+
assert(String.prototype.charAt.call(123, 2) === "3");

0 commit comments

Comments
 (0)