jslike 1.8.2 → 1.8.3
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 +81 -26
- package/dist/index.cjs +69 -16
- package/dist/index.d.cts +81 -26
- package/dist/index.d.ts +81 -26
- package/dist/index.js +69 -16
- package/package.json +1 -1
|
@@ -2666,6 +2666,7 @@ export class Interpreter {
|
|
|
2666
2666
|
let constructor = null;
|
|
2667
2667
|
const methods = {};
|
|
2668
2668
|
const staticMethods = {};
|
|
2669
|
+
const instanceFields = [];
|
|
2669
2670
|
|
|
2670
2671
|
for (const member of node.body.body) {
|
|
2671
2672
|
if (member.type === 'MethodDefinition') {
|
|
@@ -2679,6 +2680,8 @@ export class Interpreter {
|
|
|
2679
2680
|
} else {
|
|
2680
2681
|
methods[methodName] = methodFunc;
|
|
2681
2682
|
}
|
|
2683
|
+
} else if (member.type === 'PropertyDefinition' && !member.static && !member.declare && !member.abstract) {
|
|
2684
|
+
instanceFields.push(member);
|
|
2682
2685
|
}
|
|
2683
2686
|
}
|
|
2684
2687
|
|
|
@@ -2686,18 +2689,11 @@ export class Interpreter {
|
|
|
2686
2689
|
const classConstructor = function(...args) {
|
|
2687
2690
|
// Create instance
|
|
2688
2691
|
const instance = Object.create(classConstructor.prototype);
|
|
2692
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
2689
2693
|
|
|
2690
|
-
//
|
|
2691
|
-
if (
|
|
2692
|
-
|
|
2693
|
-
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
2694
|
-
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
2695
|
-
return result.value;
|
|
2696
|
-
}
|
|
2697
|
-
} else if (superClass) {
|
|
2698
|
-
// If no constructor defined but has superClass, implicitly call super()
|
|
2699
|
-
// Call the superClass constructor properly - it's a classConstructor function
|
|
2700
|
-
superClass.call(instance, ...args);
|
|
2694
|
+
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
2695
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
2696
|
+
return result.value;
|
|
2701
2697
|
}
|
|
2702
2698
|
|
|
2703
2699
|
return instance;
|
|
@@ -2708,6 +2704,12 @@ export class Interpreter {
|
|
|
2708
2704
|
classConstructor.__constructor = constructor;
|
|
2709
2705
|
}
|
|
2710
2706
|
|
|
2707
|
+
classConstructor.__instanceFields = instanceFields;
|
|
2708
|
+
classConstructor.__superClass = superClass;
|
|
2709
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
2710
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
2711
|
+
};
|
|
2712
|
+
|
|
2711
2713
|
// Set up prototype chain
|
|
2712
2714
|
if (superClass) {
|
|
2713
2715
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
@@ -2742,6 +2744,69 @@ export class Interpreter {
|
|
|
2742
2744
|
return classConstructor;
|
|
2743
2745
|
}
|
|
2744
2746
|
|
|
2747
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
2748
|
+
const superClass = classConstructor.__superClass;
|
|
2749
|
+
const constructor = classConstructor.__constructor;
|
|
2750
|
+
let fieldsInitialized = false;
|
|
2751
|
+
|
|
2752
|
+
const initializeOwnFields = () => {
|
|
2753
|
+
if (!fieldsInitialized) {
|
|
2754
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
2755
|
+
fieldsInitialized = true;
|
|
2756
|
+
}
|
|
2757
|
+
};
|
|
2758
|
+
|
|
2759
|
+
if (!superClass) {
|
|
2760
|
+
initializeOwnFields();
|
|
2761
|
+
if (constructor) {
|
|
2762
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
2763
|
+
}
|
|
2764
|
+
return undefined;
|
|
2765
|
+
}
|
|
2766
|
+
|
|
2767
|
+
if (constructor) {
|
|
2768
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
2769
|
+
initializeOwnFields();
|
|
2770
|
+
return result;
|
|
2771
|
+
}
|
|
2772
|
+
|
|
2773
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
2774
|
+
initializeOwnFields();
|
|
2775
|
+
return undefined;
|
|
2776
|
+
}
|
|
2777
|
+
|
|
2778
|
+
initializeSuperClass(superClass, instance, args) {
|
|
2779
|
+
if (superClass.__constructInto) {
|
|
2780
|
+
return superClass.__constructInto(instance, args);
|
|
2781
|
+
}
|
|
2782
|
+
|
|
2783
|
+
// For native constructors like Error, use Reflect.construct and copy instance properties.
|
|
2784
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
2785
|
+
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
2786
|
+
instance[name] = tempInstance[name];
|
|
2787
|
+
});
|
|
2788
|
+
return undefined;
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
2792
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
2793
|
+
const fieldEnv = new Environment(env);
|
|
2794
|
+
fieldEnv.define('this', instance);
|
|
2795
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
2796
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
|
|
2800
|
+
getClassFieldName(field, env) {
|
|
2801
|
+
if (field.computed) {
|
|
2802
|
+
return this.evaluate(field.key, env);
|
|
2803
|
+
}
|
|
2804
|
+
if (field.key.type === 'Identifier' || field.key.type === 'PrivateIdentifier') {
|
|
2805
|
+
return field.key.name;
|
|
2806
|
+
}
|
|
2807
|
+
return field.key.value;
|
|
2808
|
+
}
|
|
2809
|
+
|
|
2745
2810
|
createMethodFunction(funcNode, env, className) {
|
|
2746
2811
|
const func = {
|
|
2747
2812
|
__isFunction: true,
|
|
@@ -2753,7 +2818,7 @@ export class Interpreter {
|
|
|
2753
2818
|
return func;
|
|
2754
2819
|
}
|
|
2755
2820
|
|
|
2756
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
2821
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
2757
2822
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
2758
2823
|
|
|
2759
2824
|
// Bind 'this'
|
|
@@ -2763,21 +2828,11 @@ export class Interpreter {
|
|
|
2763
2828
|
if (superClass) {
|
|
2764
2829
|
// Create a super function that calls the parent constructor
|
|
2765
2830
|
const superFunc = (...superArgs) => {
|
|
2766
|
-
|
|
2767
|
-
if (
|
|
2768
|
-
|
|
2769
|
-
}
|
|
2770
|
-
// Otherwise, call superClass as a regular constructor (for native/external classes)
|
|
2771
|
-
else {
|
|
2772
|
-
// For native constructors like Error, we need to use Reflect.construct
|
|
2773
|
-
// to properly initialize the instance properties
|
|
2774
|
-
const tempInstance = Reflect.construct(superClass, superArgs, thisContext.constructor);
|
|
2775
|
-
// Copy properties from the temp instance to our thisContext
|
|
2776
|
-
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
2777
|
-
thisContext[name] = tempInstance[name];
|
|
2778
|
-
});
|
|
2831
|
+
const result = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
2832
|
+
if (afterSuper) {
|
|
2833
|
+
afterSuper();
|
|
2779
2834
|
}
|
|
2780
|
-
return undefined;
|
|
2835
|
+
return result && result.__explicitReturn ? result.value : undefined;
|
|
2781
2836
|
};
|
|
2782
2837
|
// Store both the function and mark it as super
|
|
2783
2838
|
superFunc.__isSuperConstructor = true;
|
package/dist/index.cjs
CHANGED
|
@@ -13573,6 +13573,7 @@ var Interpreter = class _Interpreter {
|
|
|
13573
13573
|
let constructor = null;
|
|
13574
13574
|
const methods = {};
|
|
13575
13575
|
const staticMethods = {};
|
|
13576
|
+
const instanceFields = [];
|
|
13576
13577
|
for (const member of node.body.body) {
|
|
13577
13578
|
if (member.type === "MethodDefinition") {
|
|
13578
13579
|
const methodName = member.key.name || member.key.value;
|
|
@@ -13584,23 +13585,26 @@ var Interpreter = class _Interpreter {
|
|
|
13584
13585
|
} else {
|
|
13585
13586
|
methods[methodName] = methodFunc;
|
|
13586
13587
|
}
|
|
13588
|
+
} else if (member.type === "PropertyDefinition" && !member.static && !member.declare && !member.abstract) {
|
|
13589
|
+
instanceFields.push(member);
|
|
13587
13590
|
}
|
|
13588
13591
|
}
|
|
13589
13592
|
const classConstructor = function(...args) {
|
|
13590
13593
|
const instance = Object.create(classConstructor.prototype);
|
|
13591
|
-
|
|
13592
|
-
|
|
13593
|
-
|
|
13594
|
-
return result.value;
|
|
13595
|
-
}
|
|
13596
|
-
} else if (superClass) {
|
|
13597
|
-
superClass.call(instance, ...args);
|
|
13594
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13595
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === "object" && result.value !== instance) {
|
|
13596
|
+
return result.value;
|
|
13598
13597
|
}
|
|
13599
13598
|
return instance;
|
|
13600
13599
|
};
|
|
13601
13600
|
if (constructor) {
|
|
13602
13601
|
classConstructor.__constructor = constructor;
|
|
13603
13602
|
}
|
|
13603
|
+
classConstructor.__instanceFields = instanceFields;
|
|
13604
|
+
classConstructor.__superClass = superClass;
|
|
13605
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
13606
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13607
|
+
};
|
|
13604
13608
|
if (superClass) {
|
|
13605
13609
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
13606
13610
|
classConstructor.prototype.constructor = classConstructor;
|
|
@@ -13626,6 +13630,59 @@ var Interpreter = class _Interpreter {
|
|
|
13626
13630
|
classConstructor.__className = className;
|
|
13627
13631
|
return classConstructor;
|
|
13628
13632
|
}
|
|
13633
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
13634
|
+
const superClass = classConstructor.__superClass;
|
|
13635
|
+
const constructor = classConstructor.__constructor;
|
|
13636
|
+
let fieldsInitialized = false;
|
|
13637
|
+
const initializeOwnFields = () => {
|
|
13638
|
+
if (!fieldsInitialized) {
|
|
13639
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
13640
|
+
fieldsInitialized = true;
|
|
13641
|
+
}
|
|
13642
|
+
};
|
|
13643
|
+
if (!superClass) {
|
|
13644
|
+
initializeOwnFields();
|
|
13645
|
+
if (constructor) {
|
|
13646
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
13647
|
+
}
|
|
13648
|
+
return void 0;
|
|
13649
|
+
}
|
|
13650
|
+
if (constructor) {
|
|
13651
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
13652
|
+
initializeOwnFields();
|
|
13653
|
+
return result;
|
|
13654
|
+
}
|
|
13655
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
13656
|
+
initializeOwnFields();
|
|
13657
|
+
return void 0;
|
|
13658
|
+
}
|
|
13659
|
+
initializeSuperClass(superClass, instance, args) {
|
|
13660
|
+
if (superClass.__constructInto) {
|
|
13661
|
+
return superClass.__constructInto(instance, args);
|
|
13662
|
+
}
|
|
13663
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
13664
|
+
Object.getOwnPropertyNames(tempInstance).forEach((name) => {
|
|
13665
|
+
instance[name] = tempInstance[name];
|
|
13666
|
+
});
|
|
13667
|
+
return void 0;
|
|
13668
|
+
}
|
|
13669
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
13670
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
13671
|
+
const fieldEnv = new Environment(env);
|
|
13672
|
+
fieldEnv.define("this", instance);
|
|
13673
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
13674
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : void 0;
|
|
13675
|
+
}
|
|
13676
|
+
}
|
|
13677
|
+
getClassFieldName(field, env) {
|
|
13678
|
+
if (field.computed) {
|
|
13679
|
+
return this.evaluate(field.key, env);
|
|
13680
|
+
}
|
|
13681
|
+
if (field.key.type === "Identifier" || field.key.type === "PrivateIdentifier") {
|
|
13682
|
+
return field.key.name;
|
|
13683
|
+
}
|
|
13684
|
+
return field.key.value;
|
|
13685
|
+
}
|
|
13629
13686
|
createMethodFunction(funcNode, env, className) {
|
|
13630
13687
|
const func = {
|
|
13631
13688
|
__isFunction: true,
|
|
@@ -13636,20 +13693,16 @@ var Interpreter = class _Interpreter {
|
|
|
13636
13693
|
};
|
|
13637
13694
|
return func;
|
|
13638
13695
|
}
|
|
13639
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
13696
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
13640
13697
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
13641
13698
|
funcEnv.define("this", thisContext);
|
|
13642
13699
|
if (superClass) {
|
|
13643
13700
|
const superFunc = (...superArgs) => {
|
|
13644
|
-
|
|
13645
|
-
|
|
13646
|
-
|
|
13647
|
-
const tempInstance = Reflect.construct(superClass, superArgs, thisContext.constructor);
|
|
13648
|
-
Object.getOwnPropertyNames(tempInstance).forEach((name) => {
|
|
13649
|
-
thisContext[name] = tempInstance[name];
|
|
13650
|
-
});
|
|
13701
|
+
const result2 = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
13702
|
+
if (afterSuper) {
|
|
13703
|
+
afterSuper();
|
|
13651
13704
|
}
|
|
13652
|
-
return void 0;
|
|
13705
|
+
return result2 && result2.__explicitReturn ? result2.value : void 0;
|
|
13653
13706
|
};
|
|
13654
13707
|
superFunc.__isSuperConstructor = true;
|
|
13655
13708
|
superFunc.__superClass = superClass;
|
package/dist/index.d.cts
CHANGED
|
@@ -14859,6 +14859,7 @@ class Interpreter {
|
|
|
14859
14859
|
let constructor = null;
|
|
14860
14860
|
const methods = {};
|
|
14861
14861
|
const staticMethods = {};
|
|
14862
|
+
const instanceFields = [];
|
|
14862
14863
|
|
|
14863
14864
|
for (const member of node.body.body) {
|
|
14864
14865
|
if (member.type === 'MethodDefinition') {
|
|
@@ -14872,6 +14873,8 @@ class Interpreter {
|
|
|
14872
14873
|
} else {
|
|
14873
14874
|
methods[methodName] = methodFunc;
|
|
14874
14875
|
}
|
|
14876
|
+
} else if (member.type === 'PropertyDefinition' && !member.static && !member.declare && !member.abstract) {
|
|
14877
|
+
instanceFields.push(member);
|
|
14875
14878
|
}
|
|
14876
14879
|
}
|
|
14877
14880
|
|
|
@@ -14879,18 +14882,11 @@ class Interpreter {
|
|
|
14879
14882
|
const classConstructor = function(...args) {
|
|
14880
14883
|
// Create instance
|
|
14881
14884
|
const instance = Object.create(classConstructor.prototype);
|
|
14885
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
14882
14886
|
|
|
14883
|
-
//
|
|
14884
|
-
if (
|
|
14885
|
-
|
|
14886
|
-
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
14887
|
-
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
14888
|
-
return result.value;
|
|
14889
|
-
}
|
|
14890
|
-
} else if (superClass) {
|
|
14891
|
-
// If no constructor defined but has superClass, implicitly call super()
|
|
14892
|
-
// Call the superClass constructor properly - it's a classConstructor function
|
|
14893
|
-
superClass.call(instance, ...args);
|
|
14887
|
+
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
14888
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
14889
|
+
return result.value;
|
|
14894
14890
|
}
|
|
14895
14891
|
|
|
14896
14892
|
return instance;
|
|
@@ -14901,6 +14897,12 @@ class Interpreter {
|
|
|
14901
14897
|
classConstructor.__constructor = constructor;
|
|
14902
14898
|
}
|
|
14903
14899
|
|
|
14900
|
+
classConstructor.__instanceFields = instanceFields;
|
|
14901
|
+
classConstructor.__superClass = superClass;
|
|
14902
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
14903
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
14904
|
+
};
|
|
14905
|
+
|
|
14904
14906
|
// Set up prototype chain
|
|
14905
14907
|
if (superClass) {
|
|
14906
14908
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
@@ -14935,6 +14937,69 @@ class Interpreter {
|
|
|
14935
14937
|
return classConstructor;
|
|
14936
14938
|
}
|
|
14937
14939
|
|
|
14940
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
14941
|
+
const superClass = classConstructor.__superClass;
|
|
14942
|
+
const constructor = classConstructor.__constructor;
|
|
14943
|
+
let fieldsInitialized = false;
|
|
14944
|
+
|
|
14945
|
+
const initializeOwnFields = () => {
|
|
14946
|
+
if (!fieldsInitialized) {
|
|
14947
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
14948
|
+
fieldsInitialized = true;
|
|
14949
|
+
}
|
|
14950
|
+
};
|
|
14951
|
+
|
|
14952
|
+
if (!superClass) {
|
|
14953
|
+
initializeOwnFields();
|
|
14954
|
+
if (constructor) {
|
|
14955
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
14956
|
+
}
|
|
14957
|
+
return undefined;
|
|
14958
|
+
}
|
|
14959
|
+
|
|
14960
|
+
if (constructor) {
|
|
14961
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
14962
|
+
initializeOwnFields();
|
|
14963
|
+
return result;
|
|
14964
|
+
}
|
|
14965
|
+
|
|
14966
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
14967
|
+
initializeOwnFields();
|
|
14968
|
+
return undefined;
|
|
14969
|
+
}
|
|
14970
|
+
|
|
14971
|
+
initializeSuperClass(superClass, instance, args) {
|
|
14972
|
+
if (superClass.__constructInto) {
|
|
14973
|
+
return superClass.__constructInto(instance, args);
|
|
14974
|
+
}
|
|
14975
|
+
|
|
14976
|
+
// For native constructors like Error, use Reflect.construct and copy instance properties.
|
|
14977
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
14978
|
+
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
14979
|
+
instance[name] = tempInstance[name];
|
|
14980
|
+
});
|
|
14981
|
+
return undefined;
|
|
14982
|
+
}
|
|
14983
|
+
|
|
14984
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
14985
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
14986
|
+
const fieldEnv = new Environment(env);
|
|
14987
|
+
fieldEnv.define('this', instance);
|
|
14988
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
14989
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
|
|
14990
|
+
}
|
|
14991
|
+
}
|
|
14992
|
+
|
|
14993
|
+
getClassFieldName(field, env) {
|
|
14994
|
+
if (field.computed) {
|
|
14995
|
+
return this.evaluate(field.key, env);
|
|
14996
|
+
}
|
|
14997
|
+
if (field.key.type === 'Identifier' || field.key.type === 'PrivateIdentifier') {
|
|
14998
|
+
return field.key.name;
|
|
14999
|
+
}
|
|
15000
|
+
return field.key.value;
|
|
15001
|
+
}
|
|
15002
|
+
|
|
14938
15003
|
createMethodFunction(funcNode, env, className) {
|
|
14939
15004
|
const func = {
|
|
14940
15005
|
__isFunction: true,
|
|
@@ -14946,7 +15011,7 @@ class Interpreter {
|
|
|
14946
15011
|
return func;
|
|
14947
15012
|
}
|
|
14948
15013
|
|
|
14949
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
15014
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
14950
15015
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
14951
15016
|
|
|
14952
15017
|
// Bind 'this'
|
|
@@ -14956,21 +15021,11 @@ class Interpreter {
|
|
|
14956
15021
|
if (superClass) {
|
|
14957
15022
|
// Create a super function that calls the parent constructor
|
|
14958
15023
|
const superFunc = (...superArgs) => {
|
|
14959
|
-
|
|
14960
|
-
if (
|
|
14961
|
-
|
|
14962
|
-
}
|
|
14963
|
-
// Otherwise, call superClass as a regular constructor (for native/external classes)
|
|
14964
|
-
else {
|
|
14965
|
-
// For native constructors like Error, we need to use Reflect.construct
|
|
14966
|
-
// to properly initialize the instance properties
|
|
14967
|
-
const tempInstance = Reflect.construct(superClass, superArgs, thisContext.constructor);
|
|
14968
|
-
// Copy properties from the temp instance to our thisContext
|
|
14969
|
-
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
14970
|
-
thisContext[name] = tempInstance[name];
|
|
14971
|
-
});
|
|
15024
|
+
const result = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
15025
|
+
if (afterSuper) {
|
|
15026
|
+
afterSuper();
|
|
14972
15027
|
}
|
|
14973
|
-
return undefined;
|
|
15028
|
+
return result && result.__explicitReturn ? result.value : undefined;
|
|
14974
15029
|
};
|
|
14975
15030
|
// Store both the function and mark it as super
|
|
14976
15031
|
superFunc.__isSuperConstructor = true;
|
package/dist/index.d.ts
CHANGED
|
@@ -14859,6 +14859,7 @@ class Interpreter {
|
|
|
14859
14859
|
let constructor = null;
|
|
14860
14860
|
const methods = {};
|
|
14861
14861
|
const staticMethods = {};
|
|
14862
|
+
const instanceFields = [];
|
|
14862
14863
|
|
|
14863
14864
|
for (const member of node.body.body) {
|
|
14864
14865
|
if (member.type === 'MethodDefinition') {
|
|
@@ -14872,6 +14873,8 @@ class Interpreter {
|
|
|
14872
14873
|
} else {
|
|
14873
14874
|
methods[methodName] = methodFunc;
|
|
14874
14875
|
}
|
|
14876
|
+
} else if (member.type === 'PropertyDefinition' && !member.static && !member.declare && !member.abstract) {
|
|
14877
|
+
instanceFields.push(member);
|
|
14875
14878
|
}
|
|
14876
14879
|
}
|
|
14877
14880
|
|
|
@@ -14879,18 +14882,11 @@ class Interpreter {
|
|
|
14879
14882
|
const classConstructor = function(...args) {
|
|
14880
14883
|
// Create instance
|
|
14881
14884
|
const instance = Object.create(classConstructor.prototype);
|
|
14885
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
14882
14886
|
|
|
14883
|
-
//
|
|
14884
|
-
if (
|
|
14885
|
-
|
|
14886
|
-
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
14887
|
-
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
14888
|
-
return result.value;
|
|
14889
|
-
}
|
|
14890
|
-
} else if (superClass) {
|
|
14891
|
-
// If no constructor defined but has superClass, implicitly call super()
|
|
14892
|
-
// Call the superClass constructor properly - it's a classConstructor function
|
|
14893
|
-
superClass.call(instance, ...args);
|
|
14887
|
+
// Only use the returned object if it's an explicit return of an object (not the instance)
|
|
14888
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === 'object' && result.value !== instance) {
|
|
14889
|
+
return result.value;
|
|
14894
14890
|
}
|
|
14895
14891
|
|
|
14896
14892
|
return instance;
|
|
@@ -14901,6 +14897,12 @@ class Interpreter {
|
|
|
14901
14897
|
classConstructor.__constructor = constructor;
|
|
14902
14898
|
}
|
|
14903
14899
|
|
|
14900
|
+
classConstructor.__instanceFields = instanceFields;
|
|
14901
|
+
classConstructor.__superClass = superClass;
|
|
14902
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
14903
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
14904
|
+
};
|
|
14905
|
+
|
|
14904
14906
|
// Set up prototype chain
|
|
14905
14907
|
if (superClass) {
|
|
14906
14908
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
@@ -14935,6 +14937,69 @@ class Interpreter {
|
|
|
14935
14937
|
return classConstructor;
|
|
14936
14938
|
}
|
|
14937
14939
|
|
|
14940
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
14941
|
+
const superClass = classConstructor.__superClass;
|
|
14942
|
+
const constructor = classConstructor.__constructor;
|
|
14943
|
+
let fieldsInitialized = false;
|
|
14944
|
+
|
|
14945
|
+
const initializeOwnFields = () => {
|
|
14946
|
+
if (!fieldsInitialized) {
|
|
14947
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
14948
|
+
fieldsInitialized = true;
|
|
14949
|
+
}
|
|
14950
|
+
};
|
|
14951
|
+
|
|
14952
|
+
if (!superClass) {
|
|
14953
|
+
initializeOwnFields();
|
|
14954
|
+
if (constructor) {
|
|
14955
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
14956
|
+
}
|
|
14957
|
+
return undefined;
|
|
14958
|
+
}
|
|
14959
|
+
|
|
14960
|
+
if (constructor) {
|
|
14961
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
14962
|
+
initializeOwnFields();
|
|
14963
|
+
return result;
|
|
14964
|
+
}
|
|
14965
|
+
|
|
14966
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
14967
|
+
initializeOwnFields();
|
|
14968
|
+
return undefined;
|
|
14969
|
+
}
|
|
14970
|
+
|
|
14971
|
+
initializeSuperClass(superClass, instance, args) {
|
|
14972
|
+
if (superClass.__constructInto) {
|
|
14973
|
+
return superClass.__constructInto(instance, args);
|
|
14974
|
+
}
|
|
14975
|
+
|
|
14976
|
+
// For native constructors like Error, use Reflect.construct and copy instance properties.
|
|
14977
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
14978
|
+
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
14979
|
+
instance[name] = tempInstance[name];
|
|
14980
|
+
});
|
|
14981
|
+
return undefined;
|
|
14982
|
+
}
|
|
14983
|
+
|
|
14984
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
14985
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
14986
|
+
const fieldEnv = new Environment(env);
|
|
14987
|
+
fieldEnv.define('this', instance);
|
|
14988
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
14989
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
|
|
14990
|
+
}
|
|
14991
|
+
}
|
|
14992
|
+
|
|
14993
|
+
getClassFieldName(field, env) {
|
|
14994
|
+
if (field.computed) {
|
|
14995
|
+
return this.evaluate(field.key, env);
|
|
14996
|
+
}
|
|
14997
|
+
if (field.key.type === 'Identifier' || field.key.type === 'PrivateIdentifier') {
|
|
14998
|
+
return field.key.name;
|
|
14999
|
+
}
|
|
15000
|
+
return field.key.value;
|
|
15001
|
+
}
|
|
15002
|
+
|
|
14938
15003
|
createMethodFunction(funcNode, env, className) {
|
|
14939
15004
|
const func = {
|
|
14940
15005
|
__isFunction: true,
|
|
@@ -14946,7 +15011,7 @@ class Interpreter {
|
|
|
14946
15011
|
return func;
|
|
14947
15012
|
}
|
|
14948
15013
|
|
|
14949
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
15014
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
14950
15015
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
14951
15016
|
|
|
14952
15017
|
// Bind 'this'
|
|
@@ -14956,21 +15021,11 @@ class Interpreter {
|
|
|
14956
15021
|
if (superClass) {
|
|
14957
15022
|
// Create a super function that calls the parent constructor
|
|
14958
15023
|
const superFunc = (...superArgs) => {
|
|
14959
|
-
|
|
14960
|
-
if (
|
|
14961
|
-
|
|
14962
|
-
}
|
|
14963
|
-
// Otherwise, call superClass as a regular constructor (for native/external classes)
|
|
14964
|
-
else {
|
|
14965
|
-
// For native constructors like Error, we need to use Reflect.construct
|
|
14966
|
-
// to properly initialize the instance properties
|
|
14967
|
-
const tempInstance = Reflect.construct(superClass, superArgs, thisContext.constructor);
|
|
14968
|
-
// Copy properties from the temp instance to our thisContext
|
|
14969
|
-
Object.getOwnPropertyNames(tempInstance).forEach(name => {
|
|
14970
|
-
thisContext[name] = tempInstance[name];
|
|
14971
|
-
});
|
|
15024
|
+
const result = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
15025
|
+
if (afterSuper) {
|
|
15026
|
+
afterSuper();
|
|
14972
15027
|
}
|
|
14973
|
-
return undefined;
|
|
15028
|
+
return result && result.__explicitReturn ? result.value : undefined;
|
|
14974
15029
|
};
|
|
14975
15030
|
// Store both the function and mark it as super
|
|
14976
15031
|
superFunc.__isSuperConstructor = true;
|
package/dist/index.js
CHANGED
|
@@ -13539,6 +13539,7 @@ var Interpreter = class _Interpreter {
|
|
|
13539
13539
|
let constructor = null;
|
|
13540
13540
|
const methods = {};
|
|
13541
13541
|
const staticMethods = {};
|
|
13542
|
+
const instanceFields = [];
|
|
13542
13543
|
for (const member of node.body.body) {
|
|
13543
13544
|
if (member.type === "MethodDefinition") {
|
|
13544
13545
|
const methodName = member.key.name || member.key.value;
|
|
@@ -13550,23 +13551,26 @@ var Interpreter = class _Interpreter {
|
|
|
13550
13551
|
} else {
|
|
13551
13552
|
methods[methodName] = methodFunc;
|
|
13552
13553
|
}
|
|
13554
|
+
} else if (member.type === "PropertyDefinition" && !member.static && !member.declare && !member.abstract) {
|
|
13555
|
+
instanceFields.push(member);
|
|
13553
13556
|
}
|
|
13554
13557
|
}
|
|
13555
13558
|
const classConstructor = function(...args) {
|
|
13556
13559
|
const instance = Object.create(classConstructor.prototype);
|
|
13557
|
-
|
|
13558
|
-
|
|
13559
|
-
|
|
13560
|
-
return result.value;
|
|
13561
|
-
}
|
|
13562
|
-
} else if (superClass) {
|
|
13563
|
-
superClass.call(instance, ...args);
|
|
13560
|
+
const result = interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13561
|
+
if (result && result.__explicitReturn && result.value && typeof result.value === "object" && result.value !== instance) {
|
|
13562
|
+
return result.value;
|
|
13564
13563
|
}
|
|
13565
13564
|
return instance;
|
|
13566
13565
|
};
|
|
13567
13566
|
if (constructor) {
|
|
13568
13567
|
classConstructor.__constructor = constructor;
|
|
13569
13568
|
}
|
|
13569
|
+
classConstructor.__instanceFields = instanceFields;
|
|
13570
|
+
classConstructor.__superClass = superClass;
|
|
13571
|
+
classConstructor.__constructInto = (instance, args) => {
|
|
13572
|
+
return interpreter.constructClassInto(classConstructor, instance, args, env);
|
|
13573
|
+
};
|
|
13570
13574
|
if (superClass) {
|
|
13571
13575
|
classConstructor.prototype = Object.create(superClass.prototype);
|
|
13572
13576
|
classConstructor.prototype.constructor = classConstructor;
|
|
@@ -13592,6 +13596,59 @@ var Interpreter = class _Interpreter {
|
|
|
13592
13596
|
classConstructor.__className = className;
|
|
13593
13597
|
return classConstructor;
|
|
13594
13598
|
}
|
|
13599
|
+
constructClassInto(classConstructor, instance, args, env) {
|
|
13600
|
+
const superClass = classConstructor.__superClass;
|
|
13601
|
+
const constructor = classConstructor.__constructor;
|
|
13602
|
+
let fieldsInitialized = false;
|
|
13603
|
+
const initializeOwnFields = () => {
|
|
13604
|
+
if (!fieldsInitialized) {
|
|
13605
|
+
this.initializeClassFields(classConstructor, instance, env);
|
|
13606
|
+
fieldsInitialized = true;
|
|
13607
|
+
}
|
|
13608
|
+
};
|
|
13609
|
+
if (!superClass) {
|
|
13610
|
+
initializeOwnFields();
|
|
13611
|
+
if (constructor) {
|
|
13612
|
+
return this.callMethodFunction(constructor, instance, args, env, null);
|
|
13613
|
+
}
|
|
13614
|
+
return void 0;
|
|
13615
|
+
}
|
|
13616
|
+
if (constructor) {
|
|
13617
|
+
const result = this.callMethodFunction(constructor, instance, args, env, superClass, initializeOwnFields);
|
|
13618
|
+
initializeOwnFields();
|
|
13619
|
+
return result;
|
|
13620
|
+
}
|
|
13621
|
+
this.initializeSuperClass(superClass, instance, args);
|
|
13622
|
+
initializeOwnFields();
|
|
13623
|
+
return void 0;
|
|
13624
|
+
}
|
|
13625
|
+
initializeSuperClass(superClass, instance, args) {
|
|
13626
|
+
if (superClass.__constructInto) {
|
|
13627
|
+
return superClass.__constructInto(instance, args);
|
|
13628
|
+
}
|
|
13629
|
+
const tempInstance = Reflect.construct(superClass, args, instance.constructor);
|
|
13630
|
+
Object.getOwnPropertyNames(tempInstance).forEach((name) => {
|
|
13631
|
+
instance[name] = tempInstance[name];
|
|
13632
|
+
});
|
|
13633
|
+
return void 0;
|
|
13634
|
+
}
|
|
13635
|
+
initializeClassFields(classConstructor, instance, env) {
|
|
13636
|
+
for (const field of classConstructor.__instanceFields || []) {
|
|
13637
|
+
const fieldEnv = new Environment(env);
|
|
13638
|
+
fieldEnv.define("this", instance);
|
|
13639
|
+
const name = this.getClassFieldName(field, fieldEnv);
|
|
13640
|
+
instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : void 0;
|
|
13641
|
+
}
|
|
13642
|
+
}
|
|
13643
|
+
getClassFieldName(field, env) {
|
|
13644
|
+
if (field.computed) {
|
|
13645
|
+
return this.evaluate(field.key, env);
|
|
13646
|
+
}
|
|
13647
|
+
if (field.key.type === "Identifier" || field.key.type === "PrivateIdentifier") {
|
|
13648
|
+
return field.key.name;
|
|
13649
|
+
}
|
|
13650
|
+
return field.key.value;
|
|
13651
|
+
}
|
|
13595
13652
|
createMethodFunction(funcNode, env, className) {
|
|
13596
13653
|
const func = {
|
|
13597
13654
|
__isFunction: true,
|
|
@@ -13602,20 +13659,16 @@ var Interpreter = class _Interpreter {
|
|
|
13602
13659
|
};
|
|
13603
13660
|
return func;
|
|
13604
13661
|
}
|
|
13605
|
-
callMethodFunction(methodFunc, thisContext, args, env, superClass = null) {
|
|
13662
|
+
callMethodFunction(methodFunc, thisContext, args, env, superClass = null, afterSuper = null) {
|
|
13606
13663
|
const funcEnv = new Environment(methodFunc.__env || env);
|
|
13607
13664
|
funcEnv.define("this", thisContext);
|
|
13608
13665
|
if (superClass) {
|
|
13609
13666
|
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
|
-
});
|
|
13667
|
+
const result2 = this.initializeSuperClass(superClass, thisContext, superArgs);
|
|
13668
|
+
if (afterSuper) {
|
|
13669
|
+
afterSuper();
|
|
13617
13670
|
}
|
|
13618
|
-
return void 0;
|
|
13671
|
+
return result2 && result2.__explicitReturn ? result2.value : void 0;
|
|
13619
13672
|
};
|
|
13620
13673
|
superFunc.__isSuperConstructor = true;
|
|
13621
13674
|
superFunc.__superClass = superClass;
|