jslike 1.8.2 → 1.8.4
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/esm/interpreter/interpreter.js +161 -92
- package/dist/index.cjs +137 -61
- package/dist/index.d.cts +161 -92
- package/dist/index.d.ts +161 -92
- package/dist/index.js +137 -61
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11514,6 +11514,9 @@ function getPatternName(pattern) {
|
|
|
11514
11514
|
if (pattern.type === "TSParameterProperty") return getPatternName(pattern.parameter);
|
|
11515
11515
|
return pattern.name;
|
|
11516
11516
|
}
|
|
11517
|
+
function unwrapTSParameterProperty(param) {
|
|
11518
|
+
return (param == null ? void 0 : param.type) === "TSParameterProperty" ? param.parameter : param;
|
|
11519
|
+
}
|
|
11517
11520
|
function collectRuntimeIdentifierReferences(node) {
|
|
11518
11521
|
const references = /* @__PURE__ */ new Set();
|
|
11519
11522
|
const skipKeys = /* @__PURE__ */ new Set([
|
|
@@ -12816,24 +12819,7 @@ var Interpreter = class _Interpreter {
|
|
|
12816
12819
|
} else if (thisContext !== void 0) {
|
|
12817
12820
|
funcEnv.define("this", thisContext);
|
|
12818
12821
|
}
|
|
12819
|
-
|
|
12820
|
-
const param = metadata.params[i].type === "TSParameterProperty" ? metadata.params[i].parameter : metadata.params[i];
|
|
12821
|
-
if (param.type === "Identifier") {
|
|
12822
|
-
funcEnv.define(param.name, args[i]);
|
|
12823
|
-
} else if (param.type === "AssignmentPattern") {
|
|
12824
|
-
const value = args[i] !== void 0 ? args[i] : this.evaluate(param.right, funcEnv);
|
|
12825
|
-
funcEnv.define(param.left.name, value);
|
|
12826
|
-
} else if (param.type === "RestElement") {
|
|
12827
|
-
funcEnv.define(param.argument.name, args.slice(i));
|
|
12828
|
-
break;
|
|
12829
|
-
} else if (param.type === "ObjectPattern") {
|
|
12830
|
-
this.bindObjectPattern(param, args[i], funcEnv);
|
|
12831
|
-
} else if (param.type === "ArrayPattern") {
|
|
12832
|
-
this.bindArrayPattern(param, args[i], funcEnv);
|
|
12833
|
-
} else {
|
|
12834
|
-
funcEnv.define(param.name, args[i]);
|
|
12835
|
-
}
|
|
12836
|
-
}
|
|
12822
|
+
this.bindFunctionParameters(metadata.params, args, funcEnv);
|
|
12837
12823
|
if (metadata.async) {
|
|
12838
12824
|
if (this.executionController) {
|
|
12839
12825
|
this.executionController._pushCall(funcName);
|
|
@@ -13098,7 +13084,7 @@ var Interpreter = class _Interpreter {
|
|
|
13098
13084
|
env.define(prop.value.name, propValue, isConst);
|
|
13099
13085
|
} else if (prop.value.type === "AssignmentPattern") {
|
|
13100
13086
|
const finalValue = propValue !== void 0 ? propValue : this.evaluate(prop.value.right, env);
|
|
13101
|
-
|
|
13087
|
+
this.bindPattern(prop.value.left, finalValue, env, isConst);
|
|
13102
13088
|
} else if (prop.value.type === "ObjectPattern") {
|
|
13103
13089
|
this.bindObjectPattern(prop.value, propValue, env, isConst);
|
|
13104
13090
|
} else if (prop.value.type === "ArrayPattern") {
|
|
@@ -13122,7 +13108,7 @@ var Interpreter = class _Interpreter {
|
|
|
13122
13108
|
env.define(element.name, value[i], isConst);
|
|
13123
13109
|
} else if (element.type === "AssignmentPattern") {
|
|
13124
13110
|
const finalValue = value[i] !== void 0 ? value[i] : this.evaluate(element.right, env);
|
|
13125
|
-
|
|
13111
|
+
this.bindPattern(element.left, finalValue, env, isConst);
|
|
13126
13112
|
} else if (element.type === "ObjectPattern") {
|
|
13127
13113
|
this.bindObjectPattern(element, value[i], env, isConst);
|
|
13128
13114
|
} else if (element.type === "ArrayPattern") {
|
|
@@ -13130,6 +13116,67 @@ var Interpreter = class _Interpreter {
|
|
|
13130
13116
|
}
|
|
13131
13117
|
}
|
|
13132
13118
|
}
|
|
13119
|
+
bindPattern(pattern, value, env, isConst = false) {
|
|
13120
|
+
if (pattern.type === "Identifier") {
|
|
13121
|
+
env.define(pattern.name, value, isConst);
|
|
13122
|
+
} else if (pattern.type === "ObjectPattern") {
|
|
13123
|
+
this.bindObjectPattern(pattern, value, env, isConst);
|
|
13124
|
+
} else if (pattern.type === "ArrayPattern") {
|
|
13125
|
+
this.bindArrayPattern(pattern, value, env, isConst);
|
|
13126
|
+
} else if (pattern.type === "AssignmentPattern") {
|
|
13127
|
+
const finalValue = value !== void 0 ? value : this.evaluate(pattern.right, env);
|
|
13128
|
+
this.bindPattern(pattern.left, finalValue, env, isConst);
|
|
13129
|
+
} else if (pattern.type === "RestElement") {
|
|
13130
|
+
this.bindPattern(pattern.argument, value, env, isConst);
|
|
13131
|
+
}
|
|
13132
|
+
}
|
|
13133
|
+
bindFunctionParameters(params, args, env, thisContext = null) {
|
|
13134
|
+
for (let i = 0; i < params.length; i++) {
|
|
13135
|
+
const originalParam = params[i];
|
|
13136
|
+
const param = unwrapTSParameterProperty(originalParam);
|
|
13137
|
+
this.bindFunctionParameter(param, args[i], args.slice(i), env);
|
|
13138
|
+
if (originalParam.type === "TSParameterProperty" && thisContext) {
|
|
13139
|
+
const propertyName = getPatternName(originalParam.parameter);
|
|
13140
|
+
if (propertyName) {
|
|
13141
|
+
thisContext[propertyName] = env.get(propertyName);
|
|
13142
|
+
}
|
|
13143
|
+
}
|
|
13144
|
+
if (param.type === "RestElement") {
|
|
13145
|
+
break;
|
|
13146
|
+
}
|
|
13147
|
+
}
|
|
13148
|
+
}
|
|
13149
|
+
bindFunctionParameter(param, arg, restArgs, env) {
|
|
13150
|
+
if (param.type === "Identifier") {
|
|
13151
|
+
this.bindPattern(param, arg, env);
|
|
13152
|
+
return;
|
|
13153
|
+
}
|
|
13154
|
+
if (param.type === "AssignmentPattern") {
|
|
13155
|
+
const value = arg !== void 0 ? arg : this.evaluate(param.right, env);
|
|
13156
|
+
this.bindPattern(param.left, value, env);
|
|
13157
|
+
return;
|
|
13158
|
+
}
|
|
13159
|
+
if (param.type === "RestElement") {
|
|
13160
|
+
const target = param.argument;
|
|
13161
|
+
if (target.type === "Identifier") {
|
|
13162
|
+
env.define(target.name, restArgs);
|
|
13163
|
+
} else if (target.type === "ArrayPattern") {
|
|
13164
|
+
this.bindArrayPattern(target, restArgs, env);
|
|
13165
|
+
} else if (target.type === "ObjectPattern") {
|
|
13166
|
+
this.bindObjectPattern(target, restArgs, env);
|
|
13167
|
+
}
|
|
13168
|
+
return;
|
|
13169
|
+
}
|
|
13170
|
+
if (param.type === "ObjectPattern") {
|
|
13171
|
+
this.bindPattern(param, arg, env);
|
|
13172
|
+
return;
|
|
13173
|
+
}
|
|
13174
|
+
if (param.type === "ArrayPattern") {
|
|
13175
|
+
this.bindPattern(param, arg, env);
|
|
13176
|
+
return;
|
|
13177
|
+
}
|
|
13178
|
+
env.define(param.name, arg);
|
|
13179
|
+
}
|
|
13133
13180
|
evaluateFunctionDeclaration(node, env) {
|
|
13134
13181
|
const funcMetadata = {
|
|
13135
13182
|
__isFunction: true,
|
|
@@ -13539,6 +13586,7 @@ var Interpreter = class _Interpreter {
|
|
|
13539
13586
|
let constructor = null;
|
|
13540
13587
|
const methods = {};
|
|
13541
13588
|
const staticMethods = {};
|
|
13589
|
+
const instanceFields = [];
|
|
13542
13590
|
for (const member of node.body.body) {
|
|
13543
13591
|
if (member.type === "MethodDefinition") {
|
|
13544
13592
|
const methodName = member.key.name || member.key.value;
|
|
@@ -13550,23 +13598,26 @@ var Interpreter = class _Interpreter {
|
|
|
13550
13598
|
} else {
|
|
13551
13599
|
methods[methodName] = methodFunc;
|
|
13552
13600
|
}
|
|
13601
|
+
} else if (member.type === "PropertyDefinition" && !member.static && !member.declare && !member.abstract) {
|
|
13602
|
+
instanceFields.push(member);
|
|
13553
13603
|
}
|
|
13554
13604
|
}
|
|
13555
13605
|
const classConstructor = function(...args) {
|
|
13556
13606
|
const instance = Object.create(classConstructor.prototype);
|
|
13557
|
-
|
|
13558
|
-
|
|
13559
|
-
|
|
13560
|
-
return result.value;
|
|
13561
|
-
}
|
|
13562
|
-
} else if (superClass) {
|
|
13563
|
-
superClass.call(instance, ...args);
|
|
13607
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13608
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === "object" && result.value !== instance) {
|
|
13609
|
+
return result.value;
|
|
13564
13610
|
}
|
|
13565
13611
|
return instance;
|
|
13566
13612
|
};
|
|
13567
13613
|
if (constructor) {
|
|
13568
13614
|
classConstructor.__constructor = constructor;
|
|
13569
13615
|
}
|
|
13616
|
+
classConstructor.__instanceFields = instanceFields;
|
|
13617
|
+
classConstructor.__superClass = superClass;
|
|
13618
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
13619
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13620
|
+
};
|
|
13570
13621
|
if (superClass) {
|
|
13571
13622
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
13572
13623
|
classConstructor.prototype.constructor = classConstructor;
|
|
@@ -13592,6 +13643,59 @@ var Interpreter = class _Interpreter {
|
|
|
13592
13643
|
classConstructor.__className = className;
|
|
13593
13644
|
return classConstructor;
|
|
13594
13645
|
}
|
|
13646
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
13647
|
+
const superClass = classConstructor.__superClass;
|
|
13648
|
+
const constructor = classConstructor.__constructor;
|
|
13649
|
+
let fieldsInitialized = false;
|
|
13650
|
+
const initializeOwnFields = () => {
|
|
13651
|
+
if (!fieldsInitialized) {
|
|
13652
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
13653
|
+
fieldsInitialized = true;
|
|
13654
|
+
}
|
|
13655
|
+
};
|
|
13656
|
+
if (!superClass) {
|
|
13657
|
+
initializeOwnFields();
|
|
13658
|
+
if (constructor) {
|
|
13659
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
13660
|
+
}
|
|
13661
|
+
return void 0;
|
|
13662
|
+
}
|
|
13663
|
+
if (constructor) {
|
|
13664
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
13665
|
+
initializeOwnFields();
|
|
13666
|
+
return result;
|
|
13667
|
+
}
|
|
13668
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
13669
|
+
initializeOwnFields();
|
|
13670
|
+
return void 0;
|
|
13671
|
+
}
|
|
13672
|
+
initializeSuperClass(superClass, instance, args) {
|
|
13673
|
+
if (superClass.__constructInto) {
|
|
13674
|
+
return superClass.__constructInto(instance, args);
|
|
13675
|
+
}
|
|
13676
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
13677
|
+
Object.getOwnPropertyNames(tempInstance).forEach((name) => {
|
|
13678
|
+
instance[name] = tempInstance[name];
|
|
13679
|
+
});
|
|
13680
|
+
return void 0;
|
|
13681
|
+
}
|
|
13682
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
13683
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
13684
|
+
const fieldEnv = new Environment(env);
|
|
13685
|
+
fieldEnv.define("this", instance);
|
|
13686
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
13687
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : void 0;
|
|
13688
|
+
}
|
|
13689
|
+
}
|
|
13690
|
+
getClassFieldName(field, env) {
|
|
13691
|
+
if (field.computed) {
|
|
13692
|
+
return this.evaluate(field.key, env);
|
|
13693
|
+
}
|
|
13694
|
+
if (field.key.type === "Identifier" || field.key.type === "PrivateIdentifier") {
|
|
13695
|
+
return field.key.name;
|
|
13696
|
+
}
|
|
13697
|
+
return field.key.value;
|
|
13698
|
+
}
|
|
13595
13699
|
createMethodFunction(funcNode, env, className) {
|
|
13596
13700
|
const func = {
|
|
13597
13701
|
__isFunction: true,
|
|
@@ -13602,50 +13706,22 @@ var Interpreter = class _Interpreter {
|
|
|
13602
13706
|
};
|
|
13603
13707
|
return func;
|
|
13604
13708
|
}
|
|
13605
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
13709
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
13606
13710
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
13607
13711
|
funcEnv.define("this", thisContext);
|
|
13608
13712
|
if (superClass) {
|
|
13609
13713
|
const superFunc = (...superArgs) => {
|
|
13610
|
-
|
|
13611
|
-
|
|
13612
|
-
|
|
13613
|
-
const tempInstance = Reflect.construct(superClass, superArgs, thisContext.constructor);
|
|
13614
|
-
Object.getOwnPropertyNames(tempInstance).forEach((name) => {
|
|
13615
|
-
thisContext[name] = tempInstance[name];
|
|
13616
|
-
});
|
|
13714
|
+
const result2 = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
13715
|
+
if (afterSuper) {
|
|
13716
|
+
afterSuper();
|
|
13617
13717
|
}
|
|
13618
|
-
return void 0;
|
|
13718
|
+
return result2 && result2.__explicitReturn ? result2.value : void 0;
|
|
13619
13719
|
};
|
|
13620
13720
|
superFunc.__isSuperConstructor = true;
|
|
13621
13721
|
superFunc.__superClass = superClass;
|
|
13622
13722
|
funcEnv.define("super", superFunc);
|
|
13623
13723
|
}
|
|
13624
|
-
|
|
13625
|
-
const originalParam = methodFunc.__params[i];
|
|
13626
|
-
const param = originalParam.type === "TSParameterProperty" ? originalParam.parameter : originalParam;
|
|
13627
|
-
if (param.type === "Identifier") {
|
|
13628
|
-
funcEnv.define(param.name, args[i]);
|
|
13629
|
-
} else if (param.type === "AssignmentPattern") {
|
|
13630
|
-
const value = args[i] !== void 0 ? args[i] : this.evaluate(param.right, funcEnv);
|
|
13631
|
-
funcEnv.define(param.left.name, value);
|
|
13632
|
-
} else if (param.type === "RestElement") {
|
|
13633
|
-
funcEnv.define(param.argument.name, args.slice(i));
|
|
13634
|
-
break;
|
|
13635
|
-
} else if (param.type === "ObjectPattern") {
|
|
13636
|
-
this.bindObjectPattern(param, args[i], funcEnv);
|
|
13637
|
-
} else if (param.type === "ArrayPattern") {
|
|
13638
|
-
this.bindArrayPattern(param, args[i], funcEnv);
|
|
13639
|
-
} else {
|
|
13640
|
-
funcEnv.define(param.name, args[i]);
|
|
13641
|
-
}
|
|
13642
|
-
if (originalParam.type === "TSParameterProperty") {
|
|
13643
|
-
const propertyName = getPatternName(originalParam.parameter);
|
|
13644
|
-
if (propertyName) {
|
|
13645
|
-
thisContext[propertyName] = funcEnv.get(propertyName);
|
|
13646
|
-
}
|
|
13647
|
-
}
|
|
13648
|
-
}
|
|
13724
|
+
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
13649
13725
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
13650
13726
|
if (result instanceof ReturnValue) {
|
|
13651
13727
|
return { __explicitReturn: true, value: result.value };
|