Skip to content

Commit 101b78b

Browse files
Implementing 'eval' routine of built-in Global object.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent a665fa7 commit 101b78b

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include "ecma-alloc.h"
1717
#include "ecma-builtins.h"
1818
#include "ecma-conversion.h"
19+
#include "ecma-eval.h"
1920
#include "ecma-gc.h"
2021
#include "ecma-globals.h"
2122
#include "ecma-helpers.h"
2223
#include "ecma-try-catch-macro.h"
2324
#include "jrt.h"
25+
#include "vm.h"
2426

2527
#define ECMA_BUILTINS_INTERNAL
2628
#include "ecma-builtins-internal.h"
@@ -52,7 +54,37 @@ static ecma_completion_value_t
5254
ecma_builtin_global_object_eval (ecma_value_t this_arg, /**< this argument */
5355
ecma_value_t x) /**< routine's first argument */
5456
{
55-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, x);
57+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
58+
59+
bool is_direct_eval = vm_is_direct_eval_form_call ();
60+
JERRY_ASSERT (!(is_direct_eval
61+
&& !ecma_is_value_undefined (this_arg)));
62+
63+
/* See also: ECMA-262 v5, 10.1.1 */
64+
bool is_called_from_strict_mode_code;
65+
if (is_direct_eval)
66+
{
67+
is_called_from_strict_mode_code = vm_is_strict_mode ();
68+
}
69+
else
70+
{
71+
is_called_from_strict_mode_code = false;
72+
}
73+
74+
if (!ecma_is_value_string (x))
75+
{
76+
/* step 1 */
77+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (x, true));
78+
}
79+
else
80+
{
81+
/* steps 2 to 8 */
82+
ret_value = ecma_op_eval (ecma_get_string_from_value (x),
83+
is_direct_eval,
84+
is_called_from_strict_mode_code);
85+
}
86+
87+
return ret_value;
5688
} /* ecma_builtin_global_object_eval */
5789

5890
/**

jerry-core/ecma/operations/ecma-eval.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* See also:
3737
* ecma_op_eval_chars_buffer
38-
* ECMA-262 v5, 15.1.2.1
38+
* ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
3939
*
4040
* @return completion value
4141
*/
@@ -72,7 +72,7 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */
7272
*
7373
* See also:
7474
* ecma_op_eval
75-
* ECMA-262 v5, 15.1.2.1
75+
* ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
7676
*
7777
* @return completion value
7878
*/

tests/jerry/eval.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
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+
assert (eval () === undefined);
16+
assert (eval (undefined) === undefined);
17+
assert (eval (null) === null);
18+
assert (eval (true) === true);
19+
assert (eval (false) === false);
20+
assert (eval (1) === 1);
21+
assert (eval (eval) === eval);
22+
23+
var y;
24+
25+
for (var i = 0; i < 100; i++)
26+
{
27+
var r = eval ('var x =' + ' 1;');
28+
assert (typeof (x) === 'number');
29+
assert (r === undefined);
30+
31+
delete x;
32+
assert (typeof (x) === 'undefined');
33+
34+
r = eval ('"use ' + 's' + 't' + 'r' + 'i' + 'c' + 't"; va' + 'r x = 1;');
35+
assert (typeof (x) === 'undefined');
36+
assert (r === "use strict");
37+
38+
y = 'str';
39+
assert (typeof (y) === 'string');
40+
41+
delete y;
42+
assert (typeof (y) === 'string');
43+
44+
r = eval ('var y = "another ' + 'string";');
45+
assert (y === 'another string');
46+
assert (r == undefined);
47+
48+
delete y;
49+
assert (typeof (y) === 'string');
50+
51+
r = eval ('if (true) 3; else 5;');
52+
assert (r === 3);
53+
}

0 commit comments

Comments
 (0)