Introduce parser scope types, add scope flags, indicating whether scope contains a function declaration, a 'try' statement, 'with' statement or 'delete' operator.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
This commit is contained in:
@@ -50,10 +50,6 @@ static token tok;
|
||||
static bool inside_eval = false;
|
||||
static bool inside_function = false;
|
||||
static bool parser_show_instrs = false;
|
||||
/**
|
||||
* flag, indicating that code contains function declarations or function expressions
|
||||
*/
|
||||
static bool code_contains_functions = false;
|
||||
|
||||
enum
|
||||
{
|
||||
@@ -383,14 +379,14 @@ parse_property_assignment (void)
|
||||
return;
|
||||
}
|
||||
|
||||
code_contains_functions = true;
|
||||
|
||||
STACK_DECLARE_USAGE (scopes);
|
||||
|
||||
const operand name = parse_property_name ();
|
||||
jsp_early_error_add_prop_name (name, is_setter ? PROP_SET : PROP_GET);
|
||||
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL));
|
||||
scopes_tree_set_contains_functions (STACK_TOP (scopes));
|
||||
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL, SCOPE_TYPE_FUNCTION));
|
||||
serializer_set_scope (STACK_TOP (scopes));
|
||||
scopes_tree_set_strict_mode (STACK_TOP (scopes), scopes_tree_strict_mode (STACK_HEAD (scopes, 2)));
|
||||
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));
|
||||
@@ -652,8 +648,6 @@ parse_function_declaration (void)
|
||||
{
|
||||
STACK_DECLARE_USAGE (scopes);
|
||||
|
||||
code_contains_functions = true;
|
||||
|
||||
assert_keyword (KW_FUNCTION);
|
||||
|
||||
jsp_label_t *masked_label_set_p = jsp_label_mask_set ();
|
||||
@@ -664,7 +658,10 @@ parse_function_declaration (void)
|
||||
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (name, is_strict_mode (), tok.loc);
|
||||
|
||||
skip_newlines ();
|
||||
STACK_PUSH (scopes, scopes_tree_init (STACK_TOP (scopes)));
|
||||
|
||||
scopes_tree_set_contains_functions (STACK_TOP (scopes));
|
||||
|
||||
STACK_PUSH (scopes, scopes_tree_init (STACK_TOP (scopes), SCOPE_TYPE_FUNCTION));
|
||||
serializer_set_scope (STACK_TOP (scopes));
|
||||
scopes_tree_set_strict_mode (STACK_TOP (scopes), scopes_tree_strict_mode (STACK_HEAD (scopes, 2)));
|
||||
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));
|
||||
@@ -709,13 +706,13 @@ parse_function_expression (void)
|
||||
STACK_DECLARE_USAGE (scopes);
|
||||
assert_keyword (KW_FUNCTION);
|
||||
|
||||
code_contains_functions = true;
|
||||
|
||||
operand res;
|
||||
|
||||
jsp_early_error_start_checking_of_vargs ();
|
||||
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL));
|
||||
scopes_tree_set_contains_functions (STACK_TOP (scopes));
|
||||
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL, SCOPE_TYPE_FUNCTION));
|
||||
serializer_set_scope (STACK_TOP (scopes));
|
||||
scopes_tree_set_strict_mode (STACK_TOP (scopes), scopes_tree_strict_mode (STACK_HEAD (scopes, 2)));
|
||||
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));
|
||||
@@ -1191,6 +1188,8 @@ parse_unary_expression (operand *this_arg_gl, operand *prop_gl)
|
||||
{
|
||||
if (is_keyword (KW_DELETE))
|
||||
{
|
||||
scopes_tree_set_contains_delete (STACK_TOP (scopes));
|
||||
|
||||
skip_newlines ();
|
||||
expr = parse_unary_expression (NULL, NULL);
|
||||
expr = dump_delete_res (expr, is_strict_mode (), tok.loc);
|
||||
@@ -2361,6 +2360,8 @@ parse_with_statement (void)
|
||||
}
|
||||
const operand expr = parse_expression_inside_parens ();
|
||||
|
||||
scopes_tree_set_contains_with (STACK_TOP (scopes));
|
||||
|
||||
bool is_raised = jsp_label_raise_nested_jumpable_border ();
|
||||
|
||||
vm_instr_counter_t with_begin_oc = dump_with_for_rewrite (expr);
|
||||
@@ -2542,6 +2543,8 @@ parse_try_statement (void)
|
||||
{
|
||||
assert_keyword (KW_TRY);
|
||||
|
||||
scopes_tree_set_contains_try (STACK_TOP (scopes));
|
||||
|
||||
bool is_raised = jsp_label_raise_nested_jumpable_border ();
|
||||
|
||||
dump_try_for_rewrite ();
|
||||
@@ -3030,18 +3033,27 @@ parse_source_element_list (bool is_global) /**< flag, indicating that we parsing
|
||||
static jsp_status_t
|
||||
parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer */
|
||||
size_t source_size, /**< source code size in bytes */
|
||||
bool in_function, /**< flag indicating if we are parsing body of a function */
|
||||
bool in_dyn_constructed_function, /**< flag indicating if we are parsing body of a function,
|
||||
* constructed using 'new Function (...)'-like expression */
|
||||
bool in_eval, /**< flag indicating if we are parsing body of eval code */
|
||||
bool is_strict, /**< flag, indicating whether current code
|
||||
* inherited strict mode from code of an outer scope */
|
||||
const vm_instr_t **out_instrs_p) /**< out: generated byte-code array
|
||||
const vm_instr_t **out_instrs_p, /**< out: generated byte-code array
|
||||
* (in case there were no syntax errors) */
|
||||
bool *out_contains_functions_p) /**< out: optional (can be NULL, if the output is not needed)
|
||||
* flag, indicating whether the compiled byte-code
|
||||
* contains a function declaration / expression */
|
||||
{
|
||||
JERRY_ASSERT (out_instrs_p != NULL);
|
||||
|
||||
inside_function = in_function;
|
||||
JERRY_ASSERT (!(in_dyn_constructed_function && in_eval));
|
||||
|
||||
inside_function = in_dyn_constructed_function;
|
||||
inside_eval = in_eval;
|
||||
code_contains_functions = false;
|
||||
|
||||
scope_type_t scope_type = (in_dyn_constructed_function ? SCOPE_TYPE_FUNCTION
|
||||
: (in_eval ? SCOPE_TYPE_EVAL
|
||||
: SCOPE_TYPE_GLOBAL));
|
||||
|
||||
#ifndef JERRY_NDEBUG
|
||||
volatile bool is_parse_finished = false;
|
||||
@@ -3057,7 +3069,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
|
||||
jsp_early_error_init ();
|
||||
|
||||
STACK_INIT (scopes);
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL));
|
||||
STACK_PUSH (scopes, scopes_tree_init (NULL, scope_type));
|
||||
serializer_set_scope (STACK_TOP (scopes));
|
||||
scopes_tree_set_strict_mode (STACK_TOP (scopes), is_strict);
|
||||
|
||||
@@ -3080,7 +3092,7 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
|
||||
skip_newlines ();
|
||||
JERRY_ASSERT (token_is (TOK_EOF));
|
||||
|
||||
if (in_function)
|
||||
if (in_dyn_constructed_function)
|
||||
{
|
||||
dump_ret ();
|
||||
}
|
||||
@@ -3103,6 +3115,13 @@ parser_parse_program (const jerry_api_char_t *source_p, /**< source code buffer
|
||||
|
||||
dumper_free ();
|
||||
|
||||
if (out_contains_functions_p != NULL)
|
||||
{
|
||||
scopes_tree scope = STACK_TOP (scopes);
|
||||
|
||||
*out_contains_functions_p = scope->contains_functions;
|
||||
}
|
||||
|
||||
serializer_set_scope (NULL);
|
||||
scopes_tree_free (STACK_TOP (scopes));
|
||||
STACK_DROP (scopes, 1);
|
||||
@@ -3155,7 +3174,7 @@ parser_parse_script (const jerry_api_char_t *source, /**< source script */
|
||||
const vm_instr_t **out_instrs_p) /**< out: generated byte-code array
|
||||
* (in case there were no syntax errors) */
|
||||
{
|
||||
return parser_parse_program (source, source_size, false, false, false, out_instrs_p);
|
||||
return parser_parse_program (source, source_size, false, false, false, out_instrs_p, NULL);
|
||||
} /* parser_parse_script */
|
||||
|
||||
/**
|
||||
@@ -3169,10 +3188,14 @@ parser_parse_eval (const jerry_api_char_t *source, /**< string passed to eval()
|
||||
size_t source_size, /**< string size in bytes */
|
||||
bool is_strict, /**< flag, indicating whether eval is called
|
||||
* from strict code in direct mode */
|
||||
const vm_instr_t **out_instrs_p) /**< out: generated byte-code array
|
||||
const vm_instr_t **out_instrs_p, /**< out: generated byte-code array
|
||||
* (in case there were no syntax errors) */
|
||||
bool *out_contains_functions_p) /**< out: flag, indicating whether the compiled byte-code
|
||||
* contains a function declaration / expression */
|
||||
{
|
||||
return parser_parse_program (source, source_size, false, true, is_strict, out_instrs_p);
|
||||
JERRY_ASSERT (out_contains_functions_p != NULL);
|
||||
|
||||
return parser_parse_program (source, source_size, false, true, is_strict, out_instrs_p, out_contains_functions_p);
|
||||
} /* parser_parse_eval */
|
||||
|
||||
/**
|
||||
@@ -3205,20 +3228,10 @@ parser_parse_new_function (const jerry_api_char_t **params, /**< array of argume
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
out_instrs_p);
|
||||
out_instrs_p,
|
||||
NULL);
|
||||
} /* parser_parse_new_function */
|
||||
|
||||
/**
|
||||
* Indicates whether code contains functions
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
bool
|
||||
parser_is_code_contains_functions ()
|
||||
{
|
||||
return code_contains_functions;
|
||||
} /* parser_is_code_contains_functions */
|
||||
|
||||
/**
|
||||
* Tell parser whether to dump bytecode
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user