@next-core/cook 2.3.0 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/ExecutionContext.js +57 -12
- package/dist/cjs/ExecutionContext.js.map +1 -1
- package/dist/cjs/context-free.js +2 -0
- package/dist/cjs/context-free.js.map +1 -1
- package/dist/cjs/cook.js +136 -43
- package/dist/cjs/cook.js.map +1 -1
- package/dist/esm/ExecutionContext.js +56 -9
- package/dist/esm/ExecutionContext.js.map +1 -1
- package/dist/esm/context-free.js +2 -0
- package/dist/esm/context-free.js.map +1 -1
- package/dist/esm/cook.js +137 -44
- package/dist/esm/cook.js.map +1 -1
- package/dist/types/ExecutionContext.d.ts +27 -8
- package/dist/types/cook.d.ts +3 -1
- package/package.json +2 -2
|
@@ -3,9 +3,25 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.IsConstructor = exports.FunctionEnvironment = exports.FormalParameters = exports.ExecutionContext = exports.EnvironmentRecord = exports.Environment = exports.Empty = exports.ECMAScriptCode = exports.DeclarativeEnvironment = exports.DebuggerScope = exports.DebuggerNode = exports.DebuggerCall = exports.CompletionRecord = void 0;
|
|
6
|
+
exports.Mode = exports.IsConstructor = exports.FunctionEnvironment = exports.FormalParameters = exports.ExecutionContext = exports.EnvironmentRecord = exports.Environment = exports.Empty = exports.ECMAScriptCode = exports.DeclarativeEnvironment = exports.DebuggerScope = exports.DebuggerReturn = exports.DebuggerNode = exports.DebuggerCall = exports.CompletionRecord = exports.BindingStatus = void 0;
|
|
7
7
|
exports.NormalCompletion = NormalCompletion;
|
|
8
|
-
exports.SourceNode = exports.ReferenceRecord = void 0;
|
|
8
|
+
exports.ThisMode = exports.SourceNode = exports.ReferenceRecord = void 0;
|
|
9
|
+
let Mode = exports.Mode = /*#__PURE__*/function (Mode) {
|
|
10
|
+
Mode[Mode["LEXICAL"] = 0] = "LEXICAL";
|
|
11
|
+
Mode[Mode["STRICT"] = 1] = "STRICT";
|
|
12
|
+
return Mode;
|
|
13
|
+
}({});
|
|
14
|
+
const SourceNode = exports.SourceNode = Symbol.for("SourceNode");
|
|
15
|
+
const FormalParameters = exports.FormalParameters = Symbol.for("FormalParameters");
|
|
16
|
+
const ECMAScriptCode = exports.ECMAScriptCode = Symbol.for("ECMAScriptCode");
|
|
17
|
+
const Environment = exports.Environment = Symbol.for("Environment");
|
|
18
|
+
const IsConstructor = exports.IsConstructor = Symbol.for("IsConstructor");
|
|
19
|
+
const ThisMode = exports.ThisMode = Symbol.for("ThisMode");
|
|
20
|
+
const DebuggerCall = exports.DebuggerCall = Symbol.for("$DebuggerCall$");
|
|
21
|
+
const DebuggerScope = exports.DebuggerScope = Symbol.for("$DebuggerScope$");
|
|
22
|
+
const DebuggerNode = exports.DebuggerNode = Symbol.for("$DebuggerNode$");
|
|
23
|
+
const DebuggerReturn = exports.DebuggerReturn = Symbol.for("$DebuggerReturn$");
|
|
24
|
+
|
|
9
25
|
// https://tc39.es/ecma262/#sec-execution-contexts
|
|
10
26
|
class ExecutionContext {
|
|
11
27
|
VariableEnvironment;
|
|
@@ -13,10 +29,17 @@ class ExecutionContext {
|
|
|
13
29
|
Function;
|
|
14
30
|
}
|
|
15
31
|
exports.ExecutionContext = ExecutionContext;
|
|
16
|
-
|
|
32
|
+
let BindingStatus = exports.BindingStatus = /*#__PURE__*/function (BindingStatus) {
|
|
33
|
+
BindingStatus[BindingStatus["UNINITIALIZED"] = 0] = "UNINITIALIZED";
|
|
34
|
+
BindingStatus[BindingStatus["LEXICAL"] = 1] = "LEXICAL";
|
|
35
|
+
BindingStatus[BindingStatus["INITIALIZED"] = 2] = "INITIALIZED";
|
|
36
|
+
return BindingStatus;
|
|
37
|
+
}({}); // https://tc39.es/ecma262/#sec-environment-records
|
|
17
38
|
class EnvironmentRecord {
|
|
18
39
|
OuterEnv;
|
|
19
40
|
bindingMap = new Map();
|
|
41
|
+
ThisValue = undefined;
|
|
42
|
+
ThisBindingStatus;
|
|
20
43
|
constructor(outer) {
|
|
21
44
|
this.OuterEnv = outer;
|
|
22
45
|
}
|
|
@@ -84,20 +107,42 @@ class EnvironmentRecord {
|
|
|
84
107
|
}
|
|
85
108
|
return binding.value;
|
|
86
109
|
}
|
|
110
|
+
HasThisBinding() {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
87
113
|
}
|
|
88
114
|
exports.EnvironmentRecord = EnvironmentRecord;
|
|
89
115
|
class DeclarativeEnvironment extends EnvironmentRecord {}
|
|
90
116
|
exports.DeclarativeEnvironment = DeclarativeEnvironment;
|
|
91
|
-
class FunctionEnvironment extends EnvironmentRecord {
|
|
117
|
+
class FunctionEnvironment extends EnvironmentRecord {
|
|
118
|
+
constructor(F) {
|
|
119
|
+
super(F[Environment]);
|
|
120
|
+
if (F[ThisMode] === Mode.LEXICAL) {
|
|
121
|
+
this.ThisBindingStatus = BindingStatus.LEXICAL;
|
|
122
|
+
} else {
|
|
123
|
+
this.ThisBindingStatus = BindingStatus.UNINITIALIZED;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
HasThisBinding() {
|
|
127
|
+
return this.ThisBindingStatus !== BindingStatus.LEXICAL;
|
|
128
|
+
}
|
|
129
|
+
BindThisValue(value) {
|
|
130
|
+
// Assert: envRec.[[ThisBindingStatus]] is not LEXICAL.
|
|
131
|
+
if (this.ThisBindingStatus === BindingStatus.INITIALIZED) {
|
|
132
|
+
throw new Error("This binding has been initialized");
|
|
133
|
+
}
|
|
134
|
+
this.ThisValue = value;
|
|
135
|
+
this.ThisBindingStatus = BindingStatus.INITIALIZED;
|
|
136
|
+
}
|
|
137
|
+
GetThisBinding() {
|
|
138
|
+
// Assert: envRec.[[ThisBindingStatus]] is not LEXICAL.
|
|
139
|
+
if (this.ThisBindingStatus === BindingStatus.UNINITIALIZED) {
|
|
140
|
+
throw new Error("This binding is not initialized");
|
|
141
|
+
}
|
|
142
|
+
return this.ThisValue;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
92
145
|
exports.FunctionEnvironment = FunctionEnvironment;
|
|
93
|
-
const SourceNode = exports.SourceNode = Symbol.for("SourceNode");
|
|
94
|
-
const FormalParameters = exports.FormalParameters = Symbol.for("FormalParameters");
|
|
95
|
-
const ECMAScriptCode = exports.ECMAScriptCode = Symbol.for("ECMAScriptCode");
|
|
96
|
-
const Environment = exports.Environment = Symbol.for("Environment");
|
|
97
|
-
const IsConstructor = exports.IsConstructor = Symbol.for("IsConstructor");
|
|
98
|
-
const DebuggerCall = exports.DebuggerCall = Symbol.for("$DebuggerCall$");
|
|
99
|
-
const DebuggerScope = exports.DebuggerScope = Symbol.for("$DebuggerScope$");
|
|
100
|
-
const DebuggerNode = exports.DebuggerNode = Symbol.for("$DebuggerNode$");
|
|
101
146
|
// https://tc39.es/ecma262/#sec-reference-record-specification-type
|
|
102
147
|
class ReferenceRecord {
|
|
103
148
|
Base;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExecutionContext.js","names":["ExecutionContext","VariableEnvironment","LexicalEnvironment","Function","exports","EnvironmentRecord","OuterEnv","bindingMap","Map","constructor","outer","HasBinding","name","has","CreateMutableBinding","deletable","set","mutable","NormalCompletion","undefined","CreateImmutableBinding","strict","InitializeBinding","value","binding","get","Object","assign","initialized","SetMutableBinding","_strict","ReferenceError","TypeError","GetBindingValue","DeclarativeEnvironment","FunctionEnvironment","SourceNode","Symbol","for","FormalParameters","ECMAScriptCode","Environment","IsConstructor","DebuggerCall","DebuggerScope","DebuggerNode","ReferenceRecord","Base","ReferenceName","Strict","base","referenceName","CompletionRecord","Type","Value","type","Empty"],"sources":["../../src/ExecutionContext.ts"],"sourcesContent":["import type {\n ArrowFunctionExpression,\n Expression,\n FunctionDeclaration,\n FunctionExpression,\n Statement,\n} from \"@babel/types\";\nimport type { EstreeNode } from \"./interfaces.js\";\n\n// https://tc39.es/ecma262/#sec-execution-contexts\nexport class ExecutionContext {\n VariableEnvironment?: EnvironmentRecord;\n LexicalEnvironment?: EnvironmentRecord;\n Function?: FunctionObject;\n}\n\nexport type EnvironmentRecordType = \"function\" | \"declarative\";\n\n// https://tc39.es/ecma262/#sec-environment-records\nexport class EnvironmentRecord {\n readonly OuterEnv: EnvironmentRecord | null | undefined;\n private readonly bindingMap = new Map<string, BindingState>();\n\n constructor(outer: EnvironmentRecord | null | undefined) {\n this.OuterEnv = outer;\n }\n\n HasBinding(name: string): boolean {\n return this.bindingMap.has(name);\n }\n\n CreateMutableBinding(name: string, deletable: boolean): CompletionRecord {\n // Assert: binding does not exist.\n this.bindingMap.set(name, {\n mutable: true,\n deletable,\n });\n return NormalCompletion(undefined);\n }\n\n /**\n * Create an immutable binding.\n *\n * @param name - The binding name.\n * @param strict - For named function expressions, strict is false, otherwise it's true.\n * @returns CompletionRecord.\n */\n CreateImmutableBinding(name: string, strict: boolean): CompletionRecord {\n // Assert: binding does not exist.\n this.bindingMap.set(name, {\n strict,\n });\n return NormalCompletion(undefined);\n }\n\n InitializeBinding(name: string, value: unknown): CompletionRecord {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists and uninitialized.\n Object.assign<BindingState, Partial<BindingState>>(binding, {\n initialized: true,\n value,\n });\n return NormalCompletion(undefined);\n }\n\n /**\n * Update a mutable binding value, including function declarations.\n *\n * @param name - The binding name.\n * @param value - The binding value.\n * @param strict - For functions, strict is always false, otherwise it depends on strict-mode.\n * @returns\n */\n SetMutableBinding(\n name: string,\n value: unknown,\n _strict?: boolean\n ): CompletionRecord {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists.\n if (!binding.initialized) {\n throw new ReferenceError(`${name} is not initialized`);\n } else if (binding.mutable) {\n binding.value = value;\n } else {\n throw new TypeError(`Assignment to constant variable`);\n }\n return NormalCompletion(undefined);\n }\n\n GetBindingValue(name: string, _strict?: boolean): unknown {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists.\n if (!binding.initialized) {\n throw new ReferenceError(`${name} is not initialized`);\n }\n return binding.value;\n }\n}\n\nexport class DeclarativeEnvironment extends EnvironmentRecord {}\n\nexport class FunctionEnvironment extends EnvironmentRecord {}\n\nexport interface BindingState {\n initialized?: boolean;\n value?: unknown;\n mutable?: boolean;\n\n /** This is used for mutable bindings only. */\n deletable?: boolean;\n\n /**\n * This is used for immutable bindings only.\n * For named function expressions, `strict` is false,\n * otherwise it's true.\n */\n strict?: boolean;\n}\n\nexport const SourceNode = Symbol.for(\"SourceNode\");\nexport const FormalParameters = Symbol.for(\"FormalParameters\");\nexport const ECMAScriptCode = Symbol.for(\"ECMAScriptCode\");\nexport const Environment = Symbol.for(\"Environment\");\nexport const IsConstructor = Symbol.for(\"IsConstructor\");\nexport const DebuggerCall = Symbol.for(\"$DebuggerCall$\");\nexport const DebuggerScope = Symbol.for(\"$DebuggerScope$\");\nexport const DebuggerNode = Symbol.for(\"$DebuggerNode$\");\n\nexport interface FunctionObject {\n (...args: unknown[]): unknown;\n [SourceNode]:\n | FunctionDeclaration\n | FunctionExpression\n | ArrowFunctionExpression;\n [FormalParameters]: FunctionDeclaration[\"params\"];\n [ECMAScriptCode]: Statement[] | Expression;\n [Environment]: EnvironmentRecord;\n [IsConstructor]: boolean;\n [DebuggerCall]?: (...args: unknown[]) => IterableIterator<unknown>;\n [DebuggerScope]?: () => EnvironmentRecord | null | undefined;\n [DebuggerNode]?: () => EstreeNode | undefined;\n}\n\n// https://tc39.es/ecma262/#sec-reference-record-specification-type\nexport class ReferenceRecord {\n readonly Base:\n | Record<PropertyKey, unknown>\n | EnvironmentRecord\n | \"unresolvable\";\n readonly ReferenceName: PropertyKey;\n /** Whether the reference is in strict mode. */\n readonly Strict?: boolean;\n\n constructor(\n base: Record<PropertyKey, unknown> | EnvironmentRecord | \"unresolvable\",\n referenceName: PropertyKey,\n strict: boolean\n ) {\n this.Base = base;\n this.ReferenceName = referenceName;\n this.Strict = strict;\n }\n}\n\n// https://tc39.es/ecma262/#sec-completion-record-specification-type\nexport class CompletionRecord {\n readonly Type: CompletionRecordType;\n readonly Value: unknown;\n\n constructor(type: CompletionRecordType, value: unknown) {\n this.Type = type;\n this.Value = value;\n }\n}\n\nexport type CompletionRecordType =\n | \"normal\"\n | \"break\"\n | \"continue\"\n | \"return\"\n | \"throw\";\n\n// https://tc39.es/ecma262/#sec-normalcompletion\nexport function NormalCompletion(value: unknown): CompletionRecord {\n return new CompletionRecord(\"normal\", value);\n}\n\nexport const Empty = Symbol(\"empty completion\");\n\nexport interface OptionalChainRef {\n skipped?: boolean;\n}\n"],"mappings":";;;;;;;;AASA;AACO,MAAMA,gBAAgB,CAAC;EAC5BC,mBAAmB;EACnBC,kBAAkB;EAClBC,QAAQ;AACV;AAACC,OAAA,CAAAJ,gBAAA,GAAAA,gBAAA;AAID;AACO,MAAMK,iBAAiB,CAAC;EACpBC,QAAQ;EACAC,UAAU,GAAG,IAAIC,GAAG,CAAuB,CAAC;EAE7DC,WAAWA,CAACC,KAA2C,EAAE;IACvD,IAAI,CAACJ,QAAQ,GAAGI,KAAK;EACvB;EAEAC,UAAUA,CAACC,IAAY,EAAW;IAChC,OAAO,IAAI,CAACL,UAAU,CAACM,GAAG,CAACD,IAAI,CAAC;EAClC;EAEAE,oBAAoBA,CAACF,IAAY,EAAEG,SAAkB,EAAoB;IACvE;IACA,IAAI,CAACR,UAAU,CAACS,GAAG,CAACJ,IAAI,EAAE;MACxBK,OAAO,EAAE,IAAI;MACbF;IACF,CAAC,CAAC;IACF,OAAOG,gBAAgB,CAACC,SAAS,CAAC;EACpC;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,sBAAsBA,CAACR,IAAY,EAAES,MAAe,EAAoB;IACtE;IACA,IAAI,CAACd,UAAU,CAACS,GAAG,CAACJ,IAAI,EAAE;MACxBS;IACF,CAAC,CAAC;IACF,OAAOH,gBAAgB,CAACC,SAAS,CAAC;EACpC;EAEAG,iBAAiBA,CAACV,IAAY,EAAEW,KAAc,EAAoB;IAChE,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nB,OAAA,CAAAgD,gBAAA,GAAAA,gBAAA;AASD;AACO,SAASlC,gBAAgBA,CAACK,KAAc,EAAoB;EACjE,OAAO,IAAI6B,gBAAgB,CAAC,QAAQ,EAAE7B,KAAK,CAAC;AAC9C;AAEO,MAAMiC,KAAK,GAAApD,OAAA,CAAAoD,KAAA,GAAGnB,MAAM,CAAC,kBAAkB,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"ExecutionContext.js","names":["Mode","exports","SourceNode","Symbol","for","FormalParameters","ECMAScriptCode","Environment","IsConstructor","ThisMode","DebuggerCall","DebuggerScope","DebuggerNode","DebuggerReturn","ExecutionContext","VariableEnvironment","LexicalEnvironment","Function","BindingStatus","EnvironmentRecord","OuterEnv","bindingMap","Map","ThisValue","undefined","ThisBindingStatus","constructor","outer","HasBinding","name","has","CreateMutableBinding","deletable","set","mutable","NormalCompletion","CreateImmutableBinding","strict","InitializeBinding","value","binding","get","Object","assign","initialized","SetMutableBinding","_strict","ReferenceError","TypeError","GetBindingValue","HasThisBinding","DeclarativeEnvironment","FunctionEnvironment","F","LEXICAL","UNINITIALIZED","BindThisValue","INITIALIZED","Error","GetThisBinding","ReferenceRecord","Base","ReferenceName","Strict","base","referenceName","CompletionRecord","Type","Value","type","Empty"],"sources":["../../src/ExecutionContext.ts"],"sourcesContent":["import type {\n ArrowFunctionExpression,\n Expression,\n FunctionDeclaration,\n FunctionExpression,\n Statement,\n} from \"@babel/types\";\nimport type { EstreeNode } from \"./interfaces.js\";\n\nexport enum Mode {\n LEXICAL,\n STRICT,\n}\n\nexport const SourceNode = Symbol.for(\"SourceNode\");\nexport const FormalParameters = Symbol.for(\"FormalParameters\");\nexport const ECMAScriptCode = Symbol.for(\"ECMAScriptCode\");\nexport const Environment = Symbol.for(\"Environment\");\nexport const IsConstructor = Symbol.for(\"IsConstructor\");\nexport const ThisMode = Symbol.for(\"ThisMode\");\nexport const DebuggerCall = Symbol.for(\"$DebuggerCall$\");\nexport const DebuggerScope = Symbol.for(\"$DebuggerScope$\");\nexport const DebuggerNode = Symbol.for(\"$DebuggerNode$\");\nexport const DebuggerReturn = Symbol.for(\"$DebuggerReturn$\");\n\n// https://tc39.es/ecma262/#sec-execution-contexts\nexport class ExecutionContext {\n VariableEnvironment?: EnvironmentRecord;\n LexicalEnvironment?: EnvironmentRecord;\n Function?: FunctionObject;\n}\n\nexport type EnvironmentRecordType = \"function\" | \"declarative\";\n\nexport enum BindingStatus {\n UNINITIALIZED,\n LEXICAL,\n INITIALIZED,\n}\n\n// https://tc39.es/ecma262/#sec-environment-records\nexport class EnvironmentRecord {\n readonly OuterEnv: EnvironmentRecord | null | undefined;\n private readonly bindingMap = new Map<string, BindingState>();\n protected ThisValue: unknown = undefined;\n protected ThisBindingStatus: BindingStatus | undefined;\n\n constructor(outer: EnvironmentRecord | null | undefined) {\n this.OuterEnv = outer;\n }\n\n HasBinding(name: string): boolean {\n return this.bindingMap.has(name);\n }\n\n CreateMutableBinding(name: string, deletable: boolean): CompletionRecord {\n // Assert: binding does not exist.\n this.bindingMap.set(name, {\n mutable: true,\n deletable,\n });\n return NormalCompletion(undefined);\n }\n\n /**\n * Create an immutable binding.\n *\n * @param name - The binding name.\n * @param strict - For named function expressions, strict is false, otherwise it's true.\n * @returns CompletionRecord.\n */\n CreateImmutableBinding(name: string, strict: boolean): CompletionRecord {\n // Assert: binding does not exist.\n this.bindingMap.set(name, {\n strict,\n });\n return NormalCompletion(undefined);\n }\n\n InitializeBinding(name: string, value: unknown): CompletionRecord {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists and uninitialized.\n Object.assign<BindingState, Partial<BindingState>>(binding, {\n initialized: true,\n value,\n });\n return NormalCompletion(undefined);\n }\n\n /**\n * Update a mutable binding value, including function declarations.\n *\n * @param name - The binding name.\n * @param value - The binding value.\n * @param strict - For functions, strict is always false, otherwise it depends on strict-mode.\n * @returns\n */\n SetMutableBinding(\n name: string,\n value: unknown,\n _strict?: boolean\n ): CompletionRecord {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists.\n if (!binding.initialized) {\n throw new ReferenceError(`${name} is not initialized`);\n } else if (binding.mutable) {\n binding.value = value;\n } else {\n throw new TypeError(`Assignment to constant variable`);\n }\n return NormalCompletion(undefined);\n }\n\n GetBindingValue(name: string, _strict?: boolean): unknown {\n const binding = this.bindingMap.get(name) as BindingState;\n // Assert: binding exists.\n if (!binding.initialized) {\n throw new ReferenceError(`${name} is not initialized`);\n }\n return binding.value;\n }\n\n HasThisBinding() {\n return false;\n }\n}\n\nexport class DeclarativeEnvironment extends EnvironmentRecord {}\n\nexport class FunctionEnvironment extends EnvironmentRecord {\n constructor(F: FunctionObject) {\n super(F[Environment]);\n if (F[ThisMode] === Mode.LEXICAL) {\n this.ThisBindingStatus = BindingStatus.LEXICAL;\n } else {\n this.ThisBindingStatus = BindingStatus.UNINITIALIZED;\n }\n }\n\n HasThisBinding() {\n return this.ThisBindingStatus !== BindingStatus.LEXICAL;\n }\n\n BindThisValue(value: unknown) {\n // Assert: envRec.[[ThisBindingStatus]] is not LEXICAL.\n if (this.ThisBindingStatus === BindingStatus.INITIALIZED) {\n throw new Error(\"This binding has been initialized\");\n }\n this.ThisValue = value;\n this.ThisBindingStatus = BindingStatus.INITIALIZED;\n }\n\n GetThisBinding() {\n // Assert: envRec.[[ThisBindingStatus]] is not LEXICAL.\n if (this.ThisBindingStatus === BindingStatus.UNINITIALIZED) {\n throw new Error(\"This binding is not initialized\");\n }\n return this.ThisValue;\n }\n}\n\nexport interface BindingState {\n initialized?: boolean;\n value?: unknown;\n mutable?: boolean;\n\n /** This is used for mutable bindings only. */\n deletable?: boolean;\n\n /**\n * This is used for immutable bindings only.\n * For named function expressions, `strict` is false,\n * otherwise it's true.\n */\n strict?: boolean;\n}\n\nexport interface FunctionObject {\n (...args: unknown[]): unknown;\n [SourceNode]:\n | FunctionDeclaration\n | FunctionExpression\n | ArrowFunctionExpression;\n [FormalParameters]: FunctionDeclaration[\"params\"];\n [ECMAScriptCode]: Statement[] | Expression;\n [Environment]: EnvironmentRecord;\n [IsConstructor]: boolean;\n [ThisMode]?: Mode;\n [DebuggerCall]?: (...args: unknown[]) => IterableIterator<unknown>;\n [DebuggerScope]?: () => EnvironmentRecord | null | undefined;\n [DebuggerNode]?: () => EstreeNode | undefined;\n}\n\n// https://tc39.es/ecma262/#sec-reference-record-specification-type\nexport class ReferenceRecord {\n readonly Base:\n | Record<PropertyKey, unknown>\n | EnvironmentRecord\n | \"unresolvable\";\n readonly ReferenceName: PropertyKey;\n /** Whether the reference is in strict mode. */\n readonly Strict?: boolean;\n\n constructor(\n base: Record<PropertyKey, unknown> | EnvironmentRecord | \"unresolvable\",\n referenceName: PropertyKey,\n strict: boolean\n ) {\n this.Base = base;\n this.ReferenceName = referenceName;\n this.Strict = strict;\n }\n}\n\n// https://tc39.es/ecma262/#sec-completion-record-specification-type\nexport class CompletionRecord {\n readonly Type: CompletionRecordType;\n readonly Value: unknown;\n\n constructor(type: CompletionRecordType, value: unknown) {\n this.Type = type;\n this.Value = value;\n }\n}\n\nexport type CompletionRecordType =\n | \"normal\"\n | \"break\"\n | \"continue\"\n | \"return\"\n | \"throw\";\n\n// https://tc39.es/ecma262/#sec-normalcompletion\nexport function NormalCompletion(value: unknown): CompletionRecord {\n return new CompletionRecord(\"normal\", value);\n}\n\nexport const Empty = Symbol(\"empty completion\");\n\nexport interface OptionalChainRef {\n skipped?: boolean;\n}\n"],"mappings":";;;;;;;;IASYA,IAAI,GAAAC,OAAA,CAAAD,IAAA,0BAAJA,IAAI;EAAJA,IAAI,CAAJA,IAAI;EAAJA,IAAI,CAAJA,IAAI;EAAA,OAAJA,IAAI;AAAA;AAKT,MAAME,UAAU,GAAAD,OAAA,CAAAC,UAAA,GAAGC,MAAM,CAACC,GAAG,CAAC,YAAY,CAAC;AAC3C,MAAMC,gBAAgB,GAAAJ,OAAA,CAAAI,gBAAA,GAAGF,MAAM,CAACC,GAAG,CAAC,kBAAkB,CAAC;AACvD,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IACf;IACA,IAAI,IAAI,CAAClC,iBAAiB,KAAKP,aAAa,CAACqC,aAAa,EAAE;MAC1D,MAAM,IAAIG,KAAK,CAAC,iCAAiC,CAAC;IACpD;IACA,OAAO,IAAI,CAACnC,SAAS;EACvB;AACF;AAACtB,OAAA,CAAAmD,mBAAA,GAAAA,mBAAA;AAkCD;AACO,MAAMQ,eAAe,CAAC;EAClBC,IAAI;EAIJC,aAAa;EACtB;EACSC,MAAM;EAEfrC,WAAWA,CACTsC,IAAuE,EACvEC,aAA0B,EAC1B5B,MAAe,EACf;IACA,IAAI,CAACwB,IAAI,GAAGG,IAAI;IAChB,IAAI,CAACF,aAAa,GAAGG,aAAa;IAClC,IAAI,CAACF,MAAM,GAAG1B,MAAM;EACtB;AACF;;AAEA;AAAApC,OAAA,CAAA2D,eAAA,GAAAA,eAAA;AACO,MAAMM,gBAAgB,CAAC;EACnBC,IAAI;EACJC,KAAK;EAEd1C,WAAWA,CAAC2C,IAA0B,EAAE9B,KAAc,EAAE;IACtD,IAAI,CAAC4B,IAAI,GAAGE,IAAI;IAChB,IAAI,CAACD,KAAK,GAAG7B,KAAK;EACpB;AACF;AAACtC,OAAA,CAAAiE,gBAAA,GAAAA,gBAAA;AASD;AACO,SAAS/B,gBAAgBA,CAACI,KAAc,EAAoB;EACjE,OAAO,IAAI2B,gBAAgB,CAAC,QAAQ,EAAE3B,KAAK,CAAC;AAC9C;AAEO,MAAM+B,KAAK,GAAArE,OAAA,CAAAqE,KAAA,GAAGnE,MAAM,CAAC,kBAAkB,CAAC","ignoreList":[]}
|
package/dist/cjs/context-free.js
CHANGED
|
@@ -177,6 +177,8 @@ function ApplyStringOrNumericBinaryOperator(leftValue, operator, rightValue) {
|
|
|
177
177
|
return leftValue >= rightValue;
|
|
178
178
|
case "<=":
|
|
179
179
|
return leftValue <= rightValue;
|
|
180
|
+
case "in":
|
|
181
|
+
return leftValue in rightValue;
|
|
180
182
|
}
|
|
181
183
|
throw new SyntaxError(`Unsupported binary operator \`${operator}\``);
|
|
182
184
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-free.js","names":["_ExecutionContext","require","_traverse","IsPropertyReference","V","Base","EnvironmentRecord","InitializeReferencedBinding","W","base","InitializeBinding","ReferenceName","CopyDataProperties","target","source","excludedItems","undefined","keys","Object","getOwnPropertyNames","concat","getOwnPropertySymbols","nextKey","has","desc","getOwnPropertyDescriptor","enumerable","ForDeclarationBindingInstantiation","forDeclaration","env","isConst","kind","name","collectBoundNames","CreateImmutableBinding","CreateMutableBinding","LoopContinues","completion","Type","UpdateEmpty","value","Value","Empty","CompletionRecord","GetValue","ReferenceRecord","ReferenceError","GetBindingValue","Strict","ToPropertyKey","arg","String","GetV","P","PutValue","SetMutableBinding","NormalCompletion","CreateListIteratorRecord","args","isIterable","TypeError","Symbol","iterator","RequireObjectCoercible","GetIdentifierReference","strict","HasBinding","OuterEnv","ApplyStringOrNumericBinaryOperator","leftValue","operator","rightValue","SyntaxError","ApplyStringOrNumericAssignment","substr","length","ApplyUnaryOperator","cooked","Array","isArray"],"sources":["../../src/context-free.ts"],"sourcesContent":["import {\n BinaryExpression,\n UnaryExpression,\n VariableDeclaration,\n} from \"@babel/types\";\nimport {\n CompletionRecord,\n Empty,\n EnvironmentRecord,\n NormalCompletion,\n ReferenceRecord,\n} from \"./ExecutionContext.js\";\nimport { collectBoundNames } from \"./traverse.js\";\n\n// https://tc39.es/ecma262/#sec-ispropertyreference\nexport function IsPropertyReference(V: ReferenceRecord): boolean {\n return V.Base !== \"unresolvable\" && !(V.Base instanceof EnvironmentRecord);\n}\n\n// https://tc39.es/ecma262/#sec-initializereferencedbinding\nexport function InitializeReferencedBinding(\n V: ReferenceRecord,\n W: unknown\n): CompletionRecord {\n const base = V.Base as EnvironmentRecord;\n return base.InitializeBinding(V.ReferenceName as string, W);\n}\n\n// https://tc39.es/ecma262/#sec-copydataproperties\nexport function CopyDataProperties(\n target: Record<PropertyKey, unknown>,\n source: unknown,\n excludedItems: Set<PropertyKey>\n): Record<PropertyKey, unknown> {\n if (source === undefined || source === null) {\n return target;\n }\n const keys = (Object.getOwnPropertyNames(source) as PropertyKey[]).concat(\n Object.getOwnPropertySymbols(source)\n );\n for (const nextKey of keys) {\n if (!excludedItems.has(nextKey)) {\n const desc = Object.getOwnPropertyDescriptor(source, nextKey);\n if (desc?.enumerable) {\n target[nextKey] = (source as Record<PropertyKey, unknown>)[nextKey];\n }\n }\n }\n return target;\n}\n\n// https://tc39.es/ecma262/#sec-runtime-semantics-fordeclarationbindinginstantiation\nexport function ForDeclarationBindingInstantiation(\n forDeclaration: VariableDeclaration,\n env: EnvironmentRecord\n): void {\n const isConst = forDeclaration.kind === \"const\";\n for (const name of collectBoundNames(forDeclaration)) {\n if (isConst) {\n env.CreateImmutableBinding(name, true);\n } else {\n env.CreateMutableBinding(name, false);\n }\n }\n}\n\n// https://tc39.es/ecma262/#sec-loopcontinues\nexport function LoopContinues(completion: CompletionRecord): boolean {\n return completion.Type === \"normal\" || completion.Type == \"continue\";\n}\n\n// https://tc39.es/ecma262/#sec-updateempty\nexport function UpdateEmpty(\n completion: CompletionRecord,\n value: unknown\n): CompletionRecord {\n if (completion.Value !== Empty) {\n return completion;\n }\n return new CompletionRecord(completion.Type, value);\n}\n\n// https://tc39.es/ecma262/#sec-getvalue\nexport function GetValue(\n V: CompletionRecord | ReferenceRecord | unknown\n): unknown {\n if (V instanceof CompletionRecord) {\n // Assert: V.Type is normal.\n V = V.Value;\n }\n if (!(V instanceof ReferenceRecord)) {\n return V;\n }\n if (V.Base === \"unresolvable\") {\n throw new ReferenceError(`${V.ReferenceName as string} is not defined`);\n }\n if (V.Base instanceof EnvironmentRecord) {\n const base = V.Base as EnvironmentRecord;\n return base.GetBindingValue(V.ReferenceName as string, V.Strict);\n }\n return V.Base[V.ReferenceName];\n}\n\n// https://tc39.es/ecma262/#sec-topropertykey\nexport function ToPropertyKey(arg: unknown): string | symbol {\n if (typeof arg === \"symbol\") {\n return arg;\n }\n return String(arg);\n}\n\n// https://tc39.es/ecma262/#sec-getv\nexport function GetV(V: unknown, P: PropertyKey): unknown {\n return (V as Record<PropertyKey, unknown>)[P];\n}\n\n// https://tc39.es/ecma262/#sec-putvalue\nexport function PutValue(V: ReferenceRecord, W: unknown): CompletionRecord {\n // Assert: V is a ReferenceRecord.\n if (V.Base === \"unresolvable\") {\n throw new ReferenceError(`${V.ReferenceName as string} is not defined`);\n }\n if (V.Base instanceof EnvironmentRecord) {\n return V.Base.SetMutableBinding(V.ReferenceName as string, W, V.Strict);\n }\n V.Base[V.ReferenceName] = W;\n return NormalCompletion(undefined);\n}\n\n// https://tc39.es/ecma262/#sec-createlistiteratorRecord\nexport function CreateListIteratorRecord(\n args: Iterable<unknown>\n): Iterator<unknown> {\n if (!isIterable(args)) {\n throw new TypeError(`${typeof args} is not iterable`);\n }\n return args[Symbol.iterator]();\n}\n\n// https://tc39.es/ecma262/#sec-requireobjectcoercible\nexport function RequireObjectCoercible(arg: unknown): void {\n if (arg === null || arg === undefined) {\n throw new TypeError(\"Cannot destructure properties of undefined or null\");\n }\n}\n\n// https://tc39.es/ecma262/#sec-getidentifierreference\nexport function GetIdentifierReference(\n env: EnvironmentRecord | null | undefined,\n name: string,\n strict: boolean\n): ReferenceRecord {\n if (!env) {\n return new ReferenceRecord(\"unresolvable\", name, strict);\n }\n if (env.HasBinding(name)) {\n return new ReferenceRecord(env, name, strict);\n }\n return GetIdentifierReference(env.OuterEnv, name, strict);\n}\n\n// https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator\nexport function ApplyStringOrNumericBinaryOperator(\n leftValue: number,\n operator: BinaryExpression[\"operator\"] | \"|>\",\n rightValue: number\n): unknown {\n switch (operator) {\n case \"+\":\n return leftValue + rightValue;\n case \"-\":\n return leftValue - rightValue;\n case \"/\":\n return leftValue / rightValue;\n case \"%\":\n return leftValue % rightValue;\n case \"*\":\n return leftValue * rightValue;\n case \"**\":\n return leftValue ** rightValue;\n case \"==\":\n return leftValue == rightValue;\n case \"===\":\n return leftValue === rightValue;\n case \"!=\":\n return leftValue != rightValue;\n case \"!==\":\n return leftValue !== rightValue;\n case \">\":\n return leftValue > rightValue;\n case \"<\":\n return leftValue < rightValue;\n case \">=\":\n return leftValue >= rightValue;\n case \"<=\":\n return leftValue <= rightValue;\n }\n throw new SyntaxError(`Unsupported binary operator \\`${operator}\\``);\n}\n\n// https://tc39.es/ecma262/#sec-assignment-operators\nexport function ApplyStringOrNumericAssignment(\n leftValue: string | number,\n operator: string,\n rightValue: string | number\n): unknown {\n switch (operator) {\n case \"+=\":\n case \"-=\":\n case \"*=\":\n case \"/=\":\n case \"%=\":\n case \"**=\":\n return ApplyStringOrNumericBinaryOperator(\n leftValue as number,\n operator.substr(0, operator.length - 1) as BinaryExpression[\"operator\"],\n rightValue as number\n );\n }\n\n throw new SyntaxError(`Unsupported assignment operator \\`${operator}\\``);\n}\n\n// https://tc39.es/ecma262/#sec-unary-operators\nexport function ApplyUnaryOperator(\n target: unknown,\n operator: UnaryExpression[\"operator\"]\n): unknown {\n switch (operator) {\n case \"!\":\n return !target;\n case \"+\":\n return +(target as string | number);\n case \"-\":\n return -(target as string | number);\n case \"void\":\n return undefined;\n }\n throw new SyntaxError(`Unsupported unary operator \\`${operator}\\``);\n}\n\nexport function isIterable(cooked: unknown): boolean {\n if (Array.isArray(cooked)) {\n return true;\n }\n if (cooked === null || cooked === undefined) {\n return false;\n }\n return typeof (cooked as Iterable<unknown>)[Symbol.iterator] === \"function\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAKA,IAAAA,iBAAA,GAAAC,OAAA;AAOA,IAAAC,SAAA,GAAAD,OAAA;AAEA;AACO,SAASE,mBAAmBA,CAACC,CAAkB,EAAW;EAC/D,OAAOA,CAAC,CAACC,IAAI,KAAK,cAAc,IAAI,EAAED,CAAC,CAACC,IAAI,YAAYC,mCAAiB,CAAC;AAC5E;;AAEA;AACO,SAASC,2BAA2BA,CACzCH,CAAkB,EAClBI,C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,qCAAqCF,QAAQ,IAAI,CAAC;AAC1E;;AAEA;AACO,SAASM,kBAAkBA,CAChC/D,MAAe,EACfyD,QAAqC,EAC5B;EACT,QAAQA,QAAQ;IACd,KAAK,GAAG;MACN,OAAO,CAACzD,MAAM;IAChB,KAAK,GAAG;MACN,OAAO,CAAEA,MAA0B;IACrC,KAAK,GAAG;MACN,OAAO,CAAEA,MAA0B;IACrC,KAAK,MAAM;MACT,OAAOG,SAAS;EACpB;EACA,MAAM,IAAIwD,WAAW,CAAC,gCAAgCF,QAAQ,IAAI,CAAC;AACrE;AAEO,SAASX,UAAUA,CAACkB,MAAe,EAAW;EACnD,IAAIC,KAAK,CAACC,OAAO,CAACF,MAAM,CAAC,EAAE;IACzB,OAAO,IAAI;EACb;EACA,IAAIA,MAAM,KAAK,IAAI,IAAIA,MAAM,KAAK7D,SAAS,EAAE;IAC3C,OAAO,KAAK;EACd;EACA,OAAO,OAAQ6D,MAAM,CAAuBhB,MAAM,CAACC,QAAQ,CAAC,KAAK,UAAU;AAC7E","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"context-free.js","names":["_ExecutionContext","require","_traverse","IsPropertyReference","V","Base","EnvironmentRecord","InitializeReferencedBinding","W","base","InitializeBinding","ReferenceName","CopyDataProperties","target","source","excludedItems","undefined","keys","Object","getOwnPropertyNames","concat","getOwnPropertySymbols","nextKey","has","desc","getOwnPropertyDescriptor","enumerable","ForDeclarationBindingInstantiation","forDeclaration","env","isConst","kind","name","collectBoundNames","CreateImmutableBinding","CreateMutableBinding","LoopContinues","completion","Type","UpdateEmpty","value","Value","Empty","CompletionRecord","GetValue","ReferenceRecord","ReferenceError","GetBindingValue","Strict","ToPropertyKey","arg","String","GetV","P","PutValue","SetMutableBinding","NormalCompletion","CreateListIteratorRecord","args","isIterable","TypeError","Symbol","iterator","RequireObjectCoercible","GetIdentifierReference","strict","HasBinding","OuterEnv","ApplyStringOrNumericBinaryOperator","leftValue","operator","rightValue","SyntaxError","ApplyStringOrNumericAssignment","substr","length","ApplyUnaryOperator","cooked","Array","isArray"],"sources":["../../src/context-free.ts"],"sourcesContent":["import {\n BinaryExpression,\n UnaryExpression,\n VariableDeclaration,\n} from \"@babel/types\";\nimport {\n CompletionRecord,\n Empty,\n EnvironmentRecord,\n NormalCompletion,\n ReferenceRecord,\n} from \"./ExecutionContext.js\";\nimport { collectBoundNames } from \"./traverse.js\";\n\n// https://tc39.es/ecma262/#sec-ispropertyreference\nexport function IsPropertyReference(V: ReferenceRecord): boolean {\n return V.Base !== \"unresolvable\" && !(V.Base instanceof EnvironmentRecord);\n}\n\n// https://tc39.es/ecma262/#sec-initializereferencedbinding\nexport function InitializeReferencedBinding(\n V: ReferenceRecord,\n W: unknown\n): CompletionRecord {\n const base = V.Base as EnvironmentRecord;\n return base.InitializeBinding(V.ReferenceName as string, W);\n}\n\n// https://tc39.es/ecma262/#sec-copydataproperties\nexport function CopyDataProperties(\n target: Record<PropertyKey, unknown>,\n source: unknown,\n excludedItems: Set<PropertyKey>\n): Record<PropertyKey, unknown> {\n if (source === undefined || source === null) {\n return target;\n }\n const keys = (Object.getOwnPropertyNames(source) as PropertyKey[]).concat(\n Object.getOwnPropertySymbols(source)\n );\n for (const nextKey of keys) {\n if (!excludedItems.has(nextKey)) {\n const desc = Object.getOwnPropertyDescriptor(source, nextKey);\n if (desc?.enumerable) {\n target[nextKey] = (source as Record<PropertyKey, unknown>)[nextKey];\n }\n }\n }\n return target;\n}\n\n// https://tc39.es/ecma262/#sec-runtime-semantics-fordeclarationbindinginstantiation\nexport function ForDeclarationBindingInstantiation(\n forDeclaration: VariableDeclaration,\n env: EnvironmentRecord\n): void {\n const isConst = forDeclaration.kind === \"const\";\n for (const name of collectBoundNames(forDeclaration)) {\n if (isConst) {\n env.CreateImmutableBinding(name, true);\n } else {\n env.CreateMutableBinding(name, false);\n }\n }\n}\n\n// https://tc39.es/ecma262/#sec-loopcontinues\nexport function LoopContinues(completion: CompletionRecord): boolean {\n return completion.Type === \"normal\" || completion.Type == \"continue\";\n}\n\n// https://tc39.es/ecma262/#sec-updateempty\nexport function UpdateEmpty(\n completion: CompletionRecord,\n value: unknown\n): CompletionRecord {\n if (completion.Value !== Empty) {\n return completion;\n }\n return new CompletionRecord(completion.Type, value);\n}\n\n// https://tc39.es/ecma262/#sec-getvalue\nexport function GetValue(\n V: CompletionRecord | ReferenceRecord | unknown\n): unknown {\n if (V instanceof CompletionRecord) {\n // Assert: V.Type is normal.\n V = V.Value;\n }\n if (!(V instanceof ReferenceRecord)) {\n return V;\n }\n if (V.Base === \"unresolvable\") {\n throw new ReferenceError(`${V.ReferenceName as string} is not defined`);\n }\n if (V.Base instanceof EnvironmentRecord) {\n const base = V.Base as EnvironmentRecord;\n return base.GetBindingValue(V.ReferenceName as string, V.Strict);\n }\n return V.Base[V.ReferenceName];\n}\n\n// https://tc39.es/ecma262/#sec-topropertykey\nexport function ToPropertyKey(arg: unknown): string | symbol {\n if (typeof arg === \"symbol\") {\n return arg;\n }\n return String(arg);\n}\n\n// https://tc39.es/ecma262/#sec-getv\nexport function GetV(V: unknown, P: PropertyKey): unknown {\n return (V as Record<PropertyKey, unknown>)[P];\n}\n\n// https://tc39.es/ecma262/#sec-putvalue\nexport function PutValue(V: ReferenceRecord, W: unknown): CompletionRecord {\n // Assert: V is a ReferenceRecord.\n if (V.Base === \"unresolvable\") {\n throw new ReferenceError(`${V.ReferenceName as string} is not defined`);\n }\n if (V.Base instanceof EnvironmentRecord) {\n return V.Base.SetMutableBinding(V.ReferenceName as string, W, V.Strict);\n }\n V.Base[V.ReferenceName] = W;\n return NormalCompletion(undefined);\n}\n\n// https://tc39.es/ecma262/#sec-createlistiteratorRecord\nexport function CreateListIteratorRecord(\n args: Iterable<unknown>\n): Iterator<unknown> {\n if (!isIterable(args)) {\n throw new TypeError(`${typeof args} is not iterable`);\n }\n return args[Symbol.iterator]();\n}\n\n// https://tc39.es/ecma262/#sec-requireobjectcoercible\nexport function RequireObjectCoercible(arg: unknown): void {\n if (arg === null || arg === undefined) {\n throw new TypeError(\"Cannot destructure properties of undefined or null\");\n }\n}\n\n// https://tc39.es/ecma262/#sec-getidentifierreference\nexport function GetIdentifierReference(\n env: EnvironmentRecord | null | undefined,\n name: string,\n strict: boolean\n): ReferenceRecord {\n if (!env) {\n return new ReferenceRecord(\"unresolvable\", name, strict);\n }\n if (env.HasBinding(name)) {\n return new ReferenceRecord(env, name, strict);\n }\n return GetIdentifierReference(env.OuterEnv, name, strict);\n}\n\n// https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator\nexport function ApplyStringOrNumericBinaryOperator(\n leftValue: number,\n operator: BinaryExpression[\"operator\"] | \"|>\",\n rightValue: number\n): unknown {\n switch (operator) {\n case \"+\":\n return leftValue + rightValue;\n case \"-\":\n return leftValue - rightValue;\n case \"/\":\n return leftValue / rightValue;\n case \"%\":\n return leftValue % rightValue;\n case \"*\":\n return leftValue * rightValue;\n case \"**\":\n return leftValue ** rightValue;\n case \"==\":\n return leftValue == rightValue;\n case \"===\":\n return leftValue === rightValue;\n case \"!=\":\n return leftValue != rightValue;\n case \"!==\":\n return leftValue !== rightValue;\n case \">\":\n return leftValue > rightValue;\n case \"<\":\n return leftValue < rightValue;\n case \">=\":\n return leftValue >= rightValue;\n case \"<=\":\n return leftValue <= rightValue;\n case \"in\":\n return leftValue in (rightValue as unknown as Record<number, unknown>);\n }\n throw new SyntaxError(`Unsupported binary operator \\`${operator}\\``);\n}\n\n// https://tc39.es/ecma262/#sec-assignment-operators\nexport function ApplyStringOrNumericAssignment(\n leftValue: string | number,\n operator: string,\n rightValue: string | number\n): unknown {\n switch (operator) {\n case \"+=\":\n case \"-=\":\n case \"*=\":\n case \"/=\":\n case \"%=\":\n case \"**=\":\n return ApplyStringOrNumericBinaryOperator(\n leftValue as number,\n operator.substr(0, operator.length - 1) as BinaryExpression[\"operator\"],\n rightValue as number\n );\n }\n\n throw new SyntaxError(`Unsupported assignment operator \\`${operator}\\``);\n}\n\n// https://tc39.es/ecma262/#sec-unary-operators\nexport function ApplyUnaryOperator(\n target: unknown,\n operator: UnaryExpression[\"operator\"]\n): unknown {\n switch (operator) {\n case \"!\":\n return !target;\n case \"+\":\n return +(target as string | number);\n case \"-\":\n return -(target as string | number);\n case \"void\":\n return undefined;\n }\n throw new SyntaxError(`Unsupported unary operator \\`${operator}\\``);\n}\n\nexport function isIterable(cooked: unknown): boolean {\n if (Array.isArray(cooked)) {\n return true;\n }\n if (cooked === null || cooked === undefined) {\n return false;\n }\n return typeof (cooked as Iterable<unknown>)[Symbol.iterator] === \"function\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAKA,IAAAA,iBAAA,GAAAC,OAAA;AAOA,IAAAC,SAAA,GAAAD,OAAA;AAEA;AACO,SAASE,mBAAmBA,CAACC,CAAkB,EAAW;EAC/D,OAAOA,CAAC,CAACC,IAAI,KAAK,cAAc,IAAI,EAAED,CAAC,CAACC,IAAI,YAAYC,mCAAiB,CAAC;AAC5E;;AAEA;AACO,SAASC,2BAA2BA,CACzCH,CAAkB,EAClBI,CAAU,EACQ;EAClB,MAAMC,IAAI,GAAGL,CAAC,CAACC,IAAyB;EACxC,OAAOI,IAAI,CAACC,iBAAiB,CAACN,CAAC,CAACO,aAAa,EAAYH,CAAC,CAAC;AAC7D;;AAEA;AACO,SAASI,kBAAkBA,CAChCC,MAAoC,EACpCC,MAAe,EACfC,aAA+B,EACD;EAC9B,IAAID,MAAM,KAAKE,SAAS,IAAIF,MAAM,KAAK,IAAI,EAAE;IAC3C,OAAOD,MAAM;EACf;EACA,MAAMI,IAAI,GAAIC,MAAM,CAACC,mBAAmB,CAACL,MAAM,CAAC,CAAmBM,MAAM,CACvEF,MAAM,CAACG,qBAAqB,CAACP,MAAM,CACrC,CAAC;EACD,KAAK,MAAMQ,OAAO,IAAIL,IAAI,EAAE;IAC1B,IAAI,CAACF,aAAa,CAACQ,GAAG,CAACD,OAAO,CAAC,EAAE;MAC/B,MAAME,IAAI,GAAGN,MAAM,CAACO,wBAA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AAE;IAC3C,OAAO,KAAK;EACd;EACA,OAAO,OAAQ6D,MAAM,CAAuBhB,MAAM,CAACC,QAAQ,CAAC,KAAK,UAAU;AAC7E","ignoreList":[]}
|
package/dist/cjs/cook.js
CHANGED
|
@@ -12,11 +12,19 @@ var _traverse = require("./traverse.js");
|
|
|
12
12
|
function cook(rootAst, codeSource, {
|
|
13
13
|
rules,
|
|
14
14
|
debug,
|
|
15
|
+
externalSourceForDebug,
|
|
15
16
|
globalVariables = {},
|
|
17
|
+
// Allow debugger to override Array constructor.
|
|
18
|
+
ArrayConstructor = Array,
|
|
16
19
|
hooks = {}
|
|
17
20
|
} = {}) {
|
|
18
21
|
var _hooks$beforeEvaluate3;
|
|
19
22
|
const expressionOnly = rootAst.type !== "FunctionDeclaration";
|
|
23
|
+
function doSanitize(cooked) {
|
|
24
|
+
if (!externalSourceForDebug) {
|
|
25
|
+
(0, _sanitize.sanitize)(cooked);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
20
28
|
const rootEnv = new _ExecutionContext.DeclarativeEnvironment(null);
|
|
21
29
|
const rootContext = new _ExecutionContext.ExecutionContext();
|
|
22
30
|
rootContext.VariableEnvironment = rootEnv;
|
|
@@ -52,7 +60,7 @@ function cook(rootAst, codeSource, {
|
|
|
52
60
|
var _hooks$beforeEvaluate, _hooks$beforeBranch2;
|
|
53
61
|
(_hooks$beforeEvaluate = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate === void 0 || _hooks$beforeEvaluate.call(hooks, node);
|
|
54
62
|
currentNode = node;
|
|
55
|
-
if (debug && (forceYield || node.type.endsWith("Statement") && node.type !== "TryStatement" && node.type !== "BlockStatement" && node.type !== "ForStatement" && node.type !== "ForInStatement" && node.type !== "ForOfStatement")) {
|
|
63
|
+
if (debug && (forceYield || node.type.endsWith("Statement") && !(node.type === "ExpressionStatement" && (node.expression.type === "CallExpression" || node.expression.type === "TaggedTemplateExpression")) && node.type !== "TryStatement" && node.type !== "BlockStatement" && node.type !== "DoWhileStatement" && node.type !== "WhileStatement" && node.type !== "ForStatement" && node.type !== "ForInStatement" && node.type !== "ForOfStatement")) {
|
|
56
64
|
yield;
|
|
57
65
|
}
|
|
58
66
|
// Expressions:
|
|
@@ -60,7 +68,7 @@ function cook(rootAst, codeSource, {
|
|
|
60
68
|
case "ArrayExpression":
|
|
61
69
|
{
|
|
62
70
|
// https://tc39.es/ecma262/#sec-array-initializer
|
|
63
|
-
const array =
|
|
71
|
+
const array = new ArrayConstructor();
|
|
64
72
|
for (const element of node.elements) {
|
|
65
73
|
if (!element) {
|
|
66
74
|
array.length += 1;
|
|
@@ -84,7 +92,7 @@ function cook(rootAst, codeSource, {
|
|
|
84
92
|
{
|
|
85
93
|
const leftRef = yield* Evaluate(node.left);
|
|
86
94
|
const leftValue = (0, _contextFree.GetValue)(leftRef);
|
|
87
|
-
const rightRef =
|
|
95
|
+
const rightRef = yield* Evaluate(node.right);
|
|
88
96
|
const rightValue = (0, _contextFree.GetValue)(rightRef);
|
|
89
97
|
if (expressionOnly && node.operator === "|>") {
|
|
90
98
|
// Minimal pipeline operator is supported only in expression-only mode.
|
|
@@ -115,7 +123,7 @@ function cook(rootAst, codeSource, {
|
|
|
115
123
|
optionalChainRef.skipped = true;
|
|
116
124
|
return (0, _ExecutionContext.NormalCompletion)(undefined);
|
|
117
125
|
}
|
|
118
|
-
(
|
|
126
|
+
doSanitize(func);
|
|
119
127
|
if (debug) yield;
|
|
120
128
|
return yield* EvaluateCall(func, ref, node.arguments, node.callee);
|
|
121
129
|
}
|
|
@@ -171,9 +179,9 @@ function cook(rootAst, codeSource, {
|
|
|
171
179
|
optionalChainRef.skipped = true;
|
|
172
180
|
return (0, _ExecutionContext.NormalCompletion)(undefined);
|
|
173
181
|
}
|
|
174
|
-
(
|
|
182
|
+
doSanitize(baseValue);
|
|
175
183
|
const result = node.computed ? yield* EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
|
|
176
|
-
(
|
|
184
|
+
doSanitize(result);
|
|
177
185
|
return (0, _ExecutionContext.NormalCompletion)(result);
|
|
178
186
|
}
|
|
179
187
|
case "NewExpression":
|
|
@@ -230,7 +238,7 @@ function cook(rootAst, codeSource, {
|
|
|
230
238
|
// https://tc39.es/ecma262/#sec-tagged-templates
|
|
231
239
|
const tagRef = (yield* Evaluate(node.tag)).Value;
|
|
232
240
|
const tagFunc = (0, _contextFree.GetValue)(tagRef);
|
|
233
|
-
(
|
|
241
|
+
doSanitize(tagFunc);
|
|
234
242
|
if (debug) yield;
|
|
235
243
|
return yield* EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
|
|
236
244
|
}
|
|
@@ -356,9 +364,16 @@ function cook(rootAst, codeSource, {
|
|
|
356
364
|
const exprRef = yield* Evaluate(node.argument);
|
|
357
365
|
v = (0, _contextFree.GetValue)(exprRef);
|
|
358
366
|
}
|
|
359
|
-
currentNode = node;
|
|
360
367
|
return new _ExecutionContext.CompletionRecord("return", v);
|
|
361
368
|
}
|
|
369
|
+
case "ThisExpression":
|
|
370
|
+
{
|
|
371
|
+
if (!externalSourceForDebug) {
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
const envRec = GetThisEnvironment();
|
|
375
|
+
return (0, _ExecutionContext.NormalCompletion)(envRec.GetThisBinding());
|
|
376
|
+
}
|
|
362
377
|
case "ThrowStatement":
|
|
363
378
|
// https://tc39.es/ecma262/#sec-throw-statement
|
|
364
379
|
throw (0, _contextFree.GetValue)(yield* Evaluate(node.argument));
|
|
@@ -573,7 +588,7 @@ function cook(rootAst, codeSource, {
|
|
|
573
588
|
let V;
|
|
574
589
|
// eslint-disable-next-line no-constant-condition
|
|
575
590
|
while (true) {
|
|
576
|
-
const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
|
|
591
|
+
const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test, undefined, true));
|
|
577
592
|
if (!exprValue) {
|
|
578
593
|
return (0, _ExecutionContext.NormalCompletion)(V);
|
|
579
594
|
}
|
|
@@ -600,7 +615,7 @@ function cook(rootAst, codeSource, {
|
|
|
600
615
|
if (stmtResult.Value !== _ExecutionContext.Empty) {
|
|
601
616
|
V = stmtResult.Value;
|
|
602
617
|
}
|
|
603
|
-
const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
|
|
618
|
+
const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test, undefined, true));
|
|
604
619
|
if (!exprValue) {
|
|
605
620
|
return (0, _ExecutionContext.NormalCompletion)(V);
|
|
606
621
|
}
|
|
@@ -902,7 +917,7 @@ function cook(rootAst, codeSource, {
|
|
|
902
917
|
}
|
|
903
918
|
} else {
|
|
904
919
|
// RestElement
|
|
905
|
-
v =
|
|
920
|
+
v = new ArrayConstructor();
|
|
906
921
|
let n = 0;
|
|
907
922
|
// eslint-disable-next-line no-constant-condition
|
|
908
923
|
while (true) {
|
|
@@ -937,6 +952,7 @@ function cook(rootAst, codeSource, {
|
|
|
937
952
|
|
|
938
953
|
// https://tc39.es/ecma262/#sec-evaluate-property-access-with-identifier-key
|
|
939
954
|
function EvaluatePropertyAccessWithIdentifierKey(baseValue, identifier, strict) {
|
|
955
|
+
currentNode = identifier;
|
|
940
956
|
const propertyNameString = identifier.name;
|
|
941
957
|
return new _ExecutionContext.ReferenceRecord(baseValue, propertyNameString, strict);
|
|
942
958
|
}
|
|
@@ -979,16 +995,16 @@ function cook(rootAst, codeSource, {
|
|
|
979
995
|
const funcName = codeSource.substring(callee.start, callee.end);
|
|
980
996
|
throw new TypeError(`${funcName} is not a function`);
|
|
981
997
|
}
|
|
982
|
-
if (debug) {
|
|
998
|
+
if (debug || externalSourceForDebug) {
|
|
983
999
|
const debuggerCall = func[_ExecutionContext.DebuggerCall];
|
|
984
1000
|
if (debuggerCall) {
|
|
985
1001
|
const result = yield* debuggerCall.apply(thisValue, argList);
|
|
986
|
-
(
|
|
1002
|
+
doSanitize(result);
|
|
987
1003
|
return (0, _ExecutionContext.NormalCompletion)(result);
|
|
988
1004
|
}
|
|
989
1005
|
}
|
|
990
1006
|
const result = func.apply(thisValue, argList);
|
|
991
|
-
(
|
|
1007
|
+
doSanitize(result);
|
|
992
1008
|
return (0, _ExecutionContext.NormalCompletion)(result);
|
|
993
1009
|
}
|
|
994
1010
|
|
|
@@ -1001,7 +1017,7 @@ function cook(rootAst, codeSource, {
|
|
|
1001
1017
|
const constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
|
|
1002
1018
|
throw new TypeError(`${constructorName} is not a constructor`);
|
|
1003
1019
|
}
|
|
1004
|
-
if (!(0, _sanitize.isAllowedConstructor)(constructor)) {
|
|
1020
|
+
if (!externalSourceForDebug && !(0, _sanitize.isAllowedConstructor)(constructor) && constructor !== ArrayConstructor) {
|
|
1005
1021
|
const constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
|
|
1006
1022
|
throw new TypeError(`${constructorName} is not an allowed constructor`);
|
|
1007
1023
|
}
|
|
@@ -1030,18 +1046,22 @@ function cook(rootAst, codeSource, {
|
|
|
1030
1046
|
}
|
|
1031
1047
|
|
|
1032
1048
|
// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
|
|
1033
|
-
function* CallFunction(closure, args) {
|
|
1034
|
-
var _hooks$beforeCall
|
|
1049
|
+
function* CallFunction(closure, thisArgument, args) {
|
|
1050
|
+
var _hooks$beforeCall;
|
|
1035
1051
|
(_hooks$beforeCall = hooks.beforeCall) === null || _hooks$beforeCall === void 0 || _hooks$beforeCall.call(hooks, closure[_ExecutionContext.SourceNode]);
|
|
1036
|
-
PrepareForOrdinaryCall(closure);
|
|
1052
|
+
const calleeContext = PrepareForOrdinaryCall(closure);
|
|
1053
|
+
OrdinaryCallBindThis(closure, calleeContext, thisArgument);
|
|
1037
1054
|
const result = yield* OrdinaryCallEvaluateBody(closure, args);
|
|
1038
|
-
if (
|
|
1039
|
-
currentNode =
|
|
1055
|
+
if (debug) {
|
|
1056
|
+
currentNode = {
|
|
1057
|
+
...closure[_ExecutionContext.SourceNode],
|
|
1058
|
+
[_ExecutionContext.DebuggerReturn]: true
|
|
1059
|
+
};
|
|
1060
|
+
yield {
|
|
1061
|
+
type: "return",
|
|
1062
|
+
value: result.Type === "return" ? result.Value : undefined
|
|
1063
|
+
};
|
|
1040
1064
|
}
|
|
1041
|
-
if (debug) yield {
|
|
1042
|
-
type: "return",
|
|
1043
|
-
value: result.Type === "return" ? result.Value : undefined
|
|
1044
|
-
};
|
|
1045
1065
|
executionContextStack.pop();
|
|
1046
1066
|
if (result.Type === "return") {
|
|
1047
1067
|
return result.Value;
|
|
@@ -1053,12 +1073,19 @@ function cook(rootAst, codeSource, {
|
|
|
1053
1073
|
function PrepareForOrdinaryCall(F) {
|
|
1054
1074
|
const calleeContext = new _ExecutionContext.ExecutionContext();
|
|
1055
1075
|
calleeContext.Function = F;
|
|
1056
|
-
const localEnv = new _ExecutionContext.FunctionEnvironment(F
|
|
1076
|
+
const localEnv = new _ExecutionContext.FunctionEnvironment(F);
|
|
1057
1077
|
calleeContext.VariableEnvironment = localEnv;
|
|
1058
1078
|
calleeContext.LexicalEnvironment = localEnv;
|
|
1059
1079
|
executionContextStack.push(calleeContext);
|
|
1060
1080
|
return calleeContext;
|
|
1061
1081
|
}
|
|
1082
|
+
function OrdinaryCallBindThis(F, calleeContext, thisArgument) {
|
|
1083
|
+
if (F[_ExecutionContext.ThisMode] === _ExecutionContext.Mode.LEXICAL) {
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
const localEnv = calleeContext.LexicalEnvironment;
|
|
1087
|
+
localEnv === null || localEnv === void 0 || localEnv.BindThisValue(thisArgument);
|
|
1088
|
+
}
|
|
1062
1089
|
|
|
1063
1090
|
// https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
|
|
1064
1091
|
function* OrdinaryCallEvaluateBody(F, args) {
|
|
@@ -1086,6 +1113,16 @@ function cook(rootAst, codeSource, {
|
|
|
1086
1113
|
}
|
|
1087
1114
|
return result;
|
|
1088
1115
|
}
|
|
1116
|
+
function GetThisEnvironment() {
|
|
1117
|
+
let env = getRunningContext().LexicalEnvironment;
|
|
1118
|
+
while (env) {
|
|
1119
|
+
if (env.HasThisBinding()) {
|
|
1120
|
+
return env;
|
|
1121
|
+
}
|
|
1122
|
+
env = env.OuterEnv;
|
|
1123
|
+
}
|
|
1124
|
+
throw new Error("Accessing global this is forbidden");
|
|
1125
|
+
}
|
|
1089
1126
|
|
|
1090
1127
|
// https://tc39.es/ecma262/#sec-isanonymousfunctiondefinition
|
|
1091
1128
|
function IsAnonymousFunctionDefinition(node) {
|
|
@@ -1137,11 +1174,33 @@ function cook(rootAst, codeSource, {
|
|
|
1137
1174
|
throw new SyntaxError("Var declaration is not recommended, use `let` or `const` instead");
|
|
1138
1175
|
}
|
|
1139
1176
|
}
|
|
1177
|
+
|
|
1178
|
+
// let argumentsObjectNeeded = true;
|
|
1179
|
+
// if (func[ThisMode] === Mode.LEXICAL) {
|
|
1180
|
+
// // NOTE: Arrow functions never have an arguments object.
|
|
1181
|
+
// argumentsObjectNeeded = false;
|
|
1182
|
+
// } else if (parameterNames.includes("arguments")) {
|
|
1183
|
+
// argumentsObjectNeeded = false;
|
|
1184
|
+
// } else if (!hasParameterExpressions && (
|
|
1185
|
+
// varNames.includes("arguments") ||
|
|
1186
|
+
// collectBoundNames(collectScopedDeclarations(code, { var: false })).includes("arguments")
|
|
1187
|
+
// )) {
|
|
1188
|
+
// argumentsObjectNeeded = false;
|
|
1189
|
+
// }
|
|
1190
|
+
// NOTE: In strict mode, no parameter/function/var/lexical names can be "arguments".
|
|
1191
|
+
const argumentsObjectNeeded = !!externalSourceForDebug && func[_ExecutionContext.ThisMode] !== _ExecutionContext.Mode.LEXICAL;
|
|
1140
1192
|
const env = calleeContext.LexicalEnvironment;
|
|
1141
1193
|
for (const paramName of parameterNames) {
|
|
1142
1194
|
// In strict mode, it's guaranteed no duplicate params exist.
|
|
1143
1195
|
env.CreateMutableBinding(paramName, false);
|
|
1144
1196
|
}
|
|
1197
|
+
let parameterBindings = parameterNames;
|
|
1198
|
+
if (argumentsObjectNeeded) {
|
|
1199
|
+
const ao = CreateUnmappedArgumentsObject(args);
|
|
1200
|
+
env.CreateImmutableBinding("arguments", false);
|
|
1201
|
+
env.InitializeBinding("arguments", ao);
|
|
1202
|
+
parameterBindings = parameterNames.concat("arguments");
|
|
1203
|
+
}
|
|
1145
1204
|
const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(args);
|
|
1146
1205
|
yield* IteratorBindingInitialization(formals, iteratorRecord, env);
|
|
1147
1206
|
let varEnv;
|
|
@@ -1149,8 +1208,10 @@ function cook(rootAst, codeSource, {
|
|
|
1149
1208
|
// NOTE: Only a single Environment Record is needed for the parameters
|
|
1150
1209
|
// and top-level vars.
|
|
1151
1210
|
// `varNames` are unique.
|
|
1211
|
+
const instantiatedVarNames = [...parameterBindings];
|
|
1152
1212
|
for (const n of varNames) {
|
|
1153
|
-
if (!
|
|
1213
|
+
if (!instantiatedVarNames.includes(n)) {
|
|
1214
|
+
instantiatedVarNames.push(n);
|
|
1154
1215
|
env.CreateMutableBinding(n, false);
|
|
1155
1216
|
env.InitializeBinding(n, undefined);
|
|
1156
1217
|
}
|
|
@@ -1163,15 +1224,19 @@ function cook(rootAst, codeSource, {
|
|
|
1163
1224
|
varEnv = new _ExecutionContext.DeclarativeEnvironment(env);
|
|
1164
1225
|
calleeContext.VariableEnvironment = varEnv;
|
|
1165
1226
|
// `varNames` are unique.
|
|
1227
|
+
const instantiatedVarNames = [];
|
|
1166
1228
|
for (const n of varNames) {
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
initialValue
|
|
1229
|
+
if (!instantiatedVarNames.includes(n)) {
|
|
1230
|
+
instantiatedVarNames.push(n);
|
|
1231
|
+
varEnv.CreateMutableBinding(n, false);
|
|
1232
|
+
let initialValue;
|
|
1233
|
+
if (parameterBindings.includes(n) && !functionNames.includes(n)) {
|
|
1234
|
+
initialValue = env.GetBindingValue(n, false);
|
|
1235
|
+
}
|
|
1236
|
+
varEnv.InitializeBinding(n, initialValue);
|
|
1237
|
+
// NOTE: A var with the same name as a formal parameter initially has
|
|
1238
|
+
// the same value as the corresponding initialized parameter.
|
|
1171
1239
|
}
|
|
1172
|
-
varEnv.InitializeBinding(n, initialValue);
|
|
1173
|
-
// NOTE: A var with the same name as a formal parameter initially has
|
|
1174
|
-
// the same value as the corresponding initialized parameter.
|
|
1175
1240
|
}
|
|
1176
1241
|
}
|
|
1177
1242
|
const lexEnv = varEnv;
|
|
@@ -1196,10 +1261,35 @@ function cook(rootAst, codeSource, {
|
|
|
1196
1261
|
varEnv.SetMutableBinding(fn, fo, false);
|
|
1197
1262
|
}
|
|
1198
1263
|
}
|
|
1264
|
+
function CreateUnmappedArgumentsObject(args) {
|
|
1265
|
+
const argList = [...args];
|
|
1266
|
+
const argumentObject = {};
|
|
1267
|
+
Object.defineProperty(argumentObject, "length", {
|
|
1268
|
+
value: argList.length,
|
|
1269
|
+
writable: true,
|
|
1270
|
+
configurable: true
|
|
1271
|
+
});
|
|
1272
|
+
for (let index = 0; index < argList.length; index++) {
|
|
1273
|
+
argumentObject[String(index)] = argList[index];
|
|
1274
|
+
}
|
|
1275
|
+
Object.defineProperty(argumentObject, Symbol.iterator, {
|
|
1276
|
+
value: Array.prototype.values,
|
|
1277
|
+
writable: true,
|
|
1278
|
+
configurable: true
|
|
1279
|
+
});
|
|
1280
|
+
const ThrowTypeError = () => {
|
|
1281
|
+
throw new TypeError("'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them");
|
|
1282
|
+
};
|
|
1283
|
+
Object.defineProperty(argumentObject, "callee", {
|
|
1284
|
+
get: ThrowTypeError,
|
|
1285
|
+
set: ThrowTypeError
|
|
1286
|
+
});
|
|
1287
|
+
return argumentObject;
|
|
1288
|
+
}
|
|
1199
1289
|
|
|
1200
1290
|
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
|
|
1201
1291
|
function InstantiateFunctionObject(func, scope) {
|
|
1202
|
-
const F = OrdinaryFunctionCreate(func, scope, true);
|
|
1292
|
+
const F = OrdinaryFunctionCreate(func, scope, true, false);
|
|
1203
1293
|
if (func.id) {
|
|
1204
1294
|
SetFunctionName(F, func.id.name);
|
|
1205
1295
|
}
|
|
@@ -1213,12 +1303,12 @@ function cook(rootAst, codeSource, {
|
|
|
1213
1303
|
const name = functionExpression.id.name;
|
|
1214
1304
|
const funcEnv = new _ExecutionContext.DeclarativeEnvironment(scope);
|
|
1215
1305
|
funcEnv.CreateImmutableBinding(name, false);
|
|
1216
|
-
const closure = OrdinaryFunctionCreate(functionExpression, funcEnv, true);
|
|
1306
|
+
const closure = OrdinaryFunctionCreate(functionExpression, funcEnv, true, false);
|
|
1217
1307
|
SetFunctionName(closure, name);
|
|
1218
1308
|
funcEnv.InitializeBinding(name, closure);
|
|
1219
1309
|
return closure;
|
|
1220
1310
|
} else {
|
|
1221
|
-
const closure = OrdinaryFunctionCreate(functionExpression, scope, true);
|
|
1311
|
+
const closure = OrdinaryFunctionCreate(functionExpression, scope, true, false);
|
|
1222
1312
|
SetFunctionName(closure, name ?? "");
|
|
1223
1313
|
return closure;
|
|
1224
1314
|
}
|
|
@@ -1227,7 +1317,7 @@ function cook(rootAst, codeSource, {
|
|
|
1227
1317
|
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatearrowfunctionexpression
|
|
1228
1318
|
function InstantiateArrowFunctionExpression(arrowFunction, name) {
|
|
1229
1319
|
const scope = getRunningContext().LexicalEnvironment;
|
|
1230
|
-
const closure = OrdinaryFunctionCreate(arrowFunction, scope, false);
|
|
1320
|
+
const closure = OrdinaryFunctionCreate(arrowFunction, scope, false, true);
|
|
1231
1321
|
SetFunctionName(closure, name ?? "");
|
|
1232
1322
|
return closure;
|
|
1233
1323
|
}
|
|
@@ -1239,10 +1329,10 @@ function cook(rootAst, codeSource, {
|
|
|
1239
1329
|
}
|
|
1240
1330
|
|
|
1241
1331
|
// https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
|
|
1242
|
-
function OrdinaryFunctionCreate(sourceNode, scope, isConstructor) {
|
|
1332
|
+
function OrdinaryFunctionCreate(sourceNode, scope, isConstructor, lexicalThis) {
|
|
1243
1333
|
const F = function () {
|
|
1244
1334
|
// eslint-disable-next-line prefer-rest-params
|
|
1245
|
-
return unwind(CallFunction(F, arguments));
|
|
1335
|
+
return unwind(CallFunction(F, this, arguments));
|
|
1246
1336
|
};
|
|
1247
1337
|
Object.defineProperties(F, {
|
|
1248
1338
|
[_ExecutionContext.SourceNode]: {
|
|
@@ -1259,13 +1349,16 @@ function cook(rootAst, codeSource, {
|
|
|
1259
1349
|
},
|
|
1260
1350
|
[_ExecutionContext.IsConstructor]: {
|
|
1261
1351
|
value: isConstructor
|
|
1352
|
+
},
|
|
1353
|
+
[_ExecutionContext.ThisMode]: {
|
|
1354
|
+
value: lexicalThis ? _ExecutionContext.Mode.LEXICAL : _ExecutionContext.Mode.STRICT
|
|
1262
1355
|
}
|
|
1263
1356
|
});
|
|
1264
|
-
if (debug) {
|
|
1357
|
+
if (debug || externalSourceForDebug) {
|
|
1265
1358
|
Object.defineProperty(F, _ExecutionContext.DebuggerCall, {
|
|
1266
1359
|
value: function () {
|
|
1267
1360
|
// eslint-disable-next-line prefer-rest-params
|
|
1268
|
-
return CallFunction(F, arguments);
|
|
1361
|
+
return CallFunction(F, this, arguments);
|
|
1269
1362
|
}
|
|
1270
1363
|
});
|
|
1271
1364
|
}
|
|
@@ -1339,7 +1432,7 @@ function cook(rootAst, codeSource, {
|
|
|
1339
1432
|
// Rest element.
|
|
1340
1433
|
if (node.argument.type === "Identifier") {
|
|
1341
1434
|
const lhs = ResolveBinding(node.argument.name, environment);
|
|
1342
|
-
const A =
|
|
1435
|
+
const A = new ArrayConstructor();
|
|
1343
1436
|
let n = 0;
|
|
1344
1437
|
// eslint-disable-next-line no-constant-condition
|
|
1345
1438
|
while (true) {
|
|
@@ -1355,7 +1448,7 @@ function cook(rootAst, codeSource, {
|
|
|
1355
1448
|
n++;
|
|
1356
1449
|
}
|
|
1357
1450
|
} else {
|
|
1358
|
-
const A =
|
|
1451
|
+
const A = new ArrayConstructor();
|
|
1359
1452
|
let n = 0;
|
|
1360
1453
|
// eslint-disable-next-line no-constant-condition
|
|
1361
1454
|
while (true) {
|