sval 0.4.9 → 0.5.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/dist/sval.js CHANGED
@@ -384,7 +384,7 @@
384
384
  }
385
385
  }
386
386
 
387
- var version = "0.4.9";
387
+ var version = "0.5.0";
388
388
 
389
389
  var AWAIT = { RES: undefined };
390
390
  var RETURN = { RES: undefined };
@@ -817,7 +817,7 @@
817
817
  }
818
818
  }
819
819
  function AssignmentExpression(node, scope) {
820
- var value = evaluate(node.right, scope);
820
+ var _a;
821
821
  var left = node.left;
822
822
  var variable;
823
823
  if (left.type === 'Identifier') {
@@ -831,8 +831,10 @@
831
831
  variable = MemberExpression(left, scope, { getVar: true });
832
832
  }
833
833
  else {
834
- return pattern$3(left, scope, { feed: value });
834
+ var value_1 = evaluate(node.right, scope);
835
+ return pattern$3(left, scope, { feed: value_1 });
835
836
  }
837
+ var value = evaluate(node.right, scope);
836
838
  switch (node.operator) {
837
839
  case '=':
838
840
  variable.set(value);
@@ -873,15 +875,27 @@
873
875
  case '&=':
874
876
  variable.set(variable.get() & value);
875
877
  return variable.get();
878
+ case '??=':
879
+ variable.set((_a = variable.get()) !== null && _a !== void 0 ? _a : value);
880
+ return variable.get();
881
+ case '&&=':
882
+ variable.set(variable.get() && value);
883
+ return variable.get();
884
+ case '||=':
885
+ variable.set(variable.get() || value);
886
+ return variable.get();
876
887
  default: throw new SyntaxError("Unexpected token " + node.operator);
877
888
  }
878
889
  }
879
890
  function LogicalExpression(node, scope) {
891
+ var _a;
880
892
  switch (node.operator) {
881
893
  case '||':
882
894
  return (evaluate(node.left, scope)) || (evaluate(node.right, scope));
883
895
  case '&&':
884
896
  return (evaluate(node.left, scope)) && (evaluate(node.right, scope));
897
+ case '??':
898
+ return (_a = (evaluate(node.left, scope))) !== null && _a !== void 0 ? _a : (evaluate(node.right, scope));
885
899
  default:
886
900
  throw new SyntaxError("Unexpected token " + node.operator);
887
901
  }
@@ -921,9 +935,15 @@
921
935
  var getter = getGetter(object, key);
922
936
  if (node.object.type === 'Super' && getter) {
923
937
  var thisObject = scope.find('this').get();
938
+ if (node.optional && thisObject == null) {
939
+ return undefined;
940
+ }
924
941
  return getter.call(thisObject);
925
942
  }
926
943
  else {
944
+ if (node.optional && object == null) {
945
+ return undefined;
946
+ }
927
947
  return object[key];
928
948
  }
929
949
  }
@@ -938,6 +958,9 @@
938
958
  var object;
939
959
  if (node.callee.type === 'MemberExpression') {
940
960
  object = MemberExpression(node.callee, scope, { getObj: true });
961
+ if (node.callee.optional && object == null) {
962
+ return undefined;
963
+ }
941
964
  var key = void 0;
942
965
  if (node.callee.computed) {
943
966
  key = evaluate(node.callee.property, scope);
@@ -952,6 +975,9 @@
952
975
  else {
953
976
  func = object[key];
954
977
  }
978
+ if (node.optional && func == null) {
979
+ return undefined;
980
+ }
955
981
  if (typeof func !== 'function') {
956
982
  throw new TypeError(key + " is not a function");
957
983
  }
@@ -962,6 +988,9 @@
962
988
  else {
963
989
  object = scope.find('this').get();
964
990
  func = evaluate(node.callee, scope);
991
+ if (node.optional && func == null) {
992
+ return undefined;
993
+ }
965
994
  if (typeof func !== 'function' || node.callee.type !== 'Super' && func[CLSCTOR]) {
966
995
  var name_1;
967
996
  if (node.callee.type === 'Identifier') {
@@ -1106,6 +1135,9 @@
1106
1135
  }
1107
1136
  function SpreadElement(node, scope) {
1108
1137
  return evaluate(node.argument, scope);
1138
+ }
1139
+ function ChainExpression(node, scope) {
1140
+ return evaluate(node.expression, scope);
1109
1141
  }
1110
1142
 
1111
1143
  var expression = /*#__PURE__*/Object.freeze({
@@ -1131,7 +1163,8 @@
1131
1163
  TemplateElement: TemplateElement,
1132
1164
  ClassExpression: ClassExpression,
1133
1165
  Super: Super,
1134
- SpreadElement: SpreadElement
1166
+ SpreadElement: SpreadElement,
1167
+ ChainExpression: ChainExpression
1135
1168
  });
1136
1169
 
1137
1170
  function ExpressionStatement(node, scope) {
@@ -1579,7 +1612,10 @@
1579
1612
  if (options === void 0) { options = {}; }
1580
1613
  var klass = options.klass, superClass = options.superClass;
1581
1614
  for (var i = 0; i < node.body.length; i++) {
1582
- MethodDefinition(node.body[i], scope, { klass: klass, superClass: superClass });
1615
+ var def = node.body[i];
1616
+ if (def.type === 'MethodDefinition') {
1617
+ MethodDefinition(def, scope, { klass: klass, superClass: superClass });
1618
+ }
1583
1619
  }
1584
1620
  }
1585
1621
  function MethodDefinition(node, scope, options) {
@@ -1927,31 +1963,35 @@
1927
1963
  });
1928
1964
  }
1929
1965
  function AssignmentExpression$1(node, scope) {
1930
- var value, left, variable, win;
1931
- return __generator(this, function (_a) {
1932
- switch (_a.label) {
1933
- case 0: return [5, __values(evaluate$1(node.right, scope))];
1934
- case 1:
1935
- value = _a.sent();
1966
+ var left, variable, win, value_1, value;
1967
+ var _a;
1968
+ return __generator(this, function (_b) {
1969
+ switch (_b.label) {
1970
+ case 0:
1936
1971
  left = node.left;
1937
- if (!(left.type === 'Identifier')) return [3, 3];
1972
+ if (!(left.type === 'Identifier')) return [3, 2];
1938
1973
  return [5, __values(Identifier$1(left, scope, { getVar: true, throwErr: false }))];
1939
- case 2:
1940
- variable = _a.sent();
1974
+ case 1:
1975
+ variable = _b.sent();
1941
1976
  if (!variable) {
1942
1977
  win = scope.global().find('window').get();
1943
1978
  variable = new Prop(win, left.name);
1944
1979
  }
1945
1980
  return [3, 7];
1946
- case 3:
1947
- if (!(left.type === 'MemberExpression')) return [3, 5];
1981
+ case 2:
1982
+ if (!(left.type === 'MemberExpression')) return [3, 4];
1948
1983
  return [5, __values(MemberExpression$1(left, scope, { getVar: true }))];
1949
- case 4:
1950
- variable = _a.sent();
1984
+ case 3:
1985
+ variable = _b.sent();
1951
1986
  return [3, 7];
1952
- case 5: return [5, __values(pattern$2(left, scope, { feed: value }))];
1953
- case 6: return [2, _a.sent()];
1954
- case 7:
1987
+ case 4: return [5, __values(evaluate$1(node.right, scope))];
1988
+ case 5:
1989
+ value_1 = _b.sent();
1990
+ return [5, __values(pattern$2(left, scope, { feed: value_1 }))];
1991
+ case 6: return [2, _b.sent()];
1992
+ case 7: return [5, __values(evaluate$1(node.right, scope))];
1993
+ case 8:
1994
+ value = _b.sent();
1955
1995
  switch (node.operator) {
1956
1996
  case '=':
1957
1997
  variable.set(value);
@@ -1992,41 +2032,62 @@
1992
2032
  case '&=':
1993
2033
  variable.set(variable.get() & value);
1994
2034
  return [2, variable.get()];
2035
+ case '??=':
2036
+ variable.set((_a = variable.get()) !== null && _a !== void 0 ? _a : value);
2037
+ return [2, variable.get()];
2038
+ case '&&=':
2039
+ variable.set(variable.get() && value);
2040
+ return [2, variable.get()];
2041
+ case '||=':
2042
+ variable.set(variable.get() || value);
2043
+ return [2, variable.get()];
1995
2044
  default: throw new SyntaxError("Unexpected token " + node.operator);
1996
2045
  }
1997
2046
  }
1998
2047
  });
1999
2048
  }
2000
2049
  function LogicalExpression$1(node, scope) {
2001
- var _a, _b, _c;
2002
- return __generator(this, function (_d) {
2003
- switch (_d.label) {
2050
+ var _a, _b, _c, _d;
2051
+ var _e;
2052
+ return __generator(this, function (_f) {
2053
+ switch (_f.label) {
2004
2054
  case 0:
2005
2055
  _a = node.operator;
2006
2056
  switch (_a) {
2007
2057
  case '||': return [3, 1];
2008
2058
  case '&&': return [3, 5];
2059
+ case '??': return [3, 9];
2009
2060
  }
2010
- return [3, 9];
2061
+ return [3, 14];
2011
2062
  case 1: return [5, __values(evaluate$1(node.left, scope))];
2012
2063
  case 2:
2013
- _b = (_d.sent());
2064
+ _b = (_f.sent());
2014
2065
  if (_b) return [3, 4];
2015
2066
  return [5, __values(evaluate$1(node.right, scope))];
2016
2067
  case 3:
2017
- _b = (_d.sent());
2018
- _d.label = 4;
2068
+ _b = (_f.sent());
2069
+ _f.label = 4;
2019
2070
  case 4: return [2, _b];
2020
2071
  case 5: return [5, __values(evaluate$1(node.left, scope))];
2021
2072
  case 6:
2022
- _c = (_d.sent());
2073
+ _c = (_f.sent());
2023
2074
  if (!_c) return [3, 8];
2024
2075
  return [5, __values(evaluate$1(node.right, scope))];
2025
2076
  case 7:
2026
- _c = (_d.sent());
2027
- _d.label = 8;
2077
+ _c = (_f.sent());
2078
+ _f.label = 8;
2028
2079
  case 8: return [2, _c];
2029
- case 9: throw new SyntaxError("Unexpected token " + node.operator);
2080
+ case 9: return [5, __values(evaluate$1(node.left, scope))];
2081
+ case 10:
2082
+ if (!((_e = (_f.sent())) !== null && _e !== void 0)) return [3, 11];
2083
+ _d = _e;
2084
+ return [3, 13];
2085
+ case 11: return [5, __values(evaluate$1(node.right, scope))];
2086
+ case 12:
2087
+ _d = (_f.sent());
2088
+ _f.label = 13;
2089
+ case 13: return [2, _d];
2090
+ case 14: throw new SyntaxError("Unexpected token " + node.operator);
2030
2091
  }
2031
2092
  });
2032
2093
  }
@@ -2074,9 +2135,15 @@
2074
2135
  getter = getGetter(object, key);
2075
2136
  if (node.object.type === 'Super' && getter) {
2076
2137
  thisObject = scope.find('this').get();
2138
+ if (node.optional && thisObject == null) {
2139
+ return [2, undefined];
2140
+ }
2077
2141
  return [2, getter.call(thisObject)];
2078
2142
  }
2079
2143
  else {
2144
+ if (node.optional && object == null) {
2145
+ return [2, undefined];
2146
+ }
2080
2147
  return [2, object[key]];
2081
2148
  }
2082
2149
  }
@@ -2111,6 +2178,9 @@
2111
2178
  return [5, __values(MemberExpression$1(node.callee, scope, { getObj: true }))];
2112
2179
  case 1:
2113
2180
  object = _e.sent();
2181
+ if (node.callee.optional && object == null) {
2182
+ return [2, undefined];
2183
+ }
2114
2184
  key = void 0;
2115
2185
  if (!node.callee.computed) return [3, 3];
2116
2186
  return [5, __values(evaluate$1(node.callee.property, scope))];
@@ -2128,6 +2198,9 @@
2128
2198
  else {
2129
2199
  func = object[key];
2130
2200
  }
2201
+ if (node.optional && func == null) {
2202
+ return [2, undefined];
2203
+ }
2131
2204
  if (typeof func !== 'function') {
2132
2205
  throw new TypeError(key + " is not a function");
2133
2206
  }
@@ -2140,6 +2213,9 @@
2140
2213
  return [5, __values(evaluate$1(node.callee, scope))];
2141
2214
  case 6:
2142
2215
  func = _e.sent();
2216
+ if (node.optional && func == null) {
2217
+ return [2, undefined];
2218
+ }
2143
2219
  if (typeof func !== 'function' || node.callee.type !== 'Super' && func[CLSCTOR]) {
2144
2220
  if (node.callee.type === 'Identifier') {
2145
2221
  name_1 = node.callee.name;
@@ -2376,6 +2452,14 @@
2376
2452
  }
2377
2453
  });
2378
2454
  }
2455
+ function ChainExpression$1(node, scope) {
2456
+ return __generator(this, function (_a) {
2457
+ switch (_a.label) {
2458
+ case 0: return [5, __values(evaluate$1(node.expression, scope))];
2459
+ case 1: return [2, _a.sent()];
2460
+ }
2461
+ });
2462
+ }
2379
2463
  function YieldExpression(node, scope) {
2380
2464
  var res, _a;
2381
2465
  return __generator(this, function (_b) {
@@ -2435,6 +2519,7 @@
2435
2519
  ClassExpression: ClassExpression$1,
2436
2520
  Super: Super$1,
2437
2521
  SpreadElement: SpreadElement$1,
2522
+ ChainExpression: ChainExpression$1,
2438
2523
  YieldExpression: YieldExpression,
2439
2524
  AwaitExpression: AwaitExpression
2440
2525
  });
@@ -3221,7 +3306,7 @@
3221
3306
  });
3222
3307
  }
3223
3308
  function ClassBody$1(node, scope, options) {
3224
- var klass, superClass, i;
3309
+ var klass, superClass, i, def;
3225
3310
  if (options === void 0) { options = {}; }
3226
3311
  return __generator(this, function (_a) {
3227
3312
  switch (_a.label) {
@@ -3231,7 +3316,9 @@
3231
3316
  _a.label = 1;
3232
3317
  case 1:
3233
3318
  if (!(i < node.body.length)) return [3, 4];
3234
- return [5, __values(MethodDefinition$1(node.body[i], scope, { klass: klass, superClass: superClass }))];
3319
+ def = node.body[i];
3320
+ if (!(def.type === 'MethodDefinition')) return [3, 3];
3321
+ return [5, __values(MethodDefinition$1(def, scope, { klass: klass, superClass: superClass }))];
3235
3322
  case 2:
3236
3323
  _a.sent();
3237
3324
  _a.label = 3;
@@ -3684,7 +3771,7 @@
3684
3771
  methodBody = node.body.body;
3685
3772
  for (i = 0; i < methodBody.length; i++) {
3686
3773
  method = methodBody[i];
3687
- if (method.kind === 'constructor') {
3774
+ if (method.type === 'MethodDefinition' && method.kind === 'constructor') {
3688
3775
  klass = createFunc(method.value, scope, { superClass: superClass, isCtor: true });
3689
3776
  break;
3690
3777
  }
@@ -3922,7 +4009,7 @@
3922
4009
  var methodBody = node.body.body;
3923
4010
  for (var i = 0; i < methodBody.length; i++) {
3924
4011
  var method = methodBody[i];
3925
- if (method.kind === 'constructor') {
4012
+ if (method.type === 'MethodDefinition' && method.kind === 'constructor') {
3926
4013
  klass = createFunc$1(method.value, scope, { superClass: superClass, isCtor: true });
3927
4014
  break;
3928
4015
  }
@@ -3962,15 +4049,18 @@
3962
4049
  return result;
3963
4050
  }
3964
4051
 
4052
+ var latestVer = 15;
3965
4053
  var Sval = (function () {
3966
4054
  function Sval(options) {
3967
4055
  if (options === void 0) { options = {}; }
3968
- this.options = {};
4056
+ this.options = { ecmaVersion: 'latest' };
3969
4057
  this.scope = new Scope(null, true);
3970
4058
  this.exports = {};
3971
- var _a = options.ecmaVer, ecmaVer = _a === void 0 ? 9 : _a, _b = options.sandBox, sandBox = _b === void 0 ? true : _b;
3972
- ecmaVer -= ecmaVer < 2015 ? 0 : 2009;
3973
- if ([3, 5, 6, 7, 8, 9, 10].indexOf(ecmaVer) === -1) {
4059
+ var _a = options.ecmaVer, ecmaVer = _a === void 0 ? 'latest' : _a, _b = options.sandBox, sandBox = _b === void 0 ? true : _b;
4060
+ if (typeof ecmaVer === 'number') {
4061
+ ecmaVer -= ecmaVer < 2015 ? 0 : 2009;
4062
+ }
4063
+ if (ecmaVer !== 'latest' && ecmaVer !== 3 && (ecmaVer < 5 || ecmaVer > latestVer)) {
3974
4064
  throw new Error("unsupported ecmaVer");
3975
4065
  }
3976
4066
  this.options.ecmaVersion = ecmaVer;