Implement String.prototype.search, and some minor regexp refactors.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg@inf.u-szeged.hu
This commit is contained in:
Zoltan Herczeg
2015-08-04 05:07:44 -07:00
parent f39a294bc6
commit 0a1b6eb4c7
6 changed files with 209 additions and 65 deletions
@@ -67,30 +67,13 @@ ecma_builtin_regexp_prototype_exec (ecma_value_t this_arg, /**< this argument */
{
ECMA_TRY_CATCH (obj_this, ecma_op_to_object (this_arg), ret_value);
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
ecma_property_t *bytecode_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE);
re_bytecode_t *bytecode_p = ECMA_GET_POINTER (re_bytecode_t, bytecode_prop_p->u.internal_property.value);
ECMA_TRY_CATCH (input_str_value,
ecma_op_to_string (arg),
ret_value);
ecma_string_t *input_str_p = ecma_get_string_from_value (input_str_value);
/* Convert ecma_String_t *to regexp_bytecode_t* */
lit_utf8_size_t input_str_size = ecma_string_get_size (input_str_p);
MEM_DEFINE_LOCAL_ARRAY (input_utf8_buffer_p, input_str_size, lit_utf8_byte_t);
ecma_string_to_utf8_string (input_str_p, input_utf8_buffer_p, (ssize_t) input_str_size);
lit_utf8_iterator_t iter = lit_utf8_iterator_create (input_utf8_buffer_p, input_str_size);
ret_value = ecma_regexp_exec_helper (obj_p, bytecode_p, &iter);
MEM_FINALIZE_LOCAL_ARRAY (input_utf8_buffer_p);
ret_value = ecma_regexp_exec_helper (obj_this, input_str_value, false);
ECMA_FINALIZE (input_str_value);
ECMA_FINALIZE (obj_this);
}
@@ -31,6 +31,10 @@
#include "jrt-libc-includes.h"
#include "lit-char-helpers.h"
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
#include "ecma-regexp-object.h"
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_STRING_BUILTIN
#define ECMA_BUILTINS_INTERNAL
@@ -560,15 +564,10 @@ ecma_builtin_string_prototype_object_match (ecma_value_t this_arg, /**< this arg
JERRY_ASSERT (ecma_is_value_boolean (global_value));
ecma_value_t exec_arguments[1] = { this_to_string_value };
if (!ecma_is_value_true (global_value))
{
/* 7. */
ret_value = ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
regexp_value,
exec_arguments,
1);
ret_value = ecma_regexp_exec_helper (regexp_value, this_to_string_value, false);
}
else
{
@@ -608,10 +607,7 @@ ecma_builtin_string_prototype_object_match (ecma_value_t this_arg, /**< this arg
{
/* 8.f.i. */
ECMA_TRY_CATCH (exec_value,
ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
regexp_value,
exec_arguments,
1),
ecma_regexp_exec_helper (regexp_value, this_to_string_value, false),
ret_value);
if (ecma_is_value_null (exec_value))
@@ -829,13 +825,10 @@ ecma_builtin_string_prototype_object_replace_match (ecma_builtin_replace_search_
if (context_p->is_regexp)
{
ecma_value_t exec_arguments[1] = { context_p->input_string };
ECMA_TRY_CATCH (match_value,
ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
context_p->regexp_or_search_string,
exec_arguments,
1),
ecma_regexp_exec_helper (context_p->regexp_or_search_string,
context_p->input_string,
false),
ret_value);
if (!ecma_is_value_null (match_value))
@@ -1504,7 +1497,6 @@ ecma_builtin_string_prototype_object_replace (ecma_value_t this_arg, /**< this a
return ret_value;
} /* ecma_builtin_string_prototype_object_replace */
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
/**
* The String.prototype object's 'search' routine
@@ -1517,11 +1509,91 @@ ecma_builtin_string_prototype_object_replace (ecma_value_t this_arg, /**< this a
*/
static ecma_completion_value_t
ecma_builtin_string_prototype_object_search (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
ecma_value_t regexp_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_value,
ecma_op_check_object_coercible (this_arg),
ret_value);
/* 2. */
ECMA_TRY_CATCH (to_string_value,
ecma_op_to_string (this_arg),
ret_value);
ecma_value_t regexp_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
/* 3. */
if (ecma_is_value_object (regexp_arg)
&& ecma_object_get_class_name (ecma_get_object_from_value (regexp_arg)) == LIT_MAGIC_STRING_REGEXP_UL)
{
regexp_value = ecma_copy_value (regexp_arg, true);
}
else
{
/* 4. */
ecma_value_t regexp_arguments[1] = { regexp_arg };
ECMA_TRY_CATCH (new_regexp_value,
ecma_builtin_regexp_dispatch_construct (regexp_arguments, 1),
ret_value);
regexp_value = ecma_copy_value (new_regexp_value, true);
ECMA_FINALIZE (new_regexp_value);
}
/* 5. */
if (ecma_is_completion_value_empty (ret_value))
{
ECMA_TRY_CATCH (match_result,
ecma_regexp_exec_helper (regexp_value, to_string_value, true),
ret_value);
ecma_number_t offset = -1;
if (!ecma_is_value_null (match_result))
{
JERRY_ASSERT (ecma_is_value_object (match_result));
ecma_object_t *match_object_p = ecma_get_object_from_value (match_result);
ecma_string_t *index_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_INDEX);
ECMA_TRY_CATCH (index_value,
ecma_op_object_get (match_object_p, index_string_p),
ret_value);
JERRY_ASSERT (ecma_is_value_number (index_value));
offset = *ecma_get_number_from_value (index_value);
ECMA_FINALIZE (index_value);
ecma_deref_ecma_string (index_string_p);
}
if (ecma_is_completion_value_empty (ret_value))
{
ecma_number_t *offset_number_p = ecma_alloc_number ();
*offset_number_p = offset;
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (offset_number_p));
}
ECMA_FINALIZE (match_result);
ecma_free_value (regexp_value, true);
}
ECMA_FINALIZE (to_string_value);
ECMA_FINALIZE (check_coercible_value);
/* 6. */
return ret_value;
} /* ecma_builtin_string_prototype_object_search */
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
/**
* The String.prototype object's 'slice' routine
*
@@ -71,9 +71,9 @@ ROUTINE (LIT_MAGIC_STRING_LOCALE_COMPARE_UL, ecma_builtin_string_prototype_objec
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
ROUTINE (LIT_MAGIC_STRING_MATCH, ecma_builtin_string_prototype_object_match, 1, 1)
ROUTINE (LIT_MAGIC_STRING_REPLACE, ecma_builtin_string_prototype_object_replace, 2, 2)
ROUTINE (LIT_MAGIC_STRING_SEARCH, ecma_builtin_string_prototype_object_search, 1, 1)
#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
ROUTINE (LIT_MAGIC_STRING_SEARCH, ecma_builtin_string_prototype_object_search, 1, 1)
ROUTINE (LIT_MAGIC_STRING_SPLIT, ecma_builtin_string_prototype_object_split, 2, 2)
ROUTINE (LIT_MAGIC_STRING_SUBSTRING, ecma_builtin_string_prototype_object_substring, 2, 2)
ROUTINE (LIT_MAGIC_STRING_TO_LOWER_CASE_UL, ecma_builtin_string_prototype_object_to_lower_case, 0, 0)