jslike 1.8.4 → 1.8.6

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.
@@ -229,6 +229,78 @@ function collectRuntimeIdentifierReferences(node) {
229
229
  return references;
230
230
  }
231
231
 
232
+ function getExportName(nameNode) {
233
+ return nameNode?.name ?? nameNode?.value;
234
+ }
235
+
236
+ function collectDeclarationExportNames(declaration) {
237
+ const names = [];
238
+
239
+ if (!declaration || isTypeOnlyDeclaration(declaration)) {
240
+ return names;
241
+ }
242
+
243
+ if (declaration.type === 'FunctionDeclaration' ||
244
+ declaration.type === 'ClassDeclaration' ||
245
+ declaration.type === 'TSEnumDeclaration') {
246
+ const name = getPatternName(declaration.id);
247
+ if (name) names.push(name);
248
+ return names;
249
+ }
250
+
251
+ if (declaration.type === 'VariableDeclaration') {
252
+ for (const declarator of declaration.declarations || []) {
253
+ const name = getPatternName(declarator.id);
254
+ if (name) names.push(name);
255
+ }
256
+ }
257
+
258
+ return names;
259
+ }
260
+
261
+ function collectStaticExportNames(moduleAst) {
262
+ const names = new Set();
263
+
264
+ for (const statement of moduleAst?.body || []) {
265
+ if (statement.type === 'ExportDefaultDeclaration') {
266
+ names.add('default');
267
+ continue;
268
+ }
269
+
270
+ if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type') {
271
+ continue;
272
+ }
273
+
274
+ if (statement.declaration) {
275
+ for (const name of collectDeclarationExportNames(statement.declaration)) {
276
+ names.add(name);
277
+ }
278
+ continue;
279
+ }
280
+
281
+ for (const specifier of statement.specifiers || []) {
282
+ if (specifier.exportKind === 'type') {
283
+ continue;
284
+ }
285
+
286
+ const exportedName = getExportName(specifier.exported);
287
+ if (exportedName) {
288
+ names.add(exportedName);
289
+ }
290
+ }
291
+ }
292
+
293
+ return names;
294
+ }
295
+
296
+ function predeclareModuleExports(moduleAst, moduleExports) {
297
+ for (const name of collectStaticExportNames(moduleAst)) {
298
+ if (!Object.prototype.hasOwnProperty.call(moduleExports, name)) {
299
+ moduleExports[name] = undefined;
300
+ }
301
+ }
302
+ }
303
+
232
304
  export class Interpreter {
233
305
  constructor(globalEnv, options = {}) {
234
306
  this.globalEnv = globalEnv;
@@ -2255,12 +2327,17 @@ export class Interpreter {
2255
2327
  });
2256
2328
  moduleInterpreter.moduleCache = this.moduleCache; // Share cache
2257
2329
 
2258
- // Execute module and collect exports
2259
- await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
2260
-
2261
- // Cache the module exports
2262
2330
  moduleExports = moduleInterpreter.moduleExports;
2331
+ predeclareModuleExports(moduleAst, moduleExports);
2263
2332
  this.moduleCache.set(resolvedPath, moduleExports);
2333
+
2334
+ try {
2335
+ // Execute module and populate the cached exports object.
2336
+ await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
2337
+ } catch (error) {
2338
+ this.moduleCache.delete(resolvedPath);
2339
+ throw error;
2340
+ }
2264
2341
  }
2265
2342
  }
2266
2343
 
@@ -2768,7 +2845,7 @@ export class Interpreter {
2768
2845
  // Add methods to prototype
2769
2846
  for (const [name, method] of Object.entries(methods)) {
2770
2847
  classConstructor.prototype[name] = function(...args) {
2771
- const result = interpreter.callMethodFunction(method, this, args, env);
2848
+ const result = interpreter.callMethodFunction(method, this, args, env, superClass);
2772
2849
  // Unwrap explicit return marker
2773
2850
  if (result && result.__explicitReturn) {
2774
2851
  return result.value;
@@ -2841,6 +2918,9 @@ export class Interpreter {
2841
2918
  for (const field of classConstructor.__instanceFields || []) {
2842
2919
  const fieldEnv = new Environment(env);
2843
2920
  fieldEnv.define('this', instance);
2921
+ if (classConstructor.__superClass) {
2922
+ fieldEnv.define('super', this.createSuperBinding(classConstructor.__superClass, instance, false));
2923
+ }
2844
2924
  const name = this.getClassFieldName(field, fieldEnv);
2845
2925
  instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
2846
2926
  }
@@ -2856,6 +2936,38 @@ export class Interpreter {
2856
2936
  return field.key.value;
2857
2937
  }
2858
2938
 
2939
+ createSuperBinding(superClass, thisContext, allowConstructor = false, afterSuper = null) {
2940
+ const superConstructor = (...superArgs) => {
2941
+ if (!allowConstructor) {
2942
+ throw new ReferenceError("'super' keyword is unexpected here");
2943
+ }
2944
+ const result = this.initializeSuperClass(superClass, thisContext, superArgs);
2945
+ if (afterSuper) {
2946
+ afterSuper();
2947
+ }
2948
+ return result && result.__explicitReturn ? result.value : undefined;
2949
+ };
2950
+
2951
+ superConstructor.__isSuperConstructor = true;
2952
+ superConstructor.__superClass = superClass;
2953
+
2954
+ return new Proxy(superConstructor, {
2955
+ get(target, prop, receiver) {
2956
+ if (prop in target) {
2957
+ return Reflect.get(target, prop, receiver);
2958
+ }
2959
+
2960
+ const prototype = superClass?.prototype;
2961
+ if (!prototype) {
2962
+ return undefined;
2963
+ }
2964
+
2965
+ const value = Reflect.get(prototype, prop, thisContext);
2966
+ return typeof value === 'function' ? value.bind(thisContext) : value;
2967
+ }
2968
+ });
2969
+ }
2970
+
2859
2971
  createMethodFunction(funcNode, env, className) {
2860
2972
  const func = {
2861
2973
  __isFunction: true,
@@ -2875,18 +2987,7 @@ export class Interpreter {
2875
2987
 
2876
2988
  // Bind 'super' if superClass exists
2877
2989
  if (superClass) {
2878
- // Create a super function that calls the parent constructor
2879
- const superFunc = (...superArgs) => {
2880
- const result = this.initializeSuperClass(superClass, thisContext, superArgs);
2881
- if (afterSuper) {
2882
- afterSuper();
2883
- }
2884
- return result && result.__explicitReturn ? result.value : undefined;
2885
- };
2886
- // Store both the function and mark it as super
2887
- superFunc.__isSuperConstructor = true;
2888
- superFunc.__superClass = superClass;
2889
- funcEnv.define('super', superFunc);
2990
+ funcEnv.define('super', this.createSuperBinding(superClass, thisContext, true, afterSuper));
2890
2991
  }
2891
2992
 
2892
2993
  this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
package/dist/index.cjs CHANGED
@@ -11710,6 +11710,62 @@ function collectRuntimeIdentifierReferences(node) {
11710
11710
  visit(node);
11711
11711
  return references;
11712
11712
  }
11713
+ function getExportName(nameNode) {
11714
+ return (nameNode == null ? void 0 : nameNode.name) ?? (nameNode == null ? void 0 : nameNode.value);
11715
+ }
11716
+ function collectDeclarationExportNames(declaration) {
11717
+ const names = [];
11718
+ if (!declaration || isTypeOnlyDeclaration(declaration)) {
11719
+ return names;
11720
+ }
11721
+ if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration" || declaration.type === "TSEnumDeclaration") {
11722
+ const name = getPatternName(declaration.id);
11723
+ if (name) names.push(name);
11724
+ return names;
11725
+ }
11726
+ if (declaration.type === "VariableDeclaration") {
11727
+ for (const declarator of declaration.declarations || []) {
11728
+ const name = getPatternName(declarator.id);
11729
+ if (name) names.push(name);
11730
+ }
11731
+ }
11732
+ return names;
11733
+ }
11734
+ function collectStaticExportNames(moduleAst) {
11735
+ const names = /* @__PURE__ */ new Set();
11736
+ for (const statement of (moduleAst == null ? void 0 : moduleAst.body) || []) {
11737
+ if (statement.type === "ExportDefaultDeclaration") {
11738
+ names.add("default");
11739
+ continue;
11740
+ }
11741
+ if (statement.type !== "ExportNamedDeclaration" || statement.exportKind === "type") {
11742
+ continue;
11743
+ }
11744
+ if (statement.declaration) {
11745
+ for (const name of collectDeclarationExportNames(statement.declaration)) {
11746
+ names.add(name);
11747
+ }
11748
+ continue;
11749
+ }
11750
+ for (const specifier of statement.specifiers || []) {
11751
+ if (specifier.exportKind === "type") {
11752
+ continue;
11753
+ }
11754
+ const exportedName = getExportName(specifier.exported);
11755
+ if (exportedName) {
11756
+ names.add(exportedName);
11757
+ }
11758
+ }
11759
+ }
11760
+ return names;
11761
+ }
11762
+ function predeclareModuleExports(moduleAst, moduleExports) {
11763
+ for (const name of collectStaticExportNames(moduleAst)) {
11764
+ if (!Object.prototype.hasOwnProperty.call(moduleExports, name)) {
11765
+ moduleExports[name] = void 0;
11766
+ }
11767
+ }
11768
+ }
11713
11769
  var Interpreter = class _Interpreter {
11714
11770
  constructor(globalEnv, options = {}) {
11715
11771
  this.globalEnv = globalEnv;
@@ -13279,9 +13335,15 @@ var Interpreter = class _Interpreter {
13279
13335
  executionController: this.executionController
13280
13336
  });
13281
13337
  moduleInterpreter.moduleCache = this.moduleCache;
13282
- await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
13283
13338
  moduleExports = moduleInterpreter.moduleExports;
13339
+ predeclareModuleExports(moduleAst, moduleExports);
13284
13340
  this.moduleCache.set(resolvedPath, moduleExports);
13341
+ try {
13342
+ await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
13343
+ } catch (error) {
13344
+ this.moduleCache.delete(resolvedPath);
13345
+ throw error;
13346
+ }
13285
13347
  }
13286
13348
  }
13287
13349
  this.bindImportSpecifiers(node, env, modulePath, moduleExports);
@@ -13658,7 +13720,7 @@ var Interpreter = class _Interpreter {
13658
13720
  }
13659
13721
  for (const [name, method] of Object.entries(methods)) {
13660
13722
  classConstructor.prototype[name] = function(...args) {
13661
- const result = interpreter.callMethodFunction(method, this, args, env);
13723
+ const result = interpreter.callMethodFunction(method, this, args, env, superClass);
13662
13724
  if (result && result.__explicitReturn) {
13663
13725
  return result.value;
13664
13726
  }
@@ -13717,6 +13779,9 @@ var Interpreter = class _Interpreter {
13717
13779
  for (const field of classConstructor.__instanceFields || []) {
13718
13780
  const fieldEnv = new Environment(env);
13719
13781
  fieldEnv.define("this", instance);
13782
+ if (classConstructor.__superClass) {
13783
+ fieldEnv.define("super", this.createSuperBinding(classConstructor.__superClass, instance, false));
13784
+ }
13720
13785
  const name = this.getClassFieldName(field, fieldEnv);
13721
13786
  instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : void 0;
13722
13787
  }
@@ -13730,6 +13795,33 @@ var Interpreter = class _Interpreter {
13730
13795
  }
13731
13796
  return field.key.value;
13732
13797
  }
13798
+ createSuperBinding(superClass, thisContext, allowConstructor = false, afterSuper = null) {
13799
+ const superConstructor = (...superArgs) => {
13800
+ if (!allowConstructor) {
13801
+ throw new ReferenceError("'super' keyword is unexpected here");
13802
+ }
13803
+ const result = this.initializeSuperClass(superClass, thisContext, superArgs);
13804
+ if (afterSuper) {
13805
+ afterSuper();
13806
+ }
13807
+ return result && result.__explicitReturn ? result.value : void 0;
13808
+ };
13809
+ superConstructor.__isSuperConstructor = true;
13810
+ superConstructor.__superClass = superClass;
13811
+ return new Proxy(superConstructor, {
13812
+ get(target, prop, receiver) {
13813
+ if (prop in target) {
13814
+ return Reflect.get(target, prop, receiver);
13815
+ }
13816
+ const prototype = superClass == null ? void 0 : superClass.prototype;
13817
+ if (!prototype) {
13818
+ return void 0;
13819
+ }
13820
+ const value = Reflect.get(prototype, prop, thisContext);
13821
+ return typeof value === "function" ? value.bind(thisContext) : value;
13822
+ }
13823
+ });
13824
+ }
13733
13825
  createMethodFunction(funcNode, env, className) {
13734
13826
  const func = {
13735
13827
  __isFunction: true,
@@ -13744,16 +13836,7 @@ var Interpreter = class _Interpreter {
13744
13836
  const funcEnv = new Environment(methodFunc.__env || env);
13745
13837
  funcEnv.define("this", thisContext);
13746
13838
  if (superClass) {
13747
- const superFunc = (...superArgs) => {
13748
- const result2 = this.initializeSuperClass(superClass, thisContext, superArgs);
13749
- if (afterSuper) {
13750
- afterSuper();
13751
- }
13752
- return result2 && result2.__explicitReturn ? result2.value : void 0;
13753
- };
13754
- superFunc.__isSuperConstructor = true;
13755
- superFunc.__superClass = superClass;
13756
- funcEnv.define("super", superFunc);
13839
+ funcEnv.define("super", this.createSuperBinding(superClass, thisContext, true, afterSuper));
13757
13840
  }
13758
13841
  this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
13759
13842
  const result = this.evaluate(methodFunc.__body, funcEnv);
package/dist/index.d.cts CHANGED
@@ -12422,6 +12422,78 @@ function collectRuntimeIdentifierReferences(node) {
12422
12422
  return references;
12423
12423
  }
12424
12424
 
12425
+ function getExportName(nameNode) {
12426
+ return nameNode?.name ?? nameNode?.value;
12427
+ }
12428
+
12429
+ function collectDeclarationExportNames(declaration) {
12430
+ const names = [];
12431
+
12432
+ if (!declaration || isTypeOnlyDeclaration(declaration)) {
12433
+ return names;
12434
+ }
12435
+
12436
+ if (declaration.type === 'FunctionDeclaration' ||
12437
+ declaration.type === 'ClassDeclaration' ||
12438
+ declaration.type === 'TSEnumDeclaration') {
12439
+ const name = getPatternName(declaration.id);
12440
+ if (name) names.push(name);
12441
+ return names;
12442
+ }
12443
+
12444
+ if (declaration.type === 'VariableDeclaration') {
12445
+ for (const declarator of declaration.declarations || []) {
12446
+ const name = getPatternName(declarator.id);
12447
+ if (name) names.push(name);
12448
+ }
12449
+ }
12450
+
12451
+ return names;
12452
+ }
12453
+
12454
+ function collectStaticExportNames(moduleAst) {
12455
+ const names = new Set();
12456
+
12457
+ for (const statement of moduleAst?.body || []) {
12458
+ if (statement.type === 'ExportDefaultDeclaration') {
12459
+ names.add('default');
12460
+ continue;
12461
+ }
12462
+
12463
+ if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type') {
12464
+ continue;
12465
+ }
12466
+
12467
+ if (statement.declaration) {
12468
+ for (const name of collectDeclarationExportNames(statement.declaration)) {
12469
+ names.add(name);
12470
+ }
12471
+ continue;
12472
+ }
12473
+
12474
+ for (const specifier of statement.specifiers || []) {
12475
+ if (specifier.exportKind === 'type') {
12476
+ continue;
12477
+ }
12478
+
12479
+ const exportedName = getExportName(specifier.exported);
12480
+ if (exportedName) {
12481
+ names.add(exportedName);
12482
+ }
12483
+ }
12484
+ }
12485
+
12486
+ return names;
12487
+ }
12488
+
12489
+ function predeclareModuleExports(moduleAst, moduleExports) {
12490
+ for (const name of collectStaticExportNames(moduleAst)) {
12491
+ if (!Object.prototype.hasOwnProperty.call(moduleExports, name)) {
12492
+ moduleExports[name] = undefined;
12493
+ }
12494
+ }
12495
+ }
12496
+
12425
12497
  class Interpreter {
12426
12498
  constructor(globalEnv, options = {}) {
12427
12499
  this.globalEnv = globalEnv;
@@ -14448,12 +14520,17 @@ class Interpreter {
14448
14520
  });
14449
14521
  moduleInterpreter.moduleCache = this.moduleCache; // Share cache
14450
14522
 
14451
- // Execute module and collect exports
14452
- await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
14453
-
14454
- // Cache the module exports
14455
14523
  moduleExports = moduleInterpreter.moduleExports;
14524
+ predeclareModuleExports(moduleAst, moduleExports);
14456
14525
  this.moduleCache.set(resolvedPath, moduleExports);
14526
+
14527
+ try {
14528
+ // Execute module and populate the cached exports object.
14529
+ await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
14530
+ } catch (error) {
14531
+ this.moduleCache.delete(resolvedPath);
14532
+ throw error;
14533
+ }
14457
14534
  }
14458
14535
  }
14459
14536
 
@@ -14961,7 +15038,7 @@ class Interpreter {
14961
15038
  // Add methods to prototype
14962
15039
  for (const [name, method] of Object.entries(methods)) {
14963
15040
  classConstructor.prototype[name] = function(...args) {
14964
- const result = interpreter.callMethodFunction(method, this, args, env);
15041
+ const result = interpreter.callMethodFunction(method, this, args, env, superClass);
14965
15042
  // Unwrap explicit return marker
14966
15043
  if (result && result.__explicitReturn) {
14967
15044
  return result.value;
@@ -15034,6 +15111,9 @@ class Interpreter {
15034
15111
  for (const field of classConstructor.__instanceFields || []) {
15035
15112
  const fieldEnv = new Environment(env);
15036
15113
  fieldEnv.define('this', instance);
15114
+ if (classConstructor.__superClass) {
15115
+ fieldEnv.define('super', this.createSuperBinding(classConstructor.__superClass, instance, false));
15116
+ }
15037
15117
  const name = this.getClassFieldName(field, fieldEnv);
15038
15118
  instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
15039
15119
  }
@@ -15049,6 +15129,38 @@ class Interpreter {
15049
15129
  return field.key.value;
15050
15130
  }
15051
15131
 
15132
+ createSuperBinding(superClass, thisContext, allowConstructor = false, afterSuper = null) {
15133
+ const superConstructor = (...superArgs) => {
15134
+ if (!allowConstructor) {
15135
+ throw new ReferenceError("'super' keyword is unexpected here");
15136
+ }
15137
+ const result = this.initializeSuperClass(superClass, thisContext, superArgs);
15138
+ if (afterSuper) {
15139
+ afterSuper();
15140
+ }
15141
+ return result && result.__explicitReturn ? result.value : undefined;
15142
+ };
15143
+
15144
+ superConstructor.__isSuperConstructor = true;
15145
+ superConstructor.__superClass = superClass;
15146
+
15147
+ return new Proxy(superConstructor, {
15148
+ get(target, prop, receiver) {
15149
+ if (prop in target) {
15150
+ return Reflect.get(target, prop, receiver);
15151
+ }
15152
+
15153
+ const prototype = superClass?.prototype;
15154
+ if (!prototype) {
15155
+ return undefined;
15156
+ }
15157
+
15158
+ const value = Reflect.get(prototype, prop, thisContext);
15159
+ return typeof value === 'function' ? value.bind(thisContext) : value;
15160
+ }
15161
+ });
15162
+ }
15163
+
15052
15164
  createMethodFunction(funcNode, env, className) {
15053
15165
  const func = {
15054
15166
  __isFunction: true,
@@ -15068,18 +15180,7 @@ class Interpreter {
15068
15180
 
15069
15181
  // Bind 'super' if superClass exists
15070
15182
  if (superClass) {
15071
- // Create a super function that calls the parent constructor
15072
- const superFunc = (...superArgs) => {
15073
- const result = this.initializeSuperClass(superClass, thisContext, superArgs);
15074
- if (afterSuper) {
15075
- afterSuper();
15076
- }
15077
- return result && result.__explicitReturn ? result.value : undefined;
15078
- };
15079
- // Store both the function and mark it as super
15080
- superFunc.__isSuperConstructor = true;
15081
- superFunc.__superClass = superClass;
15082
- funcEnv.define('super', superFunc);
15183
+ funcEnv.define('super', this.createSuperBinding(superClass, thisContext, true, afterSuper));
15083
15184
  }
15084
15185
 
15085
15186
  this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
package/dist/index.d.ts CHANGED
@@ -12422,6 +12422,78 @@ function collectRuntimeIdentifierReferences(node) {
12422
12422
  return references;
12423
12423
  }
12424
12424
 
12425
+ function getExportName(nameNode) {
12426
+ return nameNode?.name ?? nameNode?.value;
12427
+ }
12428
+
12429
+ function collectDeclarationExportNames(declaration) {
12430
+ const names = [];
12431
+
12432
+ if (!declaration || isTypeOnlyDeclaration(declaration)) {
12433
+ return names;
12434
+ }
12435
+
12436
+ if (declaration.type === 'FunctionDeclaration' ||
12437
+ declaration.type === 'ClassDeclaration' ||
12438
+ declaration.type === 'TSEnumDeclaration') {
12439
+ const name = getPatternName(declaration.id);
12440
+ if (name) names.push(name);
12441
+ return names;
12442
+ }
12443
+
12444
+ if (declaration.type === 'VariableDeclaration') {
12445
+ for (const declarator of declaration.declarations || []) {
12446
+ const name = getPatternName(declarator.id);
12447
+ if (name) names.push(name);
12448
+ }
12449
+ }
12450
+
12451
+ return names;
12452
+ }
12453
+
12454
+ function collectStaticExportNames(moduleAst) {
12455
+ const names = new Set();
12456
+
12457
+ for (const statement of moduleAst?.body || []) {
12458
+ if (statement.type === 'ExportDefaultDeclaration') {
12459
+ names.add('default');
12460
+ continue;
12461
+ }
12462
+
12463
+ if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type') {
12464
+ continue;
12465
+ }
12466
+
12467
+ if (statement.declaration) {
12468
+ for (const name of collectDeclarationExportNames(statement.declaration)) {
12469
+ names.add(name);
12470
+ }
12471
+ continue;
12472
+ }
12473
+
12474
+ for (const specifier of statement.specifiers || []) {
12475
+ if (specifier.exportKind === 'type') {
12476
+ continue;
12477
+ }
12478
+
12479
+ const exportedName = getExportName(specifier.exported);
12480
+ if (exportedName) {
12481
+ names.add(exportedName);
12482
+ }
12483
+ }
12484
+ }
12485
+
12486
+ return names;
12487
+ }
12488
+
12489
+ function predeclareModuleExports(moduleAst, moduleExports) {
12490
+ for (const name of collectStaticExportNames(moduleAst)) {
12491
+ if (!Object.prototype.hasOwnProperty.call(moduleExports, name)) {
12492
+ moduleExports[name] = undefined;
12493
+ }
12494
+ }
12495
+ }
12496
+
12425
12497
  class Interpreter {
12426
12498
  constructor(globalEnv, options = {}) {
12427
12499
  this.globalEnv = globalEnv;
@@ -14448,12 +14520,17 @@ class Interpreter {
14448
14520
  });
14449
14521
  moduleInterpreter.moduleCache = this.moduleCache; // Share cache
14450
14522
 
14451
- // Execute module and collect exports
14452
- await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
14453
-
14454
- // Cache the module exports
14455
14523
  moduleExports = moduleInterpreter.moduleExports;
14524
+ predeclareModuleExports(moduleAst, moduleExports);
14456
14525
  this.moduleCache.set(resolvedPath, moduleExports);
14526
+
14527
+ try {
14528
+ // Execute module and populate the cached exports object.
14529
+ await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
14530
+ } catch (error) {
14531
+ this.moduleCache.delete(resolvedPath);
14532
+ throw error;
14533
+ }
14457
14534
  }
14458
14535
  }
14459
14536
 
@@ -14961,7 +15038,7 @@ class Interpreter {
14961
15038
  // Add methods to prototype
14962
15039
  for (const [name, method] of Object.entries(methods)) {
14963
15040
  classConstructor.prototype[name] = function(...args) {
14964
- const result = interpreter.callMethodFunction(method, this, args, env);
15041
+ const result = interpreter.callMethodFunction(method, this, args, env, superClass);
14965
15042
  // Unwrap explicit return marker
14966
15043
  if (result && result.__explicitReturn) {
14967
15044
  return result.value;
@@ -15034,6 +15111,9 @@ class Interpreter {
15034
15111
  for (const field of classConstructor.__instanceFields || []) {
15035
15112
  const fieldEnv = new Environment(env);
15036
15113
  fieldEnv.define('this', instance);
15114
+ if (classConstructor.__superClass) {
15115
+ fieldEnv.define('super', this.createSuperBinding(classConstructor.__superClass, instance, false));
15116
+ }
15037
15117
  const name = this.getClassFieldName(field, fieldEnv);
15038
15118
  instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : undefined;
15039
15119
  }
@@ -15049,6 +15129,38 @@ class Interpreter {
15049
15129
  return field.key.value;
15050
15130
  }
15051
15131
 
15132
+ createSuperBinding(superClass, thisContext, allowConstructor = false, afterSuper = null) {
15133
+ const superConstructor = (...superArgs) => {
15134
+ if (!allowConstructor) {
15135
+ throw new ReferenceError("'super' keyword is unexpected here");
15136
+ }
15137
+ const result = this.initializeSuperClass(superClass, thisContext, superArgs);
15138
+ if (afterSuper) {
15139
+ afterSuper();
15140
+ }
15141
+ return result && result.__explicitReturn ? result.value : undefined;
15142
+ };
15143
+
15144
+ superConstructor.__isSuperConstructor = true;
15145
+ superConstructor.__superClass = superClass;
15146
+
15147
+ return new Proxy(superConstructor, {
15148
+ get(target, prop, receiver) {
15149
+ if (prop in target) {
15150
+ return Reflect.get(target, prop, receiver);
15151
+ }
15152
+
15153
+ const prototype = superClass?.prototype;
15154
+ if (!prototype) {
15155
+ return undefined;
15156
+ }
15157
+
15158
+ const value = Reflect.get(prototype, prop, thisContext);
15159
+ return typeof value === 'function' ? value.bind(thisContext) : value;
15160
+ }
15161
+ });
15162
+ }
15163
+
15052
15164
  createMethodFunction(funcNode, env, className) {
15053
15165
  const func = {
15054
15166
  __isFunction: true,
@@ -15068,18 +15180,7 @@ class Interpreter {
15068
15180
 
15069
15181
  // Bind 'super' if superClass exists
15070
15182
  if (superClass) {
15071
- // Create a super function that calls the parent constructor
15072
- const superFunc = (...superArgs) => {
15073
- const result = this.initializeSuperClass(superClass, thisContext, superArgs);
15074
- if (afterSuper) {
15075
- afterSuper();
15076
- }
15077
- return result && result.__explicitReturn ? result.value : undefined;
15078
- };
15079
- // Store both the function and mark it as super
15080
- superFunc.__isSuperConstructor = true;
15081
- superFunc.__superClass = superClass;
15082
- funcEnv.define('super', superFunc);
15183
+ funcEnv.define('super', this.createSuperBinding(superClass, thisContext, true, afterSuper));
15083
15184
  }
15084
15185
 
15085
15186
  this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
package/dist/index.js CHANGED
@@ -11676,6 +11676,62 @@ function collectRuntimeIdentifierReferences(node) {
11676
11676
  visit(node);
11677
11677
  return references;
11678
11678
  }
11679
+ function getExportName(nameNode) {
11680
+ return (nameNode == null ? void 0 : nameNode.name) ?? (nameNode == null ? void 0 : nameNode.value);
11681
+ }
11682
+ function collectDeclarationExportNames(declaration) {
11683
+ const names = [];
11684
+ if (!declaration || isTypeOnlyDeclaration(declaration)) {
11685
+ return names;
11686
+ }
11687
+ if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration" || declaration.type === "TSEnumDeclaration") {
11688
+ const name = getPatternName(declaration.id);
11689
+ if (name) names.push(name);
11690
+ return names;
11691
+ }
11692
+ if (declaration.type === "VariableDeclaration") {
11693
+ for (const declarator of declaration.declarations || []) {
11694
+ const name = getPatternName(declarator.id);
11695
+ if (name) names.push(name);
11696
+ }
11697
+ }
11698
+ return names;
11699
+ }
11700
+ function collectStaticExportNames(moduleAst) {
11701
+ const names = /* @__PURE__ */ new Set();
11702
+ for (const statement of (moduleAst == null ? void 0 : moduleAst.body) || []) {
11703
+ if (statement.type === "ExportDefaultDeclaration") {
11704
+ names.add("default");
11705
+ continue;
11706
+ }
11707
+ if (statement.type !== "ExportNamedDeclaration" || statement.exportKind === "type") {
11708
+ continue;
11709
+ }
11710
+ if (statement.declaration) {
11711
+ for (const name of collectDeclarationExportNames(statement.declaration)) {
11712
+ names.add(name);
11713
+ }
11714
+ continue;
11715
+ }
11716
+ for (const specifier of statement.specifiers || []) {
11717
+ if (specifier.exportKind === "type") {
11718
+ continue;
11719
+ }
11720
+ const exportedName = getExportName(specifier.exported);
11721
+ if (exportedName) {
11722
+ names.add(exportedName);
11723
+ }
11724
+ }
11725
+ }
11726
+ return names;
11727
+ }
11728
+ function predeclareModuleExports(moduleAst, moduleExports) {
11729
+ for (const name of collectStaticExportNames(moduleAst)) {
11730
+ if (!Object.prototype.hasOwnProperty.call(moduleExports, name)) {
11731
+ moduleExports[name] = void 0;
11732
+ }
11733
+ }
11734
+ }
11679
11735
  var Interpreter = class _Interpreter {
11680
11736
  constructor(globalEnv, options = {}) {
11681
11737
  this.globalEnv = globalEnv;
@@ -13245,9 +13301,15 @@ var Interpreter = class _Interpreter {
13245
13301
  executionController: this.executionController
13246
13302
  });
13247
13303
  moduleInterpreter.moduleCache = this.moduleCache;
13248
- await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
13249
13304
  moduleExports = moduleInterpreter.moduleExports;
13305
+ predeclareModuleExports(moduleAst, moduleExports);
13250
13306
  this.moduleCache.set(resolvedPath, moduleExports);
13307
+ try {
13308
+ await moduleInterpreter.evaluateAsync(moduleAst, moduleEnv);
13309
+ } catch (error) {
13310
+ this.moduleCache.delete(resolvedPath);
13311
+ throw error;
13312
+ }
13251
13313
  }
13252
13314
  }
13253
13315
  this.bindImportSpecifiers(node, env, modulePath, moduleExports);
@@ -13624,7 +13686,7 @@ var Interpreter = class _Interpreter {
13624
13686
  }
13625
13687
  for (const [name, method] of Object.entries(methods)) {
13626
13688
  classConstructor.prototype[name] = function(...args) {
13627
- const result = interpreter.callMethodFunction(method, this, args, env);
13689
+ const result = interpreter.callMethodFunction(method, this, args, env, superClass);
13628
13690
  if (result && result.__explicitReturn) {
13629
13691
  return result.value;
13630
13692
  }
@@ -13683,6 +13745,9 @@ var Interpreter = class _Interpreter {
13683
13745
  for (const field of classConstructor.__instanceFields || []) {
13684
13746
  const fieldEnv = new Environment(env);
13685
13747
  fieldEnv.define("this", instance);
13748
+ if (classConstructor.__superClass) {
13749
+ fieldEnv.define("super", this.createSuperBinding(classConstructor.__superClass, instance, false));
13750
+ }
13686
13751
  const name = this.getClassFieldName(field, fieldEnv);
13687
13752
  instance[name] = field.value ? this.evaluate(field.value, fieldEnv) : void 0;
13688
13753
  }
@@ -13696,6 +13761,33 @@ var Interpreter = class _Interpreter {
13696
13761
  }
13697
13762
  return field.key.value;
13698
13763
  }
13764
+ createSuperBinding(superClass, thisContext, allowConstructor = false, afterSuper = null) {
13765
+ const superConstructor = (...superArgs) => {
13766
+ if (!allowConstructor) {
13767
+ throw new ReferenceError("'super' keyword is unexpected here");
13768
+ }
13769
+ const result = this.initializeSuperClass(superClass, thisContext, superArgs);
13770
+ if (afterSuper) {
13771
+ afterSuper();
13772
+ }
13773
+ return result && result.__explicitReturn ? result.value : void 0;
13774
+ };
13775
+ superConstructor.__isSuperConstructor = true;
13776
+ superConstructor.__superClass = superClass;
13777
+ return new Proxy(superConstructor, {
13778
+ get(target, prop, receiver) {
13779
+ if (prop in target) {
13780
+ return Reflect.get(target, prop, receiver);
13781
+ }
13782
+ const prototype = superClass == null ? void 0 : superClass.prototype;
13783
+ if (!prototype) {
13784
+ return void 0;
13785
+ }
13786
+ const value = Reflect.get(prototype, prop, thisContext);
13787
+ return typeof value === "function" ? value.bind(thisContext) : value;
13788
+ }
13789
+ });
13790
+ }
13699
13791
  createMethodFunction(funcNode, env, className) {
13700
13792
  const func = {
13701
13793
  __isFunction: true,
@@ -13710,16 +13802,7 @@ var Interpreter = class _Interpreter {
13710
13802
  const funcEnv = new Environment(methodFunc.__env || env);
13711
13803
  funcEnv.define("this", thisContext);
13712
13804
  if (superClass) {
13713
- const superFunc = (...superArgs) => {
13714
- const result2 = this.initializeSuperClass(superClass, thisContext, superArgs);
13715
- if (afterSuper) {
13716
- afterSuper();
13717
- }
13718
- return result2 && result2.__explicitReturn ? result2.value : void 0;
13719
- };
13720
- superFunc.__isSuperConstructor = true;
13721
- superFunc.__superClass = superClass;
13722
- funcEnv.define("super", superFunc);
13805
+ funcEnv.define("super", this.createSuperBinding(superClass, thisContext, true, afterSuper));
13723
13806
  }
13724
13807
  this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
13725
13808
  const result = this.evaluate(methodFunc.__body, funcEnv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jslike",
3
- "version": "1.8.4",
3
+ "version": "1.8.6",
4
4
  "description": "Production-ready JavaScript interpreter with full ES6+ support using Acorn parser",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -62,7 +62,7 @@
62
62
  }
63
63
  },
64
64
  "bin": {
65
- "jslike": "./bin/jslike.js"
65
+ "jslike": "bin/jslike.js"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "node scripts/bundle-acorn.js && tsup && node scripts/post-build.js",