diff options
Diffstat (limited to 'deps/v8/test/unittests/api/v8-script-unittest.cc')
-rw-r--r-- | deps/v8/test/unittests/api/v8-script-unittest.cc | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/deps/v8/test/unittests/api/v8-script-unittest.cc b/deps/v8/test/unittests/api/v8-script-unittest.cc index 98040cf662..79de0c2c67 100644 --- a/deps/v8/test/unittests/api/v8-script-unittest.cc +++ b/deps/v8/test/unittests/api/v8-script-unittest.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <algorithm> + #include "include/v8-context.h" #include "include/v8-isolate.h" #include "include/v8-local-handle.h" @@ -157,5 +159,173 @@ TEST_F(ScriptTest, GetEmptyStalledTopLevelAwaitMessage) { {}); } +TEST_F(ScriptTest, ProduceCompileHints) { + const char* url = "http://www.foo.com/foo.js"; + v8::ScriptOrigin origin(isolate(), NewString(url), 13, 0); + + const char* code = "function lazy1() {} function lazy2() {} lazy1();"; + v8::ScriptCompiler::Source script_source(NewString(code), origin); + + // Test producing compile hints. + { + Local<Script> script = + v8::ScriptCompiler::Compile( + v8_context(), &script_source, + v8::ScriptCompiler::CompileOptions::kProduceCompileHints) + .ToLocalChecked(); + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(0u, compile_hints.size()); + } + + v8::Local<v8::Context> context = v8::Context::New(isolate()); + v8::MaybeLocal<v8::Value> result = script->Run(context); + EXPECT_FALSE(result.IsEmpty()); + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(1u, compile_hints.size()); + EXPECT_EQ(14, compile_hints[0]); + } + + // The previous data is cleared if we retrieve compile hints again. + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(0u, compile_hints.size()); + } + + // Call the other lazy function and retrieve compile hints again. + const char* code2 = "lazy2();"; + v8::ScriptCompiler::Source script_source2(NewString(code2), origin); + + Local<Script> script2 = + v8::ScriptCompiler::Compile(v8_context(), &script_source2) + .ToLocalChecked(); + v8::MaybeLocal<v8::Value> result2 = script2->Run(context); + EXPECT_FALSE(result2.IsEmpty()); + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(1u, compile_hints.size()); + EXPECT_EQ(34, compile_hints[0]); + } + } + + // Test that compile hints are not produced unless the relevant compile option + // is set. + { + Local<Script> script = + v8::ScriptCompiler::Compile(v8_context(), &script_source) + .ToLocalChecked(); + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(0u, compile_hints.size()); + } + + v8::Local<v8::Context> context = v8::Context::New(isolate()); + v8::MaybeLocal<v8::Value> result = script->Run(context); + EXPECT_FALSE(result.IsEmpty()); + { + auto compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(0u, compile_hints.size()); + } + } +} + +namespace { +bool CompileHintsCallback(int position, void* data) { + std::vector<int>* hints = reinterpret_cast<std::vector<int>*>(data); + return std::find(hints->begin(), hints->end(), position) != hints->end(); +} +} // namespace + +TEST_F(ScriptTest, LocalCompileHints) { + const char* url = "http://www.foo.com/foo.js"; + v8::ScriptOrigin origin(isolate(), NewString(url), 13, 0); + v8::Local<v8::Context> context = v8::Context::New(isolate()); + + // Produce compile hints. + std::vector<int> compile_hints; + { + // Run the top level code. + const char* code = "function lazy1() {} function lazy2() {}"; + v8::ScriptCompiler::Source script_source(NewString(code), origin); + Local<Script> script = + v8::ScriptCompiler::Compile( + v8_context(), &script_source, + v8::ScriptCompiler::CompileOptions::kProduceCompileHints) + .ToLocalChecked(); + + v8::MaybeLocal<v8::Value> result = script->Run(context); + EXPECT_FALSE(result.IsEmpty()); + + // Run lazy1. + const char* code2 = "lazy1();"; + v8::ScriptCompiler::Source script_source2(NewString(code2), origin); + + Local<Script> script2 = + v8::ScriptCompiler::Compile(v8_context(), &script_source2) + .ToLocalChecked(); + v8::MaybeLocal<v8::Value> result2 = script2->Run(context); + EXPECT_FALSE(result2.IsEmpty()); + + // Retrieve compile hints. + compile_hints = script->GetProducedCompileHints(); + EXPECT_EQ(1u, compile_hints.size()); + } + + // Consume compile hints. We use the produced compile hints to test that the + // positions of the requested compile hints match the positions of the + // produced compile hints. + { + // Artificially change the code so that the isolate cache won't hit. + const char* code = "function lazy1() {} function lazy2() {} //"; + v8::ScriptCompiler::Source script_source( + NewString(code), origin, CompileHintsCallback, + reinterpret_cast<void*>(&compile_hints)); + Local<Script> script = + v8::ScriptCompiler::Compile( + v8_context(), &script_source, + v8::ScriptCompiler::CompileOptions::kConsumeCompileHints) + .ToLocalChecked(); + USE(script); + + // Retrieve the function object for lazy1. + { + const char* code2 = "lazy1"; + v8::ScriptCompiler::Source script_source2(NewString(code2), origin); + + Local<Script> script2 = + v8::ScriptCompiler::Compile(v8_context(), &script_source2) + .ToLocalChecked(); + v8::MaybeLocal<v8::Value> result2 = script2->Run(context); + + auto function = i::Handle<i::JSFunction>::cast( + Utils::OpenHandle(*result2.ToLocalChecked())); + i::Builtin builtin = function->code().builtin_id(); + + // lazy1 was not compiled lazily (there was a compile hint for it). + EXPECT_NE(i::Builtin::kCompileLazy, builtin); + } + + // Retrieve the function object for lazy2. + { + const char* code2 = "lazy2"; + v8::ScriptCompiler::Source script_source2(NewString(code2), origin); + + Local<Script> script2 = + v8::ScriptCompiler::Compile(v8_context(), &script_source2) + .ToLocalChecked(); + v8::MaybeLocal<v8::Value> result2 = script2->Run(context); + + auto function = i::Handle<i::JSFunction>::cast( + Utils::OpenHandle(*result2.ToLocalChecked())); + + i::Builtin builtin = function->code().builtin_id(); + + // lazy2 was compiled lazily (there was no compile hint for it). + EXPECT_EQ(i::Builtin::kCompileLazy, builtin); + } + } +} + } // namespace } // namespace v8 |