Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: test/cctest/interpreter/test-interpreter.cc

Issue 1524803003: [Interpreter] Add support for Load / Store to Lookup slots. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@init_eval_impl
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/execution.h" 7 #include "src/execution.h"
8 #include "src/handles.h" 8 #include "src/handles.h"
9 #include "src/interpreter/bytecode-array-builder.h" 9 #include "src/interpreter/bytecode-array-builder.h"
10 #include "src/interpreter/interpreter.h" 10 #include "src/interpreter/interpreter.h"
(...skipping 3033 matching lines...) Expand 10 before | Expand all | Expand 10 after
3044 InterpreterTester tester(handles.main_isolate(), samples[i].first); 3044 InterpreterTester tester(handles.main_isolate(), samples[i].first);
3045 auto callable = tester.GetCallable<Handle<Object>>(); 3045 auto callable = tester.GetCallable<Handle<Object>>();
3046 Handle<Object> return_val = 3046 Handle<Object> return_val =
3047 callable(handle(Smi::FromInt(arg_value), handles.main_isolate())) 3047 callable(handle(Smi::FromInt(arg_value), handles.main_isolate()))
3048 .ToHandleChecked(); 3048 .ToHandleChecked();
3049 CHECK_EQ(Handle<Smi>::cast(return_val)->value(), samples[i].second); 3049 CHECK_EQ(Handle<Smi>::cast(return_val)->value(), samples[i].second);
3050 } 3050 }
3051 } 3051 }
3052 3052
3053 3053
3054 TEST(InterpreterEval) { 3054 TEST(InterpreterEvalLocal) {
3055 HandleAndZoneScope handles; 3055 HandleAndZoneScope handles;
3056 i::Isolate* isolate = handles.main_isolate(); 3056 i::Isolate* isolate = handles.main_isolate();
3057 3057
3058 std::pair<const char*, Handle<Object>> eval[] = { 3058 std::pair<const char*, Handle<Object>> eval[] = {
3059 std::make_pair("return eval('1;');", handle(Smi::FromInt(1), isolate)), 3059 std::make_pair("return eval('1;');", handle(Smi::FromInt(1), isolate)),
3060 std::make_pair("return eval('100 * 20;');", 3060 std::make_pair("return eval('100 * 20;');",
3061 handle(Smi::FromInt(2000), isolate)), 3061 handle(Smi::FromInt(2000), isolate)),
3062 // TODO(mythria): Add more tests here where eval uses / modifies variables 3062 std::make_pair("var x = 10; return eval('x + 20;');",
3063 // and parameters from the function's scope when we have support for 3063 handle(Smi::FromInt(30), isolate)),
3064 // lookup variables. 3064 std::make_pair("var x = 10; eval('x = 33;'); return x;",
3065 handle(Smi::FromInt(33), isolate)),
3066 std::make_pair("'use strict'; var x = 20; var z = 0; eval('var x = 33; z "
3067 "= x;'); return x + z;",
3068 handle(Smi::FromInt(53), isolate)),
3069 std::make_pair(
3070 "eval('var x = 33;'); eval('var y = x + 20'); return x + y;",
3071 handle(Smi::FromInt(86), isolate)),
3065 }; 3072 };
3066 3073
3067 for (size_t i = 0; i < arraysize(eval); i++) { 3074 for (size_t i = 0; i < arraysize(eval); i++) {
3075 std::string source(InterpreterTester::SourceForBody(eval[i].first));
3076 InterpreterTester tester(handles.main_isolate(), source.c_str());
3077 auto callable = tester.GetCallable<>();
3078
3079 Handle<i::Object> return_value = callable().ToHandleChecked();
3080 CHECK(return_value->SameValue(*eval[i].second));
3081 }
3082 }
3083
3084
3085 TEST(InterpreterEvalParams) {
3086 HandleAndZoneScope handles;
3087 i::Isolate* isolate = handles.main_isolate();
3088
3089 std::pair<const char*, Handle<Object>> eval_params[] = {
3090 std::make_pair("var x = 10; return eval('x + p1;');",
3091 handle(Smi::FromInt(30), isolate)),
3092 std::make_pair("var x = 10; eval('p1 = x;'); return p1;",
3093 handle(Smi::FromInt(10), isolate)),
3094 std::make_pair("var a = 10;"
3095 "function inner() { return eval('a + p1;');}"
3096 "return inner();",
3097 handle(Smi::FromInt(30), isolate)),
3098 };
3099
3100 for (size_t i = 0; i < arraysize(eval_params); i++) {
3101 std::string source = "function " + InterpreterTester::function_name() +
3102 "(p1) {" + eval_params[i].first + "}";
3103 InterpreterTester tester(handles.main_isolate(), source.c_str());
3104 auto callable = tester.GetCallable<Handle<Object>>();
3105
3106 Handle<i::Object> return_value =
3107 callable(handle(Smi::FromInt(20), isolate)).ToHandleChecked();
3108 CHECK(return_value->SameValue(*eval_params[i].second));
3109 }
3110 }
3111
3112
3113 TEST(InterpreterEvalGlobal) {
3114 HandleAndZoneScope handles;
3115 i::Isolate* isolate = handles.main_isolate();
3116
3117 std::pair<const char*, Handle<Object>> eval_global[] = {
3118 std::make_pair("function add_global() { eval('x = 33;'); }; "
3119 "function f() { add_global(); return x; }"
3120 "f();",
3121 handle(Smi::FromInt(33), isolate)),
3122 std::make_pair(
3123 "function add_global() { 'use strict'; eval('x = 33;'); }; "
3124 "function f() { add_global(); return x; }"
3125 "f();",
3126 handle(Smi::FromInt(33), isolate)),
3127 };
3128
3129 for (size_t i = 0; i < arraysize(eval_global); i++) {
3130 InterpreterTester tester(handles.main_isolate(), eval_global[i].first, "*");
3131 auto callable = tester.GetCallable<>();
3132
3133 Handle<i::Object> return_value = callable().ToHandleChecked();
3134 CHECK(return_value->SameValue(*eval_global[i].second));
3135 }
3136 }
3137
3138
3139 TEST(InterpreterEval) {
3140 HandleAndZoneScope handles;
3141 i::Isolate* isolate = handles.main_isolate();
3142 i::Factory* factory = isolate->factory();
3143
3144 const char* func =
3145 "var x = 42;"
3146 "function f(str, p1) {"
3147 " if(p1) { eval(str) }"
3148 " x = x + 10;"
3149 " return x;"
3150 "}"
3151 "f('var x = 10', 1);";
3152
3153 Handle<Object> params[][3] = {
3154 {factory->NewNumberFromInt(30),
3155 factory->NewStringFromStaticChars("var x = 20;"), factory->true_value()},
3156 {factory->NewNumberFromInt(52),
3157 factory->NewStringFromStaticChars("var x = 20;"),
3158 factory->false_value()},
3159 {factory->NewNumberFromInt(52),
3160 factory->NewStringFromStaticChars("'use strict'; var x = 20;"),
3161 factory->true_value()},
3162 {factory->NewNumberFromInt(52),
3163 factory->NewStringFromStaticChars("'use strict'; var x = 20;"),
3164 factory->false_value()},
3165 {factory->NewNumberFromInt(62),
3166 factory->NewStringFromStaticChars("for(i = 0; i < 10; i++) x = x + 1;"),
3167 factory->true_value()},
3168 };
3169
3170 for (size_t i = 0; i < arraysize(params); i++) {
3171 InterpreterTester tester(handles.main_isolate(), func);
3172 auto callable = tester.GetCallable<Handle<Object>, Handle<Object>>();
3173 Handle<i::Object> return_value =
3174 callable(params[i][1], params[i][2]).ToHandleChecked();
3175 CHECK(return_value->SameValue(*params[i][0]));
3176 }
3177 }
3178
3179
3180 TEST(InterpreterEvalInsideTypeOf) {
3181 HandleAndZoneScope handles;
3182 i::Isolate* isolate = handles.main_isolate();
3183 i::Factory* factory = isolate->factory();
3184
3185 std::pair<const char*, Handle<Object>> eval[] = {
3186 std::make_pair("var x = 10; eval('x + 20;'); return typeof x;",
3187 factory->NewStringFromStaticChars("number")),
3188 std::make_pair("eval('var y = 10;');"
3189 "return typeof x;",
3190 factory->NewStringFromStaticChars("undefined")),
3191 std::make_pair("'use strict';"
3192 "eval('var y = 10;');"
3193 "return typeof x;",
3194 factory->NewStringFromStaticChars("undefined")),
3195 std::make_pair("eval('var x = 10;'); return typeof x;",
3196 factory->NewStringFromStaticChars("number")),
3197 std::make_pair("var x = {};"
3198 "eval('var x = 10;');"
3199 "return typeof x;",
3200 factory->NewStringFromStaticChars("number")),
3201 std::make_pair("'use strict';"
3202 "var x = {};"
3203 "eval('var x = 10;');"
3204 "return typeof x;",
3205 factory->NewStringFromStaticChars("object")),
3206 };
3207
3208 for (size_t i = 0; i < arraysize(eval); i++) {
3068 std::string source(InterpreterTester::SourceForBody(eval[i].first)); 3209 std::string source(InterpreterTester::SourceForBody(eval[i].first));
3069 InterpreterTester tester(handles.main_isolate(), source.c_str()); 3210 InterpreterTester tester(handles.main_isolate(), source.c_str());
3070 auto callable = tester.GetCallable<>(); 3211 auto callable = tester.GetCallable<>();
3071 3212
3072 Handle<i::Object> return_value = callable().ToHandleChecked(); 3213 Handle<i::Object> return_value = callable().ToHandleChecked();
3073 CHECK(return_value->SameValue(*eval[i].second)); 3214 CHECK(return_value->SameValue(*eval[i].second));
3074 } 3215 }
3075 } 3216 }
3076 3217
3077 } // namespace interpreter 3218 } // namespace interpreter
3078 } // namespace internal 3219 } // namespace internal
3079 } // namespace v8 3220 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698