Compact Byte Code parser and executor for Jerry.
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: Tamas Gergely tgergely.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com JerryScript-DCO-1.0-Signed-off-by: István Kádár ikadar@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/* Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015 University of Szeged.
|
||||
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015-2016 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.
|
||||
@@ -65,7 +65,7 @@
|
||||
*/
|
||||
ecma_completion_value_t
|
||||
re_parse_regexp_flags (ecma_string_t *flags_str_p, /**< Input string with flags */
|
||||
uint8_t *flags_p) /**< Output: parsed flag bits */
|
||||
uint16_t *flags_p) /**< Output: parsed flag bits */
|
||||
{
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
@@ -129,7 +129,7 @@ re_parse_regexp_flags (ecma_string_t *flags_str_p, /**< Input string with flags
|
||||
void
|
||||
re_initialize_props (ecma_object_t *re_obj_p, /**< RegExp obejct */
|
||||
ecma_string_t *source_p, /**< source string */
|
||||
uint8_t flags) /**< flags */
|
||||
uint16_t flags) /**< flags */
|
||||
{
|
||||
/* Set source property. ECMA-262 v5, 15.10.7.1 */
|
||||
ecma_string_t *magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_SOURCE);
|
||||
@@ -218,6 +218,42 @@ re_initialize_props (ecma_object_t *re_obj_p, /**< RegExp obejct */
|
||||
ecma_dealloc_number (lastindex_num_p);
|
||||
} /* re_initialize_props */
|
||||
|
||||
/**
|
||||
* RegExp object creation operation.
|
||||
*
|
||||
* See also: ECMA-262 v5, 15.10.4.1
|
||||
*
|
||||
* @return completion value
|
||||
* Returned value must be freed with ecma_free_completion_value
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_op_create_regexp_object_from_bytecode (re_compiled_code_t *bytecode_p) /**< input pattern */
|
||||
{
|
||||
JERRY_ASSERT (bytecode_p != NULL);
|
||||
|
||||
ecma_object_t *re_prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_REGEXP_PROTOTYPE);
|
||||
|
||||
ecma_object_t *obj_p = ecma_create_object (re_prototype_obj_p, true, ECMA_OBJECT_TYPE_GENERAL);
|
||||
ecma_deref_object (re_prototype_obj_p);
|
||||
|
||||
/* Set the internal [[Class]] property */
|
||||
ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_CLASS);
|
||||
class_prop_p->u.internal_property.value = LIT_MAGIC_STRING_REGEXP_UL;
|
||||
|
||||
/* Set bytecode internal property. */
|
||||
ecma_property_t *bytecode_prop_p;
|
||||
bytecode_prop_p = ecma_create_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE);
|
||||
ECMA_SET_NON_NULL_POINTER (bytecode_prop_p->u.internal_property.value, bytecode_p);
|
||||
ecma_bytecode_ref ((ecma_compiled_code_t *) bytecode_p);
|
||||
|
||||
/* Initialize RegExp object properties */
|
||||
re_initialize_props (obj_p,
|
||||
ECMA_GET_NON_NULL_POINTER (ecma_string_t, bytecode_p->pattern_cp),
|
||||
bytecode_p->flags);
|
||||
|
||||
return ecma_make_object_value (obj_p);
|
||||
} /* ecma_op_create_regexp_object_from_bytecode */
|
||||
|
||||
/**
|
||||
* RegExp object creation operation.
|
||||
*
|
||||
@@ -232,7 +268,7 @@ ecma_op_create_regexp_object (ecma_string_t *pattern_p, /**< input pattern */
|
||||
{
|
||||
JERRY_ASSERT (pattern_p != NULL);
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
uint8_t flags = 0;
|
||||
uint16_t flags = 0;
|
||||
|
||||
if (flags_str_p != NULL)
|
||||
{
|
||||
@@ -261,7 +297,7 @@ ecma_op_create_regexp_object (ecma_string_t *pattern_p, /**< input pattern */
|
||||
bytecode_prop_p = ecma_create_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE);
|
||||
|
||||
/* Compile bytecode. */
|
||||
re_bytecode_t *bc_p = NULL;
|
||||
re_compiled_code_t *bc_p = NULL;
|
||||
ECMA_TRY_CATCH (empty, re_compile_bytecode (&bc_p, pattern_p, flags), ret_value);
|
||||
|
||||
ECMA_SET_POINTER (bytecode_prop_p->u.internal_property.value, bc_p);
|
||||
@@ -336,7 +372,7 @@ re_canonicalize (ecma_char_t ch, /**< character */
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
re_bytecode_t *bc_p, /**< pointer to the current RegExp bytecode */
|
||||
uint8_t *bc_p, /**< pointer to the current RegExp bytecode */
|
||||
lit_utf8_byte_t *str_p, /**< input string pointer */
|
||||
lit_utf8_byte_t **out_str_p) /**< Output: matching substring iterator */
|
||||
{
|
||||
@@ -651,7 +687,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
}
|
||||
case RE_OP_SAVE_AT_START:
|
||||
{
|
||||
re_bytecode_t *old_bc_p;
|
||||
uint8_t *old_bc_p;
|
||||
|
||||
JERRY_DDLOG ("Execute RE_OP_SAVE_AT_START\n");
|
||||
lit_utf8_byte_t *old_start_p = re_ctx_p->saved_p[RE_GLOBAL_START_IDX];
|
||||
@@ -717,7 +753,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
uint32_t start_idx, iter_idx, offset;
|
||||
lit_utf8_byte_t *old_start_p = NULL;
|
||||
lit_utf8_byte_t *sub_str_p = NULL;
|
||||
re_bytecode_t *old_bc_p;
|
||||
uint8_t *old_bc_p;
|
||||
|
||||
old_bc_p = bc_p; /* save the bytecode start position of the group start */
|
||||
start_idx = re_get_value (&bc_p);
|
||||
@@ -770,8 +806,8 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
{
|
||||
uint32_t start_idx, iter_idx, old_iteration_cnt, offset;
|
||||
lit_utf8_byte_t *sub_str_p = NULL;
|
||||
re_bytecode_t *old_bc_p;
|
||||
re_bytecode_t *end_bc_p = NULL;
|
||||
uint8_t *old_bc_p;
|
||||
uint8_t *end_bc_p = NULL;
|
||||
start_idx = re_get_value (&bc_p);
|
||||
|
||||
if (op != RE_OP_CAPTURE_GROUP_START
|
||||
@@ -844,7 +880,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
case RE_OP_NON_CAPTURE_NON_GREEDY_GROUP_END:
|
||||
{
|
||||
uint32_t end_idx, iter_idx, min, max;
|
||||
re_bytecode_t *old_bc_p;
|
||||
uint8_t *old_bc_p;
|
||||
|
||||
/*
|
||||
* On non-greedy iterations we have to execute the bytecode
|
||||
@@ -904,7 +940,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
|
||||
lit_utf8_byte_t *old_start_p = NULL;
|
||||
lit_utf8_byte_t *old_end_p = NULL;
|
||||
lit_utf8_byte_t *sub_str_p = NULL;
|
||||
re_bytecode_t *old_bc_p;
|
||||
uint8_t *old_bc_p;
|
||||
|
||||
end_idx = re_get_value (&bc_p);
|
||||
min = re_get_value (&bc_p);
|
||||
@@ -1217,7 +1253,8 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
|
||||
ecma_property_t *bytecode_prop_p = ecma_get_internal_property (regexp_object_p,
|
||||
ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE);
|
||||
re_bytecode_t *bc_p = ECMA_GET_POINTER (re_bytecode_t, bytecode_prop_p->u.internal_property.value);
|
||||
re_compiled_code_t *bc_p = ECMA_GET_POINTER (re_compiled_code_t,
|
||||
bytecode_prop_p->u.internal_property.value);
|
||||
|
||||
ecma_string_t *input_string_p = ecma_get_string_from_value (input_string);
|
||||
lit_utf8_size_t input_string_size = ecma_string_get_size (input_string_p);
|
||||
@@ -1240,11 +1277,11 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
re_ctx.input_end_p = input_buffer_p + input_string_size;
|
||||
|
||||
/* 1. Read bytecode header and init regexp matcher context. */
|
||||
re_ctx.flags = (uint8_t) re_get_value (&bc_p);
|
||||
re_ctx.flags = bc_p->flags;
|
||||
|
||||
if (ignore_global)
|
||||
{
|
||||
re_ctx.flags &= (uint8_t) ~RE_FLAG_GLOBAL;
|
||||
re_ctx.flags &= (uint16_t) ~RE_FLAG_GLOBAL;
|
||||
}
|
||||
|
||||
JERRY_DDLOG ("Exec with flags [global: %d, ignoreCase: %d, multiline: %d]\n",
|
||||
@@ -1252,10 +1289,9 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
re_ctx.flags & RE_FLAG_IGNORE_CASE,
|
||||
re_ctx.flags & RE_FLAG_MULTILINE);
|
||||
|
||||
re_ctx.num_of_captures = re_get_value (&bc_p);
|
||||
re_ctx.num_of_captures = bc_p->num_of_captures;
|
||||
JERRY_ASSERT (re_ctx.num_of_captures % 2 == 0);
|
||||
re_ctx.num_of_non_captures = re_get_value (&bc_p);
|
||||
|
||||
re_ctx.num_of_non_captures = bc_p->num_of_non_captures;
|
||||
|
||||
MEM_DEFINE_LOCAL_ARRAY (saved_p, re_ctx.num_of_captures + re_ctx.num_of_non_captures, lit_utf8_byte_t *);
|
||||
|
||||
@@ -1301,6 +1337,7 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
|
||||
/* 2. Try to match */
|
||||
lit_utf8_byte_t *sub_str_p = NULL;
|
||||
uint8_t *bc_start_p = (uint8_t *) (bc_p + 1);
|
||||
|
||||
while (ecma_is_completion_value_empty (ret_value))
|
||||
{
|
||||
@@ -1321,7 +1358,12 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
}
|
||||
else
|
||||
{
|
||||
ECMA_TRY_CATCH (match_value, re_match_regexp (&re_ctx, bc_p, input_curr_p, &sub_str_p), ret_value);
|
||||
ECMA_TRY_CATCH (match_value, re_match_regexp (&re_ctx,
|
||||
bc_start_p,
|
||||
input_curr_p,
|
||||
&sub_str_p),
|
||||
ret_value);
|
||||
|
||||
if (ecma_is_value_true (match_value))
|
||||
{
|
||||
is_match = true;
|
||||
@@ -1346,7 +1388,7 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
|
||||
if (sub_str_p)
|
||||
{
|
||||
*lastindex_num_p = lit_utf8_string_length (input_buffer_p,
|
||||
(lit_utf8_size_t) (sub_str_p - input_buffer_p));
|
||||
(lit_utf8_size_t) (sub_str_p - input_buffer_p));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user