From a0a09eb0c73ed9ea9ae019db61ff0160cf69f1bf Mon Sep 17 00:00:00 2001 From: Laszlo Vidacs Date: Mon, 15 Jun 2015 15:37:27 +0200 Subject: [PATCH 1/2] Implement String.prototype.trim() JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com --- .../ecma-builtin-string-prototype.cpp | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) 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 d2d139c2ee..f3b3fe047b 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "ecma-alloc.h" #include "ecma-builtins.h" #include "ecma-conversion.h" @@ -554,7 +555,62 @@ ecma_builtin_string_prototype_object_to_locale_upper_case (ecma_value_t this_arg static ecma_completion_value_t ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_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); + + ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); + JERRY_ASSERT (ecma_string_get_length (original_string_p) >= 0); + + /* 3 */ + const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p); + + uint32_t prefix = 0, postfix = 0; + uint32_t new_len = 0; + + while (prefix < len && + ecma_is_completion_value_empty (ret_value) && + isspace (ecma_string_get_char_at_pos (original_string_p, prefix))) + { + prefix++; + } + + while (postfix < len && + ecma_is_completion_value_empty (ret_value) && + isspace (ecma_string_get_char_at_pos (original_string_p, len-postfix-1))) + { + postfix++; + } + + new_len = len - prefix - postfix; + + MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t); + + for (uint32_t idx = 0; idx < new_len; ++idx) + { + new_str_buffer[idx] = ecma_string_get_char_at_pos (original_string_p, idx + prefix); + } + + new_str_buffer[new_len] = '\0'; + ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer); + + /* 4 */ + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); + + MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer); + + ECMA_FINALIZE (to_string_val); + ECMA_FINALIZE (check_coercible_val); + + return ret_value; } /* ecma_builtin_string_prototype_object_trim */ /** From 58f49ca29f6e1184bac64bfe6fa4721e96232324 Mon Sep 17 00:00:00 2001 From: Laszlo Vidacs Date: Tue, 16 Jun 2015 15:02:37 +0200 Subject: [PATCH 2/2] String.prototype.trim() fixes and tests. JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com --- .../ecma-builtin-string-prototype.cpp | 22 +++--- tests/jerry/string-prototype-trim.js | 69 +++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 tests/jerry/string-prototype-trim.js 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 f3b3fe047b..83f14a23f5 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include "ecma-alloc.h" #include "ecma-builtins.h" #include "ecma-conversion.h" @@ -26,6 +25,7 @@ #include "ecma-string-object.h" #include "ecma-try-catch-macro.h" #include "jrt.h" +#include "jrt-libc-includes.h" #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_STRING_BUILTIN @@ -573,30 +573,32 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu /* 3 */ const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p); + /* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */ + uint32_t zt_str_size = (uint32_t) sizeof (ecma_char_t) * (len + 1); + ecma_char_t *original_zt_str_p = (ecma_char_t*) mem_heap_alloc_block (zt_str_size, + MEM_HEAP_ALLOC_SHORT_TERM); + ecma_string_to_zt_string (original_string_p, original_zt_str_p, (ssize_t) zt_str_size); + uint32_t prefix = 0, postfix = 0; uint32_t new_len = 0; - while (prefix < len && - ecma_is_completion_value_empty (ret_value) && - isspace (ecma_string_get_char_at_pos (original_string_p, prefix))) + while (prefix < len && isspace (original_zt_str_p[prefix])) { prefix++; } - while (postfix < len && - ecma_is_completion_value_empty (ret_value) && - isspace (ecma_string_get_char_at_pos (original_string_p, len-postfix-1))) + while (postfix < len - prefix && isspace (original_zt_str_p[len - postfix - 1])) { postfix++; } - new_len = len - prefix - postfix; + new_len = prefix < len ? len - prefix - postfix : 0; MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t); for (uint32_t idx = 0; idx < new_len; ++idx) { - new_str_buffer[idx] = ecma_string_get_char_at_pos (original_string_p, idx + prefix); + new_str_buffer[idx] = original_zt_str_p[idx + prefix]; } new_str_buffer[new_len] = '\0'; @@ -607,6 +609,8 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer); + mem_heap_free_block (original_zt_str_p); + ECMA_FINALIZE (to_string_val); ECMA_FINALIZE (check_coercible_val); diff --git a/tests/jerry/string-prototype-trim.js b/tests/jerry/string-prototype-trim.js new file mode 100644 index 0000000000..41e6e4b3d0 --- /dev/null +++ b/tests/jerry/string-prototype-trim.js @@ -0,0 +1,69 @@ +// 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.trim, 'length').configurable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.trim, 'length').enumerable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.trim, 'length').writable === false); + +assert(String.prototype.trim.length === 0); + +// check this value +assert(String.prototype.trim.call(new String()) === ""); + +assert(String.prototype.trim.call({}) === "[object Object]"); + +// check undefined +try { + String.prototype.trim.call(undefined); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// check null +try { + String.prototype.trim.call(null); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// simple checks +assert(" hello world".trim() === "hello world"); + +assert("hello world ".trim() === "hello world"); + +assert(" hello world ".trim() === "hello world"); + +assert("\t hello world\n".trim() === "hello world"); + +assert("\t\n hello world\t \n ".trim() === "hello world"); + +assert("hello world\n \t\t".trim() === "hello world"); + +assert(" hello world \\ ".trim() === "hello world \\"); + +assert("**hello world**".trim() === "**hello world**"); + +assert(" \t \n".trim() === ""); + +assert(" ".trim() === ""); + +assert("".trim() === ""); + +// FIXME: add unicode tests when unicode support available