OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
6 | 6 |
| 7 #include "include/v8.h" |
| 8 #include "src/api.h" |
| 9 #include "src/ast/scopes.h" |
7 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" | 10 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
8 #include "src/flags.h" | 11 #include "src/flags.h" |
9 #include "src/isolate-inl.h" | 12 #include "src/isolate-inl.h" |
| 13 #include "src/parsing/parser.h" |
10 #include "test/unittests/test-utils.h" | 14 #include "test/unittests/test-utils.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
12 | 16 |
13 namespace v8 { | 17 namespace v8 { |
14 namespace internal { | 18 namespace internal { |
15 | 19 |
16 typedef TestWithContext CompilerDispatcherJobTest; | 20 typedef TestWithContext CompilerDispatcherJobTest; |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 const char test_script[] = "x*x"; | 24 const char test_script[] = "(x) { x*x; }"; |
21 | 25 |
22 class ScriptResource : public v8::String::ExternalOneByteStringResource { | 26 class ScriptResource : public v8::String::ExternalOneByteStringResource { |
23 public: | 27 public: |
24 ScriptResource(const char* data, size_t length) | 28 ScriptResource(const char* data, size_t length) |
25 : data_(data), length_(length) {} | 29 : data_(data), length_(length) {} |
26 ~ScriptResource() override = default; | 30 ~ScriptResource() override = default; |
27 | 31 |
28 const char* data() const override { return data_; } | 32 const char* data() const override { return data_; } |
29 size_t length() const override { return length_; } | 33 size_t length() const override { return length_; } |
30 | 34 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 105 |
102 job->PrepareToParseOnMainThread(); | 106 job->PrepareToParseOnMainThread(); |
103 job->Parse(); | 107 job->Parse(); |
104 job->FinalizeParsingOnMainThread(); | 108 job->FinalizeParsingOnMainThread(); |
105 | 109 |
106 ASSERT_TRUE(job->status() == CompileJobStatus::kFailed); | 110 ASSERT_TRUE(job->status() == CompileJobStatus::kFailed); |
107 ASSERT_FALSE(i_isolate()->has_pending_exception()); | 111 ASSERT_FALSE(i_isolate()->has_pending_exception()); |
108 job->ReportErrorsOnMainThread(); | 112 job->ReportErrorsOnMainThread(); |
109 ASSERT_TRUE(job->status() == CompileJobStatus::kDone); | 113 ASSERT_TRUE(job->status() == CompileJobStatus::kDone); |
110 ASSERT_TRUE(i_isolate()->has_pending_exception()); | 114 ASSERT_TRUE(i_isolate()->has_pending_exception()); |
| 115 i_isolate()->clear_pending_exception(); |
| 116 } |
| 117 |
| 118 TEST_F(CompilerDispatcherJobTest, ScopeChain) { |
| 119 const char script[] = |
| 120 "function g() { var g = 1; function f(x) { return x * g }; return f; } " |
| 121 "g();"; |
| 122 Handle<JSFunction> f = Handle<JSFunction>::cast(Utils::OpenHandle( |
| 123 *v8::Script::Compile(isolate()->GetCurrentContext(), |
| 124 v8::String::NewFromUtf8(isolate(), script, |
| 125 v8::NewStringType::kNormal) |
| 126 .ToLocalChecked()) |
| 127 .ToLocalChecked() |
| 128 ->Run(isolate()->GetCurrentContext()) |
| 129 .ToLocalChecked())); |
| 130 |
| 131 std::unique_ptr<CompilerDispatcherJob> job( |
| 132 new CompilerDispatcherJob(i_isolate(), f, FLAG_stack_size)); |
| 133 |
| 134 job->PrepareToParseOnMainThread(); |
| 135 job->Parse(); |
| 136 job->FinalizeParsingOnMainThread(); |
| 137 ASSERT_TRUE(job->status() == CompileJobStatus::kReadyToCompile); |
| 138 |
| 139 const AstRawString* var_x = |
| 140 job->parse_info_->ast_value_factory()->GetOneByteString("x"); |
| 141 Variable* var = job->parse_info_->literal()->scope()->Lookup(var_x); |
| 142 ASSERT_TRUE(var); |
| 143 ASSERT_TRUE(var->IsUnallocated()); |
| 144 |
| 145 const AstRawString* var_g = |
| 146 job->parse_info_->ast_value_factory()->GetOneByteString("g"); |
| 147 var = job->parse_info_->literal()->scope()->Lookup(var_g); |
| 148 ASSERT_TRUE(var); |
| 149 ASSERT_TRUE(var->IsContextSlot()); |
| 150 |
| 151 job->ResetOnMainThread(); |
| 152 ASSERT_TRUE(job->status() == CompileJobStatus::kInitial); |
111 } | 153 } |
112 | 154 |
113 } // namespace internal | 155 } // namespace internal |
114 } // namespace v8 | 156 } // namespace v8 |
OLD | NEW |