Create identifier for any global or unscoped variable
Noticed this while running test262 tests that many variables were throwing an invariant for being undefined. This includes things like the special `arguments` object, a global `assert` function used by test262, etc.
Lauren Tan committed
Nov 15, 2022 at 13:32 UTC
ec64edae213019f8fd0ecc58dace3e439e0a635a
1 file changed
+14
-2
compiler/forget/src/HIR/BuildHIR.ts
+14
-2
@@ -38,6 +38,18 @@ const GLOBALS: Map<string, t.Identifier> = new Map([
38
["Math", t.identifier("Math")],
39
]);
40
41
+// TODO: This will work as a stopgap but it isn't really correct. We need proper handling of globals
42
+// and module-scoped variables, which means understanding module constants and imports.
43
+function getOrAddGlobal(identifierName: string): t.Identifier {
44
+ const ident = GLOBALS.get(identifierName);
45
+ if (ident != null) {
46
+ return ident;
47
+ }
48
+ const newIdent = t.identifier(identifierName);
49
+ GLOBALS.set(identifierName, newIdent);
50
+ return newIdent;
51
+}
52
+
53
/**
54
* Lower a function declaration into a control flow graph that models aspects of
55
* control flow that are necessary for memoization. Notably, only control flow
@@ -1095,7 +1107,7 @@ function lowerJsxElementName(
1107
const tag: string = exprPath.node.name;
1108
if (tag.match(/^[A-Z]/)) {
1109
const binding =
1098
- exprPath.scope.getBindingIdentifier(tag) ?? GLOBALS.get(tag);
1110
+ exprPath.scope.getBindingIdentifier(tag) ?? getOrAddGlobal(tag);
1111
invariant(
1112
binding != null,
1113
`Expected to find a binding for variable '%s'`,
@@ -1229,7 +1241,7 @@ function lowerLVal(builder: HIRBuilder, exprPath: NodePath<t.LVal>): Place {
1241
// const name: string = expr.get("name");
1242
const binding =
1243
exprPath.scope.getBindingIdentifier(exprNode.name) ??
1232
- GLOBALS.get(exprNode.name);
1244
+ getOrAddGlobal(exprNode.name);
1245
invariant(
1246
binding != null,
1247
`Expected to find a binding for variable '%s'`,