From 5d5e75fad66d53c84c00cc1eabf2783264caba3a Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Thu, 11 Jun 2015 12:11:08 +0300 Subject: [PATCH] Implementing 'eval' routine of built-in Global object. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com --- .../builtin-objects/ecma-builtin-global.cpp | 34 ++++++- jerry-core/ecma/operations/ecma-eval.cpp | 4 +- tests/jerry/eval.js | 88 +++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/jerry/eval.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp index e9017c6d9..f7d6db0ba 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp @@ -16,11 +16,13 @@ #include "ecma-alloc.h" #include "ecma-builtins.h" #include "ecma-conversion.h" +#include "ecma-eval.h" #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" #include "ecma-try-catch-macro.h" #include "jrt.h" +#include "vm.h" #define ECMA_BUILTINS_INTERNAL #include "ecma-builtins-internal.h" @@ -52,7 +54,37 @@ static ecma_completion_value_t ecma_builtin_global_object_eval (ecma_value_t this_arg, /**< this argument */ ecma_value_t x) /**< routine's first argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, x); + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + bool is_direct_eval = vm_is_direct_eval_form_call (); + JERRY_ASSERT (!(is_direct_eval + && !ecma_is_value_undefined (this_arg))); + + /* See also: ECMA-262 v5, 10.1.1 */ + bool is_called_from_strict_mode_code; + if (is_direct_eval) + { + is_called_from_strict_mode_code = vm_is_strict_mode (); + } + else + { + is_called_from_strict_mode_code = false; + } + + if (!ecma_is_value_string (x)) + { + /* step 1 */ + ret_value = ecma_make_normal_completion_value (ecma_copy_value (x, true)); + } + else + { + /* steps 2 to 8 */ + ret_value = ecma_op_eval (ecma_get_string_from_value (x), + is_direct_eval, + is_called_from_strict_mode_code); + } + + return ret_value; } /* ecma_builtin_global_object_eval */ /** diff --git a/jerry-core/ecma/operations/ecma-eval.cpp b/jerry-core/ecma/operations/ecma-eval.cpp index 97f456d9d..85c777204 100644 --- a/jerry-core/ecma/operations/ecma-eval.cpp +++ b/jerry-core/ecma/operations/ecma-eval.cpp @@ -35,7 +35,7 @@ * * See also: * ecma_op_eval_chars_buffer - * ECMA-262 v5, 15.1.2.1 + * ECMA-262 v5, 15.1.2.1 (steps 2 to 8) * * @return completion value */ @@ -72,7 +72,7 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */ * * See also: * ecma_op_eval - * ECMA-262 v5, 15.1.2.1 + * ECMA-262 v5, 15.1.2.1 (steps 2 to 8) * * @return completion value */ diff --git a/tests/jerry/eval.js b/tests/jerry/eval.js new file mode 100644 index 000000000..22eb187fd --- /dev/null +++ b/tests/jerry/eval.js @@ -0,0 +1,88 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +assert (eval () === undefined); +assert (eval (undefined) === undefined); +assert (eval (null) === null); +assert (eval (true) === true); +assert (eval (false) === false); +assert (eval (1) === 1); +assert (eval (eval) === eval); + +/* Indirect eval */ +function f1() +{ + var v1 = 'local value'; + + assert (v1 === 'local value'); + assert (typeof (this.v1) === 'undefined'); + + r = this.eval ('var v1 = "global value";'); + + assert (v1 === 'local value'); + assert (this.v1 === 'global value'); + assert (r === undefined); +}; + +f1 (); + +/* Direct eval from strict mode code */ +function f2 (global) +{ + 'use strict'; + var v2 = 'local value'; + + assert (v2 === 'local value'); + assert (typeof (global.v2) === 'undefined'); + + r = eval ('var v2 = "global value";'); + + assert (v2 === 'local value'); + assert (typeof (global.v2) === 'undefined'); + assert (r === undefined); +} + +f2 (this); + +var y; + +for (var i = 0; i < 100; i++) +{ + var r = eval ('var x =' + ' 1;'); + assert (typeof (x) === 'number'); + assert (r === undefined); + + delete x; + assert (typeof (x) === 'undefined'); + + r = eval ('"use ' + 's' + 't' + 'r' + 'i' + 'c' + 't"; va' + 'r x = 1;'); + assert (typeof (x) === 'undefined'); + assert (r === "use strict"); + + y = 'str'; + assert (typeof (y) === 'string'); + + delete y; + assert (typeof (y) === 'string'); + + r = eval ('var y = "another ' + 'string";'); + assert (y === 'another string'); + assert (r == undefined); + + delete y; + assert (typeof (y) === 'string'); + + r = eval ('if (true) 3; else 5;'); + assert (r === 3); +}