jslike 1.7.0 → 1.7.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.
@@ -1308,8 +1308,14 @@ export class Interpreter {
1308
1308
  // Get function name for call stack tracking
1309
1309
  const funcName = metadata.name || func.name || 'anonymous';
1310
1310
 
1311
- // Bind 'this' if provided (for method calls)
1312
- if (thisContext !== undefined) {
1311
+ // Bind 'this': for arrow functions use captured this (lexical), for regular functions use call-site this
1312
+ if (metadata.isArrow) {
1313
+ // Arrow functions use lexically captured 'this', ignoring call-site 'this'
1314
+ if (metadata.capturedThis !== undefined) {
1315
+ funcEnv.define('this', metadata.capturedThis);
1316
+ }
1317
+ } else if (thisContext !== undefined) {
1318
+ // Regular functions use 'this' from the call site
1313
1319
  funcEnv.define('this', thisContext);
1314
1320
  }
1315
1321
 
@@ -1490,13 +1496,28 @@ export class Interpreter {
1490
1496
  }
1491
1497
 
1492
1498
  evaluateFunctionExpression(node, env) {
1499
+ const isArrow = node.type === 'ArrowFunctionExpression';
1500
+
1501
+ // For arrow functions, capture 'this' lexically from the enclosing scope
1502
+ let capturedThis;
1503
+ if (isArrow) {
1504
+ try {
1505
+ capturedThis = env.get('this');
1506
+ } catch (e) {
1507
+ // 'this' not defined in enclosing scope, leave undefined
1508
+ capturedThis = undefined;
1509
+ }
1510
+ }
1511
+
1493
1512
  const funcMetadata = {
1494
1513
  __isFunction: true,
1495
1514
  params: node.params,
1496
1515
  body: node.body,
1497
1516
  closure: env,
1498
- expression: node.type === 'ArrowFunctionExpression' && node.expression,
1499
- async: node.async || false
1517
+ expression: isArrow && node.expression,
1518
+ async: node.async || false,
1519
+ isArrow: isArrow,
1520
+ capturedThis: isArrow ? capturedThis : undefined
1500
1521
  };
1501
1522
 
1502
1523
  // Wrap in actual JavaScript function so it can be called by native code
package/dist/index.cjs CHANGED
@@ -7504,7 +7504,11 @@ var Interpreter = class _Interpreter {
7504
7504
  const metadata = func.__metadata || func;
7505
7505
  const funcEnv = new Environment(metadata.closure);
7506
7506
  const funcName = metadata.name || func.name || "anonymous";
7507
- if (thisContext !== void 0) {
7507
+ if (metadata.isArrow) {
7508
+ if (metadata.capturedThis !== void 0) {
7509
+ funcEnv.define("this", metadata.capturedThis);
7510
+ }
7511
+ } else if (thisContext !== void 0) {
7508
7512
  funcEnv.define("this", thisContext);
7509
7513
  }
7510
7514
  for (let i = 0; i < metadata.params.length; i++) {
@@ -7639,13 +7643,24 @@ var Interpreter = class _Interpreter {
7639
7643
  return obj;
7640
7644
  }
7641
7645
  evaluateFunctionExpression(node, env) {
7646
+ const isArrow = node.type === "ArrowFunctionExpression";
7647
+ let capturedThis;
7648
+ if (isArrow) {
7649
+ try {
7650
+ capturedThis = env.get("this");
7651
+ } catch (e) {
7652
+ capturedThis = void 0;
7653
+ }
7654
+ }
7642
7655
  const funcMetadata = {
7643
7656
  __isFunction: true,
7644
7657
  params: node.params,
7645
7658
  body: node.body,
7646
7659
  closure: env,
7647
- expression: node.type === "ArrowFunctionExpression" && node.expression,
7648
- async: node.async || false
7660
+ expression: isArrow && node.expression,
7661
+ async: node.async || false,
7662
+ isArrow,
7663
+ capturedThis: isArrow ? capturedThis : void 0
7649
7664
  };
7650
7665
  const interpreter = this;
7651
7666
  const wrappedFunc = funcMetadata.async ? async function(...args) {
package/dist/index.d.cts CHANGED
@@ -8473,8 +8473,14 @@ class Interpreter {
8473
8473
  // Get function name for call stack tracking
8474
8474
  const funcName = metadata.name || func.name || 'anonymous';
8475
8475
 
8476
- // Bind 'this' if provided (for method calls)
8477
- if (thisContext !== undefined) {
8476
+ // Bind 'this': for arrow functions use captured this (lexical), for regular functions use call-site this
8477
+ if (metadata.isArrow) {
8478
+ // Arrow functions use lexically captured 'this', ignoring call-site 'this'
8479
+ if (metadata.capturedThis !== undefined) {
8480
+ funcEnv.define('this', metadata.capturedThis);
8481
+ }
8482
+ } else if (thisContext !== undefined) {
8483
+ // Regular functions use 'this' from the call site
8478
8484
  funcEnv.define('this', thisContext);
8479
8485
  }
8480
8486
 
@@ -8655,13 +8661,28 @@ class Interpreter {
8655
8661
  }
8656
8662
 
8657
8663
  evaluateFunctionExpression(node, env) {
8664
+ const isArrow = node.type === 'ArrowFunctionExpression';
8665
+
8666
+ // For arrow functions, capture 'this' lexically from the enclosing scope
8667
+ let capturedThis;
8668
+ if (isArrow) {
8669
+ try {
8670
+ capturedThis = env.get('this');
8671
+ } catch (e) {
8672
+ // 'this' not defined in enclosing scope, leave undefined
8673
+ capturedThis = undefined;
8674
+ }
8675
+ }
8676
+
8658
8677
  const funcMetadata = {
8659
8678
  __isFunction: true,
8660
8679
  params: node.params,
8661
8680
  body: node.body,
8662
8681
  closure: env,
8663
- expression: node.type === 'ArrowFunctionExpression' && node.expression,
8664
- async: node.async || false
8682
+ expression: isArrow && node.expression,
8683
+ async: node.async || false,
8684
+ isArrow: isArrow,
8685
+ capturedThis: isArrow ? capturedThis : undefined
8665
8686
  };
8666
8687
 
8667
8688
  // Wrap in actual JavaScript function so it can be called by native code
package/dist/index.d.ts CHANGED
@@ -8473,8 +8473,14 @@ class Interpreter {
8473
8473
  // Get function name for call stack tracking
8474
8474
  const funcName = metadata.name || func.name || 'anonymous';
8475
8475
 
8476
- // Bind 'this' if provided (for method calls)
8477
- if (thisContext !== undefined) {
8476
+ // Bind 'this': for arrow functions use captured this (lexical), for regular functions use call-site this
8477
+ if (metadata.isArrow) {
8478
+ // Arrow functions use lexically captured 'this', ignoring call-site 'this'
8479
+ if (metadata.capturedThis !== undefined) {
8480
+ funcEnv.define('this', metadata.capturedThis);
8481
+ }
8482
+ } else if (thisContext !== undefined) {
8483
+ // Regular functions use 'this' from the call site
8478
8484
  funcEnv.define('this', thisContext);
8479
8485
  }
8480
8486
 
@@ -8655,13 +8661,28 @@ class Interpreter {
8655
8661
  }
8656
8662
 
8657
8663
  evaluateFunctionExpression(node, env) {
8664
+ const isArrow = node.type === 'ArrowFunctionExpression';
8665
+
8666
+ // For arrow functions, capture 'this' lexically from the enclosing scope
8667
+ let capturedThis;
8668
+ if (isArrow) {
8669
+ try {
8670
+ capturedThis = env.get('this');
8671
+ } catch (e) {
8672
+ // 'this' not defined in enclosing scope, leave undefined
8673
+ capturedThis = undefined;
8674
+ }
8675
+ }
8676
+
8658
8677
  const funcMetadata = {
8659
8678
  __isFunction: true,
8660
8679
  params: node.params,
8661
8680
  body: node.body,
8662
8681
  closure: env,
8663
- expression: node.type === 'ArrowFunctionExpression' && node.expression,
8664
- async: node.async || false
8682
+ expression: isArrow && node.expression,
8683
+ async: node.async || false,
8684
+ isArrow: isArrow,
8685
+ capturedThis: isArrow ? capturedThis : undefined
8665
8686
  };
8666
8687
 
8667
8688
  // Wrap in actual JavaScript function so it can be called by native code
package/dist/index.js CHANGED
@@ -7470,7 +7470,11 @@ var Interpreter = class _Interpreter {
7470
7470
  const metadata = func.__metadata || func;
7471
7471
  const funcEnv = new Environment(metadata.closure);
7472
7472
  const funcName = metadata.name || func.name || "anonymous";
7473
- if (thisContext !== void 0) {
7473
+ if (metadata.isArrow) {
7474
+ if (metadata.capturedThis !== void 0) {
7475
+ funcEnv.define("this", metadata.capturedThis);
7476
+ }
7477
+ } else if (thisContext !== void 0) {
7474
7478
  funcEnv.define("this", thisContext);
7475
7479
  }
7476
7480
  for (let i = 0; i < metadata.params.length; i++) {
@@ -7605,13 +7609,24 @@ var Interpreter = class _Interpreter {
7605
7609
  return obj;
7606
7610
  }
7607
7611
  evaluateFunctionExpression(node, env) {
7612
+ const isArrow = node.type === "ArrowFunctionExpression";
7613
+ let capturedThis;
7614
+ if (isArrow) {
7615
+ try {
7616
+ capturedThis = env.get("this");
7617
+ } catch (e) {
7618
+ capturedThis = void 0;
7619
+ }
7620
+ }
7608
7621
  const funcMetadata = {
7609
7622
  __isFunction: true,
7610
7623
  params: node.params,
7611
7624
  body: node.body,
7612
7625
  closure: env,
7613
- expression: node.type === "ArrowFunctionExpression" && node.expression,
7614
- async: node.async || false
7626
+ expression: isArrow && node.expression,
7627
+ async: node.async || false,
7628
+ isArrow,
7629
+ capturedThis: isArrow ? capturedThis : void 0
7615
7630
  };
7616
7631
  const interpreter = this;
7617
7632
  const wrappedFunc = funcMetadata.async ? async function(...args) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jslike",
3
- "version": "1.7.0",
3
+ "version": "1.7.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",