jslike 1.4.0 → 1.4.1

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.cjs CHANGED
@@ -5652,7 +5652,14 @@ var Environment = class _Environment {
5652
5652
  }
5653
5653
  define(name, value, isConst = false) {
5654
5654
  if (this.vars.has(name)) {
5655
- throw new Error(`Variable '${name}' already declared`);
5655
+ if (this.consts.has(name)) {
5656
+ throw new Error(`Cannot redeclare const '${name}'`);
5657
+ }
5658
+ this.vars.set(name, value);
5659
+ if (isConst) {
5660
+ this.consts.add(name);
5661
+ }
5662
+ return value;
5656
5663
  }
5657
5664
  this.vars.set(name, value);
5658
5665
  if (isConst) {
@@ -8046,17 +8053,16 @@ async function execute(code, env = null, options = {}) {
8046
8053
  if (!env) {
8047
8054
  env = createGlobalEnvironment(new Environment());
8048
8055
  }
8049
- const userEnv = env.extend();
8050
8056
  const interpreter = new Interpreter(env, {
8051
8057
  moduleResolver: options.moduleResolver,
8052
8058
  abortSignal: options.abortSignal
8053
8059
  });
8054
8060
  const needsAsync = options.sourceType === "module" || containsModuleDeclarations(ast) || containsTopLevelAwait(ast);
8055
8061
  if (needsAsync) {
8056
- const result = await interpreter.evaluateAsync(ast, userEnv);
8062
+ const result = await interpreter.evaluateAsync(ast, env);
8057
8063
  return result instanceof ReturnValue ? result.value : result;
8058
8064
  } else {
8059
- const result = interpreter.evaluate(ast, userEnv);
8065
+ const result = interpreter.evaluate(ast, env);
8060
8066
  return result instanceof ReturnValue ? result.value : result;
8061
8067
  }
8062
8068
  }
package/dist/index.d.cts CHANGED
@@ -6233,7 +6233,17 @@ class Environment {
6233
6233
 
6234
6234
  define(name, value, isConst = false) {
6235
6235
  if (this.vars.has(name)) {
6236
- throw new Error(`Variable '${name}' already declared`);
6236
+ // Allow redeclaration for non-const (REPL-style behavior)
6237
+ // But cannot redeclare a const
6238
+ if (this.consts.has(name)) {
6239
+ throw new Error(`Cannot redeclare const '${name}'`);
6240
+ }
6241
+ // Update existing variable
6242
+ this.vars.set(name, value);
6243
+ if (isConst) {
6244
+ this.consts.add(name);
6245
+ }
6246
+ return value;
6237
6247
  }
6238
6248
  this.vars.set(name, value);
6239
6249
  if (isConst) {
@@ -9397,10 +9407,6 @@ async function execute(code, env = null, options = {}) {
9397
9407
  env = createGlobalEnvironment(new Environment());
9398
9408
  }
9399
9409
 
9400
- // Create a child environment for user code to allow shadowing of built-ins
9401
- // This prevents conflicts when user code declares variables with same names as stdlib functions
9402
- const userEnv = env.extend();
9403
-
9404
9410
  // Create interpreter with module resolver and abort signal if provided
9405
9411
  const interpreter = new Interpreter(env, {
9406
9412
  moduleResolver: options.moduleResolver,
@@ -9416,10 +9422,10 @@ async function execute(code, env = null, options = {}) {
9416
9422
  containsTopLevelAwait(ast);
9417
9423
 
9418
9424
  if (needsAsync) {
9419
- const result = await interpreter.evaluateAsync(ast, userEnv);
9425
+ const result = await interpreter.evaluateAsync(ast, env);
9420
9426
  return result instanceof ReturnValue ? result.value : result;
9421
9427
  } else {
9422
- const result = interpreter.evaluate(ast, userEnv);
9428
+ const result = interpreter.evaluate(ast, env);
9423
9429
  return result instanceof ReturnValue ? result.value : result;
9424
9430
  }
9425
9431
  }
package/dist/index.d.ts CHANGED
@@ -6233,7 +6233,17 @@ class Environment {
6233
6233
 
6234
6234
  define(name, value, isConst = false) {
6235
6235
  if (this.vars.has(name)) {
6236
- throw new Error(`Variable '${name}' already declared`);
6236
+ // Allow redeclaration for non-const (REPL-style behavior)
6237
+ // But cannot redeclare a const
6238
+ if (this.consts.has(name)) {
6239
+ throw new Error(`Cannot redeclare const '${name}'`);
6240
+ }
6241
+ // Update existing variable
6242
+ this.vars.set(name, value);
6243
+ if (isConst) {
6244
+ this.consts.add(name);
6245
+ }
6246
+ return value;
6237
6247
  }
6238
6248
  this.vars.set(name, value);
6239
6249
  if (isConst) {
@@ -9397,10 +9407,6 @@ async function execute(code, env = null, options = {}) {
9397
9407
  env = createGlobalEnvironment(new Environment());
9398
9408
  }
9399
9409
 
9400
- // Create a child environment for user code to allow shadowing of built-ins
9401
- // This prevents conflicts when user code declares variables with same names as stdlib functions
9402
- const userEnv = env.extend();
9403
-
9404
9410
  // Create interpreter with module resolver and abort signal if provided
9405
9411
  const interpreter = new Interpreter(env, {
9406
9412
  moduleResolver: options.moduleResolver,
@@ -9416,10 +9422,10 @@ async function execute(code, env = null, options = {}) {
9416
9422
  containsTopLevelAwait(ast);
9417
9423
 
9418
9424
  if (needsAsync) {
9419
- const result = await interpreter.evaluateAsync(ast, userEnv);
9425
+ const result = await interpreter.evaluateAsync(ast, env);
9420
9426
  return result instanceof ReturnValue ? result.value : result;
9421
9427
  } else {
9422
- const result = interpreter.evaluate(ast, userEnv);
9428
+ const result = interpreter.evaluate(ast, env);
9423
9429
  return result instanceof ReturnValue ? result.value : result;
9424
9430
  }
9425
9431
  }
package/dist/index.js CHANGED
@@ -5619,7 +5619,14 @@ var Environment = class _Environment {
5619
5619
  }
5620
5620
  define(name, value, isConst = false) {
5621
5621
  if (this.vars.has(name)) {
5622
- throw new Error(`Variable '${name}' already declared`);
5622
+ if (this.consts.has(name)) {
5623
+ throw new Error(`Cannot redeclare const '${name}'`);
5624
+ }
5625
+ this.vars.set(name, value);
5626
+ if (isConst) {
5627
+ this.consts.add(name);
5628
+ }
5629
+ return value;
5623
5630
  }
5624
5631
  this.vars.set(name, value);
5625
5632
  if (isConst) {
@@ -8013,17 +8020,16 @@ async function execute(code, env = null, options = {}) {
8013
8020
  if (!env) {
8014
8021
  env = createGlobalEnvironment(new Environment());
8015
8022
  }
8016
- const userEnv = env.extend();
8017
8023
  const interpreter = new Interpreter(env, {
8018
8024
  moduleResolver: options.moduleResolver,
8019
8025
  abortSignal: options.abortSignal
8020
8026
  });
8021
8027
  const needsAsync = options.sourceType === "module" || containsModuleDeclarations(ast) || containsTopLevelAwait(ast);
8022
8028
  if (needsAsync) {
8023
- const result = await interpreter.evaluateAsync(ast, userEnv);
8029
+ const result = await interpreter.evaluateAsync(ast, env);
8024
8030
  return result instanceof ReturnValue ? result.value : result;
8025
8031
  } else {
8026
- const result = interpreter.evaluate(ast, userEnv);
8032
+ const result = interpreter.evaluate(ast, env);
8027
8033
  return result instanceof ReturnValue ? result.value : result;
8028
8034
  }
8029
8035
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jslike",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
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",
@@ -52,7 +52,8 @@
52
52
  "jslike": "./bin/jslike.js"
53
53
  },
54
54
  "scripts": {
55
- "build": "node scripts/bundle-acorn.js && tsup",
55
+ "build": "node scripts/bundle-acorn.js && tsup && node scripts/post-build.js",
56
+ "build:playground": "node scripts/build-playground.js",
56
57
  "start": "node bin/jslike.js",
57
58
  "repl": "node bin/repl.js",
58
59
  "test": "vitest run",