Parse functions directly (#2015)
This patch adds direct function source code parsing, which is useful to avoid source code duplications. The patch also improves the Function constructor. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
committed by
Dániel Bátyai
parent
7713d30702
commit
8d916a44f1
+158
-105
@@ -40,7 +40,7 @@ parser_copy_identifiers (parser_context_t *context_p) /**< context */
|
||||
{
|
||||
parser_saved_context_t *parent_p = context_p->last_context_p;
|
||||
|
||||
if (parent_p == NULL || parent_p->prev_context_p == NULL)
|
||||
if (parent_p == NULL || !(parent_p->status_flags & PARSER_IS_FUNCTION))
|
||||
{
|
||||
/* Return if this function is not a nested function. */
|
||||
return;
|
||||
@@ -1965,6 +1965,109 @@ parser_free_literals (parser_list_t *literal_pool_p) /**< literals */
|
||||
parser_list_free (literal_pool_p);
|
||||
} /* parser_free_literals */
|
||||
|
||||
/**
|
||||
* Parse function arguments
|
||||
*/
|
||||
static void
|
||||
parser_parse_function_arguments (parser_context_t *context_p, /**< context */
|
||||
lexer_token_type_t end_type) /**< expected end type */
|
||||
{
|
||||
if (context_p->token.type == end_type)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
uint16_t literal_count = context_p->literal_count;
|
||||
|
||||
if (context_p->token.type != LEXER_LITERAL
|
||||
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
|
||||
}
|
||||
|
||||
lexer_construct_literal_object (context_p,
|
||||
&context_p->token.lit_location,
|
||||
LEXER_IDENT_LITERAL);
|
||||
|
||||
if (literal_count == context_p->literal_count
|
||||
|| context_p->token.literal_is_reserved
|
||||
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
||||
{
|
||||
context_p->status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
||||
}
|
||||
|
||||
if (context_p->lit_object.type == LEXER_LITERAL_OBJECT_ARGUMENTS)
|
||||
{
|
||||
uint8_t literal_status_flags = context_p->lit_object.literal_p->status_flags;
|
||||
|
||||
literal_status_flags = (uint8_t) (literal_status_flags & ~LEXER_FLAG_NO_REG_STORE);
|
||||
context_p->lit_object.literal_p->status_flags = literal_status_flags;
|
||||
|
||||
context_p->status_flags |= PARSER_ARGUMENTS_NOT_NEEDED;
|
||||
context_p->status_flags &= ~(PARSER_LEXICAL_ENV_NEEDED | PARSER_ARGUMENTS_NEEDED);
|
||||
}
|
||||
|
||||
if (context_p->literal_count == literal_count)
|
||||
{
|
||||
lexer_literal_t *literal_p;
|
||||
|
||||
if (context_p->literal_count >= PARSER_MAXIMUM_NUMBER_OF_LITERALS)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_LITERAL_LIMIT_REACHED);
|
||||
}
|
||||
|
||||
literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
|
||||
*literal_p = *context_p->lit_object.literal_p;
|
||||
|
||||
literal_p->status_flags &= LEXER_FLAG_SOURCE_PTR;
|
||||
literal_p->status_flags |= LEXER_FLAG_VAR | LEXER_FLAG_INITIALIZED | LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
|
||||
context_p->literal_count++;
|
||||
|
||||
/* There cannot be references from the byte code to these literals
|
||||
* since no byte code has been emitted yet. Therefore there is no
|
||||
* need to set the index field. */
|
||||
context_p->lit_object.literal_p->type = LEXER_UNUSED_LITERAL;
|
||||
|
||||
/* Only the LEXER_FLAG_FUNCTION_ARGUMENT flag is kept. */
|
||||
context_p->lit_object.literal_p->status_flags &= LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
context_p->lit_object.literal_p->u.char_p = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t lexer_flags = LEXER_FLAG_VAR | LEXER_FLAG_INITIALIZED | LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
context_p->lit_object.literal_p->status_flags |= lexer_flags;
|
||||
}
|
||||
|
||||
context_p->argument_count++;
|
||||
if (context_p->argument_count >= PARSER_MAXIMUM_NUMBER_OF_REGISTERS)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_REGISTER_LIMIT_REACHED);
|
||||
}
|
||||
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type != LEXER_COMMA)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lexer_next_token (context_p);
|
||||
}
|
||||
|
||||
if (context_p->token.type != end_type)
|
||||
{
|
||||
parser_error_t error = ((end_type == LEXER_RIGHT_PAREN) ? PARSER_ERR_RIGHT_PAREN_EXPECTED
|
||||
: PARSER_ERR_IDENTIFIER_EXPECTED);
|
||||
|
||||
parser_raise_error (context_p, error);
|
||||
}
|
||||
|
||||
context_p->register_count = context_p->argument_count;
|
||||
} /* parser_parse_function_arguments */
|
||||
|
||||
/**
|
||||
* Parse and compile EcmaScript source code
|
||||
*
|
||||
@@ -1973,8 +2076,10 @@ parser_free_literals (parser_list_t *literal_pool_p) /**< literals */
|
||||
* @return compiled code
|
||||
*/
|
||||
static ecma_compiled_code_t *
|
||||
parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
size_t size, /**< size of the source code */
|
||||
parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
|
||||
size_t arg_list_size, /**< size of function argument list */
|
||||
const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
size_t source_size, /**< size of the source code */
|
||||
int strict_mode, /**< strict mode */
|
||||
parser_error_location_t *error_location_p) /**< error location */
|
||||
{
|
||||
@@ -1989,7 +2094,19 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
error_location_p->error = PARSER_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
context.status_flags = PARSER_NO_REG_STORE | PARSER_LEXICAL_ENV_NEEDED | PARSER_ARGUMENTS_NOT_NEEDED;
|
||||
if (arg_list_p == NULL)
|
||||
{
|
||||
context.status_flags = PARSER_NO_REG_STORE | PARSER_LEXICAL_ENV_NEEDED | PARSER_ARGUMENTS_NOT_NEEDED;
|
||||
context.source_p = source_p;
|
||||
context.source_end_p = source_p + source_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.status_flags = PARSER_IS_FUNCTION;
|
||||
context.source_p = arg_list_p;
|
||||
context.source_end_p = arg_list_p + arg_list_size;
|
||||
}
|
||||
|
||||
context.stack_depth = 0;
|
||||
context.stack_limit = 0;
|
||||
context.last_context_p = NULL;
|
||||
@@ -2000,8 +2117,6 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
context.status_flags |= PARSER_IS_STRICT;
|
||||
}
|
||||
|
||||
context.source_p = source_p;
|
||||
context.source_end_p = source_p + size;
|
||||
context.line = 1;
|
||||
context.column = 1;
|
||||
|
||||
@@ -2028,7 +2143,9 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
|
||||
if (context.is_show_opcodes)
|
||||
{
|
||||
JERRY_DEBUG_MSG ("\n--- Script parsing start ---\n\n");
|
||||
JERRY_DEBUG_MSG ("\n--- %s parsing start ---\n\n",
|
||||
(arg_list_p == NULL) ? "Script"
|
||||
: "Function");
|
||||
}
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
|
||||
@@ -2046,6 +2163,18 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
* lexer_next_token() must be immediately called. */
|
||||
lexer_next_token (&context);
|
||||
|
||||
if (arg_list_p != NULL)
|
||||
{
|
||||
parser_parse_function_arguments (&context, LEXER_EOS);
|
||||
|
||||
context.source_p = source_p;
|
||||
context.source_end_p = source_p + source_size;
|
||||
context.line = 1;
|
||||
context.column = 1;
|
||||
|
||||
lexer_next_token (&context);
|
||||
}
|
||||
|
||||
parser_parse_statements (&context);
|
||||
|
||||
/* When the parsing is successful, only the
|
||||
@@ -2066,7 +2195,9 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
#ifdef PARSER_DUMP_BYTE_CODE
|
||||
if (context.is_show_opcodes)
|
||||
{
|
||||
JERRY_DEBUG_MSG ("\nScript parsing successfully completed. Total byte code size: %d bytes\n",
|
||||
JERRY_DEBUG_MSG ("\n%s parsing successfully completed. Total byte code size: %d bytes\n",
|
||||
(arg_list_p == NULL) ? "Script"
|
||||
: "Function",
|
||||
(int) context.total_byte_code_size);
|
||||
}
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
@@ -2100,7 +2231,9 @@ parser_parse_source (const uint8_t *source_p, /**< valid UTF-8 source code */
|
||||
#ifdef PARSER_DUMP_BYTE_CODE
|
||||
if (context.is_show_opcodes)
|
||||
{
|
||||
JERRY_DEBUG_MSG ("\n--- Script parsing end ---\n\n");
|
||||
JERRY_DEBUG_MSG ("\n--- %s parsing end ---\n\n",
|
||||
(arg_list_p == NULL) ? "Script"
|
||||
: "Function");
|
||||
}
|
||||
#endif /* PARSER_DUMP_BYTE_CODE */
|
||||
|
||||
@@ -2241,99 +2374,9 @@ parser_parse_function (parser_context_t *context_p, /**< context */
|
||||
|
||||
lexer_next_token (context_p);
|
||||
|
||||
/* Argument parsing. */
|
||||
if (context_p->token.type != LEXER_RIGHT_PAREN)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
uint16_t literal_count = context_p->literal_count;
|
||||
|
||||
if (context_p->token.type != LEXER_LITERAL
|
||||
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
|
||||
}
|
||||
|
||||
lexer_construct_literal_object (context_p,
|
||||
&context_p->token.lit_location,
|
||||
LEXER_IDENT_LITERAL);
|
||||
|
||||
if (literal_count == context_p->literal_count
|
||||
|| context_p->token.literal_is_reserved
|
||||
|| context_p->lit_object.type != LEXER_LITERAL_OBJECT_ANY)
|
||||
{
|
||||
context_p->status_flags |= PARSER_HAS_NON_STRICT_ARG;
|
||||
}
|
||||
|
||||
if (context_p->lit_object.type == LEXER_LITERAL_OBJECT_ARGUMENTS)
|
||||
{
|
||||
uint8_t literal_status_flags = context_p->lit_object.literal_p->status_flags;
|
||||
|
||||
literal_status_flags = (uint8_t) (literal_status_flags & ~LEXER_FLAG_NO_REG_STORE);
|
||||
context_p->lit_object.literal_p->status_flags = literal_status_flags;
|
||||
|
||||
context_p->status_flags |= PARSER_ARGUMENTS_NOT_NEEDED;
|
||||
context_p->status_flags &= ~(PARSER_LEXICAL_ENV_NEEDED | PARSER_ARGUMENTS_NEEDED);
|
||||
}
|
||||
|
||||
if (context_p->literal_count == literal_count)
|
||||
{
|
||||
lexer_literal_t *literal_p;
|
||||
|
||||
if (context_p->literal_count >= PARSER_MAXIMUM_NUMBER_OF_LITERALS)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_LITERAL_LIMIT_REACHED);
|
||||
}
|
||||
|
||||
literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
|
||||
*literal_p = *context_p->lit_object.literal_p;
|
||||
|
||||
literal_p->status_flags &= LEXER_FLAG_SOURCE_PTR;
|
||||
literal_p->status_flags |= LEXER_FLAG_VAR | LEXER_FLAG_INITIALIZED | LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
|
||||
context_p->literal_count++;
|
||||
|
||||
/* There cannot be references from the byte code to these literals
|
||||
* since no byte code has been emitted yet. Therefore there is no
|
||||
* need to set the index field. */
|
||||
context_p->lit_object.literal_p->type = LEXER_UNUSED_LITERAL;
|
||||
|
||||
/* Only the LEXER_FLAG_FUNCTION_ARGUMENT flag is kept. */
|
||||
context_p->lit_object.literal_p->status_flags &= LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
context_p->lit_object.literal_p->u.char_p = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t lexer_flags = LEXER_FLAG_VAR | LEXER_FLAG_INITIALIZED | LEXER_FLAG_FUNCTION_ARGUMENT;
|
||||
context_p->lit_object.literal_p->status_flags |= lexer_flags;
|
||||
}
|
||||
|
||||
context_p->argument_count++;
|
||||
if (context_p->argument_count >= PARSER_MAXIMUM_NUMBER_OF_REGISTERS)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_REGISTER_LIMIT_REACHED);
|
||||
}
|
||||
|
||||
lexer_next_token (context_p);
|
||||
|
||||
if (context_p->token.type != LEXER_COMMA)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lexer_next_token (context_p);
|
||||
}
|
||||
}
|
||||
|
||||
if (context_p->token.type != LEXER_RIGHT_PAREN)
|
||||
{
|
||||
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
|
||||
}
|
||||
|
||||
parser_parse_function_arguments (context_p, LEXER_RIGHT_PAREN);
|
||||
lexer_next_token (context_p);
|
||||
|
||||
context_p->register_count = context_p->argument_count;
|
||||
|
||||
if ((context_p->status_flags & PARSER_IS_PROPERTY_GETTER)
|
||||
&& context_p->argument_count != 0)
|
||||
{
|
||||
@@ -2480,14 +2523,17 @@ parser_send_breakpoints (parser_context_t *context_p, /**< context */
|
||||
* Parse EcamScript source code
|
||||
*
|
||||
* Note:
|
||||
* if arg_list_p is not NULL, a function body is parsed
|
||||
* returned value must be freed with ecma_free_value
|
||||
*
|
||||
* @return true - if success
|
||||
* syntax error - otherwise
|
||||
*/
|
||||
ecma_value_t
|
||||
parser_parse_script (const uint8_t *source_p, /**< source code */
|
||||
size_t size, /**< size of the source code */
|
||||
parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
|
||||
size_t arg_list_size, /**< size of function argument list */
|
||||
const uint8_t *source_p, /**< source code */
|
||||
size_t source_size, /**< size of the source code */
|
||||
bool is_strict, /**< strict mode */
|
||||
ecma_compiled_code_t **bytecode_data_p) /**< [out] JS bytecode */
|
||||
{
|
||||
@@ -2500,11 +2546,16 @@ parser_parse_script (const uint8_t *source_p, /**< source code */
|
||||
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE,
|
||||
JERRY_DEBUGGER_NO_SUBTYPE,
|
||||
source_p,
|
||||
size);
|
||||
source_size);
|
||||
}
|
||||
#endif /* JERRY_DEBUGGER */
|
||||
|
||||
*bytecode_data_p = parser_parse_source (source_p, size, is_strict, &parser_error);
|
||||
*bytecode_data_p = parser_parse_source (arg_list_p,
|
||||
arg_list_size,
|
||||
source_p,
|
||||
source_size,
|
||||
is_strict,
|
||||
&parser_error);
|
||||
|
||||
if (!*bytecode_data_p)
|
||||
{
|
||||
@@ -2547,8 +2598,10 @@ parser_parse_script (const uint8_t *source_p, /**< source code */
|
||||
}
|
||||
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_TRUE);
|
||||
#else /* !JERRY_JS_PARSER */
|
||||
JERRY_UNUSED (arg_list_p);
|
||||
JERRY_UNUSED (arg_list_size);
|
||||
JERRY_UNUSED (source_p);
|
||||
JERRY_UNUSED (size);
|
||||
JERRY_UNUSED (source_size);
|
||||
JERRY_UNUSED (is_strict);
|
||||
JERRY_UNUSED (bytecode_data_p);
|
||||
|
||||
|
||||
@@ -128,8 +128,9 @@ typedef struct
|
||||
} parser_error_location_t;
|
||||
|
||||
/* Note: source must be a valid UTF-8 string */
|
||||
ecma_value_t parser_parse_script (const uint8_t *source_p, size_t size, bool is_strict,
|
||||
ecma_compiled_code_t **bytecode_data_p);
|
||||
ecma_value_t parser_parse_script (const uint8_t *arg_list_p, size_t arg_list_size,
|
||||
const uint8_t *source_p, size_t source_size,
|
||||
bool is_strict, ecma_compiled_code_t **bytecode_data_p);
|
||||
|
||||
const char *parser_error_to_string (parser_error_t);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user