sval 0.4.7 → 0.4.9

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.
@@ -0,0 +1,21 @@
1
+ name: Coverage
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ jobs:
7
+ coverage:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - name: Check out repository code
11
+ uses: actions/checkout@v3
12
+ - name: Setup node environment
13
+ uses: actions/setup-node@v3.4.1
14
+ - name: Install node modules
15
+ run: npm install
16
+ - name: Test code
17
+ run: npm test
18
+ - name: Send coverage info to Coveralls
19
+ uses: coverallsapp/github-action@1.1.3
20
+ with:
21
+ github-token: ${{ secrets.GITHUB_TOKEN }}
package/README.md CHANGED
@@ -1,8 +1,4 @@
1
- # Sval
2
-
3
- [![npm](https://img.shields.io/npm/v/sval.svg?style=flat-square)](https://www.npmjs.com/package/sval)
4
- [![travis-ci](https://img.shields.io/travis/Siubaak/sval.svg?style=flat-square)](https://travis-ci.org/Siubaak/sval)
5
- [![coveralls](https://img.shields.io/coveralls/github/Siubaak/sval.svg?style=flat-square)](https://coveralls.io/github/Siubaak/sval)
1
+ # Sval · [![npm](https://img.shields.io/npm/v/sval.svg?style=flat-square)](https://www.npmjs.com/package/sval) [![github-actions](https://img.shields.io/github/workflow/status/Siubaak/sval/Coverage.svg?style=flat-square)](https://github.com/Siubaak/sval/actions/workflows/coverage.yml) [![coveralls](https://img.shields.io/coveralls/github/Siubaak/sval.svg?style=flat-square)](https://coveralls.io/github/Siubaak/sval)
6
2
 
7
3
  A JavaScript interpreter writen in JavaScript, based on parser [Acorn](https://github.com/acornjs/acorn).
8
4
 
@@ -72,7 +68,7 @@ Sval constructor has options with two fields, **ecmaVer** and **sandBox**.
72
68
 
73
69
  - **sandBox** is true for sandbox mode or false for invasived mode. Sandbox mode will run code in an isolated sandbox and won't pollute your global scope. Invasived mode allows you run code in the same global scope of your current environment. The default setting is true.
74
70
 
75
- Sval instance has two methods, **import** and **run**.
71
+ Sval instance has three methods, **import**, **parse** and **run**.
76
72
 
77
73
  - **import** is to import modules into your Sval instance scope, expecting a name and a module as arguments like `import(name: string, mod: any)`, or an object which contains the modules as argument like `import({ [name: string]: any })`. The modules will be automatically declared as global variables. This method is more likely to be used in sandbox mode.
78
74
 
package/dist/sval.es6.js CHANGED
@@ -84,7 +84,7 @@
84
84
  }
85
85
  const assign = Object.assign || _assign;
86
86
  let names = [];
87
- let globalObj = Object.create(null);
87
+ let globalObj = create(null);
88
88
  try {
89
89
  if (!window.Object)
90
90
  throw 0;
@@ -344,7 +344,7 @@
344
344
  !globalObj.Symbol.iterator && (globalObj.Symbol.iterator = createSymbol('iterator'));
345
345
  !globalObj.Symbol.asyncIterator && (globalObj.Symbol.asyncIterator = createSymbol('asynciterator'));
346
346
  }
347
- const win = Object.create(null);
347
+ const win = create({});
348
348
  for (let i = 0; i < names.length; i++) {
349
349
  const name = names[i];
350
350
  try {
@@ -352,8 +352,9 @@
352
352
  }
353
353
  catch (err) { }
354
354
  }
355
+ const WINDOW = createSymbol('window');
355
356
  function createSandBox() {
356
- return assign(Object.create(null), win);
357
+ return assign(create({ [WINDOW]: globalObj }), win);
357
358
  }
358
359
  function createSymbol(key) {
359
360
  return key + Math.random().toString(36).substring(2);
@@ -382,7 +383,7 @@
382
383
  }
383
384
  }
384
385
 
385
- var version = "0.4.7";
386
+ var version = "0.4.9";
386
387
 
387
388
  const AWAIT = { RES: undefined };
388
389
  const RETURN = { RES: undefined };
@@ -432,7 +433,7 @@
432
433
 
433
434
  class Scope {
434
435
  constructor(parent = null, isolated = false) {
435
- this.context = Object.create(null);
436
+ this.context = create(null);
436
437
  this.parent = parent;
437
438
  this.isolated = isolated;
438
439
  }
@@ -916,6 +917,9 @@
916
917
  scope.find(SUPERCALL).set(true);
917
918
  }
918
919
  }
920
+ if (object && object[WINDOW] && func.toString().indexOf('[native code]') !== -1) {
921
+ return func.apply(object[WINDOW], args);
922
+ }
919
923
  return func.apply(object, args);
920
924
  }
921
925
  function NewExpression(node, scope) {
@@ -964,8 +968,8 @@
964
968
  return createFunc$1(node, scope);
965
969
  }
966
970
  function TemplateLiteral(node, scope) {
967
- const quasis = node.quasis;
968
- const expressions = node.expressions;
971
+ const quasis = node.quasis.slice();
972
+ const expressions = node.expressions.slice();
969
973
  let result = '';
970
974
  let temEl;
971
975
  let expr;
@@ -1913,6 +1917,9 @@
1913
1917
  scope.find(SUPERCALL).set(true);
1914
1918
  }
1915
1919
  }
1920
+ if (object && object[WINDOW] && func.toString().indexOf('[native code]') !== -1) {
1921
+ return func.apply(object[WINDOW], args);
1922
+ }
1916
1923
  return func.apply(object, args);
1917
1924
  }
1918
1925
  function* NewExpression$1(node, scope) {
@@ -1961,8 +1968,8 @@
1961
1968
  return createFunc(node, scope);
1962
1969
  }
1963
1970
  function* TemplateLiteral$1(node, scope) {
1964
- const quasis = node.quasis;
1965
- const expressions = node.expressions;
1971
+ const quasis = node.quasis.slice();
1972
+ const expressions = node.expressions.slice();
1966
1973
  let result = '';
1967
1974
  let temEl;
1968
1975
  let expr;
@@ -2610,13 +2617,25 @@
2610
2617
  case 'VariableDeclaration':
2611
2618
  yield* VariableDeclaration$1(statement, scope, { hoist: true });
2612
2619
  break;
2613
- case 'WhileStatement':
2614
- case 'DoWhileStatement':
2615
- case 'ForStatement':
2616
2620
  case 'ForInStatement':
2617
2621
  case 'ForOfStatement':
2622
+ if (statement.left.type === 'VariableDeclaration') {
2623
+ yield* VariableDeclaration$1(statement.left, scope, { hoist: true });
2624
+ }
2625
+ case 'ForStatement':
2626
+ if (statement.type === 'ForStatement' && statement.init.type === 'VariableDeclaration') {
2627
+ yield* VariableDeclaration$1(statement.init, scope, { hoist: true });
2628
+ }
2629
+ case 'WhileStatement':
2630
+ case 'DoWhileStatement':
2618
2631
  yield* hoistVarRecursion(statement.body, scope);
2619
2632
  break;
2633
+ case 'IfStatement':
2634
+ yield* hoistVarRecursion(statement.consequent, scope);
2635
+ if (statement.alternate) {
2636
+ yield* hoistVarRecursion(statement.alternate, scope);
2637
+ }
2638
+ break;
2620
2639
  case 'BlockStatement':
2621
2640
  for (let i = 0; i < statement.body.length; i++) {
2622
2641
  yield* hoistVarRecursion(statement.body[i], scope);
@@ -2691,7 +2710,7 @@
2691
2710
  yield* RestElement$1(param, subScope, { kind: 'var', feed: args.slice(i) });
2692
2711
  }
2693
2712
  else {
2694
- yield* pattern$2(param, subScope, { feed: args[i] });
2713
+ yield* pattern$2(param, subScope, { kind: 'var', feed: args[i] });
2695
2714
  }
2696
2715
  }
2697
2716
  let result;
@@ -2837,13 +2856,25 @@
2837
2856
  case 'VariableDeclaration':
2838
2857
  VariableDeclaration(statement, scope, { hoist: true });
2839
2858
  break;
2840
- case 'WhileStatement':
2841
- case 'DoWhileStatement':
2842
- case 'ForStatement':
2843
2859
  case 'ForInStatement':
2844
2860
  case 'ForOfStatement':
2861
+ if (statement.left.type === 'VariableDeclaration') {
2862
+ VariableDeclaration(statement.left, scope, { hoist: true });
2863
+ }
2864
+ case 'ForStatement':
2865
+ if (statement.type === 'ForStatement' && statement.init.type === 'VariableDeclaration') {
2866
+ VariableDeclaration(statement.init, scope, { hoist: true });
2867
+ }
2868
+ case 'WhileStatement':
2869
+ case 'DoWhileStatement':
2845
2870
  hoistVarRecursion$1(statement.body, scope);
2846
2871
  break;
2872
+ case 'IfStatement':
2873
+ hoistVarRecursion$1(statement.consequent, scope);
2874
+ if (statement.alternate) {
2875
+ hoistVarRecursion$1(statement.alternate, scope);
2876
+ }
2877
+ break;
2847
2878
  case 'BlockStatement':
2848
2879
  for (let i = 0; i < statement.body.length; i++) {
2849
2880
  hoistVarRecursion$1(statement.body[i], scope);
@@ -2918,7 +2949,7 @@
2918
2949
  RestElement(param, subScope, { kind: 'var', feed: args.slice(i) });
2919
2950
  }
2920
2951
  else {
2921
- pattern$3(param, subScope, { feed: args[i] });
2952
+ pattern$3(param, subScope, { kind: 'var', feed: args[i] });
2922
2953
  }
2923
2954
  }
2924
2955
  let result;
package/dist/sval.js CHANGED
@@ -84,7 +84,7 @@
84
84
  }
85
85
  var assign = Object.assign || _assign;
86
86
  var names = [];
87
- var globalObj = Object.create(null);
87
+ var globalObj = create(null);
88
88
  try {
89
89
  if (!window.Object)
90
90
  throw 0;
@@ -344,7 +344,7 @@
344
344
  !globalObj.Symbol.iterator && (globalObj.Symbol.iterator = createSymbol('iterator'));
345
345
  !globalObj.Symbol.asyncIterator && (globalObj.Symbol.asyncIterator = createSymbol('asynciterator'));
346
346
  }
347
- var win = Object.create(null);
347
+ var win = create({});
348
348
  for (var i = 0; i < names.length; i++) {
349
349
  var name_1 = names[i];
350
350
  try {
@@ -352,8 +352,10 @@
352
352
  }
353
353
  catch (err) { }
354
354
  }
355
+ var WINDOW = createSymbol('window');
355
356
  function createSandBox() {
356
- return assign(Object.create(null), win);
357
+ var _a;
358
+ return assign(create((_a = {}, _a[WINDOW] = globalObj, _a)), win);
357
359
  }
358
360
  function createSymbol(key) {
359
361
  return key + Math.random().toString(36).substring(2);
@@ -382,7 +384,7 @@
382
384
  }
383
385
  }
384
386
 
385
- var version = "0.4.7";
387
+ var version = "0.4.9";
386
388
 
387
389
  var AWAIT = { RES: undefined };
388
390
  var RETURN = { RES: undefined };
@@ -436,7 +438,7 @@
436
438
  function Scope(parent, isolated) {
437
439
  if (parent === void 0) { parent = null; }
438
440
  if (isolated === void 0) { isolated = false; }
439
- this.context = Object.create(null);
441
+ this.context = create(null);
440
442
  this.parent = parent;
441
443
  this.isolated = isolated;
442
444
  }
@@ -1000,6 +1002,9 @@
1000
1002
  scope.find(SUPERCALL).set(true);
1001
1003
  }
1002
1004
  }
1005
+ if (object && object[WINDOW] && func.toString().indexOf('[native code]') !== -1) {
1006
+ return func.apply(object[WINDOW], args);
1007
+ }
1003
1008
  return func.apply(object, args);
1004
1009
  }
1005
1010
  function NewExpression(node, scope) {
@@ -1048,8 +1053,8 @@
1048
1053
  return createFunc$1(node, scope);
1049
1054
  }
1050
1055
  function TemplateLiteral(node, scope) {
1051
- var quasis = node.quasis;
1052
- var expressions = node.expressions;
1056
+ var quasis = node.quasis.slice();
1057
+ var expressions = node.expressions.slice();
1053
1058
  var result = '';
1054
1059
  var temEl;
1055
1060
  var expr;
@@ -2187,6 +2192,9 @@
2187
2192
  scope.find(SUPERCALL).set(true);
2188
2193
  }
2189
2194
  }
2195
+ if (object && object[WINDOW] && func.toString().indexOf('[native code]') !== -1) {
2196
+ return [2, func.apply(object[WINDOW], args)];
2197
+ }
2190
2198
  return [2, func.apply(object, args)];
2191
2199
  }
2192
2200
  });
@@ -2275,8 +2283,8 @@
2275
2283
  return __generator(this, function (_c) {
2276
2284
  switch (_c.label) {
2277
2285
  case 0:
2278
- quasis = node.quasis;
2279
- expressions = node.expressions;
2286
+ quasis = node.quasis.slice();
2287
+ expressions = node.expressions.slice();
2280
2288
  result = '';
2281
2289
  _c.label = 1;
2282
2290
  case 1:
@@ -3393,100 +3401,122 @@
3393
3401
  _a = statement.type;
3394
3402
  switch (_a) {
3395
3403
  case 'VariableDeclaration': return [3, 1];
3396
- case 'WhileStatement': return [3, 3];
3397
- case 'DoWhileStatement': return [3, 3];
3398
- case 'ForStatement': return [3, 3];
3399
3404
  case 'ForInStatement': return [3, 3];
3400
3405
  case 'ForOfStatement': return [3, 3];
3401
- case 'BlockStatement': return [3, 5];
3402
- case 'SwitchStatement': return [3, 10];
3403
- case 'TryStatement': return [3, 17];
3404
- }
3405
- return [3, 30];
3406
+ case 'ForStatement': return [3, 5];
3407
+ case 'WhileStatement': return [3, 7];
3408
+ case 'DoWhileStatement': return [3, 7];
3409
+ case 'IfStatement': return [3, 9];
3410
+ case 'BlockStatement': return [3, 13];
3411
+ case 'SwitchStatement': return [3, 18];
3412
+ case 'TryStatement': return [3, 25];
3413
+ }
3414
+ return [3, 38];
3406
3415
  case 1: return [5, __values(VariableDeclaration$1(statement, scope, { hoist: true }))];
3407
3416
  case 2:
3408
3417
  _b.sent();
3409
- return [3, 30];
3410
- case 3: return [5, __values(hoistVarRecursion(statement.body, scope))];
3418
+ return [3, 38];
3419
+ case 3:
3420
+ if (!(statement.left.type === 'VariableDeclaration')) return [3, 5];
3421
+ return [5, __values(VariableDeclaration$1(statement.left, scope, { hoist: true }))];
3411
3422
  case 4:
3412
3423
  _b.sent();
3413
- return [3, 30];
3424
+ _b.label = 5;
3414
3425
  case 5:
3415
- i = 0;
3416
- _b.label = 6;
3426
+ if (!(statement.type === 'ForStatement' && statement.init.type === 'VariableDeclaration')) return [3, 7];
3427
+ return [5, __values(VariableDeclaration$1(statement.init, scope, { hoist: true }))];
3417
3428
  case 6:
3418
- if (!(i < statement.body.length)) return [3, 9];
3419
- return [5, __values(hoistVarRecursion(statement.body[i], scope))];
3420
- case 7:
3421
3429
  _b.sent();
3422
- _b.label = 8;
3430
+ _b.label = 7;
3431
+ case 7: return [5, __values(hoistVarRecursion(statement.body, scope))];
3423
3432
  case 8:
3424
- i++;
3425
- return [3, 6];
3426
- case 9: return [3, 30];
3433
+ _b.sent();
3434
+ return [3, 38];
3435
+ case 9: return [5, __values(hoistVarRecursion(statement.consequent, scope))];
3427
3436
  case 10:
3428
- i = 0;
3429
- _b.label = 11;
3437
+ _b.sent();
3438
+ if (!statement.alternate) return [3, 12];
3439
+ return [5, __values(hoistVarRecursion(statement.alternate, scope))];
3430
3440
  case 11:
3431
- if (!(i < statement.cases.length)) return [3, 16];
3432
- j = 0;
3441
+ _b.sent();
3433
3442
  _b.label = 12;
3434
- case 12:
3435
- if (!(j < statement.cases[i].consequent.length)) return [3, 15];
3436
- return [5, __values(hoistVarRecursion(statement.cases[i].consequent[j], scope))];
3443
+ case 12: return [3, 38];
3437
3444
  case 13:
3438
- _b.sent();
3445
+ i = 0;
3439
3446
  _b.label = 14;
3440
3447
  case 14:
3441
- j++;
3442
- return [3, 12];
3448
+ if (!(i < statement.body.length)) return [3, 17];
3449
+ return [5, __values(hoistVarRecursion(statement.body[i], scope))];
3443
3450
  case 15:
3451
+ _b.sent();
3452
+ _b.label = 16;
3453
+ case 16:
3444
3454
  i++;
3445
- return [3, 11];
3446
- case 16: return [3, 30];
3447
- case 17:
3448
- tryBlock = statement.block.body;
3449
- i = 0;
3450
- _b.label = 18;
3455
+ return [3, 14];
3456
+ case 17: return [3, 38];
3451
3457
  case 18:
3452
- if (!(i < tryBlock.length)) return [3, 21];
3453
- return [5, __values(hoistVarRecursion(tryBlock[i], scope))];
3458
+ i = 0;
3459
+ _b.label = 19;
3454
3460
  case 19:
3455
- _b.sent();
3461
+ if (!(i < statement.cases.length)) return [3, 24];
3462
+ j = 0;
3456
3463
  _b.label = 20;
3457
3464
  case 20:
3458
- i++;
3459
- return [3, 18];
3465
+ if (!(j < statement.cases[i].consequent.length)) return [3, 23];
3466
+ return [5, __values(hoistVarRecursion(statement.cases[i].consequent[j], scope))];
3460
3467
  case 21:
3461
- catchBlock = statement.handler && statement.handler.body.body;
3462
- if (!catchBlock) return [3, 25];
3463
- i = 0;
3468
+ _b.sent();
3464
3469
  _b.label = 22;
3465
3470
  case 22:
3466
- if (!(i < catchBlock.length)) return [3, 25];
3467
- return [5, __values(hoistVarRecursion(catchBlock[i], scope))];
3471
+ j++;
3472
+ return [3, 20];
3468
3473
  case 23:
3469
- _b.sent();
3470
- _b.label = 24;
3471
- case 24:
3472
3474
  i++;
3473
- return [3, 22];
3475
+ return [3, 19];
3476
+ case 24: return [3, 38];
3474
3477
  case 25:
3475
- finalBlock = statement.finalizer && statement.finalizer.body;
3476
- if (!finalBlock) return [3, 29];
3478
+ tryBlock = statement.block.body;
3477
3479
  i = 0;
3478
3480
  _b.label = 26;
3479
3481
  case 26:
3480
- if (!(i < finalBlock.length)) return [3, 29];
3481
- return [5, __values(hoistVarRecursion(finalBlock[i], scope))];
3482
+ if (!(i < tryBlock.length)) return [3, 29];
3483
+ return [5, __values(hoistVarRecursion(tryBlock[i], scope))];
3482
3484
  case 27:
3483
3485
  _b.sent();
3484
3486
  _b.label = 28;
3485
3487
  case 28:
3486
3488
  i++;
3487
3489
  return [3, 26];
3488
- case 29: return [3, 30];
3489
- case 30: return [2];
3490
+ case 29:
3491
+ catchBlock = statement.handler && statement.handler.body.body;
3492
+ if (!catchBlock) return [3, 33];
3493
+ i = 0;
3494
+ _b.label = 30;
3495
+ case 30:
3496
+ if (!(i < catchBlock.length)) return [3, 33];
3497
+ return [5, __values(hoistVarRecursion(catchBlock[i], scope))];
3498
+ case 31:
3499
+ _b.sent();
3500
+ _b.label = 32;
3501
+ case 32:
3502
+ i++;
3503
+ return [3, 30];
3504
+ case 33:
3505
+ finalBlock = statement.finalizer && statement.finalizer.body;
3506
+ if (!finalBlock) return [3, 37];
3507
+ i = 0;
3508
+ _b.label = 34;
3509
+ case 34:
3510
+ if (!(i < finalBlock.length)) return [3, 37];
3511
+ return [5, __values(hoistVarRecursion(finalBlock[i], scope))];
3512
+ case 35:
3513
+ _b.sent();
3514
+ _b.label = 36;
3515
+ case 36:
3516
+ i++;
3517
+ return [3, 34];
3518
+ case 37: return [3, 38];
3519
+ case 38: return [2];
3490
3520
  }
3491
3521
  });
3492
3522
  }
@@ -3558,7 +3588,7 @@
3558
3588
  case 3:
3559
3589
  _a.sent();
3560
3590
  return [3, 6];
3561
- case 4: return [5, __values(pattern$2(param, subScope, { feed: args[i] }))];
3591
+ case 4: return [5, __values(pattern$2(param, subScope, { kind: 'var', feed: args[i] }))];
3562
3592
  case 5:
3563
3593
  _a.sent();
3564
3594
  _a.label = 6;
@@ -3744,13 +3774,25 @@
3744
3774
  case 'VariableDeclaration':
3745
3775
  VariableDeclaration(statement, scope, { hoist: true });
3746
3776
  break;
3747
- case 'WhileStatement':
3748
- case 'DoWhileStatement':
3749
- case 'ForStatement':
3750
3777
  case 'ForInStatement':
3751
3778
  case 'ForOfStatement':
3779
+ if (statement.left.type === 'VariableDeclaration') {
3780
+ VariableDeclaration(statement.left, scope, { hoist: true });
3781
+ }
3782
+ case 'ForStatement':
3783
+ if (statement.type === 'ForStatement' && statement.init.type === 'VariableDeclaration') {
3784
+ VariableDeclaration(statement.init, scope, { hoist: true });
3785
+ }
3786
+ case 'WhileStatement':
3787
+ case 'DoWhileStatement':
3752
3788
  hoistVarRecursion$1(statement.body, scope);
3753
3789
  break;
3790
+ case 'IfStatement':
3791
+ hoistVarRecursion$1(statement.consequent, scope);
3792
+ if (statement.alternate) {
3793
+ hoistVarRecursion$1(statement.alternate, scope);
3794
+ }
3795
+ break;
3754
3796
  case 'BlockStatement':
3755
3797
  for (var i = 0; i < statement.body.length; i++) {
3756
3798
  hoistVarRecursion$1(statement.body[i], scope);
@@ -3832,7 +3874,7 @@
3832
3874
  RestElement(param, subScope, { kind: 'var', feed: args.slice(i) });
3833
3875
  }
3834
3876
  else {
3835
- pattern$3(param, subScope, { feed: args[i] });
3877
+ pattern$3(param, subScope, { kind: 'var', feed: args[i] });
3836
3878
  }
3837
3879
  }
3838
3880
  var result;