diff --git a/jerry-core/ecma/base/ecma-helpers-string.cpp b/jerry-core/ecma/base/ecma-helpers-string.cpp index 78a59d1ea..538df18a2 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.cpp +++ b/jerry-core/ecma/base/ecma-helpers-string.cpp @@ -27,6 +27,7 @@ #include "ecma-lcache.h" #include "jrt.h" #include "jrt-libc-includes.h" +#include "lit-char-helpers.h" #include "lit-magic-strings.h" #include "serializer.h" #include "vm.h" @@ -1879,6 +1880,81 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin JERRY_UNREACHABLE (); } /* ecma_string_substr */ +/** + * Trim leading and trailing whitespace characters from string. + * + * @return trimmed ecma string + */ +ecma_string_t * +ecma_string_trim (const ecma_string_t *string_p) /**< pointer to an ecma string */ +{ + ecma_string_t *ret_string_p; + + lit_utf8_size_t buffer_size = ecma_string_get_size (string_p); + + if (buffer_size > 0) + { + MEM_DEFINE_LOCAL_ARRAY (utf8_str_p, buffer_size, lit_utf8_byte_t); + ecma_string_to_utf8_string (string_p, utf8_str_p, (ssize_t) buffer_size); + + lit_utf8_iterator_t front = lit_utf8_iterator_create (utf8_str_p, buffer_size); + + lit_utf8_iterator_t back = lit_utf8_iterator_create (utf8_str_p, buffer_size); + lit_utf8_iterator_seek_eos (&back); + + lit_utf8_iterator_pos_t start = lit_utf8_iterator_get_pos (&back); + lit_utf8_iterator_pos_t end = lit_utf8_iterator_get_pos (&front); + + ecma_char_t current; + + /* Trim front. */ + while (!lit_utf8_iterator_is_eos (&front)) + { + current = lit_utf8_iterator_read_next (&front); + if (!lit_char_is_white_space (current) + && !lit_char_is_line_terminator (current)) + { + lit_utf8_iterator_decr (&front); + start = lit_utf8_iterator_get_pos (&front); + break; + } + } + + /* Trim back. */ + while (!lit_utf8_iterator_is_bos (&back)) + { + current = lit_utf8_iterator_read_prev (&back); + if (!lit_char_is_white_space (current) + && !lit_char_is_line_terminator (current)) + { + lit_utf8_iterator_incr (&back); + end = lit_utf8_iterator_get_pos (&back); + break; + } + } + + /* Construct new string. */ + if (end.offset > start.offset) + { + ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_str_p + start.offset, + (lit_utf8_size_t) (end.offset - start.offset)); + } + else + { + ret_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); + } + + MEM_FINALIZE_LOCAL_ARRAY (utf8_str_p); + } + else + { + ret_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); + } + + return ret_string_p; + +} /* ecma_string_trim */ + /** * @} * @} diff --git a/jerry-core/ecma/base/ecma-helpers.h b/jerry-core/ecma/base/ecma-helpers.h index 0046ba38f..eed78de66 100644 --- a/jerry-core/ecma/base/ecma-helpers.h +++ b/jerry-core/ecma/base/ecma-helpers.h @@ -147,6 +147,7 @@ extern bool ecma_is_ex_string_magic (const ecma_string_t *string_p, lit_magic_st extern lit_string_hash_t ecma_string_hash (const ecma_string_t *string_p); extern ecma_string_t *ecma_string_substr (const ecma_string_t *string_p, ecma_length_t, ecma_length_t); +extern ecma_string_t *ecma_string_trim (const ecma_string_t *string_p); /* ecma-helpers-number.cpp */ extern const ecma_number_t ecma_number_relative_eps; diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp index b071fc7b7..8a8df5005 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp @@ -190,6 +190,7 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, lit_utf8_iterator_t iter = lit_utf8_iterator_create (start_p, str_size); ecma_length_t last_separator = lit_utf8_iterator_get_index (&iter); ecma_length_t end_position; + ecma_string_t *param_str_p; while (!lit_utf8_iterator_is_eos (&iter)) { @@ -200,7 +201,9 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, lit_utf8_iterator_decr (&iter); end_position = lit_utf8_iterator_get_index (&iter); - string_params_p[params_count] = ecma_string_substr (arguments_str_p, last_separator, end_position); + param_str_p = ecma_string_substr (arguments_str_p, last_separator, end_position); + string_params_p[params_count] = ecma_string_trim (param_str_p); + ecma_deref_ecma_string (param_str_p); lit_utf8_iterator_incr (&iter); last_separator = lit_utf8_iterator_get_index (&iter); @@ -210,7 +213,9 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, } end_position = lit_utf8_string_length (start_p, str_size); - string_params_p[params_count] = ecma_string_substr (arguments_str_p, last_separator, end_position); + param_str_p = ecma_string_substr (arguments_str_p, last_separator, end_position); + string_params_p[params_count] = ecma_string_trim (param_str_p); + ecma_deref_ecma_string (param_str_p); params_count++; MEM_FINALIZE_LOCAL_ARRAY (start_p); diff --git a/tests/jerry/function-construct.js b/tests/jerry/function-construct.js index 757fd13f4..fed3df1b9 100644 --- a/tests/jerry/function-construct.js +++ b/tests/jerry/function-construct.js @@ -71,6 +71,15 @@ assert (f (1,2,3,4) === 10); f = new Function ("a" , "b", "c,d", "return a + b + c + d;"); assert (f (1,2,3,4) === 10); +var f = new Function (" a\t , b", "\u0020c", "return a + b + c;"); +assert (f (1,2,3) === 6); + +f = new Function ("a, \n b \u0020", "c \t, d\n", "return a + b + c + d;"); +assert (f (1,2,3,4) === 10); + +f = new Function (" a\t" , "\nb ", " \u0020c , d ", "return a + b + c + d;"); +assert (f (1,2,3,4) === 10); + try { new Function ({