Skip to content

Implement String.prototype.charAt() #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you, please, clarify why step 4 is performed before step 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 */

/**
Expand Down
103 changes: 103 additions & 0 deletions tests/jerry/string-prototype-charat.js
Original file line number Diff line number Diff line change
@@ -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");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test case for boolean arguments also.

// 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");