Skip to content

Trim whitespace from argument names in Function constructor #539

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

Merged
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
76 changes: 76 additions & 0 deletions jerry-core/ecma/base/ecma-helpers-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */

/**
* @}
* @}
Expand Down
1 change: 1 addition & 0 deletions jerry-core/ecma/base/ecma-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/jerry/function-construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({
Expand Down