Skip to content

Commit

Permalink
Do not create let/const Variables when TDZ is disabled.
Browse files Browse the repository at this point in the history
Summary:
Ensure that all program declarations are handled as vars (which is almost what Hermes does)
when tdz is disabled, after which ```Variable::getObeysTDZ()``` can be implemented in terms of
the Variable's DeclKind.

Reviewed By: mattbfb

Differential Revision: D39337023

fbshipit-source-id: 2e11262bd9f74554564b2b06398776e75fffa351
  • Loading branch information
jpporto authored and facebook-github-bot committed Sep 20, 2022
1 parent d2555ef commit 439ed6a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
13 changes: 1 addition & 12 deletions include/hermes/IR/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,11 +900,6 @@ class Variable : public Value {
Const,
};

/// Return true if this DeclKind needs to track TDZ.
static bool declKindNeedsTDZ(DeclKind dk) {
return dk != DeclKind::Var;
}

private:
Variable(const Variable &) = delete;
void operator=(const Variable &) = delete;
Expand All @@ -918,9 +913,6 @@ class Variable : public Value {
/// The scope that owns the variable.
VariableScope *parent;

/// If true, this variable obeys the TDZ rules.
bool obeysTDZ_ = false;

protected:
explicit Variable(
ValueKind k,
Expand All @@ -946,10 +938,7 @@ class Variable : public Value {
}

bool getObeysTDZ() const {
return obeysTDZ_;
}
void setObeysTDZ(bool value) {
obeysTDZ_ = value;
return declKind != DeclKind::Var;
}

/// Return the index of this variable in the function's variable list.
Expand Down
11 changes: 4 additions & 7 deletions lib/IRGen/ESTreeIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,12 @@ std::pair<Value *, bool> ESTreeIRGen::declareVariableOrGlobalProperty(
vdc = Variable::DeclKind::Var;
}

auto *var = Builder.createVariable(inFunc->getFunctionScope(), vdc, name);

// For "let" and "const" create the related TDZ flag.
if (Variable::declKindNeedsTDZ(vdc) &&
Mod->getContext().getCodeGenerationSettings().enableTDZ) {
var->setObeysTDZ(true);
if (!Mod->getContext().getCodeGenerationSettings().enableTDZ) {
// short-circuit all declarations to be Var. TDZ dies here.
vdc = Variable::DeclKind::Var;
}

res = var;
res = Builder.createVariable(inFunc->getFunctionScope(), vdc, name);
}

// Register the variable in the scoped hash table.
Expand Down

0 comments on commit 439ed6a

Please sign in to comment.