Skip to content

Commit

Permalink
Reenable lazy compilation in formal parameters
Browse files Browse the repository at this point in the history
Summary:
This works properly now.

Partial revert of D58147864 (didn't want to revert the test).

Reviewed By: tmikov

Differential Revision: D58532227

fbshipit-source-id: 5608761366506c9eb51d6a182249432c00bb1808
  • Loading branch information
avp authored and facebook-github-bot committed Nov 12, 2024
1 parent 6d165dc commit a4defc9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
9 changes: 1 addition & 8 deletions lib/Parser/JSParserImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,6 @@ bool JSParserImpl::parseFormalParameters(
Param param,
ESTree::NodeList &paramList) {
assert(check(TokenKind::l_paren) && "FormalParameters must start with '('");

llvh::SaveAndRestore oldFormalParams{isFormalParams_, true};

// (
SMLoc lparenLoc = advance().Start;

Expand Down Expand Up @@ -718,11 +715,7 @@ Optional<ESTree::BlockStatementNode *> JSParserImpl::parseFunctionBody(
bool paramAwait,
JSLexer::GrammarContext grammarContext,
bool parseDirectives) {
// Disable lazy compilation in formal parameters,
// because initializer expressions capture a different environment than the
// environment the function body names are added to, and it would add some
// complexity to optimize a use case that's never actually encountered.
if (pass_ == LazyParse && !eagerly && !isFormalParams_) {
if (pass_ == LazyParse && !eagerly) {
auto startLoc = tok_->getStartLoc();
assert(
preParsed_->functionInfo.count(startLoc) == 1 &&
Expand Down
5 changes: 0 additions & 5 deletions lib/Parser/JSParserImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ class JSParserImpl {
/// so we can recover directive nodes back in the lazyParse pass.
llvh::SmallVector<UniqueString *, 1> seenDirectives_{};

/// Whether we're currently parsing formal parameters of a function.
/// Saved/restored by parseFormalParameters.
/// Used to control lazy compilation, which is disabled in parameters.
bool isFormalParams_ = false;

/// Whether the current function is an arrow function.
/// Only set/restored by SaveFunctionState when entering/exiting a new
/// function.
Expand Down
34 changes: 34 additions & 0 deletions test/hermes/lazy-capture-in-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,37 @@
print(paramsFunc());
// CHECK: outside
})();

(function arrowCapture() {
var x = 'outside';
var paramsFunc;

function foo(
_ = (paramsFunc = () => {
// Capture the outer 'x'
return x;
})
) {
let x = 'inside';
}
foo();

print(paramsFunc());
// CHECK: outside
})();

(function setCapture() {
var x = 'outside';
var probeParams;

var obj = {
set a(_ = probeParams = function() { return x; }) {
var x = 'inside';
}
};
// Force default params on setter to trigger.
obj.a = undefined;

print(probeParams());
// CHECK: outside
})();

0 comments on commit a4defc9

Please sign in to comment.