Skip to content

Commit 5d5e75f

Browse files
Implementing 'eval' routine of built-in Global object.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
1 parent b310d76 commit 5d5e75f

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
/* Indirect eval */
24+
function f1()
25+
{
26+
var v1 = 'local value';
27+
28+
assert (v1 === 'local value');
29+
assert (typeof (this.v1) === 'undefined');
30+
31+
r = this.eval ('var v1 = "global value";');
32+
33+
assert (v1 === 'local value');
34+
assert (this.v1 === 'global value');
35+
assert (r === undefined);
36+
};
37+
38+
f1 ();
39+
40+
/* Direct eval from strict mode code */
41+
function f2 (global)
42+
{
43+
'use strict';
44+
var v2 = 'local value';
45+
46+
assert (v2 === 'local value');
47+
assert (typeof (global.v2) === 'undefined');
48+
49+
r = eval ('var v2 = "global value";');
50+
51+
assert (v2 === 'local value');
52+
assert (typeof (global.v2) === 'undefined');
53+
assert (r === undefined);
54+
}
55+
56+
f2 (this);
57+
58+
var y;
59+
60+
for (var i = 0; i < 100; i++)
61+
{
62+
var r = eval ('var x =' + ' 1;');
63+
assert (typeof (x) === 'number');
64+
assert (r === undefined);
65+
66+
delete x;
67+
assert (typeof (x) === 'undefined');
68+
69+
r = eval ('"use ' + 's' + 't' + 'r' + 'i' + 'c' + 't"; va' + 'r x = 1;');
70+
assert (typeof (x) === 'undefined');
71+
assert (r === "use strict");
72+
73+
y = 'str';
74+
assert (typeof (y) === 'string');
75+
76+
delete y;
77+
assert (typeof (y) === 'string');
78+
79+
r = eval ('var y = "another ' + 'string";');
80+
assert (y === 'another string');
81+
assert (r == undefined);
82+
83+
delete y;
84+
assert (typeof (y) === 'string');
85+
86+
r = eval ('if (true) 3; else 5;');
87+
assert (r === 3);
88+
}

0 commit comments

Comments
 (0)