Chromium Code Reviews| 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 "src/compiler-dispatcher/compiler-dispatcher.h" | 5 #include "src/compiler-dispatcher/compiler-dispatcher.h" |
| 6 | 6 |
| 7 #include "include/v8-platform.h" | 7 #include "include/v8-platform.h" |
| 8 #include "src/base/platform/semaphore.h" | 8 #include "src/base/platform/semaphore.h" |
| 9 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" | 9 #include "src/compiler-dispatcher/compiler-dispatcher-job.h" |
| 10 #include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" | 10 #include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" |
| 11 #include "src/compiler.h" | 11 #include "src/compiler.h" |
| 12 #include "src/flags.h" | 12 #include "src/flags.h" |
| 13 #include "src/handles.h" | 13 #include "src/handles.h" |
| 14 #include "src/objects-inl.h" | 14 #include "src/objects-inl.h" |
| 15 #include "src/parsing/parse-info.h" | 15 #include "src/parsing/parse-info.h" |
| 16 #include "src/v8.h" | 16 #include "src/v8.h" |
| 17 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" | 17 #include "test/unittests/compiler-dispatcher/compiler-dispatcher-helper.h" |
| 18 #include "test/unittests/test-utils.h" | 18 #include "test/unittests/test-utils.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace v8 { | 21 namespace v8 { |
| 22 namespace internal { | 22 namespace internal { |
| 23 | 23 |
| 24 class CompilerDispatcherTestFlags { | |
| 25 public: | |
| 26 static void SetFlagsForTest() { | |
| 27 old_compiler_dispatcher_flag_ = i::FLAG_compiler_dispatcher; | |
| 28 i::FLAG_compiler_dispatcher = true; | |
| 29 old_ignition_flag_ = i::FLAG_ignition; | |
| 30 i::FLAG_ignition = true; | |
| 31 old_allow_natives_syntax_flag_ = i::FLAG_allow_natives_syntax; | |
| 32 i::FLAG_allow_natives_syntax = true; | |
| 33 } | |
| 34 | |
| 35 static void RestoreFlags() { | |
| 36 i::FLAG_compiler_dispatcher = old_compiler_dispatcher_flag_; | |
| 37 i::FLAG_ignition = old_ignition_flag_; | |
| 38 i::FLAG_allow_natives_syntax = old_allow_natives_syntax_flag_; | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 static bool old_compiler_dispatcher_flag_; | |
| 43 static bool old_ignition_flag_; | |
| 44 static bool old_allow_natives_syntax_flag_; | |
| 45 | |
| 46 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilerDispatcherTestFlags); | |
| 47 }; | |
| 48 | |
| 49 bool CompilerDispatcherTestFlags::old_compiler_dispatcher_flag_; | |
| 50 bool CompilerDispatcherTestFlags::old_ignition_flag_; | |
| 51 bool CompilerDispatcherTestFlags::old_allow_natives_syntax_flag_; | |
| 52 | |
| 24 class CompilerDispatcherTest : public TestWithContext { | 53 class CompilerDispatcherTest : public TestWithContext { |
| 25 public: | 54 public: |
| 26 CompilerDispatcherTest() = default; | 55 CompilerDispatcherTest() = default; |
| 27 ~CompilerDispatcherTest() override = default; | 56 ~CompilerDispatcherTest() override = default; |
| 28 | 57 |
| 29 static void SetUpTestCase() { | 58 static void SetUpTestCase() { |
| 30 old_flag_ = i::FLAG_ignition; | 59 CompilerDispatcherTestFlags::SetFlagsForTest(); |
| 31 i::FLAG_compiler_dispatcher = true; | |
| 32 old_ignition_flag_ = i::FLAG_ignition; | |
| 33 i::FLAG_ignition = true; | |
| 34 TestWithContext::SetUpTestCase(); | 60 TestWithContext::SetUpTestCase(); |
| 35 } | 61 } |
| 36 | 62 |
| 37 static void TearDownTestCase() { | 63 static void TearDownTestCase() { |
| 38 TestWithContext::TearDownTestCase(); | 64 TestWithContext::TearDownTestCase(); |
| 39 i::FLAG_compiler_dispatcher = old_flag_; | 65 CompilerDispatcherTestFlags::RestoreFlags(); |
| 40 i::FLAG_ignition = old_ignition_flag_; | |
| 41 } | 66 } |
| 42 | 67 |
| 43 private: | 68 private: |
| 44 static bool old_flag_; | |
| 45 static bool old_ignition_flag_; | |
| 46 | |
| 47 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); | 69 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTest); |
| 48 }; | 70 }; |
| 49 | 71 |
| 50 bool CompilerDispatcherTest::old_flag_; | 72 class CompilerDispatcherTestWithoutContext : public v8::TestWithIsolate { |
| 51 bool CompilerDispatcherTest::old_ignition_flag_; | 73 public: |
| 74 CompilerDispatcherTestWithoutContext() = default; | |
| 75 ~CompilerDispatcherTestWithoutContext() override = default; | |
| 76 | |
| 77 static void SetUpTestCase() { | |
| 78 CompilerDispatcherTestFlags::SetFlagsForTest(); | |
| 79 TestWithContext::SetUpTestCase(); | |
| 80 } | |
| 81 | |
| 82 static void TearDownTestCase() { | |
| 83 TestWithContext::TearDownTestCase(); | |
| 84 CompilerDispatcherTestFlags::RestoreFlags(); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTestWithoutContext); | |
| 89 }; | |
| 52 | 90 |
| 53 namespace { | 91 namespace { |
| 54 | 92 |
| 55 class MockPlatform : public v8::Platform { | 93 class MockPlatform : public v8::Platform { |
| 56 public: | 94 public: |
| 57 MockPlatform() : time_(0.0), time_step_(0.0), idle_task_(nullptr), sem_(0) {} | 95 MockPlatform() : time_(0.0), time_step_(0.0), idle_task_(nullptr), sem_(0) {} |
| 58 ~MockPlatform() override { | 96 ~MockPlatform() override { |
| 59 base::LockGuard<base::Mutex> lock(&mutex_); | 97 base::LockGuard<base::Mutex> lock(&mutex_); |
| 60 EXPECT_TRUE(foreground_tasks_.empty()); | 98 EXPECT_TRUE(foreground_tasks_.empty()); |
| 61 EXPECT_TRUE(background_tasks_.empty()); | 99 EXPECT_TRUE(background_tasks_.empty()); |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 893 | 931 |
| 894 // Finishing removes the SFI from the queue. | 932 // Finishing removes the SFI from the queue. |
| 895 ASSERT_FALSE(dispatcher.IsEnqueued(shared1)); | 933 ASSERT_FALSE(dispatcher.IsEnqueued(shared1)); |
| 896 ASSERT_FALSE(dispatcher.IsEnqueued(shared2)); | 934 ASSERT_FALSE(dispatcher.IsEnqueued(shared2)); |
| 897 ASSERT_TRUE(shared1->is_compiled()); | 935 ASSERT_TRUE(shared1->is_compiled()); |
| 898 ASSERT_TRUE(shared2->is_compiled()); | 936 ASSERT_TRUE(shared2->is_compiled()); |
| 899 ASSERT_TRUE(platform.IdleTaskPending()); | 937 ASSERT_TRUE(platform.IdleTaskPending()); |
| 900 platform.ClearIdleTask(); | 938 platform.ClearIdleTask(); |
| 901 } | 939 } |
| 902 | 940 |
| 941 static const char kExtensionSource[] = "native function Dummy();"; | |
|
jochen (gone - plz use gerrit)
2017/02/08 18:52:58
put this (and the class) in an anonymous namespace
rmcilroy
2017/02/08 22:49:23
Done.
| |
| 942 | |
| 943 class MockNativeFunctionExtension : public Extension { | |
| 944 public: | |
| 945 MockNativeFunctionExtension() | |
| 946 : Extension("mock-extension", kExtensionSource), function_(&Dummy) {} | |
| 947 | |
| 948 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | |
| 949 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
| 950 return v8::FunctionTemplate::New(isolate, function_); | |
| 951 } | |
| 952 | |
| 953 static void Dummy(const v8::FunctionCallbackInfo<v8::Value>& args) { return; } | |
| 954 | |
| 955 private: | |
| 956 v8::FunctionCallback function_; | |
| 957 }; | |
|
jochen (gone - plz use gerrit)
2017/02/08 18:52:58
disallow copy/assign
rmcilroy
2017/02/08 22:49:23
Done.
| |
| 958 | |
| 959 TEST_F(CompilerDispatcherTestWithoutContext, CompileExtensionWithoutContext) { | |
| 960 MockPlatform platform; | |
| 961 CompilerDispatcher dispatcher(i_isolate(), &platform, FLAG_stack_size); | |
| 962 Local<v8::Context> context = v8::Context::New(isolate()); | |
| 963 | |
| 964 MockNativeFunctionExtension extension; | |
| 965 Handle<String> script_str = | |
| 966 i_isolate() | |
| 967 ->factory() | |
| 968 ->NewStringFromUtf8(CStrVector(kExtensionSource)) | |
| 969 .ToHandleChecked(); | |
| 970 Handle<Script> script = i_isolate()->factory()->NewScript(script_str); | |
| 971 script->set_type(Script::TYPE_EXTENSION); | |
| 972 | |
| 973 ParseInfo parse_info(script); | |
| 974 parse_info.set_extension(&extension); | |
| 975 | |
| 976 std::shared_ptr<DeferredHandles> handles; | |
| 977 Handle<SharedFunctionInfo> shared; | |
| 978 { | |
| 979 v8::Context::Scope scope(context); | |
| 980 ASSERT_TRUE(Compiler::ParseAndAnalyze(&parse_info)); | |
| 981 | |
| 982 shared = i_isolate()->factory()->NewSharedFunctionInfoForLiteral( | |
| 983 parse_info.literal(), script); | |
| 984 parse_info.set_shared_info(shared); | |
| 985 | |
| 986 ASSERT_FALSE(platform.IdleTaskPending()); | |
| 987 ASSERT_TRUE(dispatcher.Enqueue(shared, parse_info.literal(), | |
| 988 parse_info.zone_shared(), handles, handles)); | |
| 989 ASSERT_TRUE(platform.IdleTaskPending()); | |
| 990 } | |
| 991 // Exit the context scope before running the idle task. | |
| 992 | |
| 993 // Since time doesn't progress on the MockPlatform, this is enough idle time | |
| 994 // to finish compiling the function. | |
| 995 platform.RunIdleTask(1000.0, 0.0); | |
| 996 | |
| 997 ASSERT_FALSE(dispatcher.IsEnqueued(shared)); | |
| 998 ASSERT_TRUE(shared->is_compiled()); | |
| 999 } | |
| 1000 | |
| 903 } // namespace internal | 1001 } // namespace internal |
| 904 } // namespace v8 | 1002 } // namespace v8 |
| OLD | NEW |