1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecma-builtins.h"
17 #include "ecma-exceptions.h"
18 #include "ecma-eval.h"
19 #include "ecma-gc.h"
20 #include "ecma-globals.h"
21 #include "ecma-helpers.h"
22 #include "ecma-lex-env.h"
23 #include "js-parser.h"
24 #include "vm.h"
25 #include "jcontext.h"
26
27 /** \addtogroup ecma ECMA
28 * @{
29 *
30 * \addtogroup eval eval
31 * @{
32 */
33
34 /**
35 * Perform 'eval' with code stored in ecma-string
36 *
37 * See also:
38 * ecma_op_eval_chars_buffer
39 * ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
40 *
41 * @return ecma value
42 */
43 ecma_value_t
ecma_op_eval(ecma_string_t * code_p,uint32_t parse_opts)44 ecma_op_eval (ecma_string_t *code_p, /**< code string */
45 uint32_t parse_opts) /**< ecma_parse_opts_t option bits */
46 {
47 ecma_value_t ret_value;
48
49 #if defined (JERRY_BUILTIN_EVAL_DISABLED) && ENABLED (JERRY_BUILTIN_EVAL_DISABLED)
50 JERRY_UNUSED(code_p);
51 JERRY_UNUSED(parse_opts);
52 ret_value = ECMA_VALUE_UNDEFINED;
53 #else /* ENABLED (JERRY_BUILTIN_EVAL_DISABLED) */
54 lit_utf8_size_t chars_num = ecma_string_get_size (code_p);
55 if (chars_num == 0)
56 {
57 ret_value = ECMA_VALUE_UNDEFINED;
58 }
59 else
60 {
61 ECMA_STRING_TO_UTF8_STRING (code_p, code_utf8_buffer_p, code_utf8_buffer_size);
62
63 ret_value = ecma_op_eval_chars_buffer (code_utf8_buffer_p,
64 chars_num,
65 parse_opts);
66
67 ECMA_FINALIZE_UTF8_STRING (code_utf8_buffer_p, code_utf8_buffer_size);
68 }
69 #endif /* ENABLED (JERRY_BUILTIN_EVAL_DISABLED) */
70
71 return ret_value;
72 } /* ecma_op_eval */
73
74 /**
75 * Perform 'eval' with code stored in continuous character buffer
76 *
77 * See also:
78 * ecma_op_eval
79 * ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
80 *
81 * @return ecma value
82 */
83 ecma_value_t
ecma_op_eval_chars_buffer(const lit_utf8_byte_t * code_p,size_t code_buffer_size,uint32_t parse_opts)84 ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters buffer */
85 size_t code_buffer_size, /**< size of the buffer */
86 uint32_t parse_opts) /**< ecma_parse_opts_t option bits */
87 {
88 #if ENABLED (JERRY_PARSER)
89 JERRY_ASSERT (code_p != NULL);
90
91 ecma_compiled_code_t *bytecode_data_p;
92
93 uint32_t is_strict_call = ECMA_PARSE_STRICT_MODE | ECMA_PARSE_DIRECT_EVAL;
94
95 if ((parse_opts & is_strict_call) != is_strict_call)
96 {
97 parse_opts &= (uint32_t) ~ECMA_PARSE_STRICT_MODE;
98 }
99
100 parse_opts |= ECMA_PARSE_EVAL;
101
102 #if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES)
103 JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL);
104 #endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */
105
106 #if ENABLED (JERRY_ES2015)
107 ECMA_CLEAR_LOCAL_PARSE_OPTS ();
108 #endif /* ENABLED (JERRY_ES2015) */
109
110 ecma_value_t parse_status = parser_parse_script (NULL,
111 0,
112 code_p,
113 code_buffer_size,
114 parse_opts,
115 &bytecode_data_p);
116
117 if (ECMA_IS_VALUE_ERROR (parse_status))
118 {
119 return parse_status;
120 }
121
122 return vm_run_eval (bytecode_data_p, parse_opts);
123 #else /* !ENABLED (JERRY_PARSER) */
124 JERRY_UNUSED (code_p);
125 JERRY_UNUSED (code_buffer_size);
126 JERRY_UNUSED (parse_opts);
127
128 return ecma_raise_syntax_error (ECMA_ERR_MSG ("The parser has been disabled."));
129 #endif /* ENABLED (JERRY_PARSER) */
130 } /* ecma_op_eval_chars_buffer */
131
132 /**
133 * @}
134 * @}
135 */
136