2016-11-18 14:56:32 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Test specific cases of the lazy/eager-parse decision.
|
|
|
|
//
|
|
|
|
// Note that presently most unit tests for parsing are found in
|
|
|
|
// cctest/test-parsing.cc.
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "include/v8.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2019-04-09 11:15:36 +00:00
|
|
|
#include "src/objects/shared-function-info-inl.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/utils.h"
|
2016-11-18 14:56:32 +00:00
|
|
|
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2016-11-18 14:56:32 +00:00
|
|
|
|
2016-11-24 14:44:59 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Record the 'compiled' state of all top level functions.
|
|
|
|
void GetTopLevelFunctionInfo(
|
|
|
|
v8::Local<v8::Script> script,
|
|
|
|
std::unordered_map<std::string, bool>* is_compiled) {
|
|
|
|
// Get the v8::internal::Script object from the API v8::Script.
|
|
|
|
// The API object 'wraps' the compiled top-level function, not the i::Script.
|
|
|
|
Handle<JSFunction> toplevel_fn = v8::Utils::OpenHandle(*script);
|
2018-06-25 13:12:29 +00:00
|
|
|
SharedFunctionInfo::ScriptIterator iterator(
|
|
|
|
toplevel_fn->GetIsolate(), Script::cast(toplevel_fn->shared().script()));
|
2016-11-24 14:44:59 +00:00
|
|
|
|
2018-11-28 19:57:30 +00:00
|
|
|
for (SharedFunctionInfo shared = iterator.Next(); !shared.is_null();
|
|
|
|
shared = iterator.Next()) {
|
2018-03-16 15:48:17 +00:00
|
|
|
std::unique_ptr<char[]> name = String::cast(shared.Name()).ToCString();
|
2016-11-24 14:44:59 +00:00
|
|
|
is_compiled->insert(std::make_pair(name.get(), shared.is_compiled()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
TEST(GetTopLevelFunctionInfo) {
|
|
|
|
if (!FLAG_lazy) return;
|
|
|
|
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
|
|
|
|
|
|
|
const char src[] = "function foo() { var a; }\n";
|
|
|
|
std::unordered_map<std::string, bool> is_compiled;
|
|
|
|
GetTopLevelFunctionInfo(v8_compile(src), &is_compiled);
|
|
|
|
|
|
|
|
// Test that our helper function GetTopLevelFunctionInfo does what it claims:
|
|
|
|
DCHECK(is_compiled.find("foo") != is_compiled.end());
|
|
|
|
DCHECK(is_compiled.find("bar") == is_compiled.end());
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:56:32 +00:00
|
|
|
TEST(EagerlyCompileImmediateUseFunctions) {
|
|
|
|
if (!FLAG_lazy) return;
|
|
|
|
|
2016-11-24 14:44:59 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
|
|
|
|
2016-11-18 14:56:32 +00:00
|
|
|
// Test parenthesized, exclaimed, and regular functions. Make sure these
|
|
|
|
// occur both intermixed and after each other, to make sure the 'reset'
|
|
|
|
// mechanism works.
|
|
|
|
const char src[] =
|
|
|
|
"function normal() { var a; }\n" // Normal: Should lazy parse.
|
|
|
|
"(function parenthesized() { var b; })()\n" // Parenthesized: Pre-parse.
|
|
|
|
"!function exclaimed() { var c; }() \n" // Exclaimed: Pre-parse.
|
|
|
|
"function normal2() { var d; }\n"
|
|
|
|
"(function parenthesized2() { var e; })()\n"
|
|
|
|
"function normal3() { var f; }\n"
|
|
|
|
"!function exclaimed2() { var g; }() \n"
|
|
|
|
"function normal4() { var h; }\n";
|
|
|
|
|
|
|
|
std::unordered_map<std::string, bool> is_compiled;
|
2016-11-24 14:44:59 +00:00
|
|
|
GetTopLevelFunctionInfo(v8_compile(src), &is_compiled);
|
2016-11-18 14:56:32 +00:00
|
|
|
|
|
|
|
DCHECK(is_compiled["parenthesized"]);
|
|
|
|
DCHECK(is_compiled["parenthesized2"]);
|
|
|
|
DCHECK(is_compiled["exclaimed"]);
|
|
|
|
DCHECK(is_compiled["exclaimed2"]);
|
|
|
|
DCHECK(!is_compiled["normal"]);
|
|
|
|
DCHECK(!is_compiled["normal2"]);
|
|
|
|
DCHECK(!is_compiled["normal3"]);
|
|
|
|
DCHECK(!is_compiled["normal4"]);
|
|
|
|
}
|
2016-11-24 14:44:59 +00:00
|
|
|
|
|
|
|
TEST(CommaFunctionSequence) {
|
|
|
|
if (!FLAG_lazy) return;
|
|
|
|
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
|
|
|
|
|
|
|
const char src[] = "!function a(){}(),function b(){}(),function c(){}();";
|
|
|
|
std::unordered_map<std::string, bool> is_compiled;
|
|
|
|
GetTopLevelFunctionInfo(v8_compile(src), &is_compiled);
|
|
|
|
|
|
|
|
DCHECK(is_compiled["a"]);
|
|
|
|
DCHECK(is_compiled["b"]);
|
|
|
|
DCHECK(is_compiled["c"]);
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|