jslike 1.8.5 → 1.8.7
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 +115 -5
- package/dist/index.cjs +93 -2
- package/dist/index.d.cts +115 -5
- package/dist/index.d.ts +115 -5
- package/dist/index.js +93 -2
- package/package.json +2 -2
|
@@ -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
|
|
|
@@ -2769,6 +2846,14 @@ export class Interpreter {
|
|
|
2769
2846
|
for (const [name, method] of Object.entries(methods)) {
|
|
2770
2847
|
classConstructor.prototype[name] = function(...args) {
|
|
2771
2848
|
const result = interpreter.callMethodFunction(method, this, args, env, superClass);
|
|
2849
|
+
if (result && typeof result.then === 'function') {
|
|
2850
|
+
return result.then(resolved => {
|
|
2851
|
+
if (resolved && resolved.__explicitReturn) {
|
|
2852
|
+
return resolved.value;
|
|
2853
|
+
}
|
|
2854
|
+
return resolved;
|
|
2855
|
+
});
|
|
2856
|
+
}
|
|
2772
2857
|
// Unwrap explicit return marker
|
|
2773
2858
|
if (result && result.__explicitReturn) {
|
|
2774
2859
|
return result.value;
|
|
@@ -2781,6 +2866,14 @@ export class Interpreter {
|
|
|
2781
2866
|
for (const [name, method] of Object.entries(staticMethods)) {
|
|
2782
2867
|
classConstructor[name] = function(...args) {
|
|
2783
2868
|
const result = interpreter.callMethodFunction(method, classConstructor, args, env);
|
|
2869
|
+
if (result && typeof result.then === 'function') {
|
|
2870
|
+
return result.then(resolved => {
|
|
2871
|
+
if (resolved && resolved.__explicitReturn) {
|
|
2872
|
+
return resolved.value;
|
|
2873
|
+
}
|
|
2874
|
+
return resolved;
|
|
2875
|
+
});
|
|
2876
|
+
}
|
|
2784
2877
|
// Unwrap explicit return marker
|
|
2785
2878
|
if (result && result.__explicitReturn) {
|
|
2786
2879
|
return result.value;
|
|
@@ -2897,7 +2990,8 @@ export class Interpreter {
|
|
|
2897
2990
|
__params: funcNode.params,
|
|
2898
2991
|
__body: funcNode.body,
|
|
2899
2992
|
__env: env,
|
|
2900
|
-
__className: className
|
|
2993
|
+
__className: className,
|
|
2994
|
+
__async: funcNode.async || false
|
|
2901
2995
|
};
|
|
2902
2996
|
return func;
|
|
2903
2997
|
}
|
|
@@ -2915,6 +3009,22 @@ export class Interpreter {
|
|
|
2915
3009
|
|
|
2916
3010
|
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
2917
3011
|
|
|
3012
|
+
if (methodFunc.__async) {
|
|
3013
|
+
return (async () => {
|
|
3014
|
+
const result = await this.evaluateAsync(methodFunc.__body, funcEnv);
|
|
3015
|
+
|
|
3016
|
+
if (result instanceof ReturnValue) {
|
|
3017
|
+
return { __explicitReturn: true, value: result.value };
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
if (result instanceof ThrowSignal) {
|
|
3021
|
+
throw result.value;
|
|
3022
|
+
}
|
|
3023
|
+
|
|
3024
|
+
return result;
|
|
3025
|
+
})();
|
|
3026
|
+
}
|
|
3027
|
+
|
|
2918
3028
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
2919
3029
|
|
|
2920
3030
|
if (result instanceof ReturnValue) {
|
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);
|
|
@@ -13659,6 +13721,14 @@ var Interpreter = class _Interpreter {
|
|
|
13659
13721
|
for (const [name, method] of Object.entries(methods)) {
|
|
13660
13722
|
classConstructor.prototype[name] = function(...args) {
|
|
13661
13723
|
const result = interpreter.callMethodFunction(method, this, args, env, superClass);
|
|
13724
|
+
if (result && typeof result.then === "function") {
|
|
13725
|
+
return result.then((resolved) => {
|
|
13726
|
+
if (resolved && resolved.__explicitReturn) {
|
|
13727
|
+
return resolved.value;
|
|
13728
|
+
}
|
|
13729
|
+
return resolved;
|
|
13730
|
+
});
|
|
13731
|
+
}
|
|
13662
13732
|
if (result && result.__explicitReturn) {
|
|
13663
13733
|
return result.value;
|
|
13664
13734
|
}
|
|
@@ -13668,6 +13738,14 @@ var Interpreter = class _Interpreter {
|
|
|
13668
13738
|
for (const [name, method] of Object.entries(staticMethods)) {
|
|
13669
13739
|
classConstructor[name] = function(...args) {
|
|
13670
13740
|
const result = interpreter.callMethodFunction(method, classConstructor, args, env);
|
|
13741
|
+
if (result && typeof result.then === "function") {
|
|
13742
|
+
return result.then((resolved) => {
|
|
13743
|
+
if (resolved && resolved.__explicitReturn) {
|
|
13744
|
+
return resolved.value;
|
|
13745
|
+
}
|
|
13746
|
+
return resolved;
|
|
13747
|
+
});
|
|
13748
|
+
}
|
|
13671
13749
|
if (result && result.__explicitReturn) {
|
|
13672
13750
|
return result.value;
|
|
13673
13751
|
}
|
|
@@ -13766,7 +13844,8 @@ var Interpreter = class _Interpreter {
|
|
|
13766
13844
|
__params: funcNode.params,
|
|
13767
13845
|
__body: funcNode.body,
|
|
13768
13846
|
__env: env,
|
|
13769
|
-
__className: className
|
|
13847
|
+
__className: className,
|
|
13848
|
+
__async: funcNode.async || false
|
|
13770
13849
|
};
|
|
13771
13850
|
return func;
|
|
13772
13851
|
}
|
|
@@ -13777,6 +13856,18 @@ var Interpreter = class _Interpreter {
|
|
|
13777
13856
|
funcEnv.define("super", this.createSuperBinding(superClass, thisContext, true, afterSuper));
|
|
13778
13857
|
}
|
|
13779
13858
|
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
13859
|
+
if (methodFunc.__async) {
|
|
13860
|
+
return (async () => {
|
|
13861
|
+
const result2 = await this.evaluateAsync(methodFunc.__body, funcEnv);
|
|
13862
|
+
if (result2 instanceof ReturnValue) {
|
|
13863
|
+
return { __explicitReturn: true, value: result2.value };
|
|
13864
|
+
}
|
|
13865
|
+
if (result2 instanceof ThrowSignal) {
|
|
13866
|
+
throw result2.value;
|
|
13867
|
+
}
|
|
13868
|
+
return result2;
|
|
13869
|
+
})();
|
|
13870
|
+
}
|
|
13780
13871
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
13781
13872
|
if (result instanceof ReturnValue) {
|
|
13782
13873
|
return { __explicitReturn: true, value: result.value };
|
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
|
|
|
@@ -14962,6 +15039,14 @@ class Interpreter {
|
|
|
14962
15039
|
for (const [name, method] of Object.entries(methods)) {
|
|
14963
15040
|
classConstructor.prototype[name] = function(...args) {
|
|
14964
15041
|
const result = interpreter.callMethodFunction(method, this, args, env, superClass);
|
|
15042
|
+
if (result && typeof result.then === 'function') {
|
|
15043
|
+
return result.then(resolved => {
|
|
15044
|
+
if (resolved && resolved.__explicitReturn) {
|
|
15045
|
+
return resolved.value;
|
|
15046
|
+
}
|
|
15047
|
+
return resolved;
|
|
15048
|
+
});
|
|
15049
|
+
}
|
|
14965
15050
|
// Unwrap explicit return marker
|
|
14966
15051
|
if (result && result.__explicitReturn) {
|
|
14967
15052
|
return result.value;
|
|
@@ -14974,6 +15059,14 @@ class Interpreter {
|
|
|
14974
15059
|
for (const [name, method] of Object.entries(staticMethods)) {
|
|
14975
15060
|
classConstructor[name] = function(...args) {
|
|
14976
15061
|
const result = interpreter.callMethodFunction(method, classConstructor, args, env);
|
|
15062
|
+
if (result && typeof result.then === 'function') {
|
|
15063
|
+
return result.then(resolved => {
|
|
15064
|
+
if (resolved && resolved.__explicitReturn) {
|
|
15065
|
+
return resolved.value;
|
|
15066
|
+
}
|
|
15067
|
+
return resolved;
|
|
15068
|
+
});
|
|
15069
|
+
}
|
|
14977
15070
|
// Unwrap explicit return marker
|
|
14978
15071
|
if (result && result.__explicitReturn) {
|
|
14979
15072
|
return result.value;
|
|
@@ -15090,7 +15183,8 @@ class Interpreter {
|
|
|
15090
15183
|
__params: funcNode.params,
|
|
15091
15184
|
__body: funcNode.body,
|
|
15092
15185
|
__env: env,
|
|
15093
|
-
__className: className
|
|
15186
|
+
__className: className,
|
|
15187
|
+
__async: funcNode.async || false
|
|
15094
15188
|
};
|
|
15095
15189
|
return func;
|
|
15096
15190
|
}
|
|
@@ -15108,6 +15202,22 @@ class Interpreter {
|
|
|
15108
15202
|
|
|
15109
15203
|
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
15110
15204
|
|
|
15205
|
+
if (methodFunc.__async) {
|
|
15206
|
+
return (async () => {
|
|
15207
|
+
const result = await this.evaluateAsync(methodFunc.__body, funcEnv);
|
|
15208
|
+
|
|
15209
|
+
if (result instanceof ReturnValue) {
|
|
15210
|
+
return { __explicitReturn: true, value: result.value };
|
|
15211
|
+
}
|
|
15212
|
+
|
|
15213
|
+
if (result instanceof ThrowSignal) {
|
|
15214
|
+
throw result.value;
|
|
15215
|
+
}
|
|
15216
|
+
|
|
15217
|
+
return result;
|
|
15218
|
+
})();
|
|
15219
|
+
}
|
|
15220
|
+
|
|
15111
15221
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
15112
15222
|
|
|
15113
15223
|
if (result instanceof ReturnValue) {
|
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
|
|
|
@@ -14962,6 +15039,14 @@ class Interpreter {
|
|
|
14962
15039
|
for (const [name, method] of Object.entries(methods)) {
|
|
14963
15040
|
classConstructor.prototype[name] = function(...args) {
|
|
14964
15041
|
const result = interpreter.callMethodFunction(method, this, args, env, superClass);
|
|
15042
|
+
if (result && typeof result.then === 'function') {
|
|
15043
|
+
return result.then(resolved => {
|
|
15044
|
+
if (resolved && resolved.__explicitReturn) {
|
|
15045
|
+
return resolved.value;
|
|
15046
|
+
}
|
|
15047
|
+
return resolved;
|
|
15048
|
+
});
|
|
15049
|
+
}
|
|
14965
15050
|
// Unwrap explicit return marker
|
|
14966
15051
|
if (result && result.__explicitReturn) {
|
|
14967
15052
|
return result.value;
|
|
@@ -14974,6 +15059,14 @@ class Interpreter {
|
|
|
14974
15059
|
for (const [name, method] of Object.entries(staticMethods)) {
|
|
14975
15060
|
classConstructor[name] = function(...args) {
|
|
14976
15061
|
const result = interpreter.callMethodFunction(method, classConstructor, args, env);
|
|
15062
|
+
if (result && typeof result.then === 'function') {
|
|
15063
|
+
return result.then(resolved => {
|
|
15064
|
+
if (resolved && resolved.__explicitReturn) {
|
|
15065
|
+
return resolved.value;
|
|
15066
|
+
}
|
|
15067
|
+
return resolved;
|
|
15068
|
+
});
|
|
15069
|
+
}
|
|
14977
15070
|
// Unwrap explicit return marker
|
|
14978
15071
|
if (result && result.__explicitReturn) {
|
|
14979
15072
|
return result.value;
|
|
@@ -15090,7 +15183,8 @@ class Interpreter {
|
|
|
15090
15183
|
__params: funcNode.params,
|
|
15091
15184
|
__body: funcNode.body,
|
|
15092
15185
|
__env: env,
|
|
15093
|
-
__className: className
|
|
15186
|
+
__className: className,
|
|
15187
|
+
__async: funcNode.async || false
|
|
15094
15188
|
};
|
|
15095
15189
|
return func;
|
|
15096
15190
|
}
|
|
@@ -15108,6 +15202,22 @@ class Interpreter {
|
|
|
15108
15202
|
|
|
15109
15203
|
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
15110
15204
|
|
|
15205
|
+
if (methodFunc.__async) {
|
|
15206
|
+
return (async () => {
|
|
15207
|
+
const result = await this.evaluateAsync(methodFunc.__body, funcEnv);
|
|
15208
|
+
|
|
15209
|
+
if (result instanceof ReturnValue) {
|
|
15210
|
+
return { __explicitReturn: true, value: result.value };
|
|
15211
|
+
}
|
|
15212
|
+
|
|
15213
|
+
if (result instanceof ThrowSignal) {
|
|
15214
|
+
throw result.value;
|
|
15215
|
+
}
|
|
15216
|
+
|
|
15217
|
+
return result;
|
|
15218
|
+
})();
|
|
15219
|
+
}
|
|
15220
|
+
|
|
15111
15221
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
15112
15222
|
|
|
15113
15223
|
if (result instanceof ReturnValue) {
|
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);
|
|
@@ -13625,6 +13687,14 @@ var Interpreter = class _Interpreter {
|
|
|
13625
13687
|
for (const [name, method] of Object.entries(methods)) {
|
|
13626
13688
|
classConstructor.prototype[name] = function(...args) {
|
|
13627
13689
|
const result = interpreter.callMethodFunction(method, this, args, env, superClass);
|
|
13690
|
+
if (result && typeof result.then === "function") {
|
|
13691
|
+
return result.then((resolved) => {
|
|
13692
|
+
if (resolved && resolved.__explicitReturn) {
|
|
13693
|
+
return resolved.value;
|
|
13694
|
+
}
|
|
13695
|
+
return resolved;
|
|
13696
|
+
});
|
|
13697
|
+
}
|
|
13628
13698
|
if (result && result.__explicitReturn) {
|
|
13629
13699
|
return result.value;
|
|
13630
13700
|
}
|
|
@@ -13634,6 +13704,14 @@ var Interpreter = class _Interpreter {
|
|
|
13634
13704
|
for (const [name, method] of Object.entries(staticMethods)) {
|
|
13635
13705
|
classConstructor[name] = function(...args) {
|
|
13636
13706
|
const result = interpreter.callMethodFunction(method, classConstructor, args, env);
|
|
13707
|
+
if (result && typeof result.then === "function") {
|
|
13708
|
+
return result.then((resolved) => {
|
|
13709
|
+
if (resolved && resolved.__explicitReturn) {
|
|
13710
|
+
return resolved.value;
|
|
13711
|
+
}
|
|
13712
|
+
return resolved;
|
|
13713
|
+
});
|
|
13714
|
+
}
|
|
13637
13715
|
if (result && result.__explicitReturn) {
|
|
13638
13716
|
return result.value;
|
|
13639
13717
|
}
|
|
@@ -13732,7 +13810,8 @@ var Interpreter = class _Interpreter {
|
|
|
13732
13810
|
__params: funcNode.params,
|
|
13733
13811
|
__body: funcNode.body,
|
|
13734
13812
|
__env: env,
|
|
13735
|
-
__className: className
|
|
13813
|
+
__className: className,
|
|
13814
|
+
__async: funcNode.async || false
|
|
13736
13815
|
};
|
|
13737
13816
|
return func;
|
|
13738
13817
|
}
|
|
@@ -13743,6 +13822,18 @@ var Interpreter = class _Interpreter {
|
|
|
13743
13822
|
funcEnv.define("super", this.createSuperBinding(superClass, thisContext, true, afterSuper));
|
|
13744
13823
|
}
|
|
13745
13824
|
this.bindFunctionParameters(methodFunc.__params, args, funcEnv, thisContext);
|
|
13825
|
+
if (methodFunc.__async) {
|
|
13826
|
+
return (async () => {
|
|
13827
|
+
const result2 = await this.evaluateAsync(methodFunc.__body, funcEnv);
|
|
13828
|
+
if (result2 instanceof ReturnValue) {
|
|
13829
|
+
return { __explicitReturn: true, value: result2.value };
|
|
13830
|
+
}
|
|
13831
|
+
if (result2 instanceof ThrowSignal) {
|
|
13832
|
+
throw result2.value;
|
|
13833
|
+
}
|
|
13834
|
+
return result2;
|
|
13835
|
+
})();
|
|
13836
|
+
}
|
|
13746
13837
|
const result = this.evaluate(methodFunc.__body, funcEnv);
|
|
13747
13838
|
if (result instanceof ReturnValue) {
|
|
13748
13839
|
return { __explicitReturn: true, value: result.value };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jslike",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.7",
|
|
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": "
|
|
65
|
+
"jslike": "bin/jslike.js"
|
|
66
66
|
},
|
|
67
67
|
"scripts": {
|
|
68
68
|
"build": "node scripts/bundle-acorn.js && tsup && node scripts/post-build.js",
|