diff --git a/jerry-core/config.h b/jerry-core/config.h index 72611064a..c877cb4ef 100644 --- a/jerry-core/config.h +++ b/jerry-core/config.h @@ -154,6 +154,7 @@ // #define CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN #define CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN #define CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN +#define CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN #endif /* CONFIG_ECMA_COMPACT_PROFILE */ /** diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index 7f0490b41..316dd7e92 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -2557,6 +2557,76 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu return ret_value; } /* ecma_builtin_string_prototype_object_trim */ +#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN + +/** + * The String.prototype object's 'substr' routine + * + * See also: + * ECMA-262 v5, B.2.3 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_string_prototype_object_substr (ecma_value_t this_arg, /**< this argument */ + ecma_value_t start, /**< routine's first argument */ + ecma_value_t length) /**< routine's second argument */ +{ + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + ECMA_TRY_CATCH (check_coercible_val, + ecma_op_check_object_coercible (this_arg), + ret_value); + + /* 1. */ + ECMA_TRY_CATCH (to_string_val, ecma_op_to_string (this_arg), ret_value); + ecma_string_t *this_string_p = ecma_get_string_from_value (to_string_val); + + /* 2. */ + ECMA_OP_TO_NUMBER_TRY_CATCH (start_num, start, ret_value); + if (ecma_number_is_nan (start_num)) + { + start_num = 0; + } + + /* 3. */ + ecma_number_t length_num = ecma_number_make_infinity (false); + if (!ecma_is_value_undefined (length)) + { + ECMA_OP_TO_NUMBER_TRY_CATCH (len, length, ret_value); + length_num = ecma_number_is_nan (len) ? 0 : len; + + ECMA_OP_TO_NUMBER_FINALIZE (len); + } + + if (ecma_is_completion_value_empty (ret_value)) + { + /* 4. */ + ecma_number_t this_len = (ecma_number_t) ecma_string_get_length (this_string_p); + + /* 5. */ + ecma_number_t from_num = (start_num < 0) ? JERRY_MAX (this_len + start_num, 0) : start_num; + uint32_t from = ecma_builtin_helper_string_index_normalize (from_num, ecma_number_to_uint32 (this_len), true); + + /* 6-7. */ + ecma_number_t to_num = JERRY_MAX (JERRY_MIN (JERRY_MAX (length_num, 0), this_len - from_num), 0); + uint32_t to = from + ecma_builtin_helper_string_index_normalize (to_num, ecma_number_to_uint32 (this_len), true); + + /* 8. */ + ecma_string_t *new_str_p = ecma_string_substr (this_string_p, from, to); + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); + } + + ECMA_OP_TO_NUMBER_FINALIZE (start_num); + ECMA_FINALIZE (to_string_val); + ECMA_FINALIZE (check_coercible_val); + + return ret_value; +} /* ecma_builtin_string_prototype_object_substr */ + +#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */ + /** * @} * @} diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.inc.h b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.inc.h index 08b1a332a..ed177aa88 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.inc.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.inc.h @@ -82,6 +82,10 @@ ROUTINE (LIT_MAGIC_STRING_TO_UPPER_CASE_UL, ecma_builtin_string_prototype_object ROUTINE (LIT_MAGIC_STRING_TO_LOCALE_UPPER_CASE_UL, ecma_builtin_string_prototype_object_to_locale_upper_case, 0, 0) ROUTINE (LIT_MAGIC_STRING_TRIM, ecma_builtin_string_prototype_object_trim, 0, 0) +#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN +ROUTINE (LIT_MAGIC_STRING_SUBSTR, ecma_builtin_string_prototype_object_substr, 2, 2) +#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */ + #undef OBJECT_ID #undef SIMPLE_VALUE #undef NUMBER_VALUE diff --git a/jerry-core/lit/lit-magic-strings.inc.h b/jerry-core/lit/lit-magic-strings.inc.h index 5f9f94062..7208a6154 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -151,6 +151,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MATCH, "match") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_REPLACE, "replace") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SEARCH, "search") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPLIT, "split") +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SUBSTR, "substr") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SUBSTRING, "substring") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_LOWER_CASE_UL, "toLowerCase") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_LOCALE_LOWER_CASE_UL, "toLocaleLowerCase") diff --git a/tests/jerry/string-prototype-substr.js b/tests/jerry/string-prototype-substr.js new file mode 100644 index 000000000..3eb380bc4 --- /dev/null +++ b/tests/jerry/string-prototype-substr.js @@ -0,0 +1,139 @@ +// 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.substr, 'length').configurable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.substr, 'length').enumerable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.substr, 'length').writable === false); + +assert(String.prototype.substr.length === 2); + +assert(String.prototype.substr.call(new String()) === ""); + +assert(String.prototype.substr.call({}) === "[object Object]"); + +// check this is undefined +try { + String.prototype.substr.call(undefined); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// check this is null +try { + String.prototype.substr.call(null); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// simple checks +assert("Hello world!".substr(0, 11) === "Hello world"); + +assert("Hello world!".substr(11, 0) === ""); + +assert("Hello world!".substr(0, 12) === "Hello world!"); + +assert("Hello world!".substr(12, 0) === ""); +// check NaN +assert("Hello world!".substr(NaN, 12) === "Hello world!"); + +// check NaN +assert("Hello world!".substr(2, NaN) === ""); + +// check end undefined +assert("Hello world!".substr(2, undefined) === "llo world!"); + +// check negative +assert("Hello world!".substr(-1,8) === "!"); + +// check negative +assert("Hello\tworld!".substr(5,-8) === ""); + +// check negative +assert("Hello world!".substr(-1,-8) === ""); + +// check ranges +assert("Hello world!".substr(-1,10000) === "!"); + +assert("Hello world!".substr(10000,1000000) === ""); + +assert("Hello world!".substr(100000,1) === ""); + +// check both undefined +assert("Hello world!".substr(undefined, undefined) === "Hello world!"); + +var undef_var; +assert("Hello world!".substr(undef_var, undef_var) === "Hello world!"); + +// check integer conversion +assert("Hello world!".substr(undefined, 5) === "Hello"); + +assert("Hello world!".substr(undefined, "bar") === ""); + +assert("Hello world!".substr(2, true) === "l"); + +assert("Hello world!".substr(2, false) === ""); + +assert("Hello world!".substr(5, obj) === " world!"); + +// check other objects +var obj = { substr : String.prototype.substr } + +obj.toString = function() { + return "Iam"; +} +assert(obj.substr(0,1) === "I"); + +obj.toString = function() { + throw new ReferenceError ("foo"); +}; + +try { + assert(obj.substr(100000,1)); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// check coercible - undefined +try { + assert(true.substr() === ""); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +// check coercible - null +try { + assert(String.prototype.substr.call(null, 0, 1) === ""); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +// check coercible - Boolean +assert(String.prototype.substr.call(true, 0, 1) === "t"); + +// check coercible - Object +var test_object = {firstName:"John", lastName:"Doe"}; +assert(String.prototype.substr.call(test_object, 0, 7) === "[object"); + +// check coercible - Number +assert(String.prototype.substr.call(123, 0, 3) === "123");