Skip to content

Commit

Permalink
Lift restriction on serializing a single scope at a time
Browse files Browse the repository at this point in the history
Summary:
Since we only ever lazily compiled one layer of functions at a time, we only
serialized a single scope and asserted that this was sufficient.

This diff lifts this restriction, so that we can lazily compile multiple depths
in one go.

#utd-hermes-ignore-android

Reviewed By: tmikov

Differential Revision: D23580248

fbshipit-source-id: 449cd68c2bc155dac5564fb154da229e37c834f5
  • Loading branch information
willholen authored and facebook-github-bot committed Sep 8, 2020
1 parent e0cd6e0 commit cba0416
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 11 additions & 14 deletions lib/IRGen/ESTreeIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,32 +1279,29 @@ void ESTreeIRGen::addLexicalDebugInfo(
addLexicalDebugInfo(current, global, scope->parentScope);
}

#ifndef HERMESVM_LEAN
std::shared_ptr<SerializedScope> ESTreeIRGen::saveCurrentScope() {
auto *func = curFunction()->function;
assert(func && "Missing function when saving scope");
std::shared_ptr<SerializedScope> ESTreeIRGen::serializeScope(
FunctionContext *ctx,
bool includeGlobal) {
// Serialize the global scope if and only if it's the only scope.
// We serialize the global scope to avoid re-declaring variables,
// and only do it once to avoid creating spurious scopes.
if (!ctx || (ctx->function->isGlobalScope() && !includeGlobal))
return lexicalScopeChain;

auto scope = std::make_shared<SerializedScope>();
auto *func = ctx->function;
assert(func && "Missing function when saving scope");

// We currently only lazy compile a single level at a time. If we later start
// compiling multiple, this method would need to walk the scopes.
assert(
((func->isGlobalScope() && !curFunction()->getPreviousContext()) ||
(!func->isGlobalScope() && curFunction()->getPreviousContext() &&
!curFunction()->getPreviousContext()->getPreviousContext())) &&
"Expected exactly one function on the stack.");

scope->parentScope = lexicalScopeChain;
scope->originalName = func->getOriginalOrInferredName();
if (auto *closure = func->getLazyClosureAlias()) {
scope->closureAlias = closure->getName();
}
for (auto *var : func->getFunctionScope()->getVariables()) {
scope->variables.push_back(var->getName());
}
scope->parentScope = serializeScope(ctx->getPreviousContext(), false);
return scope;
}
#endif

} // namespace irgen
} // namespace hermes
10 changes: 9 additions & 1 deletion lib/IRGen/ESTreeIRGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,15 @@ class ESTreeIRGen {
const std::shared_ptr<const SerializedScope> &scope);

/// Save all variables currently in scope, for lazy compilation.
std::shared_ptr<SerializedScope> saveCurrentScope();
std::shared_ptr<SerializedScope> saveCurrentScope() {
return serializeScope(curFunction(), true);
}

/// Recursively serialize scopes. The global scope is serialized
/// if and only if it's the first scope and includeGlobal is true.
std::shared_ptr<SerializedScope> serializeScope(
FunctionContext *ctx,
bool includeGlobal);
};

template <typename EB, typename EF, typename EH>
Expand Down

0 comments on commit cba0416

Please sign in to comment.