Skip to content

Simplify source evaluation options. #2431

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
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
6 changes: 3 additions & 3 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,12 @@ Perform JavaScript `eval`.
jerry_value_t
jerry_eval (const jerry_char_t *source_p,
size_t source_size,
bool is_strict);
uint32_t parse_opts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is an api change please add it to #2213.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I've added it.

```

- `source_p` - source code to evaluate, it must be a valid utf8 string.
- `source_size` - length of the source code
- `is_strict` - perform `eval` as it is called from "strict mode" code.
- `parse_opts` - any combination of [jerry_parse_opts_t](#jerry_parse_opts_t) flags.
- return value - result of eval, may be error value.

**Example**
Expand All @@ -983,7 +983,7 @@ jerry_eval (const jerry_char_t *source_p,
{
jerry_value_t ret_val = jerry_eval (str_to_eval,
strlen (str_to_eval),
false);
JERRY_PARSE_NO_OPTS);
}
```

Expand Down
16 changes: 8 additions & 8 deletions docs/03.API-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ main (void)
/* Evaluate script1 */
eval_ret = jerry_eval (script_1,
strlen ((const char *) script_1),
false);
JERRY_PARSE_NO_OPTS);

/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);

/* Evaluate script2 */
eval_ret = jerry_eval (script_2,
strlen ((const char *) script_2),
false);
JERRY_PARSE_NO_OPTS);

/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
Expand Down Expand Up @@ -169,7 +169,7 @@ main (void)
/* Now starting script that would output value of just initialized field */
jerry_value_t eval_ret = jerry_eval (script,
strlen ((const char *) script),
false);
JERRY_PARSE_NO_OPTS);

/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
Expand Down Expand Up @@ -322,7 +322,7 @@ main (void)
/* Evaluate entered command */
ret_val = jerry_eval ((const jerry_char_t *) cmd,
len,
false);
JERRY_PARSE_NO_OPTS);

/* If command evaluated successfully, print value, returned by eval */
if (jerry_value_is_error (ret_val))
Expand Down Expand Up @@ -420,7 +420,7 @@ main (void)
size_t script_size = strlen ((const char *) script);

/* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, false);
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);

/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
Expand Down Expand Up @@ -509,7 +509,7 @@ main (void)
/* Evaluate script */
my_js_obj_val = jerry_eval (my_js_object,
strlen ((const char *) my_js_object),
false);
JERRY_PARSE_NO_OPTS);

/* Create a JS function object and wrap into a jerry value */
jerry_value_t add_func_obj = jerry_create_external_function (add_handler);
Expand All @@ -532,7 +532,7 @@ main (void)
size_t script_size = strlen ((const char *) script);

/* Evaluate script */
jerry_value_t eval_ret = jerry_eval (script, script_size, false);
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);

/* Free JavaScript value, returned by eval */
jerry_release_value (eval_ret);
Expand Down Expand Up @@ -583,7 +583,7 @@ main (void)
jerryx_handler_print);

/* Evaluate the script */
jerry_value_t eval_ret = jerry_eval (script, script_size, false);
jerry_value_t eval_ret = jerry_eval (script, script_size, JERRY_PARSE_NO_OPTS);

/* Free the JavaScript value returned by eval */
jerry_release_value (eval_ret);
Expand Down
2 changes: 1 addition & 1 deletion docs/12.EXT-REFERENCE-MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ load_and_evaluate_js_file (const jerry_value_t name, jerry_value_t *result)
{
/* We read the file into memory and call jerry_eval (), assigning the result to the out-parameter. */
fread (js_file_contents, file_size, 1, js_file);
(*result) = jerry_eval (js_file_contents, file_size, false);
(*result) = jerry_eval (js_file_contents, file_size, JERRY_PARSE_NO_OPTS);

/* We release the memory holding the contents of the file. */
free (js_file_contents);
Expand Down
9 changes: 4 additions & 5 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
0,
source_p,
source_size,
(parse_opts & JERRY_PARSE_STRICT_MODE) != 0,
parse_opts,
&bytecode_data_p);

if (ECMA_IS_VALUE_ERROR (parse_status))
Expand Down Expand Up @@ -485,7 +485,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
arg_list_size,
source_p,
source_size,
(parse_opts & JERRY_PARSE_STRICT_MODE) != 0,
parse_opts,
&bytecode_data_p);

if (ECMA_IS_VALUE_ERROR (parse_status))
Expand Down Expand Up @@ -563,14 +563,13 @@ jerry_run (const jerry_value_t func_val) /**< function to run */
jerry_value_t
jerry_eval (const jerry_char_t *source_p, /**< source code */
size_t source_size, /**< length of source code */
bool is_strict) /**< source must conform with strict mode */
uint32_t parse_opts) /**< jerry_parse_opts_t option bits */
{
jerry_assert_api_available ();

return jerry_return (ecma_op_eval_chars_buffer ((const lit_utf8_byte_t *) source_p,
source_size,
false,
is_strict));
parse_opts));
} /* jerry_eval */

/**
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jerry_debugger_send_eval (const lit_utf8_byte_t *eval_string_p, /**< evaluated s
JERRY_ASSERT (!(JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_VM_IGNORE));

JERRY_DEBUGGER_SET_FLAGS (JERRY_DEBUGGER_VM_IGNORE);
ecma_value_t result = ecma_op_eval_chars_buffer (eval_string_p + 1, eval_string_size - 1, true, false);
ecma_value_t result = ecma_op_eval_chars_buffer (eval_string_p + 1, eval_string_size - 1, ECMA_PARSE_DIRECT_EVAL);
JERRY_DEBUGGER_CLEAR_FLAGS (JERRY_DEBUGGER_VM_IGNORE);

if (!ECMA_IS_VALUE_ERROR (result))
Expand Down
11 changes: 11 additions & 0 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ typedef enum
ECMA_TYPE___MAX = ECMA_TYPE_ERROR /** highest value for ecma types */
} ecma_type_t;


/**
* Option flags for script parsing.
*/
typedef enum
{
ECMA_PARSE_NO_OPTS = 0, /**< no options passed */
ECMA_PARSE_STRICT_MODE = (1 << 0), /**< enable strict mode */
ECMA_PARSE_DIRECT_EVAL = (1 << 1) /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
} ecma_parse_opts_t;

/**
* Description of an ecma value
*
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/ecma/builtin-objects/ecma-builtin-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p,
arguments_buffer_size,
function_body_buffer_p,
function_body_buffer_size,
false,
ECMA_PARSE_NO_OPTS,
&bytecode_data_p);

if (!ECMA_IS_VALUE_ERROR (ret_value))
Expand Down
15 changes: 5 additions & 10 deletions jerry-core/ecma/builtin-objects/ecma-builtin-global.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@ ecma_builtin_global_object_eval (ecma_value_t this_arg, /**< this argument */
JERRY_UNUSED (this_arg);
ecma_value_t ret_value = ECMA_VALUE_EMPTY;

bool is_direct_eval = vm_is_direct_eval_form_call ();
uint32_t parse_opts = vm_is_direct_eval_form_call () ? ECMA_PARSE_DIRECT_EVAL : ECMA_PARSE_NO_OPTS;

/* See also: ECMA-262 v5, 10.1.1 */
bool is_called_from_strict_mode_code;
if (is_direct_eval)
if (parse_opts && vm_is_strict_mode ())
{
is_called_from_strict_mode_code = vm_is_strict_mode ();
}
else
{
is_called_from_strict_mode_code = false;
JERRY_ASSERT (parse_opts & ECMA_PARSE_DIRECT_EVAL);
parse_opts |= ECMA_PARSE_STRICT_MODE;
}

if (!ecma_is_value_string (x))
Expand All @@ -85,8 +81,7 @@ ecma_builtin_global_object_eval (ecma_value_t this_arg, /**< this argument */
{
/* steps 2 to 8 */
ret_value = ecma_op_eval (ecma_get_string_from_value (x),
is_direct_eval,
is_called_from_strict_mode_code);
parse_opts);
}

return ret_value;
Expand Down
23 changes: 12 additions & 11 deletions jerry-core/ecma/operations/ecma-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
*/
ecma_value_t
ecma_op_eval (ecma_string_t *code_p, /**< code string */
bool is_direct, /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
bool is_called_from_strict_mode_code) /**< is eval is called from strict mode code */
uint32_t parse_opts) /**< ecma_parse_opts_t option bits */
{
ecma_value_t ret_value;

Expand All @@ -61,8 +60,7 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */

ret_value = ecma_op_eval_chars_buffer (code_utf8_buffer_p,
chars_num,
is_direct,
is_called_from_strict_mode_code);
parse_opts);

ECMA_FINALIZE_UTF8_STRING (code_utf8_buffer_p, code_utf8_buffer_size);
}
Expand All @@ -82,15 +80,19 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */
ecma_value_t
ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters buffer */
size_t code_buffer_size, /**< size of the buffer */
bool is_direct, /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
bool is_called_from_strict_mode_code) /**< is eval is called from strict mode code */
uint32_t parse_opts) /**< ecma_parse_opts_t option bits */
{
#ifndef JERRY_DISABLE_JS_PARSER
JERRY_ASSERT (code_p != NULL);

ecma_compiled_code_t *bytecode_data_p;

bool is_strict_call = (is_direct && is_called_from_strict_mode_code);
uint32_t is_strict_call = ECMA_PARSE_STRICT_MODE | ECMA_PARSE_DIRECT_EVAL;

if ((parse_opts & is_strict_call) != is_strict_call)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's an equivalent conversion. In the previous version the is_strict_call variable was only true, if both options were true. So it means whether one of the ECMA_PARSE_STRICT_MODE or ECMA_PARSE_DIRECT_EVAL flags are not enabled, the script must be executed in non-strict mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

{
parse_opts &= (uint32_t) ~ECMA_PARSE_STRICT_MODE;
}

#ifdef JERRY_ENABLE_LINE_INFO
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
Expand All @@ -100,20 +102,19 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
0,
code_p,
code_buffer_size,
is_strict_call,
parse_opts,
&bytecode_data_p);

if (ECMA_IS_VALUE_ERROR (parse_status))
{
return parse_status;
}

return vm_run_eval (bytecode_data_p, is_direct);
return vm_run_eval (bytecode_data_p, parse_opts);
#else /* JERRY_DISABLE_JS_PARSER */
JERRY_UNUSED (code_p);
JERRY_UNUSED (code_buffer_size);
JERRY_UNUSED (is_direct);
JERRY_UNUSED (is_called_from_strict_mode_code);
JERRY_UNUSED (parse_opts);

return ecma_raise_syntax_error (ECMA_ERR_MSG ("The parser has been disabled."));
#endif /* !JERRY_DISABLE_JS_PARSER */
Expand Down
5 changes: 2 additions & 3 deletions jerry-core/ecma/operations/ecma-eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@
*/

ecma_value_t
ecma_op_eval (ecma_string_t *code_p, bool is_direct, bool is_called_from_strict_mode_code);
ecma_op_eval (ecma_string_t *code_p, uint32_t parse_opts);

ecma_value_t
ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, size_t code_buffer_size, bool is_direct,
bool is_called_from_strict_mode_code);
ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, size_t code_buffer_size, uint32_t parse_opts);

/**
* @}
Expand Down
4 changes: 2 additions & 2 deletions jerry-core/ecma/operations/ecma-function-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
ecma_value_t ret_value = vm_run (bytecode_data_p,
this_binding,
local_env_p,
false,
ECMA_PARSE_NO_OPTS,
arguments_list_p,
arguments_list_len);

Expand Down Expand Up @@ -629,7 +629,7 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
ecma_value_t ret_value = vm_run (bytecode_data_p,
arrow_func_p->this_binding,
local_env_p,
false,
ECMA_PARSE_NO_OPTS,
arguments_list_p,
arguments_list_len);

Expand Down
2 changes: 1 addition & 1 deletion jerry-core/include/jerryscript-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ jerry_value_t jerry_parse_function (const jerry_char_t *resource_name_p, size_t
const jerry_char_t *arg_list_p, size_t arg_list_size,
const jerry_char_t *source_p, size_t source_size, uint32_t parse_opts);
jerry_value_t jerry_run (const jerry_value_t func_val);
jerry_value_t jerry_eval (const jerry_char_t *source_p, size_t source_size, bool is_strict);
jerry_value_t jerry_eval (const jerry_char_t *source_p, size_t source_size, uint32_t parse_opts);

jerry_value_t jerry_run_all_enqueued_jobs (void);

Expand Down
10 changes: 5 additions & 5 deletions jerry-core/parser/js/js-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@ 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 */
uint32_t parse_opts, /**< ecma_parse_opts_t option bits */
parser_error_location_t *error_location_p) /**< error location */
{
parser_context_t context;
Expand Down Expand Up @@ -2264,7 +2264,7 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
context.last_context_p = NULL;
context.last_statement.current_p = NULL;

if (strict_mode)
if (parse_opts & ECMA_PARSE_STRICT_MODE)
{
context.status_flags |= PARSER_IS_STRICT;
}
Expand Down Expand Up @@ -2867,7 +2867,7 @@ 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 */
uint32_t parse_opts, /**< ecma_parse_opts_t option bits */
ecma_compiled_code_t **bytecode_data_p) /**< [out] JS bytecode */
{
#ifndef JERRY_DISABLE_JS_PARSER
Expand All @@ -2887,7 +2887,7 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
arg_list_size,
source_p,
source_size,
is_strict,
parse_opts,
&parser_error);

if (!*bytecode_data_p)
Expand Down Expand Up @@ -2959,7 +2959,7 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */
JERRY_UNUSED (arg_list_size);
JERRY_UNUSED (source_p);
JERRY_UNUSED (source_size);
JERRY_UNUSED (is_strict);
JERRY_UNUSED (parse_opts);
JERRY_UNUSED (bytecode_data_p);

return ecma_raise_syntax_error (ECMA_ERR_MSG ("The parser has been disabled."));
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/parser/js/js-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ typedef struct
/* Note: source must be a valid UTF-8 string */
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);
uint32_t parse_opts, ecma_compiled_code_t **bytecode_data_p);

#ifdef JERRY_ENABLE_ERROR_MESSAGES
const char *parser_error_to_string (parser_error_t);
Expand Down
Loading