OLD | NEW |
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 4125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4136 for (size_t i = 0; i < arraysize(const_decl); i++) { | 4136 for (size_t i = 0; i < arraysize(const_decl); i++) { |
4137 std::string script = std::string(prologue) + | 4137 std::string script = std::string(prologue) + |
4138 std::string(const_decl[i].first) + | 4138 std::string(const_decl[i].first) + |
4139 std::string(epilogue); | 4139 std::string(epilogue); |
4140 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); | 4140 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); |
4141 auto callable = tester.GetCallable<>(); | 4141 auto callable = tester.GetCallable<>(); |
4142 | 4142 |
4143 Handle<i::Object> return_value = callable().ToHandleChecked(); | 4143 Handle<i::Object> return_value = callable().ToHandleChecked(); |
4144 CHECK(return_value->SameValue(*const_decl[i].second)); | 4144 CHECK(return_value->SameValue(*const_decl[i].second)); |
4145 } | 4145 } |
4146 | |
4147 // Tests for Legacy constant. | |
4148 bool old_flag_legacy_const = FLAG_legacy_const; | |
4149 FLAG_legacy_const = true; | |
4150 | |
4151 std::pair<const char*, Handle<Object>> legacy_const_decl[] = { | |
4152 {"return outerConst = 23;", handle(Smi::FromInt(23), isolate)}, | |
4153 {"outerConst = 30; return outerConst;", | |
4154 handle(Smi::FromInt(10), isolate)}, | |
4155 }; | |
4156 | |
4157 for (size_t i = 0; i < arraysize(legacy_const_decl); i++) { | |
4158 std::string script = std::string(prologue) + | |
4159 std::string(legacy_const_decl[i].first) + | |
4160 std::string(epilogue); | |
4161 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); | |
4162 auto callable = tester.GetCallable<>(); | |
4163 | |
4164 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
4165 CHECK(return_value->SameValue(*legacy_const_decl[i].second)); | |
4166 } | |
4167 | |
4168 FLAG_legacy_const = old_flag_legacy_const; | |
4169 } | 4146 } |
4170 | 4147 |
4171 TEST(InterpreterIllegalConstDeclaration) { | 4148 TEST(InterpreterIllegalConstDeclaration) { |
4172 HandleAndZoneScope handles; | 4149 HandleAndZoneScope handles; |
4173 | 4150 |
4174 std::pair<const char*, const char*> const_decl[] = { | 4151 std::pair<const char*, const char*> const_decl[] = { |
4175 {"const x = x = 10 + 3; return x;", | 4152 {"const x = x = 10 + 3; return x;", |
4176 "Uncaught ReferenceError: x is not defined"}, | 4153 "Uncaught ReferenceError: x is not defined"}, |
4177 {"const x = 10; x = 20; return x;", | 4154 {"const x = 10; x = 20; return x;", |
4178 "Uncaught TypeError: Assignment to constant variable."}, | 4155 "Uncaught TypeError: Assignment to constant variable."}, |
(...skipping 25 matching lines...) Expand all Loading... |
4204 std::string source(InterpreterTester::SourceForBody(strict_body.c_str())); | 4181 std::string source(InterpreterTester::SourceForBody(strict_body.c_str())); |
4205 InterpreterTester tester(handles.main_isolate(), source.c_str()); | 4182 InterpreterTester tester(handles.main_isolate(), source.c_str()); |
4206 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); | 4183 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); |
4207 v8::Local<v8::String> expected_string = v8_str(const_decl[i].second); | 4184 v8::Local<v8::String> expected_string = v8_str(const_decl[i].second); |
4208 CHECK( | 4185 CHECK( |
4209 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) | 4186 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) |
4210 .FromJust()); | 4187 .FromJust()); |
4211 } | 4188 } |
4212 } | 4189 } |
4213 | 4190 |
4214 TEST(InterpreterLegacyConstDeclaration) { | |
4215 bool old_flag_legacy_const = FLAG_legacy_const; | |
4216 FLAG_legacy_const = true; | |
4217 | |
4218 HandleAndZoneScope handles; | |
4219 i::Isolate* isolate = handles.main_isolate(); | |
4220 | |
4221 std::pair<const char*, Handle<Object>> const_decl[] = { | |
4222 {"const x = (x = 10) + 3; return x;", handle(Smi::FromInt(13), isolate)}, | |
4223 {"const x = 10; x = 20; return x;", handle(Smi::FromInt(10), isolate)}, | |
4224 {"var a = 10;\n" | |
4225 "for (var i = 0; i < 10; ++i) {\n" | |
4226 " const x = i;\n" // Legacy constants are not block scoped. | |
4227 " a = a + x;\n" | |
4228 "}\n" | |
4229 "return a;\n", | |
4230 handle(Smi::FromInt(10), isolate)}, | |
4231 {"const x = 20; eval('x = 10;'); return x;", | |
4232 handle(Smi::FromInt(20), isolate)}, | |
4233 }; | |
4234 | |
4235 for (size_t i = 0; i < arraysize(const_decl); i++) { | |
4236 std::string source(InterpreterTester::SourceForBody(const_decl[i].first)); | |
4237 InterpreterTester tester(handles.main_isolate(), source.c_str()); | |
4238 auto callable = tester.GetCallable<>(); | |
4239 | |
4240 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
4241 CHECK(return_value->SameValue(*const_decl[i].second)); | |
4242 } | |
4243 | |
4244 FLAG_legacy_const = old_flag_legacy_const; | |
4245 } | |
4246 | |
4247 } // namespace interpreter | 4191 } // namespace interpreter |
4248 } // namespace internal | 4192 } // namespace internal |
4249 } // namespace v8 | 4193 } // namespace v8 |
OLD | NEW |