jslike 1.8.7 → 1.8.10

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/index.js CHANGED
@@ -11286,13 +11286,15 @@ var Environment = class _Environment {
11286
11286
  constructor(parent = null) {
11287
11287
  this.parent = parent;
11288
11288
  this.vars = /* @__PURE__ */ new Map();
11289
+ this.liveBindings = /* @__PURE__ */ new Map();
11289
11290
  this.consts = /* @__PURE__ */ new Set();
11290
11291
  }
11291
11292
  define(name, value, isConst = false) {
11292
- if (this.vars.has(name)) {
11293
+ if (this.vars.has(name) || this.liveBindings.has(name)) {
11293
11294
  if (this.consts.has(name)) {
11294
11295
  throw new Error(`Cannot redeclare const '${name}'`);
11295
11296
  }
11297
+ this.liveBindings.delete(name);
11296
11298
  this.vars.set(name, value);
11297
11299
  if (isConst) {
11298
11300
  this.consts.add(name);
@@ -11305,7 +11307,23 @@ var Environment = class _Environment {
11305
11307
  }
11306
11308
  return value;
11307
11309
  }
11310
+ defineLive(name, getter, isConst = true) {
11311
+ if (this.vars.has(name) || this.liveBindings.has(name)) {
11312
+ if (this.consts.has(name)) {
11313
+ throw new Error(`Cannot redeclare const '${name}'`);
11314
+ }
11315
+ this.vars.delete(name);
11316
+ }
11317
+ this.liveBindings.set(name, getter);
11318
+ if (isConst) {
11319
+ this.consts.add(name);
11320
+ }
11321
+ return getter();
11322
+ }
11308
11323
  get(name) {
11324
+ if (this.liveBindings.has(name)) {
11325
+ return this.liveBindings.get(name)();
11326
+ }
11309
11327
  if (this.vars.has(name)) {
11310
11328
  return this.vars.get(name);
11311
11329
  }
@@ -11315,10 +11333,11 @@ var Environment = class _Environment {
11315
11333
  throw new ReferenceError(`Variable "${name}" is not defined`);
11316
11334
  }
11317
11335
  set(name, value) {
11318
- if (this.vars.has(name)) {
11336
+ if (this.vars.has(name) || this.liveBindings.has(name)) {
11319
11337
  if (this.consts.has(name)) {
11320
11338
  throw new TypeError(`Cannot reassign const variable '${name}'`);
11321
11339
  }
11340
+ this.liveBindings.delete(name);
11322
11341
  this.vars.set(name, value);
11323
11342
  return value;
11324
11343
  }
@@ -11328,7 +11347,7 @@ var Environment = class _Environment {
11328
11347
  throw new ReferenceError(`Variable "${name}" is not defined`);
11329
11348
  }
11330
11349
  has(name) {
11331
- return this.vars.has(name) || (this.parent ? this.parent.has(name) : false);
11350
+ return this.vars.has(name) || this.liveBindings.has(name) || (this.parent ? this.parent.has(name) : false);
11332
11351
  }
11333
11352
  // For let/const block scoping
11334
11353
  extend() {
@@ -11469,6 +11488,16 @@ function createMethodNotFoundError(objectName, methodName, objectValue) {
11469
11488
  }
11470
11489
 
11471
11490
  // src/interpreter/interpreter.js
11491
+ var ASYNC_EXPRESSION_COMPLETION = Symbol("jslike.asyncExpressionCompletion");
11492
+ function createAsyncExpressionCompletion(value) {
11493
+ return { [ASYNC_EXPRESSION_COMPLETION]: true, value };
11494
+ }
11495
+ function isAsyncExpressionCompletion(value) {
11496
+ return value && value[ASYNC_EXPRESSION_COMPLETION] === true;
11497
+ }
11498
+ function unwrapAsyncExpressionCompletion(value) {
11499
+ return isAsyncExpressionCompletion(value) ? value.value : value;
11500
+ }
11472
11501
  function isTypeScriptPath(sourcePath) {
11473
11502
  return typeof sourcePath === "string" && /\.(ts|tsx|mts|cts)$/i.test(sourcePath);
11474
11503
  }
@@ -11809,6 +11838,36 @@ var Interpreter = class _Interpreter {
11809
11838
  }
11810
11839
  return await this.evaluateAsyncRawValue(node.expressions[node.expressions.length - 1], env);
11811
11840
  }
11841
+ if (node.type === "AssignmentExpression") {
11842
+ const value = (await this.evaluateAsyncRawValue(node.right, env)).value;
11843
+ if (node.left.type === "Identifier") {
11844
+ const name = node.left.name;
11845
+ if (node.operator === "=") {
11846
+ if (env.has(name)) {
11847
+ env.set(name, value);
11848
+ } else {
11849
+ env.define(name, value);
11850
+ }
11851
+ return { value };
11852
+ }
11853
+ const current2 = env.get(name);
11854
+ const newValue = this.applyCompoundAssignment(node.operator, current2, value);
11855
+ env.set(name, newValue);
11856
+ return { value: newValue };
11857
+ }
11858
+ if (node.left.type === "MemberExpression") {
11859
+ const obj = await this.evaluateAsync(node.left.object, env);
11860
+ const prop = node.left.computed ? await this.evaluateAsync(node.left.property, env) : node.left.property.name;
11861
+ if (node.operator === "=") {
11862
+ obj[prop] = value;
11863
+ return { value };
11864
+ }
11865
+ const newValue = this.applyCompoundAssignment(node.operator, obj[prop], value);
11866
+ obj[prop] = newValue;
11867
+ return { value: newValue };
11868
+ }
11869
+ throw new Error("Invalid assignment target");
11870
+ }
11812
11871
  if (node.type === "NewExpression") {
11813
11872
  return { value: this.evaluateNewExpression(node, env) };
11814
11873
  }
@@ -11839,7 +11898,7 @@ var Interpreter = class _Interpreter {
11839
11898
  }
11840
11899
  const rawArgs = [];
11841
11900
  for (const arg of node.arguments) {
11842
- rawArgs.push(await this.evaluateAsync(arg, env));
11901
+ rawArgs.push((await this.evaluateAsyncRawValue(arg, env)).value);
11843
11902
  }
11844
11903
  const args = this.flattenSpreadArgs(rawArgs);
11845
11904
  if (typeof callee === "function") {
@@ -11861,9 +11920,12 @@ var Interpreter = class _Interpreter {
11861
11920
  if (isTypeOnlyDeclaration(node)) {
11862
11921
  return void 0;
11863
11922
  }
11864
- if (node.type === "TSExportAssignment" || node.type === "TSImportEqualsDeclaration") {
11923
+ if (node.type === "TSExportAssignment") {
11865
11924
  throw createUnsupportedTypeScriptRuntimeError(node);
11866
11925
  }
11926
+ if (node.type === "TSImportEqualsDeclaration") {
11927
+ return await this.evaluateTSImportEqualsDeclaration(node, env);
11928
+ }
11867
11929
  if (node.type === "TSEnumDeclaration") {
11868
11930
  return this.evaluateTSEnumDeclaration(node, env);
11869
11931
  }
@@ -11890,7 +11952,7 @@ var Interpreter = class _Interpreter {
11890
11952
  return result;
11891
11953
  }
11892
11954
  if (node.type === "ExpressionStatement") {
11893
- return await this.evaluateAsync(node.expression, env);
11955
+ return createAsyncExpressionCompletion((await this.evaluateAsyncRawValue(node.expression, env)).value);
11894
11956
  }
11895
11957
  if (node.type === "VariableDeclaration") {
11896
11958
  for (const declarator of node.declarations) {
@@ -11917,7 +11979,7 @@ var Interpreter = class _Interpreter {
11917
11979
  return result;
11918
11980
  }
11919
11981
  }
11920
- return result;
11982
+ return unwrapAsyncExpressionCompletion(result);
11921
11983
  } finally {
11922
11984
  this.runtimeIdentifierReferences = previousReferences;
11923
11985
  }
@@ -12304,7 +12366,7 @@ var Interpreter = class _Interpreter {
12304
12366
  throw new TypeError("Spread syntax requires an iterable");
12305
12367
  }
12306
12368
  } else {
12307
- result.push(await this.evaluateAsync(elem, env));
12369
+ result.push((await this.evaluateAsyncRawValue(elem, env)).value);
12308
12370
  }
12309
12371
  }
12310
12372
  return result;
@@ -12319,7 +12381,7 @@ var Interpreter = class _Interpreter {
12319
12381
  }
12320
12382
  } else {
12321
12383
  const key = prop.key.type === "Identifier" && !prop.computed ? prop.key.name : await this.evaluateAsync(prop.key, env);
12322
- const value = prop.value ? await this.evaluateAsync(prop.value, env) : env.get(key);
12384
+ const value = prop.value ? (await this.evaluateAsyncRawValue(prop.value, env)).value : env.get(key);
12323
12385
  if (prop.method && prop.value.type === "FunctionExpression") {
12324
12386
  obj[key] = (...args) => {
12325
12387
  const funcValue = this.evaluate(prop.value, env);
@@ -12404,9 +12466,12 @@ var Interpreter = class _Interpreter {
12404
12466
  if (isTypeOnlyDeclaration(node)) {
12405
12467
  return void 0;
12406
12468
  }
12407
- if (node.type === "TSExportAssignment" || node.type === "TSImportEqualsDeclaration") {
12469
+ if (node.type === "TSExportAssignment") {
12408
12470
  throw createUnsupportedTypeScriptRuntimeError(node);
12409
12471
  }
12472
+ if (node.type === "TSImportEqualsDeclaration") {
12473
+ return this.evaluateTSImportEqualsDeclaration(node, env);
12474
+ }
12410
12475
  if (node.type === "TSEnumDeclaration") {
12411
12476
  return this.evaluateTSEnumDeclaration(node, env);
12412
12477
  }
@@ -13261,6 +13326,11 @@ var Interpreter = class _Interpreter {
13261
13326
  if (this.isTypeScriptModule && node.specifiers.length > 0 && node.specifiers.every((specifier) => !this.isRuntimeImportSpecifier(specifier))) {
13262
13327
  return void 0;
13263
13328
  }
13329
+ const moduleExports = await this.loadModuleExports(modulePath);
13330
+ this.bindImportSpecifiers(node, env, modulePath, moduleExports);
13331
+ return void 0;
13332
+ }
13333
+ async loadModuleExports(modulePath) {
13264
13334
  if (!this.moduleResolver) {
13265
13335
  throw new Error("Module resolver not configured - cannot import modules");
13266
13336
  }
@@ -13282,8 +13352,7 @@ var Interpreter = class _Interpreter {
13282
13352
  resolvedPath = typeof resolution === "string" ? modulePath : resolution.path || modulePath;
13283
13353
  this.moduleResolutionCache.set(resolutionCacheKey, resolvedPath);
13284
13354
  if (this.moduleCache.has(resolvedPath)) {
13285
- moduleExports = this.moduleCache.get(resolvedPath);
13286
- return this.bindImportSpecifiers(node, env, modulePath, moduleExports);
13355
+ return this.moduleCache.get(resolvedPath);
13287
13356
  }
13288
13357
  if (resolution.exports) {
13289
13358
  moduleExports = resolution.exports;
@@ -13312,7 +13381,27 @@ var Interpreter = class _Interpreter {
13312
13381
  }
13313
13382
  }
13314
13383
  }
13315
- this.bindImportSpecifiers(node, env, modulePath, moduleExports);
13384
+ return moduleExports;
13385
+ }
13386
+ async evaluateTSImportEqualsDeclaration(node, env) {
13387
+ var _a, _b;
13388
+ if (node.importKind === "type") {
13389
+ return void 0;
13390
+ }
13391
+ const localName = (_a = node.id) == null ? void 0 : _a.name;
13392
+ const moduleReference = node.moduleReference;
13393
+ if ((moduleReference == null ? void 0 : moduleReference.type) !== "TSExternalModuleReference") {
13394
+ throw createUnsupportedTypeScriptRuntimeError(node);
13395
+ }
13396
+ const modulePath = (_b = moduleReference.expression) == null ? void 0 : _b.value;
13397
+ if (typeof modulePath !== "string") {
13398
+ throw createUnsupportedTypeScriptRuntimeError(node);
13399
+ }
13400
+ const moduleExports = await this.loadModuleExports(modulePath);
13401
+ env.define(localName, moduleExports);
13402
+ if (node.isExport) {
13403
+ this.moduleExports[localName] = moduleExports;
13404
+ }
13316
13405
  return void 0;
13317
13406
  }
13318
13407
  bindImportSpecifiers(node, env, modulePath, moduleExports) {
@@ -13326,13 +13415,13 @@ var Interpreter = class _Interpreter {
13326
13415
  if (!(importedName in moduleExports)) {
13327
13416
  throw new Error(`Module '${modulePath}' has no export '${importedName}'`);
13328
13417
  }
13329
- env.define(localName, moduleExports[importedName]);
13418
+ env.defineLive(localName, () => moduleExports[importedName]);
13330
13419
  } else if (specifier.type === "ImportDefaultSpecifier") {
13331
13420
  const localName = specifier.local.name;
13332
13421
  if (!("default" in moduleExports)) {
13333
13422
  throw new Error(`Module '${modulePath}' has no default export`);
13334
13423
  }
13335
- env.define(localName, moduleExports.default);
13424
+ env.defineLive(localName, () => moduleExports.default);
13336
13425
  } else if (specifier.type === "ImportNamespaceSpecifier") {
13337
13426
  const localName = specifier.local.name;
13338
13427
  env.define(localName, moduleExports);
@@ -14873,7 +14962,7 @@ function parse3(code, options = {}) {
14873
14962
  }
14874
14963
  function containsModuleDeclarations(node) {
14875
14964
  if (!node || typeof node !== "object") return false;
14876
- if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") {
14965
+ if (node.type === "ImportDeclaration" || node.type === "TSImportEqualsDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") {
14877
14966
  return true;
14878
14967
  }
14879
14968
  if (node.type === "Program" && node.body) {
@@ -11306,6 +11306,9 @@ function tsxParse(input, options) {
11306
11306
  return TSXParser.parse(input, options);
11307
11307
  }
11308
11308
 
11309
+ // src/interpreter/interpreter.js
11310
+ var ASYNC_EXPRESSION_COMPLETION = Symbol("jslike.asyncExpressionCompletion");
11311
+
11309
11312
  // src/index.js
11310
11313
  function containsModuleSyntax(code) {
11311
11314
  if (/(^|[;{\n])\s*(import|export)\s+/m.test(code)) {
@@ -11281,6 +11281,9 @@ function tsxParse(input, options) {
11281
11281
  return TSXParser.parse(input, options);
11282
11282
  }
11283
11283
 
11284
+ // src/interpreter/interpreter.js
11285
+ var ASYNC_EXPRESSION_COMPLETION = Symbol("jslike.asyncExpressionCompletion");
11286
+
11284
11287
  // src/index.js
11285
11288
  function containsModuleSyntax(code) {
11286
11289
  if (/(^|[;{\n])\s*(import|export)\s+/m.test(code)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jslike",
3
- "version": "1.8.7",
3
+ "version": "1.8.10",
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",