Commit 9d0f5ab7 authored by vogelheim's avatar vogelheim Committed by Commit bot

Treat all functions in a 'comma sequence' the same for (pre-)parsing.

R=verwaest@chromium.org
BUG=v8:5643

Review-Url: https://codereview.chromium.org/2524263003
Cr-Commit-Position: refs/heads/master@{#41267}
parent 56daccb8
......@@ -462,6 +462,10 @@ class ParserBase {
return next_function_is_likely_called_;
}
bool previous_function_was_likely_called() const {
return previous_function_was_likely_called_;
}
void set_next_function_is_likely_called() {
next_function_is_likely_called_ = true;
}
......@@ -505,11 +509,12 @@ class ParserBase {
ZoneList<typename ExpressionClassifier::Error> reported_errors_;
// Record whether the next (=== immediately following) function literal is
// preceded by a parenthesis / exclamation mark.
// The FunctionState constructor will reset a parents'
// next_function_is_likely_called_ to prevent it from being 'reused' in the
// next function literal.
// preceded by a parenthesis / exclamation mark. Also record the previous
// state.
// These are managed by the FunctionState constructor; the caller may only
// call set_next_function_is_likely_called.
bool next_function_is_likely_called_;
bool previous_function_was_likely_called_;
friend Impl;
friend class Checkpoint;
......@@ -1461,9 +1466,12 @@ ParserBase<Impl>::FunctionState::FunctionState(
return_expr_context_(ReturnExprContext::kInsideValidBlock),
non_patterns_to_rewrite_(0, scope->zone()),
reported_errors_(16, scope->zone()),
next_function_is_likely_called_(false) {
next_function_is_likely_called_(false),
previous_function_was_likely_called_(false) {
*function_state_stack = this;
if (outer_function_state_) {
outer_function_state_->previous_function_was_likely_called_ =
outer_function_state_->next_function_is_likely_called_;
outer_function_state_->next_function_is_likely_called_ = false;
}
}
......@@ -1895,6 +1903,13 @@ ParserBase<Impl>::ParseExpressionCoverGrammar(bool accept_IN, bool* ok) {
// a trailing comma is allowed at the end of an arrow parameter list
break;
}
// Pass on the 'set_next_function_is_likely_called' flag if we have
// several function literals separated by comma.
if (peek() == Token::FUNCTION &&
function_state_->previous_function_was_likely_called()) {
function_state_->set_next_function_is_likely_called();
}
}
return result;
......
......@@ -19,9 +19,50 @@
using namespace v8::internal;
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);
Handle<Script> i_script =
handle(Script::cast(toplevel_fn->shared()->script()));
WeakFixedArray::Iterator iter(i_script->shared_function_infos());
while (SharedFunctionInfo* shared = iter.Next<SharedFunctionInfo>()) {
std::unique_ptr<char[]> name = String::cast(shared->name())->ToCString();
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());
}
TEST(EagerlyCompileImmediateUseFunctions) {
if (!FLAG_lazy) return;
Isolate* isolate = CcTest::i_isolate();
HandleScope scope(isolate);
LocalContext env;
// Test parenthesized, exclaimed, and regular functions. Make sure these
// occur both intermixed and after each other, to make sure the 'reset'
// mechanism works.
......@@ -35,27 +76,8 @@ TEST(EagerlyCompileImmediateUseFunctions) {
"!function exclaimed2() { var g; }() \n"
"function normal4() { var h; }\n";
Isolate* isolate = CcTest::i_isolate();
HandleScope scope(isolate);
LocalContext env;
// Compile src & record the 'compiled' state of all top level functions in
// is_compiled.
std::unordered_map<std::string, bool> is_compiled;
{
v8::Local<v8::Script> api_script = v8_compile(src);
Handle<JSFunction> toplevel_fn = v8::Utils::OpenHandle(*api_script);
Handle<Script> script =
handle(Script::cast(toplevel_fn->shared()->script()));
WeakFixedArray::Iterator iter(script->shared_function_infos());
while (SharedFunctionInfo* shared = iter.Next<SharedFunctionInfo>()) {
std::unique_ptr<char[]> name = String::cast(shared->name())->ToCString();
is_compiled[name.get()] = shared->is_compiled();
}
}
DCHECK(is_compiled.find("normal") != is_compiled.end());
GetTopLevelFunctionInfo(v8_compile(src), &is_compiled);
DCHECK(is_compiled["parenthesized"]);
DCHECK(is_compiled["parenthesized2"]);
......@@ -66,3 +88,19 @@ TEST(EagerlyCompileImmediateUseFunctions) {
DCHECK(!is_compiled["normal3"]);
DCHECK(!is_compiled["normal4"]);
}
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"]);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment