jslike 1.1.0 → 1.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jslike",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Production-ready JavaScript interpreter with full ES6+ support using Acorn parser",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Use bundled Acorn parser for zero runtime dependencies
2
2
  import { parse as acornParse } from './parser.js';
3
3
  import { Interpreter } from './interpreter/interpreter.js';
4
- import { Environment } from './runtime/environment.js';
4
+ import { Environment, ReturnValue } from './runtime/environment.js';
5
5
  import { createGlobalEnvironment } from './runtime/builtins.js';
6
6
 
7
7
  // Helper to detect if code contains module syntax or top-level await
@@ -231,7 +231,8 @@ export function parse(code, options = {}) {
231
231
  return acornParse(processedCode, {
232
232
  ecmaVersion: 2022, // Support ES2022 features (including top-level await)
233
233
  sourceType: sourceType,
234
- locations: false // Don't track source locations (faster)
234
+ locations: true, // Track source locations for better error messages
235
+ allowReturnOutsideFunction: true // Allow top-level return statements
235
236
  });
236
237
  } catch (error) {
237
238
  // Reformat error message for consistency
@@ -318,10 +319,10 @@ export async function execute(code, env = null, options = {}) {
318
319
 
319
320
  if (needsAsync) {
320
321
  const result = await interpreter.evaluateAsync(ast, userEnv);
321
- return result;
322
+ return result instanceof ReturnValue ? result.value : result;
322
323
  } else {
323
324
  const result = interpreter.evaluate(ast, userEnv);
324
- return result;
325
+ return result instanceof ReturnValue ? result.value : result;
325
326
  }
326
327
  }
327
328
 
@@ -75,6 +75,10 @@ export class Interpreter {
75
75
  let result = undefined;
76
76
  for (const statement of node.body) {
77
77
  result = await this.evaluateAsync(statement, env);
78
+ // Handle top-level return and throw
79
+ if (result instanceof ReturnValue || result instanceof ThrowSignal) {
80
+ return result;
81
+ }
78
82
  }
79
83
  return result;
80
84
  }