Implement String.prototype.charAt()

JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
This commit is contained in:
Laszlo Vidacs
2015-07-06 19:08:38 +02:00
committed by Peter Gal
parent 01c4325588
commit 632618da99
2 changed files with 144 additions and 1 deletions
@@ -112,7 +112,47 @@ static ecma_completion_value_t
ecma_builtin_string_prototype_object_char_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);
/* 5 */
if (index_num < 0 || index_num >= len || !len)
{
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (
ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY)));
}
else
{
/* 6 */
ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num));
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (
ecma_new_ecma_string_from_code_unit (new_ecma_char)));
}
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_at */
/**
+103
View File
@@ -0,0 +1,103 @@
// 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.charAt, 'length').configurable === false);
assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').enumerable === false);
assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').writable === false);
assert(String.prototype.charAt.length === 1);
// check empty string
assert(String.prototype.charAt.call(new String()) === "");
// check NaN
assert("hello world!".charAt(NaN) === "h");
// check Object
assert(String.prototype.charAt.call({}) === "[");
// simple checks
assert("hello world!".charAt(0) === "h");
assert("hello world!".charAt(1) === "e");
// check +-Inf
assert("hello world!".charAt(-Infinity) === "");
assert("hello world!".charAt(Infinity) === "");
assert("hello world!".charAt(11) === "!");
assert("hello world!".charAt(12) === "");
// check negative
assert("hello world!".charAt(-1) === "");
assert("hello world!".charAt(-9999999) === "");
assert("hello world!".charAt(-0) === "h");
// check undefined
assert("hello world!".charAt(undefined) === "h");
// check booleans
assert("hello world!".charAt(true) === "e");
assert("hello world!".charAt(false) === "h");
// check this is undefined
try {
String.prototype.charAt.call(undefined);
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
// check this is null
try {
String.prototype.charAt.call(null);
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
// check coercible - undefined
try {
assert(true.charAt() === "");
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// check coercible - null
try {
assert(String.prototype.charAt.call(null, 0) === "");
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// check coercible - Boolean
assert(String.prototype.charAt.call(true, 1) === "r");
// check coercible - Object
var test_object = {firstName:"John", lastName:"Doe"};
assert(String.prototype.charAt.call(test_object, 1) === "o");
// check coercible - Number
assert(String.prototype.charAt.call(123, 2) === "3");