| 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 3990 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4001 for (size_t i = 0; i < arraysize(const_decl); i++) { | 4001 for (size_t i = 0; i < arraysize(const_decl); i++) { |
| 4002 std::string script = std::string(prologue) + | 4002 std::string script = std::string(prologue) + |
| 4003 std::string(const_decl[i].first) + | 4003 std::string(const_decl[i].first) + |
| 4004 std::string(epilogue); | 4004 std::string(epilogue); |
| 4005 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); | 4005 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); |
| 4006 auto callable = tester.GetCallable<>(); | 4006 auto callable = tester.GetCallable<>(); |
| 4007 | 4007 |
| 4008 Handle<i::Object> return_value = callable().ToHandleChecked(); | 4008 Handle<i::Object> return_value = callable().ToHandleChecked(); |
| 4009 CHECK(return_value->SameValue(*const_decl[i].second)); | 4009 CHECK(return_value->SameValue(*const_decl[i].second)); |
| 4010 } | 4010 } |
| 4011 | |
| 4012 // Tests for Legacy constant. | |
| 4013 bool old_flag_legacy_const = FLAG_legacy_const; | |
| 4014 FLAG_legacy_const = true; | |
| 4015 | |
| 4016 std::pair<const char*, Handle<Object>> legacy_const_decl[] = { | |
| 4017 {"return outerConst = 23;", handle(Smi::FromInt(23), isolate)}, | |
| 4018 {"outerConst = 30; return outerConst;", | |
| 4019 handle(Smi::FromInt(10), isolate)}, | |
| 4020 }; | |
| 4021 | |
| 4022 for (size_t i = 0; i < arraysize(legacy_const_decl); i++) { | |
| 4023 std::string script = std::string(prologue) + | |
| 4024 std::string(legacy_const_decl[i].first) + | |
| 4025 std::string(epilogue); | |
| 4026 InterpreterTester tester(handles.main_isolate(), script.c_str(), "*"); | |
| 4027 auto callable = tester.GetCallable<>(); | |
| 4028 | |
| 4029 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
| 4030 CHECK(return_value->SameValue(*legacy_const_decl[i].second)); | |
| 4031 } | |
| 4032 | |
| 4033 FLAG_legacy_const = old_flag_legacy_const; | |
| 4034 } | 4011 } |
| 4035 | 4012 |
| 4036 TEST(InterpreterIllegalConstDeclaration) { | 4013 TEST(InterpreterIllegalConstDeclaration) { |
| 4037 HandleAndZoneScope handles; | 4014 HandleAndZoneScope handles; |
| 4038 | 4015 |
| 4039 std::pair<const char*, const char*> const_decl[] = { | 4016 std::pair<const char*, const char*> const_decl[] = { |
| 4040 {"const x = x = 10 + 3; return x;", | 4017 {"const x = x = 10 + 3; return x;", |
| 4041 "Uncaught ReferenceError: x is not defined"}, | 4018 "Uncaught ReferenceError: x is not defined"}, |
| 4042 {"const x = 10; x = 20; return x;", | 4019 {"const x = 10; x = 20; return x;", |
| 4043 "Uncaught TypeError: Assignment to constant variable."}, | 4020 "Uncaught TypeError: Assignment to constant variable."}, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 4069 std::string source(InterpreterTester::SourceForBody(strict_body.c_str())); | 4046 std::string source(InterpreterTester::SourceForBody(strict_body.c_str())); |
| 4070 InterpreterTester tester(handles.main_isolate(), source.c_str()); | 4047 InterpreterTester tester(handles.main_isolate(), source.c_str()); |
| 4071 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); | 4048 v8::Local<v8::String> message = tester.CheckThrowsReturnMessage()->Get(); |
| 4072 v8::Local<v8::String> expected_string = v8_str(const_decl[i].second); | 4049 v8::Local<v8::String> expected_string = v8_str(const_decl[i].second); |
| 4073 CHECK( | 4050 CHECK( |
| 4074 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) | 4051 message->Equals(CcTest::isolate()->GetCurrentContext(), expected_string) |
| 4075 .FromJust()); | 4052 .FromJust()); |
| 4076 } | 4053 } |
| 4077 } | 4054 } |
| 4078 | 4055 |
| 4079 TEST(InterpreterLegacyConstDeclaration) { | |
| 4080 bool old_flag_legacy_const = FLAG_legacy_const; | |
| 4081 FLAG_legacy_const = true; | |
| 4082 | |
| 4083 HandleAndZoneScope handles; | |
| 4084 i::Isolate* isolate = handles.main_isolate(); | |
| 4085 | |
| 4086 std::pair<const char*, Handle<Object>> const_decl[] = { | |
| 4087 {"const x = (x = 10) + 3; return x;", handle(Smi::FromInt(13), isolate)}, | |
| 4088 {"const x = 10; x = 20; return x;", handle(Smi::FromInt(10), isolate)}, | |
| 4089 {"var a = 10;\n" | |
| 4090 "for (var i = 0; i < 10; ++i) {\n" | |
| 4091 " const x = i;\n" // Legacy constants are not block scoped. | |
| 4092 " a = a + x;\n" | |
| 4093 "}\n" | |
| 4094 "return a;\n", | |
| 4095 handle(Smi::FromInt(10), isolate)}, | |
| 4096 {"const x = 20; eval('x = 10;'); return x;", | |
| 4097 handle(Smi::FromInt(20), isolate)}, | |
| 4098 }; | |
| 4099 | |
| 4100 for (size_t i = 0; i < arraysize(const_decl); i++) { | |
| 4101 std::string source(InterpreterTester::SourceForBody(const_decl[i].first)); | |
| 4102 InterpreterTester tester(handles.main_isolate(), source.c_str()); | |
| 4103 auto callable = tester.GetCallable<>(); | |
| 4104 | |
| 4105 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
| 4106 CHECK(return_value->SameValue(*const_decl[i].second)); | |
| 4107 } | |
| 4108 | |
| 4109 FLAG_legacy_const = old_flag_legacy_const; | |
| 4110 } | |
| 4111 | |
| 4112 } // namespace interpreter | 4056 } // namespace interpreter |
| 4113 } // namespace internal | 4057 } // namespace internal |
| 4114 } // namespace v8 | 4058 } // namespace v8 |
| OLD | NEW |