sval 0.5.0 → 0.5.2

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.es6.js CHANGED
@@ -4,6 +4,27 @@
4
4
  (global = global || self, global.Sval = factory(global.acorn));
5
5
  }(this, (function (acorn) { 'use strict';
6
6
 
7
+ var statement = /*#__PURE__*/Object.freeze({
8
+ __proto__: null,
9
+ get ExpressionStatement () { return ExpressionStatement; },
10
+ get BlockStatement () { return BlockStatement; },
11
+ get EmptyStatement () { return EmptyStatement; },
12
+ get DebuggerStatement () { return DebuggerStatement; },
13
+ get ReturnStatement () { return ReturnStatement; },
14
+ get BreakStatement () { return BreakStatement; },
15
+ get ContinueStatement () { return ContinueStatement; },
16
+ get IfStatement () { return IfStatement; },
17
+ get SwitchStatement () { return SwitchStatement; },
18
+ get SwitchCase () { return SwitchCase; },
19
+ get ThrowStatement () { return ThrowStatement; },
20
+ get TryStatement () { return TryStatement; },
21
+ get CatchClause () { return CatchClause; },
22
+ get WhileStatement () { return WhileStatement; },
23
+ get DoWhileStatement () { return DoWhileStatement; },
24
+ get ForStatement () { return ForStatement; },
25
+ get ForInStatement () { return ForInStatement; },
26
+ get ForOfStatement () { return ForOfStatement; }
27
+ });
7
28
  var declaration = /*#__PURE__*/Object.freeze({
8
29
  __proto__: null,
9
30
  get FunctionDeclaration () { return FunctionDeclaration; },
@@ -11,7 +32,34 @@
11
32
  get VariableDeclarator () { return VariableDeclarator; },
12
33
  get ClassDeclaration () { return ClassDeclaration; },
13
34
  get ClassBody () { return ClassBody; },
14
- get MethodDefinition () { return MethodDefinition; }
35
+ get MethodDefinition () { return MethodDefinition; },
36
+ get PropertyDefinition () { return PropertyDefinition; },
37
+ get StaticBlock () { return StaticBlock; },
38
+ get ImportDeclaration () { return ImportDeclaration; },
39
+ get ExportDefaultDeclaration () { return ExportDefaultDeclaration; },
40
+ get ExportNamedDeclaration () { return ExportNamedDeclaration; },
41
+ get ExportAllDeclaration () { return ExportAllDeclaration; }
42
+ });
43
+ var statement$1 = /*#__PURE__*/Object.freeze({
44
+ __proto__: null,
45
+ get ExpressionStatement () { return ExpressionStatement$1; },
46
+ get BlockStatement () { return BlockStatement$1; },
47
+ get EmptyStatement () { return EmptyStatement$1; },
48
+ get DebuggerStatement () { return DebuggerStatement$1; },
49
+ get ReturnStatement () { return ReturnStatement$1; },
50
+ get BreakStatement () { return BreakStatement$1; },
51
+ get ContinueStatement () { return ContinueStatement$1; },
52
+ get IfStatement () { return IfStatement$1; },
53
+ get SwitchStatement () { return SwitchStatement$1; },
54
+ get SwitchCase () { return SwitchCase$1; },
55
+ get ThrowStatement () { return ThrowStatement$1; },
56
+ get TryStatement () { return TryStatement$1; },
57
+ get CatchClause () { return CatchClause$1; },
58
+ get WhileStatement () { return WhileStatement$1; },
59
+ get DoWhileStatement () { return DoWhileStatement$1; },
60
+ get ForStatement () { return ForStatement$1; },
61
+ get ForInStatement () { return ForInStatement$1; },
62
+ get ForOfStatement () { return ForOfStatement$1; }
15
63
  });
16
64
  var declaration$1 = /*#__PURE__*/Object.freeze({
17
65
  __proto__: null,
@@ -20,7 +68,13 @@
20
68
  get VariableDeclarator () { return VariableDeclarator$1; },
21
69
  get ClassDeclaration () { return ClassDeclaration$1; },
22
70
  get ClassBody () { return ClassBody$1; },
23
- get MethodDefinition () { return MethodDefinition$1; }
71
+ get MethodDefinition () { return MethodDefinition$1; },
72
+ get PropertyDefinition () { return PropertyDefinition$1; },
73
+ get StaticBlock () { return StaticBlock$1; },
74
+ get ImportDeclaration () { return ImportDeclaration$1; },
75
+ get ExportDefaultDeclaration () { return ExportDefaultDeclaration$1; },
76
+ get ExportNamedDeclaration () { return ExportNamedDeclaration$1; },
77
+ get ExportAllDeclaration () { return ExportAllDeclaration$1; }
24
78
  });
25
79
 
26
80
  const freeze = Object.freeze;
@@ -269,6 +323,10 @@
269
323
  globalObj.Reflect = Reflect;
270
324
  }
271
325
  catch (err) { }
326
+ try {
327
+ globalObj.BigInt = BigInt;
328
+ }
329
+ catch (err) { }
272
330
  try {
273
331
  globalObj.decodeURI = decodeURI;
274
332
  }
@@ -383,8 +441,6 @@
383
441
  }
384
442
  }
385
443
 
386
- var version = "0.5.0";
387
-
388
444
  const AWAIT = { RES: undefined };
389
445
  const RETURN = { RES: undefined };
390
446
  const CONTINUE = createSymbol('continue');
@@ -394,8 +450,13 @@
394
450
  const NOCTOR = createSymbol('noctor');
395
451
  const CLSCTOR = createSymbol('clsctor');
396
452
  const NEWTARGET = createSymbol('newtarget');
453
+ const PRIVATE = createSymbol('private');
397
454
  const NOINIT = createSymbol('noinit');
398
- const DEADZONE = createSymbol('deadzone');
455
+ const DEADZONE = createSymbol('deadzone');
456
+ const IMPORT = createSymbol('import');
457
+ const EXPORTS = createSymbol('exports');
458
+
459
+ var version = "0.5.2";
399
460
 
400
461
  class Var {
401
462
  constructor(kind, value) {
@@ -704,8 +765,17 @@
704
765
  }
705
766
  }
706
767
  function BinaryExpression(node, scope) {
707
- const left = evaluate(node.left, scope);
708
- const right = evaluate(node.right, scope);
768
+ let left;
769
+ let right;
770
+ if (node.left.type === 'PrivateIdentifier') {
771
+ left = node.left.name;
772
+ right = evaluate(node.right, scope);
773
+ right = right[PRIVATE];
774
+ }
775
+ else {
776
+ left = evaluate(node.left, scope);
777
+ right = evaluate(node.right, scope);
778
+ }
709
779
  switch (node.operator) {
710
780
  case '==': return left == right;
711
781
  case '!=': return left != right;
@@ -828,12 +898,20 @@
828
898
  if (getObj)
829
899
  return object;
830
900
  let key;
901
+ let priv = false;
831
902
  if (node.computed) {
832
903
  key = evaluate(node.property, scope);
833
904
  }
905
+ else if (node.property.type === 'PrivateIdentifier') {
906
+ key = node.property.name;
907
+ priv = true;
908
+ }
834
909
  else {
835
910
  key = node.property.name;
836
911
  }
912
+ if (priv) {
913
+ object = object[PRIVATE];
914
+ }
837
915
  if (getVar) {
838
916
  const setter = getSetter(object, key);
839
917
  if (node.object.type === 'Super' && setter) {
@@ -877,18 +955,27 @@
877
955
  return undefined;
878
956
  }
879
957
  let key;
958
+ let priv = false;
880
959
  if (node.callee.computed) {
881
960
  key = evaluate(node.callee.property, scope);
882
961
  }
962
+ else if (node.callee.property.type === 'PrivateIdentifier') {
963
+ key = node.callee.property.name;
964
+ priv = true;
965
+ }
883
966
  else {
884
967
  key = node.callee.property.name;
885
968
  }
969
+ let obj = object;
970
+ if (priv) {
971
+ obj = obj[PRIVATE];
972
+ }
886
973
  if (node.callee.object.type === 'Super') {
887
974
  const thisObject = scope.find('this').get();
888
- func = object[key].bind(thisObject);
975
+ func = obj[key].bind(thisObject);
889
976
  }
890
977
  else {
891
- func = object[key];
978
+ func = obj[key];
892
979
  }
893
980
  if (node.optional && func == null) {
894
981
  return undefined;
@@ -984,7 +1071,12 @@
984
1071
  return new constructor(...args);
985
1072
  }
986
1073
  function MetaProperty(node, scope) {
987
- return scope.find(NEWTARGET).get();
1074
+ if (node.meta.name === 'new' && node.property.name === 'target') {
1075
+ return scope.find(NEWTARGET).get();
1076
+ }
1077
+ else if (node.meta.name === 'import' && node.property.name === 'meta') {
1078
+ return { url: '' };
1079
+ }
988
1080
  }
989
1081
  function SequenceExpression(node, scope) {
990
1082
  let result;
@@ -1048,10 +1140,32 @@
1048
1140
  return getProto ? superClass.prototype : superClass;
1049
1141
  }
1050
1142
  function SpreadElement(node, scope) {
1051
- return evaluate(node.argument, scope);
1143
+ const result = evaluate(node.argument, scope);
1144
+ return typeof result === 'string' ? [...result] : result;
1052
1145
  }
1053
1146
  function ChainExpression(node, scope) {
1054
1147
  return evaluate(node.expression, scope);
1148
+ }
1149
+ function ImportExpression(node, scope) {
1150
+ const globalScope = scope.global();
1151
+ const source = evaluate(node.source, scope);
1152
+ const module = globalScope.find(IMPORT + source);
1153
+ let value;
1154
+ if (module) {
1155
+ const result = module.get();
1156
+ if (result) {
1157
+ if (typeof result === 'function') {
1158
+ value = result();
1159
+ }
1160
+ else if (typeof result === 'object') {
1161
+ value = result;
1162
+ }
1163
+ }
1164
+ }
1165
+ if (!value || typeof value !== 'object') {
1166
+ return Promise.reject(new TypeError(`Failed to resolve module specifier "${source}"`));
1167
+ }
1168
+ return Promise.resolve(value);
1055
1169
  }
1056
1170
 
1057
1171
  var expression = /*#__PURE__*/Object.freeze({
@@ -1078,9 +1192,176 @@
1078
1192
  ClassExpression: ClassExpression,
1079
1193
  Super: Super,
1080
1194
  SpreadElement: SpreadElement,
1081
- ChainExpression: ChainExpression
1195
+ ChainExpression: ChainExpression,
1196
+ ImportExpression: ImportExpression
1197
+ });
1198
+
1199
+ function ObjectPattern(node, scope, options = {}) {
1200
+ const { kind = 'var', hoist = false, onlyBlock = false, feed = {} } = options;
1201
+ const fedKeys = [];
1202
+ for (let i = 0; i < node.properties.length; i++) {
1203
+ const property = node.properties[i];
1204
+ if (hoist) {
1205
+ if (onlyBlock || kind === 'var') {
1206
+ if (property.type === 'Property') {
1207
+ const value = property.value;
1208
+ if (value.type === 'Identifier') {
1209
+ scope[kind](value.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1210
+ }
1211
+ else {
1212
+ pattern$3(value, scope, { kind, hoist, onlyBlock });
1213
+ }
1214
+ }
1215
+ else {
1216
+ RestElement(property, scope, { kind, hoist, onlyBlock });
1217
+ }
1218
+ }
1219
+ }
1220
+ else if (property.type === 'Property') {
1221
+ let key;
1222
+ if (property.computed) {
1223
+ key = evaluate(property.key, scope);
1224
+ }
1225
+ else {
1226
+ key = property.key.name;
1227
+ }
1228
+ fedKeys.push(key);
1229
+ const value = property.value;
1230
+ if (value.type === 'Identifier') {
1231
+ scope[kind](value.name, feed[key]);
1232
+ }
1233
+ else {
1234
+ pattern$3(value, scope, { kind, feed: feed[key] });
1235
+ }
1236
+ }
1237
+ else {
1238
+ const rest = assign({}, feed);
1239
+ for (let i = 0; i < fedKeys.length; i++)
1240
+ delete rest[fedKeys[i]];
1241
+ RestElement(property, scope, { kind, feed: rest });
1242
+ }
1243
+ }
1244
+ }
1245
+ function ArrayPattern(node, scope, options = {}) {
1246
+ const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
1247
+ const result = [];
1248
+ for (let i = 0; i < node.elements.length; i++) {
1249
+ const element = node.elements[i];
1250
+ if (!element)
1251
+ continue;
1252
+ if (hoist) {
1253
+ if (onlyBlock || kind === 'var') {
1254
+ if (element.type === 'Identifier') {
1255
+ scope[kind](element.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1256
+ }
1257
+ else {
1258
+ pattern$3(element, scope, { kind, hoist, onlyBlock });
1259
+ }
1260
+ }
1261
+ }
1262
+ else if (element.type === 'Identifier') {
1263
+ if (kind) {
1264
+ scope[kind](element.name, feed[i]);
1265
+ }
1266
+ else {
1267
+ const variable = Identifier(element, scope, { getVar: true });
1268
+ variable.set(feed[i]);
1269
+ result.push(variable.get());
1270
+ }
1271
+ }
1272
+ else if (element.type === 'RestElement') {
1273
+ RestElement(element, scope, { kind, feed: feed.slice(i) });
1274
+ }
1275
+ else {
1276
+ pattern$3(element, scope, { kind, feed: feed[i] });
1277
+ }
1278
+ }
1279
+ if (result.length) {
1280
+ return result;
1281
+ }
1282
+ }
1283
+ function RestElement(node, scope, options = {}) {
1284
+ const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
1285
+ const arg = node.argument;
1286
+ if (hoist) {
1287
+ if (onlyBlock || kind === 'var') {
1288
+ if (arg.type === 'Identifier') {
1289
+ scope[kind](arg.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1290
+ }
1291
+ else {
1292
+ pattern$3(arg, scope, { kind, hoist, onlyBlock });
1293
+ }
1294
+ }
1295
+ }
1296
+ else if (arg.type === 'Identifier') {
1297
+ if (kind) {
1298
+ scope[kind](arg.name, feed);
1299
+ }
1300
+ else {
1301
+ const variable = Identifier(arg, scope, { getVar: true });
1302
+ variable.set(feed);
1303
+ }
1304
+ }
1305
+ else {
1306
+ pattern$3(arg, scope, { kind, feed });
1307
+ }
1308
+ }
1309
+ function AssignmentPattern(node, scope, options = {}) {
1310
+ const { kind = 'var', hoist = false, onlyBlock = false, feed = evaluate(node.right, scope) } = options;
1311
+ const left = node.left;
1312
+ if (hoist) {
1313
+ if (onlyBlock || kind === 'var') {
1314
+ if (left.type === 'Identifier') {
1315
+ scope[kind](left.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1316
+ }
1317
+ else {
1318
+ pattern$3(left, scope, { kind, hoist, onlyBlock });
1319
+ }
1320
+ }
1321
+ }
1322
+ else if (left.type === 'Identifier') {
1323
+ scope[kind](left.name, feed);
1324
+ }
1325
+ else {
1326
+ pattern$3(left, scope, { kind, feed });
1327
+ }
1328
+ }
1329
+
1330
+ var pattern = /*#__PURE__*/Object.freeze({
1331
+ __proto__: null,
1332
+ ObjectPattern: ObjectPattern,
1333
+ ArrayPattern: ArrayPattern,
1334
+ RestElement: RestElement,
1335
+ AssignmentPattern: AssignmentPattern
1336
+ });
1337
+
1338
+ function Program(program, scope) {
1339
+ for (let i = 0; i < program.body.length; i++) {
1340
+ evaluate(program.body[i], scope);
1341
+ }
1342
+ }
1343
+
1344
+ var program = /*#__PURE__*/Object.freeze({
1345
+ __proto__: null,
1346
+ Program: Program
1082
1347
  });
1083
1348
 
1349
+ let evaluateOps;
1350
+ function evaluate(node, scope) {
1351
+ if (!node)
1352
+ return;
1353
+ if (!evaluateOps) {
1354
+ evaluateOps = assign({}, declaration, expression, identifier, statement, literal, pattern, program);
1355
+ }
1356
+ const handler = evaluateOps[node.type];
1357
+ if (handler) {
1358
+ return handler(node, scope);
1359
+ }
1360
+ else {
1361
+ throw new Error(`${node.type} isn't implemented`);
1362
+ }
1363
+ }
1364
+
1084
1365
  function ExpressionStatement(node, scope) {
1085
1366
  evaluate(node.expression, scope);
1086
1367
  }
@@ -1267,224 +1548,36 @@
1267
1548
  }
1268
1549
  }
1269
1550
 
1270
- var statement = /*#__PURE__*/Object.freeze({
1271
- __proto__: null,
1272
- ExpressionStatement: ExpressionStatement,
1273
- BlockStatement: BlockStatement,
1274
- EmptyStatement: EmptyStatement,
1275
- DebuggerStatement: DebuggerStatement,
1276
- ReturnStatement: ReturnStatement,
1277
- BreakStatement: BreakStatement,
1278
- ContinueStatement: ContinueStatement,
1279
- IfStatement: IfStatement,
1280
- SwitchStatement: SwitchStatement,
1281
- SwitchCase: SwitchCase,
1282
- ThrowStatement: ThrowStatement,
1283
- TryStatement: TryStatement,
1284
- CatchClause: CatchClause,
1285
- WhileStatement: WhileStatement,
1286
- DoWhileStatement: DoWhileStatement,
1287
- ForStatement: ForStatement,
1288
- ForInStatement: ForInStatement,
1289
- ForOfStatement: ForOfStatement
1290
- });
1291
-
1292
- function ObjectPattern(node, scope, options = {}) {
1293
- const { kind = 'var', hoist = false, onlyBlock = false, feed = {} } = options;
1294
- const fedKeys = [];
1295
- for (let i = 0; i < node.properties.length; i++) {
1296
- const property = node.properties[i];
1297
- if (hoist) {
1298
- if (onlyBlock || kind === 'var') {
1299
- if (property.type === 'Property') {
1300
- const value = property.value;
1301
- if (value.type === 'Identifier') {
1302
- scope[kind](value.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1303
- }
1304
- else {
1305
- pattern$3(value, scope, { kind, hoist, onlyBlock });
1306
- }
1307
- }
1308
- else {
1309
- RestElement(property, scope, { kind, hoist, onlyBlock });
1310
- }
1311
- }
1312
- }
1313
- else if (property.type === 'Property') {
1314
- let key;
1315
- if (property.computed) {
1316
- key = evaluate(property.key, scope);
1551
+ function FunctionDeclaration(node, scope) {
1552
+ scope.func(node.id.name, createFunc$1(node, scope));
1553
+ }
1554
+ function VariableDeclaration(node, scope, options = {}) {
1555
+ for (let i = 0; i < node.declarations.length; i++) {
1556
+ VariableDeclarator(node.declarations[i], scope, assign({ kind: node.kind }, options));
1557
+ }
1558
+ }
1559
+ function VariableDeclarator(node, scope, options = {}) {
1560
+ const { kind = 'var', hoist = false, onlyBlock = false, feed } = options;
1561
+ if (hoist) {
1562
+ if (onlyBlock || kind === 'var') {
1563
+ if (node.id.type === 'Identifier') {
1564
+ scope[kind](node.id.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1317
1565
  }
1318
1566
  else {
1319
- key = property.key.name;
1567
+ pattern$3(node.id, scope, { kind, hoist, onlyBlock });
1320
1568
  }
1321
- fedKeys.push(key);
1322
- const value = property.value;
1323
- if (value.type === 'Identifier') {
1324
- scope[kind](value.name, feed[key]);
1569
+ }
1570
+ }
1571
+ else {
1572
+ const hasFeed = 'feed' in options;
1573
+ const value = hasFeed ? feed : evaluate(node.init, scope);
1574
+ if (node.id.type === 'Identifier') {
1575
+ const name = node.id.name;
1576
+ if (kind === 'var' && !node.init && !hasFeed) {
1577
+ scope.var(name, NOINIT);
1325
1578
  }
1326
1579
  else {
1327
- pattern$3(value, scope, { kind, feed: feed[key] });
1328
- }
1329
- }
1330
- else {
1331
- const rest = assign({}, feed);
1332
- for (let i = 0; i < fedKeys.length; i++)
1333
- delete rest[fedKeys[i]];
1334
- RestElement(property, scope, { kind, feed: rest });
1335
- }
1336
- }
1337
- }
1338
- function ArrayPattern(node, scope, options = {}) {
1339
- const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
1340
- const result = [];
1341
- for (let i = 0; i < node.elements.length; i++) {
1342
- const element = node.elements[i];
1343
- if (!element)
1344
- continue;
1345
- if (hoist) {
1346
- if (onlyBlock || kind === 'var') {
1347
- if (element.type === 'Identifier') {
1348
- scope[kind](element.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1349
- }
1350
- else {
1351
- pattern$3(element, scope, { kind, hoist, onlyBlock });
1352
- }
1353
- }
1354
- }
1355
- else if (element.type === 'Identifier') {
1356
- if (kind) {
1357
- scope[kind](element.name, feed[i]);
1358
- }
1359
- else {
1360
- const variable = Identifier(element, scope, { getVar: true });
1361
- variable.set(feed[i]);
1362
- result.push(variable.get());
1363
- }
1364
- }
1365
- else if (element.type === 'RestElement') {
1366
- RestElement(element, scope, { kind, feed: feed.slice(i) });
1367
- }
1368
- else {
1369
- pattern$3(element, scope, { kind, feed: feed[i] });
1370
- }
1371
- }
1372
- if (result.length) {
1373
- return result;
1374
- }
1375
- }
1376
- function RestElement(node, scope, options = {}) {
1377
- const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
1378
- const arg = node.argument;
1379
- if (hoist) {
1380
- if (onlyBlock || kind === 'var') {
1381
- if (arg.type === 'Identifier') {
1382
- scope[kind](arg.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1383
- }
1384
- else {
1385
- pattern$3(arg, scope, { kind, hoist, onlyBlock });
1386
- }
1387
- }
1388
- }
1389
- else if (arg.type === 'Identifier') {
1390
- if (kind) {
1391
- scope[kind](arg.name, feed);
1392
- }
1393
- else {
1394
- const variable = Identifier(arg, scope, { getVar: true });
1395
- variable.set(feed);
1396
- }
1397
- }
1398
- else {
1399
- pattern$3(arg, scope, { kind, feed });
1400
- }
1401
- }
1402
- function AssignmentPattern(node, scope, options = {}) {
1403
- const { kind = 'var', hoist = false, onlyBlock = false, feed = evaluate(node.right, scope) } = options;
1404
- const left = node.left;
1405
- if (hoist) {
1406
- if (onlyBlock || kind === 'var') {
1407
- if (left.type === 'Identifier') {
1408
- scope[kind](left.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1409
- }
1410
- else {
1411
- pattern$3(left, scope, { kind, hoist, onlyBlock });
1412
- }
1413
- }
1414
- }
1415
- else if (left.type === 'Identifier') {
1416
- scope[kind](left.name, feed);
1417
- }
1418
- else {
1419
- pattern$3(left, scope, { kind, feed });
1420
- }
1421
- }
1422
-
1423
- var pattern = /*#__PURE__*/Object.freeze({
1424
- __proto__: null,
1425
- ObjectPattern: ObjectPattern,
1426
- ArrayPattern: ArrayPattern,
1427
- RestElement: RestElement,
1428
- AssignmentPattern: AssignmentPattern
1429
- });
1430
-
1431
- function Program(program, scope) {
1432
- for (let i = 0; i < program.body.length; i++) {
1433
- evaluate(program.body[i], scope);
1434
- }
1435
- }
1436
-
1437
- var program = /*#__PURE__*/Object.freeze({
1438
- __proto__: null,
1439
- Program: Program
1440
- });
1441
-
1442
- let evaluateOps;
1443
- function evaluate(node, scope) {
1444
- if (!node)
1445
- return;
1446
- if (!evaluateOps) {
1447
- evaluateOps = assign({}, declaration, expression, identifier, statement, literal, pattern, program);
1448
- }
1449
- const handler = evaluateOps[node.type];
1450
- if (handler) {
1451
- return handler(node, scope);
1452
- }
1453
- else {
1454
- throw new Error(`${node.type} isn't implemented`);
1455
- }
1456
- }
1457
-
1458
- function FunctionDeclaration(node, scope) {
1459
- scope.func(node.id.name, createFunc$1(node, scope));
1460
- }
1461
- function VariableDeclaration(node, scope, options = {}) {
1462
- for (let i = 0; i < node.declarations.length; i++) {
1463
- VariableDeclarator(node.declarations[i], scope, assign({ kind: node.kind }, options));
1464
- }
1465
- }
1466
- function VariableDeclarator(node, scope, options = {}) {
1467
- const { kind = 'var', hoist = false, onlyBlock = false, feed } = options;
1468
- if (hoist) {
1469
- if (onlyBlock || kind === 'var') {
1470
- if (node.id.type === 'Identifier') {
1471
- scope[kind](node.id.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
1472
- }
1473
- else {
1474
- pattern$3(node.id, scope, { kind, hoist, onlyBlock });
1475
- }
1476
- }
1477
- }
1478
- else {
1479
- const hasFeed = 'feed' in options;
1480
- const value = hasFeed ? feed : evaluate(node.init, scope);
1481
- if (node.id.type === 'Identifier') {
1482
- const name = node.id.name;
1483
- if (kind === 'var' && !node.init && !hasFeed) {
1484
- scope.var(name, NOINIT);
1485
- }
1486
- else {
1487
- scope[kind](name, value);
1580
+ scope[kind](name, value);
1488
1581
  }
1489
1582
  if (node.init
1490
1583
  && ['ClassExpression', 'FunctionExpression', 'ArrowFunctionExpression']
@@ -1511,21 +1604,38 @@
1511
1604
  if (def.type === 'MethodDefinition') {
1512
1605
  MethodDefinition(def, scope, { klass, superClass });
1513
1606
  }
1607
+ else if (def.type === 'PropertyDefinition' && def.static) {
1608
+ PropertyDefinition(def, scope, { klass, superClass });
1609
+ }
1610
+ else if (def.type === 'StaticBlock') {
1611
+ StaticBlock(def, scope, { klass, superClass });
1612
+ }
1514
1613
  }
1515
1614
  }
1516
1615
  function MethodDefinition(node, scope, options = {}) {
1517
1616
  const { klass, superClass } = options;
1518
1617
  let key;
1618
+ let priv = false;
1519
1619
  if (node.computed) {
1520
1620
  key = evaluate(node.key, scope);
1521
1621
  }
1522
1622
  else if (node.key.type === 'Identifier') {
1523
1623
  key = node.key.name;
1524
1624
  }
1625
+ else if (node.key.type === 'PrivateIdentifier') {
1626
+ key = node.key.name;
1627
+ priv = true;
1628
+ }
1525
1629
  else {
1526
1630
  throw new SyntaxError('Unexpected token');
1527
1631
  }
1528
- const obj = node.static ? klass : klass.prototype;
1632
+ let obj = node.static ? klass : klass.prototype;
1633
+ if (priv) {
1634
+ if (!obj[PRIVATE]) {
1635
+ define(obj, PRIVATE, { value: {} });
1636
+ }
1637
+ obj = obj[PRIVATE];
1638
+ }
1529
1639
  const value = createFunc$1(node.value, scope, { superClass });
1530
1640
  switch (node.kind) {
1531
1641
  case 'constructor':
@@ -1558,6 +1668,191 @@
1558
1668
  default:
1559
1669
  throw new SyntaxError('Unexpected token');
1560
1670
  }
1671
+ }
1672
+ function PropertyDefinition(node, scope, options = {}) {
1673
+ const { klass, superClass } = options;
1674
+ let key;
1675
+ let priv = false;
1676
+ if (node.computed) {
1677
+ key = evaluate(node.key, scope);
1678
+ }
1679
+ else if (node.key.type === 'Identifier') {
1680
+ key = node.key.name;
1681
+ }
1682
+ else if (node.key.type === 'PrivateIdentifier') {
1683
+ key = node.key.name;
1684
+ priv = true;
1685
+ }
1686
+ else {
1687
+ throw new SyntaxError('Unexpected token');
1688
+ }
1689
+ const subScope = new Scope(scope, true);
1690
+ subScope.const('this', klass);
1691
+ let obj = klass;
1692
+ if (priv) {
1693
+ if (!obj[PRIVATE]) {
1694
+ define(obj, PRIVATE, { value: {} });
1695
+ }
1696
+ obj = obj[PRIVATE];
1697
+ }
1698
+ if (node.value.type === 'FunctionExpression' || node.value.type === 'ArrowFunctionExpression') {
1699
+ obj[key] = createFunc$1(node.value, subScope, { superClass });
1700
+ }
1701
+ else {
1702
+ obj[key] = evaluate(node.value, subScope);
1703
+ }
1704
+ }
1705
+ function StaticBlock(node, scope, options = {}) {
1706
+ const { klass } = options;
1707
+ const subScope = new Scope(scope, true);
1708
+ subScope.const('this', klass);
1709
+ return BlockStatement(node, subScope, { invasived: true });
1710
+ }
1711
+ function ImportDeclaration(node, scope) {
1712
+ const globalScope = scope.global();
1713
+ const module = globalScope.find(IMPORT + node.source.value);
1714
+ let value;
1715
+ if (module) {
1716
+ const result = module.get();
1717
+ if (result) {
1718
+ if (typeof result === 'function') {
1719
+ value = result();
1720
+ }
1721
+ else if (typeof result === 'object') {
1722
+ value = result;
1723
+ }
1724
+ }
1725
+ }
1726
+ if (!value || typeof value !== 'object') {
1727
+ throw new TypeError(`Failed to resolve module specifier "${node.source.value}"`);
1728
+ }
1729
+ for (let i = 0; i < node.specifiers.length; i++) {
1730
+ const spec = node.specifiers[i];
1731
+ let name;
1732
+ if (spec.type === 'ImportSpecifier') {
1733
+ name = spec.imported.type === 'Identifier'
1734
+ ? spec.imported.name : spec.imported.value;
1735
+ }
1736
+ else if (spec.type === 'ImportDefaultSpecifier') {
1737
+ name = 'default';
1738
+ }
1739
+ else if (spec.type === 'ImportNamespaceSpecifier') {
1740
+ name = '*';
1741
+ }
1742
+ if (name !== '*' && !hasOwn(value, name)) {
1743
+ throw new SyntaxError(`The requested module "${node.source.value}" does not provide an export named "${name}"`);
1744
+ }
1745
+ scope.var(spec.local.name, name === '*' ? assign({}, value) : value[name]);
1746
+ }
1747
+ }
1748
+ function ExportDefaultDeclaration(node, scope) {
1749
+ const globalScope = scope.global();
1750
+ let value;
1751
+ if (node.declaration.type === 'FunctionDeclaration') {
1752
+ value = createFunc$1(node.declaration, scope);
1753
+ scope.func(node.declaration.id.name, value);
1754
+ }
1755
+ else if (node.declaration.type === 'ClassDeclaration') {
1756
+ value = createClass$1(node.declaration, scope);
1757
+ scope.func(node.declaration.id.name, value);
1758
+ }
1759
+ else {
1760
+ value = evaluate(node.declaration, scope);
1761
+ }
1762
+ const variable = globalScope.find(EXPORTS);
1763
+ if (variable) {
1764
+ const exports = variable.get();
1765
+ if (exports && typeof exports === 'object') {
1766
+ exports.default = value;
1767
+ }
1768
+ }
1769
+ }
1770
+ function ExportNamedDeclaration(node, scope) {
1771
+ const globalScope = scope.global();
1772
+ if (node.declaration) {
1773
+ if (node.declaration.type === 'FunctionDeclaration') {
1774
+ const value = createFunc$1(node.declaration, scope);
1775
+ scope.func(node.declaration.id.name, value);
1776
+ const variable = globalScope.find(EXPORTS);
1777
+ if (variable) {
1778
+ const exports = variable.get();
1779
+ if (exports && typeof exports === 'object') {
1780
+ exports[node.declaration.id.name] = value;
1781
+ }
1782
+ }
1783
+ }
1784
+ else if (node.declaration.type === 'ClassDeclaration') {
1785
+ const value = createClass$1(node.declaration, scope);
1786
+ scope.func(node.declaration.id.name, value);
1787
+ const variable = globalScope.find(EXPORTS);
1788
+ if (variable) {
1789
+ const exports = variable.get();
1790
+ if (exports && typeof exports === 'object') {
1791
+ exports[node.declaration.id.name] = value;
1792
+ }
1793
+ }
1794
+ }
1795
+ else if (node.declaration.type === 'VariableDeclaration') {
1796
+ VariableDeclaration(node.declaration, scope);
1797
+ const variable = globalScope.find(EXPORTS);
1798
+ if (variable) {
1799
+ const exports = variable.get();
1800
+ if (exports && typeof exports === 'object') {
1801
+ for (let i = 0; i < node.declaration.declarations.length; i++) {
1802
+ const name = node.declaration.declarations[i].id.name;
1803
+ const item = scope.find(name);
1804
+ if (item) {
1805
+ exports[name] = item.get();
1806
+ }
1807
+ }
1808
+ }
1809
+ }
1810
+ }
1811
+ }
1812
+ else if (node.specifiers) {
1813
+ const variable = globalScope.find(EXPORTS);
1814
+ if (variable) {
1815
+ const exports = variable.get();
1816
+ if (exports && typeof exports === 'object') {
1817
+ for (let i = 0; i < node.specifiers.length; i++) {
1818
+ const spec = node.specifiers[i];
1819
+ const name = spec.local.type === 'Identifier'
1820
+ ? spec.local.name : spec.local.value;
1821
+ const item = scope.find(name);
1822
+ if (item) {
1823
+ exports[spec.exported.type === 'Identifier'
1824
+ ? spec.exported.name : spec.exported.value] = item.get();
1825
+ }
1826
+ }
1827
+ }
1828
+ }
1829
+ }
1830
+ }
1831
+ function ExportAllDeclaration(node, scope) {
1832
+ const globalScope = scope.global();
1833
+ const module = globalScope.find(IMPORT + node.source.value);
1834
+ let value;
1835
+ if (module) {
1836
+ const result = module.get();
1837
+ if (result) {
1838
+ if (typeof result === 'function') {
1839
+ value = result();
1840
+ }
1841
+ else if (typeof result === 'object') {
1842
+ value = result;
1843
+ }
1844
+ }
1845
+ }
1846
+ if (!value || typeof value !== 'object') {
1847
+ throw new TypeError(`Failed to resolve module specifier "${node.source.value}"`);
1848
+ }
1849
+ const variable = globalScope.find(EXPORTS);
1850
+ if (variable) {
1851
+ const exports = variable.get();
1852
+ if (exports && typeof exports === 'object') {
1853
+ assign(exports, value);
1854
+ }
1855
+ }
1561
1856
  }
1562
1857
 
1563
1858
  function* Identifier$1(node, scope, options = {}) {
@@ -1740,8 +2035,17 @@
1740
2035
  }
1741
2036
  }
1742
2037
  function* BinaryExpression$1(node, scope) {
1743
- const left = yield* evaluate$1(node.left, scope);
1744
- const right = yield* evaluate$1(node.right, scope);
2038
+ let left;
2039
+ let right;
2040
+ if (node.left.type === 'PrivateIdentifier') {
2041
+ left = node.left.name;
2042
+ right = yield* evaluate$1(node.right, scope);
2043
+ right = right[PRIVATE];
2044
+ }
2045
+ else {
2046
+ left = yield* evaluate$1(node.left, scope);
2047
+ right = yield* evaluate$1(node.right, scope);
2048
+ }
1745
2049
  switch (node.operator) {
1746
2050
  case '==': return left == right;
1747
2051
  case '!=': return left != right;
@@ -1864,12 +2168,20 @@
1864
2168
  if (getObj)
1865
2169
  return object;
1866
2170
  let key;
2171
+ let priv = false;
1867
2172
  if (node.computed) {
1868
2173
  key = yield* evaluate$1(node.property, scope);
1869
2174
  }
2175
+ else if (node.property.type === 'PrivateIdentifier') {
2176
+ key = node.property.name;
2177
+ priv = true;
2178
+ }
1870
2179
  else {
1871
2180
  key = node.property.name;
1872
2181
  }
2182
+ if (priv) {
2183
+ object = object[PRIVATE];
2184
+ }
1873
2185
  if (getVar) {
1874
2186
  const setter = getSetter(object, key);
1875
2187
  if (node.object.type === 'Super' && setter) {
@@ -1913,18 +2225,27 @@
1913
2225
  return undefined;
1914
2226
  }
1915
2227
  let key;
2228
+ let priv = false;
1916
2229
  if (node.callee.computed) {
1917
2230
  key = yield* evaluate$1(node.callee.property, scope);
1918
2231
  }
2232
+ else if (node.callee.property.type === 'PrivateIdentifier') {
2233
+ key = node.callee.property.name;
2234
+ priv = true;
2235
+ }
1919
2236
  else {
1920
2237
  key = node.callee.property.name;
1921
2238
  }
2239
+ let obj = object;
2240
+ if (priv) {
2241
+ obj = obj[PRIVATE];
2242
+ }
1922
2243
  if (node.callee.object.type === 'Super') {
1923
2244
  const thisObject = scope.find('this').get();
1924
- func = object[key].bind(thisObject);
2245
+ func = obj[key].bind(thisObject);
1925
2246
  }
1926
2247
  else {
1927
- func = object[key];
2248
+ func = obj[key];
1928
2249
  }
1929
2250
  if (node.optional && func == null) {
1930
2251
  return undefined;
@@ -2020,7 +2341,12 @@
2020
2341
  return new constructor(...args);
2021
2342
  }
2022
2343
  function* MetaProperty$1(node, scope) {
2023
- return scope.find(NEWTARGET).get();
2344
+ if (node.meta.name === 'new' && node.property.name === 'target') {
2345
+ return scope.find(NEWTARGET).get();
2346
+ }
2347
+ else if (node.meta.name === 'import' && node.property.name === 'meta') {
2348
+ return { url: '' };
2349
+ }
2024
2350
  }
2025
2351
  function* SequenceExpression$1(node, scope) {
2026
2352
  let result;
@@ -2084,11 +2410,33 @@
2084
2410
  return getProto ? superClass.prototype : superClass;
2085
2411
  }
2086
2412
  function* SpreadElement$1(node, scope) {
2087
- return yield* evaluate$1(node.argument, scope);
2413
+ const result = yield* evaluate$1(node.argument, scope);
2414
+ return typeof result === 'string' ? [...result] : result;
2088
2415
  }
2089
2416
  function* ChainExpression$1(node, scope) {
2090
2417
  return yield* evaluate$1(node.expression, scope);
2091
2418
  }
2419
+ function* ImportExpression$1(node, scope) {
2420
+ const globalScope = scope.global();
2421
+ const source = yield* evaluate$1(node.source, scope);
2422
+ const module = globalScope.find(IMPORT + source);
2423
+ let value;
2424
+ if (module) {
2425
+ const result = module.get();
2426
+ if (result) {
2427
+ if (typeof result === 'function') {
2428
+ value = result();
2429
+ }
2430
+ else if (typeof result === 'object') {
2431
+ value = result;
2432
+ }
2433
+ }
2434
+ }
2435
+ if (!value || typeof value !== 'object') {
2436
+ return Promise.reject(new TypeError(`Failed to resolve module specifier "${source}"`));
2437
+ }
2438
+ return Promise.resolve(value);
2439
+ }
2092
2440
  function* YieldExpression(node, scope) {
2093
2441
  const res = yield* evaluate$1(node.argument, scope);
2094
2442
  return node.delegate ? yield* res : yield res;
@@ -2123,10 +2471,166 @@
2123
2471
  Super: Super$1,
2124
2472
  SpreadElement: SpreadElement$1,
2125
2473
  ChainExpression: ChainExpression$1,
2474
+ ImportExpression: ImportExpression$1,
2126
2475
  YieldExpression: YieldExpression,
2127
2476
  AwaitExpression: AwaitExpression
2128
2477
  });
2129
2478
 
2479
+ function* ObjectPattern$1(node, scope, options = {}) {
2480
+ const { kind = 'var', hoist = false, onlyBlock = false, feed = {} } = options;
2481
+ const fedKeys = [];
2482
+ for (let i = 0; i < node.properties.length; i++) {
2483
+ const property = node.properties[i];
2484
+ if (hoist) {
2485
+ if (onlyBlock || kind === 'var') {
2486
+ if (property.type === 'Property') {
2487
+ const value = property.value;
2488
+ if (value.type === 'Identifier') {
2489
+ scope[kind](value.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2490
+ }
2491
+ else {
2492
+ yield* pattern$2(value, scope, { kind, hoist, onlyBlock });
2493
+ }
2494
+ }
2495
+ else {
2496
+ yield* RestElement$1(property, scope, { kind, hoist, onlyBlock });
2497
+ }
2498
+ }
2499
+ }
2500
+ else if (property.type === 'Property') {
2501
+ let key;
2502
+ if (property.computed) {
2503
+ key = yield* evaluate$1(property.key, scope);
2504
+ }
2505
+ else {
2506
+ key = property.key.name;
2507
+ }
2508
+ fedKeys.push(key);
2509
+ const value = property.value;
2510
+ if (value.type === 'Identifier') {
2511
+ scope[kind](value.name, feed[key]);
2512
+ }
2513
+ else {
2514
+ yield* pattern$2(value, scope, { kind, feed: feed[key] });
2515
+ }
2516
+ }
2517
+ else {
2518
+ const rest = assign({}, feed);
2519
+ for (let i = 0; i < fedKeys.length; i++)
2520
+ delete rest[fedKeys[i]];
2521
+ yield* RestElement$1(property, scope, { kind, feed: rest });
2522
+ }
2523
+ }
2524
+ }
2525
+ function* ArrayPattern$1(node, scope, options = {}) {
2526
+ const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
2527
+ const result = [];
2528
+ for (let i = 0; i < node.elements.length; i++) {
2529
+ const element = node.elements[i];
2530
+ if (!element)
2531
+ continue;
2532
+ if (hoist) {
2533
+ if (onlyBlock || kind === 'var') {
2534
+ if (element.type === 'Identifier') {
2535
+ scope[kind](element.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2536
+ }
2537
+ else {
2538
+ yield* pattern$2(element, scope, { kind, hoist, onlyBlock });
2539
+ }
2540
+ }
2541
+ }
2542
+ else if (element.type === 'Identifier') {
2543
+ if (kind) {
2544
+ scope[kind](element.name, feed[i]);
2545
+ }
2546
+ else {
2547
+ const variable = yield* Identifier$1(element, scope, { getVar: true });
2548
+ variable.set(feed[i]);
2549
+ result.push(variable.get());
2550
+ }
2551
+ }
2552
+ else if (element.type === 'RestElement') {
2553
+ yield* RestElement$1(element, scope, { kind, feed: feed.slice(i) });
2554
+ }
2555
+ else {
2556
+ yield* pattern$2(element, scope, { kind, feed: feed[i] });
2557
+ }
2558
+ }
2559
+ if (result.length) {
2560
+ return result;
2561
+ }
2562
+ }
2563
+ function* RestElement$1(node, scope, options = {}) {
2564
+ const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
2565
+ const arg = node.argument;
2566
+ if (hoist) {
2567
+ if (onlyBlock || kind === 'var') {
2568
+ if (arg.type === 'Identifier') {
2569
+ scope[kind](arg.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2570
+ }
2571
+ else {
2572
+ yield* pattern$2(arg, scope, { kind, hoist, onlyBlock });
2573
+ }
2574
+ }
2575
+ }
2576
+ else if (arg.type === 'Identifier') {
2577
+ if (kind) {
2578
+ scope[kind](arg.name, feed);
2579
+ }
2580
+ else {
2581
+ const variable = yield* Identifier$1(arg, scope, { getVar: true });
2582
+ variable.set(feed);
2583
+ }
2584
+ }
2585
+ else {
2586
+ yield* pattern$2(arg, scope, { kind, feed });
2587
+ }
2588
+ }
2589
+ function* AssignmentPattern$1(node, scope, options = {}) {
2590
+ const { kind = 'var', hoist = false, onlyBlock = false, feed = yield* evaluate$1(node.right, scope) } = options;
2591
+ const left = node.left;
2592
+ if (hoist) {
2593
+ if (onlyBlock || kind === 'var') {
2594
+ if (left.type === 'Identifier') {
2595
+ scope[kind](left.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2596
+ }
2597
+ else {
2598
+ yield* pattern$2(left, scope, { kind, hoist, onlyBlock });
2599
+ }
2600
+ }
2601
+ }
2602
+ else if (left.type === 'Identifier') {
2603
+ scope[kind](left.name, feed);
2604
+ }
2605
+ else {
2606
+ yield* pattern$2(left, scope, { kind, feed });
2607
+ }
2608
+ }
2609
+
2610
+ var pattern$1 = /*#__PURE__*/Object.freeze({
2611
+ __proto__: null,
2612
+ ObjectPattern: ObjectPattern$1,
2613
+ ArrayPattern: ArrayPattern$1,
2614
+ RestElement: RestElement$1,
2615
+ AssignmentPattern: AssignmentPattern$1
2616
+ });
2617
+
2618
+ let evaluateOps$1;
2619
+ function* evaluate$1(node, scope) {
2620
+ if (!node)
2621
+ return;
2622
+ if (!evaluateOps$1) {
2623
+ evaluateOps$1 = assign({}, declaration$1, expression$1, identifier$1, statement$1, literal$1, pattern$1);
2624
+ }
2625
+ const handler = evaluateOps$1[node.type];
2626
+ if (handler) {
2627
+ return yield* handler(node, scope);
2628
+ }
2629
+ else {
2630
+ throw new Error(`${node.type} isn't implemented`);
2631
+ }
2632
+ }
2633
+
2130
2634
  function* ExpressionStatement$1(node, scope) {
2131
2635
  yield* evaluate$1(node.expression, scope);
2132
2636
  }
@@ -2331,183 +2835,6 @@
2331
2835
  }
2332
2836
  }
2333
2837
 
2334
- var statement$1 = /*#__PURE__*/Object.freeze({
2335
- __proto__: null,
2336
- ExpressionStatement: ExpressionStatement$1,
2337
- BlockStatement: BlockStatement$1,
2338
- EmptyStatement: EmptyStatement$1,
2339
- DebuggerStatement: DebuggerStatement$1,
2340
- ReturnStatement: ReturnStatement$1,
2341
- BreakStatement: BreakStatement$1,
2342
- ContinueStatement: ContinueStatement$1,
2343
- IfStatement: IfStatement$1,
2344
- SwitchStatement: SwitchStatement$1,
2345
- SwitchCase: SwitchCase$1,
2346
- ThrowStatement: ThrowStatement$1,
2347
- TryStatement: TryStatement$1,
2348
- CatchClause: CatchClause$1,
2349
- WhileStatement: WhileStatement$1,
2350
- DoWhileStatement: DoWhileStatement$1,
2351
- ForStatement: ForStatement$1,
2352
- ForInStatement: ForInStatement$1,
2353
- ForOfStatement: ForOfStatement$1
2354
- });
2355
-
2356
- function* ObjectPattern$1(node, scope, options = {}) {
2357
- const { kind = 'var', hoist = false, onlyBlock = false, feed = {} } = options;
2358
- const fedKeys = [];
2359
- for (let i = 0; i < node.properties.length; i++) {
2360
- const property = node.properties[i];
2361
- if (hoist) {
2362
- if (onlyBlock || kind === 'var') {
2363
- if (property.type === 'Property') {
2364
- const value = property.value;
2365
- if (value.type === 'Identifier') {
2366
- scope[kind](value.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2367
- }
2368
- else {
2369
- yield* pattern$2(value, scope, { kind, hoist, onlyBlock });
2370
- }
2371
- }
2372
- else {
2373
- yield* RestElement$1(property, scope, { kind, hoist, onlyBlock });
2374
- }
2375
- }
2376
- }
2377
- else if (property.type === 'Property') {
2378
- let key;
2379
- if (property.computed) {
2380
- key = yield* evaluate$1(property.key, scope);
2381
- }
2382
- else {
2383
- key = property.key.name;
2384
- }
2385
- fedKeys.push(key);
2386
- const value = property.value;
2387
- if (value.type === 'Identifier') {
2388
- scope[kind](value.name, feed[key]);
2389
- }
2390
- else {
2391
- yield* pattern$2(value, scope, { kind, feed: feed[key] });
2392
- }
2393
- }
2394
- else {
2395
- const rest = assign({}, feed);
2396
- for (let i = 0; i < fedKeys.length; i++)
2397
- delete rest[fedKeys[i]];
2398
- yield* RestElement$1(property, scope, { kind, feed: rest });
2399
- }
2400
- }
2401
- }
2402
- function* ArrayPattern$1(node, scope, options = {}) {
2403
- const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
2404
- const result = [];
2405
- for (let i = 0; i < node.elements.length; i++) {
2406
- const element = node.elements[i];
2407
- if (!element)
2408
- continue;
2409
- if (hoist) {
2410
- if (onlyBlock || kind === 'var') {
2411
- if (element.type === 'Identifier') {
2412
- scope[kind](element.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2413
- }
2414
- else {
2415
- yield* pattern$2(element, scope, { kind, hoist, onlyBlock });
2416
- }
2417
- }
2418
- }
2419
- else if (element.type === 'Identifier') {
2420
- if (kind) {
2421
- scope[kind](element.name, feed[i]);
2422
- }
2423
- else {
2424
- const variable = yield* Identifier$1(element, scope, { getVar: true });
2425
- variable.set(feed[i]);
2426
- result.push(variable.get());
2427
- }
2428
- }
2429
- else if (element.type === 'RestElement') {
2430
- yield* RestElement$1(element, scope, { kind, feed: feed.slice(i) });
2431
- }
2432
- else {
2433
- yield* pattern$2(element, scope, { kind, feed: feed[i] });
2434
- }
2435
- }
2436
- if (result.length) {
2437
- return result;
2438
- }
2439
- }
2440
- function* RestElement$1(node, scope, options = {}) {
2441
- const { kind, hoist = false, onlyBlock = false, feed = [] } = options;
2442
- const arg = node.argument;
2443
- if (hoist) {
2444
- if (onlyBlock || kind === 'var') {
2445
- if (arg.type === 'Identifier') {
2446
- scope[kind](arg.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2447
- }
2448
- else {
2449
- yield* pattern$2(arg, scope, { kind, hoist, onlyBlock });
2450
- }
2451
- }
2452
- }
2453
- else if (arg.type === 'Identifier') {
2454
- if (kind) {
2455
- scope[kind](arg.name, feed);
2456
- }
2457
- else {
2458
- const variable = yield* Identifier$1(arg, scope, { getVar: true });
2459
- variable.set(feed);
2460
- }
2461
- }
2462
- else {
2463
- yield* pattern$2(arg, scope, { kind, feed });
2464
- }
2465
- }
2466
- function* AssignmentPattern$1(node, scope, options = {}) {
2467
- const { kind = 'var', hoist = false, onlyBlock = false, feed = yield* evaluate$1(node.right, scope) } = options;
2468
- const left = node.left;
2469
- if (hoist) {
2470
- if (onlyBlock || kind === 'var') {
2471
- if (left.type === 'Identifier') {
2472
- scope[kind](left.name, onlyBlock ? DEADZONE : kind === 'var' ? NOINIT : undefined);
2473
- }
2474
- else {
2475
- yield* pattern$2(left, scope, { kind, hoist, onlyBlock });
2476
- }
2477
- }
2478
- }
2479
- else if (left.type === 'Identifier') {
2480
- scope[kind](left.name, feed);
2481
- }
2482
- else {
2483
- yield* pattern$2(left, scope, { kind, feed });
2484
- }
2485
- }
2486
-
2487
- var pattern$1 = /*#__PURE__*/Object.freeze({
2488
- __proto__: null,
2489
- ObjectPattern: ObjectPattern$1,
2490
- ArrayPattern: ArrayPattern$1,
2491
- RestElement: RestElement$1,
2492
- AssignmentPattern: AssignmentPattern$1
2493
- });
2494
-
2495
- let evaluateOps$1;
2496
- function* evaluate$1(node, scope) {
2497
- if (!node)
2498
- return;
2499
- if (!evaluateOps$1) {
2500
- evaluateOps$1 = assign({}, declaration$1, expression$1, identifier$1, statement$1, literal$1, pattern$1);
2501
- }
2502
- const handler = evaluateOps$1[node.type];
2503
- if (handler) {
2504
- return yield* handler(node, scope);
2505
- }
2506
- else {
2507
- throw new Error(`${node.type} isn't implemented`);
2508
- }
2509
- }
2510
-
2511
2838
  function* FunctionDeclaration$1(node, scope) {
2512
2839
  scope.func(node.id.name, createFunc(node, scope));
2513
2840
  }
@@ -2564,21 +2891,38 @@
2564
2891
  if (def.type === 'MethodDefinition') {
2565
2892
  yield* MethodDefinition$1(def, scope, { klass, superClass });
2566
2893
  }
2894
+ else if (def.type === 'PropertyDefinition' && def.static) {
2895
+ yield* PropertyDefinition$1(def, scope, { klass, superClass });
2896
+ }
2897
+ else if (def.type === 'StaticBlock') {
2898
+ yield* StaticBlock$1(def, scope, { klass, superClass });
2899
+ }
2567
2900
  }
2568
2901
  }
2569
2902
  function* MethodDefinition$1(node, scope, options = {}) {
2570
2903
  const { klass, superClass } = options;
2571
2904
  let key;
2905
+ let priv = false;
2572
2906
  if (node.computed) {
2573
2907
  key = yield* evaluate$1(node.key, scope);
2574
2908
  }
2575
2909
  else if (node.key.type === 'Identifier') {
2576
2910
  key = node.key.name;
2577
2911
  }
2912
+ else if (node.key.type === 'PrivateIdentifier') {
2913
+ key = node.key.name;
2914
+ priv = true;
2915
+ }
2578
2916
  else {
2579
2917
  throw new SyntaxError('Unexpected token');
2580
2918
  }
2581
- const obj = node.static ? klass : klass.prototype;
2919
+ let obj = node.static ? klass : klass.prototype;
2920
+ if (priv) {
2921
+ if (!obj[PRIVATE]) {
2922
+ define(obj, PRIVATE, { value: {} });
2923
+ }
2924
+ obj = obj[PRIVATE];
2925
+ }
2582
2926
  const value = createFunc(node.value, scope, { superClass });
2583
2927
  switch (node.kind) {
2584
2928
  case 'constructor':
@@ -2611,6 +2955,191 @@
2611
2955
  default:
2612
2956
  throw new SyntaxError('Unexpected token');
2613
2957
  }
2958
+ }
2959
+ function* PropertyDefinition$1(node, scope, options = {}) {
2960
+ const { klass, superClass } = options;
2961
+ let key;
2962
+ let priv = false;
2963
+ if (node.computed) {
2964
+ key = yield* evaluate$1(node.key, scope);
2965
+ }
2966
+ else if (node.key.type === 'Identifier') {
2967
+ key = node.key.name;
2968
+ }
2969
+ else if (node.key.type === 'PrivateIdentifier') {
2970
+ key = node.key.name;
2971
+ priv = true;
2972
+ }
2973
+ else {
2974
+ throw new SyntaxError('Unexpected token');
2975
+ }
2976
+ const subScope = new Scope(scope, true);
2977
+ subScope.const('this', klass);
2978
+ let obj = klass;
2979
+ if (priv) {
2980
+ if (!obj[PRIVATE]) {
2981
+ define(obj, PRIVATE, { value: {} });
2982
+ }
2983
+ obj = obj[PRIVATE];
2984
+ }
2985
+ if (node.value.type === 'FunctionExpression' || node.value.type === 'ArrowFunctionExpression') {
2986
+ obj[key] = createFunc(node.value, subScope, { superClass });
2987
+ }
2988
+ else {
2989
+ obj[key] = yield* evaluate$1(node.value, subScope);
2990
+ }
2991
+ }
2992
+ function* StaticBlock$1(node, scope, options = {}) {
2993
+ const { klass } = options;
2994
+ const subScope = new Scope(scope, true);
2995
+ subScope.const('this', klass);
2996
+ return yield* BlockStatement$1(node, subScope, { invasived: true });
2997
+ }
2998
+ function* ImportDeclaration$1(node, scope) {
2999
+ const globalScope = scope.global();
3000
+ const module = globalScope.find(IMPORT + node.source.value);
3001
+ let value;
3002
+ if (module) {
3003
+ const result = module.get();
3004
+ if (result) {
3005
+ if (typeof result === 'function') {
3006
+ value = result();
3007
+ }
3008
+ else if (typeof result === 'object') {
3009
+ value = result;
3010
+ }
3011
+ }
3012
+ }
3013
+ if (!value || typeof value !== 'object') {
3014
+ throw new TypeError(`Failed to resolve module specifier "${node.source.value}"`);
3015
+ }
3016
+ for (let i = 0; i < node.specifiers.length; i++) {
3017
+ const spec = node.specifiers[i];
3018
+ let name;
3019
+ if (spec.type === 'ImportSpecifier') {
3020
+ name = spec.imported.type === 'Identifier'
3021
+ ? spec.imported.name : spec.imported.value;
3022
+ }
3023
+ else if (spec.type === 'ImportDefaultSpecifier') {
3024
+ name = 'default';
3025
+ }
3026
+ else if (spec.type === 'ImportNamespaceSpecifier') {
3027
+ name = '*';
3028
+ }
3029
+ if (name !== '*' && !hasOwn(value, name)) {
3030
+ throw new SyntaxError(`The requested module "${node.source.value}" does not provide an export named "${name}"`);
3031
+ }
3032
+ scope.var(spec.local.name, name === '*' ? assign({}, value) : value[name]);
3033
+ }
3034
+ }
3035
+ function* ExportDefaultDeclaration$1(node, scope) {
3036
+ const globalScope = scope.global();
3037
+ let value;
3038
+ if (node.declaration.type === 'FunctionDeclaration') {
3039
+ value = createFunc(node.declaration, scope);
3040
+ scope.func(node.declaration.id.name, value);
3041
+ }
3042
+ else if (node.declaration.type === 'ClassDeclaration') {
3043
+ value = yield* createClass(node.declaration, scope);
3044
+ scope.func(node.declaration.id.name, value);
3045
+ }
3046
+ else {
3047
+ value = yield* evaluate$1(node.declaration, scope);
3048
+ }
3049
+ const variable = globalScope.find(EXPORTS);
3050
+ if (variable) {
3051
+ const exports = variable.get();
3052
+ if (exports && typeof exports === 'object') {
3053
+ exports.default = value;
3054
+ }
3055
+ }
3056
+ }
3057
+ function* ExportNamedDeclaration$1(node, scope) {
3058
+ const globalScope = scope.global();
3059
+ if (node.declaration) {
3060
+ if (node.declaration.type === 'FunctionDeclaration') {
3061
+ const value = createFunc(node.declaration, scope);
3062
+ scope.func(node.declaration.id.name, value);
3063
+ const variable = globalScope.find(EXPORTS);
3064
+ if (variable) {
3065
+ const exports = variable.get();
3066
+ if (exports && typeof exports === 'object') {
3067
+ exports[node.declaration.id.name] = value;
3068
+ }
3069
+ }
3070
+ }
3071
+ else if (node.declaration.type === 'ClassDeclaration') {
3072
+ const value = yield* createClass(node.declaration, scope);
3073
+ scope.func(node.declaration.id.name, value);
3074
+ const variable = globalScope.find(EXPORTS);
3075
+ if (variable) {
3076
+ const exports = variable.get();
3077
+ if (exports && typeof exports === 'object') {
3078
+ exports[node.declaration.id.name] = value;
3079
+ }
3080
+ }
3081
+ }
3082
+ else if (node.declaration.type === 'VariableDeclaration') {
3083
+ yield* VariableDeclaration$1(node.declaration, scope);
3084
+ const variable = globalScope.find(EXPORTS);
3085
+ if (variable) {
3086
+ const exports = variable.get();
3087
+ if (exports && typeof exports === 'object') {
3088
+ for (let i = 0; i < node.declaration.declarations.length; i++) {
3089
+ const name = node.declaration.declarations[i].id.name;
3090
+ const item = scope.find(name);
3091
+ if (item) {
3092
+ exports[name] = item.get();
3093
+ }
3094
+ }
3095
+ }
3096
+ }
3097
+ }
3098
+ }
3099
+ else if (node.specifiers) {
3100
+ const variable = globalScope.find(EXPORTS);
3101
+ if (variable) {
3102
+ const exports = variable.get();
3103
+ if (exports && typeof exports === 'object') {
3104
+ for (let i = 0; i < node.specifiers.length; i++) {
3105
+ const spec = node.specifiers[i];
3106
+ const name = spec.local.type === 'Identifier'
3107
+ ? spec.local.name : spec.local.value;
3108
+ const item = scope.find(name);
3109
+ if (item) {
3110
+ exports[spec.exported.type === 'Identifier'
3111
+ ? spec.exported.name : spec.exported.value] = item.get();
3112
+ }
3113
+ }
3114
+ }
3115
+ }
3116
+ }
3117
+ }
3118
+ function* ExportAllDeclaration$1(node, scope) {
3119
+ const globalScope = scope.global();
3120
+ const module = globalScope.find(IMPORT + node.source.value);
3121
+ let value;
3122
+ if (module) {
3123
+ const result = module.get();
3124
+ if (result) {
3125
+ if (typeof result === 'function') {
3126
+ value = result();
3127
+ }
3128
+ else if (typeof result === 'object') {
3129
+ value = result;
3130
+ }
3131
+ }
3132
+ }
3133
+ if (!value || typeof value !== 'object') {
3134
+ throw new TypeError(`Failed to resolve module specifier "${node.source.value}"`);
3135
+ }
3136
+ const variable = globalScope.find(EXPORTS);
3137
+ if (variable) {
3138
+ const exports = variable.get();
3139
+ if (exports && typeof exports === 'object') {
3140
+ assign(exports, value);
3141
+ }
3142
+ }
2614
3143
  }
2615
3144
 
2616
3145
  function runAsync(iterator, options = {}) {
@@ -2759,7 +3288,7 @@
2759
3288
  if (!node.generator && !node.async) {
2760
3289
  return createFunc$1(node, scope, options);
2761
3290
  }
2762
- const { superClass, isCtor } = options;
3291
+ const { superClass, construct } = options;
2763
3292
  const params = node.params;
2764
3293
  const tmpFunc = function* (...args) {
2765
3294
  const subScope = new Scope(scope, true);
@@ -2767,9 +3296,12 @@
2767
3296
  subScope.const('this', this);
2768
3297
  subScope.let('arguments', arguments);
2769
3298
  subScope.const(NEWTARGET, new.target);
3299
+ if (construct) {
3300
+ yield* construct(this);
3301
+ }
2770
3302
  if (superClass) {
2771
3303
  subScope.const(SUPER, superClass);
2772
- if (isCtor)
3304
+ if (construct)
2773
3305
  subScope.let(SUPERCALL, false);
2774
3306
  }
2775
3307
  }
@@ -2850,16 +3382,25 @@
2850
3382
  }
2851
3383
  function* createClass(node, scope) {
2852
3384
  const superClass = yield* evaluate$1(node.superClass, scope);
2853
- let klass = function () {
3385
+ const methodBody = node.body.body;
3386
+ const construct = function* (object) {
3387
+ for (let i = 0; i < methodBody.length; i++) {
3388
+ const def = methodBody[i];
3389
+ if (def.type === 'PropertyDefinition' && !def.static) {
3390
+ yield* PropertyDefinition$1(def, scope, { klass: object, superClass });
3391
+ }
3392
+ }
3393
+ };
3394
+ let klass = function* () {
3395
+ yield* construct(this);
2854
3396
  if (superClass) {
2855
3397
  superClass.apply(this);
2856
3398
  }
2857
3399
  };
2858
- const methodBody = node.body.body;
2859
3400
  for (let i = 0; i < methodBody.length; i++) {
2860
3401
  const method = methodBody[i];
2861
3402
  if (method.type === 'MethodDefinition' && method.kind === 'constructor') {
2862
- klass = createFunc(method.value, scope, { superClass, isCtor: true });
3403
+ klass = createFunc(method.value, scope, { superClass, construct });
2863
3404
  break;
2864
3405
  }
2865
3406
  }
@@ -2998,7 +3539,7 @@
2998
3539
  if (node.generator || node.async) {
2999
3540
  return createFunc(node, scope, options);
3000
3541
  }
3001
- const { superClass, isCtor } = options;
3542
+ const { superClass, construct } = options;
3002
3543
  const params = node.params;
3003
3544
  const tmpFunc = function (...args) {
3004
3545
  const subScope = new Scope(scope, true);
@@ -3006,9 +3547,12 @@
3006
3547
  subScope.const('this', this);
3007
3548
  subScope.let('arguments', arguments);
3008
3549
  subScope.const(NEWTARGET, new.target);
3550
+ if (construct) {
3551
+ construct(this);
3552
+ }
3009
3553
  if (superClass) {
3010
3554
  subScope.const(SUPER, superClass);
3011
- if (isCtor)
3555
+ if (construct)
3012
3556
  subScope.let(SUPERCALL, false);
3013
3557
  }
3014
3558
  }
@@ -3061,16 +3605,25 @@
3061
3605
  }
3062
3606
  function createClass$1(node, scope) {
3063
3607
  const superClass = evaluate(node.superClass, scope);
3608
+ const methodBody = node.body.body;
3609
+ const construct = function (object) {
3610
+ for (let i = 0; i < methodBody.length; i++) {
3611
+ const def = methodBody[i];
3612
+ if (def.type === 'PropertyDefinition' && !def.static) {
3613
+ PropertyDefinition(def, scope, { klass: object, superClass });
3614
+ }
3615
+ }
3616
+ };
3064
3617
  let klass = function () {
3618
+ construct(this);
3065
3619
  if (superClass) {
3066
3620
  superClass.apply(this);
3067
3621
  }
3068
3622
  };
3069
- const methodBody = node.body.body;
3070
3623
  for (let i = 0; i < methodBody.length; i++) {
3071
3624
  const method = methodBody[i];
3072
3625
  if (method.type === 'MethodDefinition' && method.kind === 'constructor') {
3073
- klass = createFunc$1(method.value, scope, { superClass, isCtor: true });
3626
+ klass = createFunc$1(method.value, scope, { superClass, construct });
3074
3627
  break;
3075
3628
  }
3076
3629
  }
@@ -3115,7 +3668,7 @@
3115
3668
  this.options = { ecmaVersion: 'latest' };
3116
3669
  this.scope = new Scope(null, true);
3117
3670
  this.exports = {};
3118
- let { ecmaVer = 'latest', sandBox = true } = options;
3671
+ let { ecmaVer = 'latest', sandBox = true, sourceType = 'script' } = options;
3119
3672
  if (typeof ecmaVer === 'number') {
3120
3673
  ecmaVer -= ecmaVer < 2015 ? 0 : 2009;
3121
3674
  }
@@ -3123,16 +3676,19 @@
3123
3676
  throw new Error(`unsupported ecmaVer`);
3124
3677
  }
3125
3678
  this.options.ecmaVersion = ecmaVer;
3679
+ this.options.sourceType = sourceType;
3126
3680
  if (sandBox) {
3127
3681
  const win = createSandBox();
3682
+ this.scope.let('globalThis', win);
3128
3683
  this.scope.let('window', win);
3129
3684
  this.scope.let('this', win);
3130
3685
  }
3131
3686
  else {
3687
+ this.scope.let('globalThis', globalObj);
3132
3688
  this.scope.let('window', globalObj);
3133
3689
  this.scope.let('this', globalObj);
3134
3690
  }
3135
- this.scope.const('exports', this.exports = {});
3691
+ this.scope.const(sourceType === 'module' ? EXPORTS : 'exports', this.exports = {});
3136
3692
  }
3137
3693
  import(nameOrModules, mod) {
3138
3694
  if (typeof nameOrModules === 'string') {
@@ -3143,7 +3699,8 @@
3143
3699
  const names = getOwnNames(nameOrModules);
3144
3700
  for (let i = 0; i < names.length; i++) {
3145
3701
  const name = names[i];
3146
- this.scope.var(name, nameOrModules[name]);
3702
+ const modName = this.options.sourceType === 'module' ? IMPORT + name : name;
3703
+ this.scope.var(modName, nameOrModules[name]);
3147
3704
  }
3148
3705
  }
3149
3706
  parse(code, parser) {