ecma-evaluator 1.0.0 → 2.0.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.
@@ -1,4 +1,145 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_acorn__ from "acorn";
1
+ import { parse as external_acorn_parse } from "acorn";
2
+ import globals from "globals";
3
+ var mutableMethods = [
4
+ "Array.prototype.push",
5
+ "Array.prototype.pop",
6
+ "Array.prototype.shift",
7
+ "Array.prototype.unshift",
8
+ "Array.prototype.splice",
9
+ "Array.prototype.reverse",
10
+ "Array.prototype.sort",
11
+ "Array.prototype.fill",
12
+ "Array.prototype.copyWithin",
13
+ "Object.defineProperty",
14
+ "Object.defineProperties",
15
+ "Object.preventExtensions",
16
+ "Object.seal",
17
+ "Object.freeze",
18
+ "Object.setPrototypeOf",
19
+ "Object.assign",
20
+ "Reflect.set",
21
+ "Reflect.defineProperty",
22
+ "Reflect.deleteProperty",
23
+ "Reflect.setPrototypeOf",
24
+ "Reflect.preventExtensions",
25
+ "Set.prototype.add",
26
+ "Set.prototype.delete",
27
+ "Set.prototype.clear",
28
+ "WeakSet.prototype.add",
29
+ "WeakSet.prototype.delete",
30
+ "Map.prototype.set",
31
+ "Map.prototype.delete",
32
+ "Map.prototype.clear",
33
+ "WeakMap.prototype.set",
34
+ "WeakMap.prototype.delete",
35
+ "Date.prototype.setTime",
36
+ "Date.prototype.setMilliseconds",
37
+ "Date.prototype.setUTCSeconds",
38
+ "Date.prototype.setSeconds",
39
+ "Date.prototype.setMinutes",
40
+ "Date.prototype.setHours",
41
+ "Date.prototype.setDate",
42
+ "Date.prototype.setMonth",
43
+ "Date.prototype.setFullYear",
44
+ "Date.prototype.setYear",
45
+ "Date.prototype.setUTCMilliseconds",
46
+ "Date.prototype.setUTCMinutes",
47
+ "Date.prototype.setUTCHours",
48
+ "Date.prototype.setUTCDate",
49
+ "Date.prototype.setUTCMonth",
50
+ "Date.prototype.setUTCFullYear",
51
+ "RegExp.prototype.compile",
52
+ "Int8Array.prototype.set",
53
+ "Uint8Array.prototype.set",
54
+ "Uint8ClampedArray.prototype.set",
55
+ "Int16Array.prototype.set",
56
+ "Uint16Array.prototype.set",
57
+ "Int32Array.prototype.set",
58
+ "Uint32Array.prototype.set",
59
+ "Float32Array.prototype.set",
60
+ "Float64Array.prototype.set",
61
+ "BigInt64Array.prototype.set",
62
+ "BigUint64Array.prototype.set",
63
+ "Int8Array.prototype.fill",
64
+ "Uint8Array.prototype.fill",
65
+ "Uint8ClampedArray.prototype.fill",
66
+ "Int16Array.prototype.fill",
67
+ "Uint16Array.prototype.fill",
68
+ "Int32Array.prototype.fill",
69
+ "Uint32Array.prototype.fill",
70
+ "Float32Array.prototype.fill",
71
+ "Float64Array.prototype.fill",
72
+ "BigInt64Array.prototype.fill",
73
+ "BigUint64Array.prototype.fill",
74
+ "Int8Array.prototype.reverse",
75
+ "Uint8Array.prototype.reverse",
76
+ "Uint8ClampedArray.prototype.reverse",
77
+ "Int16Array.prototype.reverse",
78
+ "Uint16Array.prototype.reverse",
79
+ "Int32Array.prototype.reverse",
80
+ "Uint32Array.prototype.reverse",
81
+ "Float32Array.prototype.reverse",
82
+ "Float64Array.prototype.reverse",
83
+ "BigInt64Array.prototype.reverse",
84
+ "BigUint64Array.prototype.reverse",
85
+ "Int8Array.prototype.sort",
86
+ "Uint8Array.prototype.sort",
87
+ "Uint8ClampedArray.prototype.sort",
88
+ "Int16Array.prototype.sort",
89
+ "Uint16Array.prototype.sort",
90
+ "Int32Array.prototype.sort",
91
+ "Uint32Array.prototype.sort",
92
+ "Float32Array.prototype.sort",
93
+ "Float64Array.prototype.sort",
94
+ "BigInt64Array.prototype.sort",
95
+ "BigUint64Array.prototype.sort",
96
+ "Int8Array.prototype.copyWithin",
97
+ "Uint8Array.prototype.copyWithin",
98
+ "Uint8ClampedArray.prototype.copyWithin",
99
+ "Int16Array.prototype.copyWithin",
100
+ "Uint16Array.prototype.copyWithin",
101
+ "Int32Array.prototype.copyWithin",
102
+ "Uint32Array.prototype.copyWithin",
103
+ "Float32Array.prototype.copyWithin",
104
+ "Float64Array.prototype.copyWithin",
105
+ "BigInt64Array.prototype.copyWithin",
106
+ "BigUint64Array.prototype.copyWithin",
107
+ "ArrayBuffer.prototype.transfer",
108
+ "ArrayBuffer.prototype.transferToFixedLength",
109
+ "SharedArrayBuffer.prototype.grow",
110
+ "DataView.prototype.setInt8",
111
+ "DataView.prototype.setUint8",
112
+ "DataView.prototype.setInt16",
113
+ "DataView.prototype.setUint16",
114
+ "DataView.prototype.setInt32",
115
+ "DataView.prototype.setUint32",
116
+ "DataView.prototype.setFloat32",
117
+ "DataView.prototype.setFloat64",
118
+ "DataView.prototype.setBigInt64",
119
+ "DataView.prototype.setBigUint64",
120
+ "Promise.prototype.catch",
121
+ "Promise.prototype.finally",
122
+ "Generator.prototype.next",
123
+ "Generator.prototype.return",
124
+ "Generator.prototype.throw",
125
+ "AsyncGenerator.prototype.next",
126
+ "AsyncGenerator.prototype.return",
127
+ "AsyncGenerator.prototype.throw",
128
+ "Iterator.prototype.next",
129
+ "WeakRef.prototype.deref",
130
+ "FinalizationRegistry.prototype.register",
131
+ "FinalizationRegistry.prototype.unregister",
132
+ "URLSearchParams.prototype.append",
133
+ "URLSearchParams.prototype.delete",
134
+ "URLSearchParams.prototype.set",
135
+ "URLSearchParams.prototype.sort",
136
+ "FormData.prototype.append",
137
+ "FormData.prototype.delete",
138
+ "FormData.prototype.set",
139
+ "Headers.prototype.append",
140
+ "Headers.prototype.delete",
141
+ "Headers.prototype.set"
142
+ ];
2
143
  function _array_like_to_array(arr, len) {
3
144
  if (null == len || len > arr.length) len = arr.length;
4
145
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
@@ -40,62 +181,19 @@ function _create_class(Constructor, protoProps, staticProps) {
40
181
  if (staticProps) _defineProperties(Constructor, staticProps);
41
182
  return Constructor;
42
183
  }
43
- function _define_property(obj, key, value) {
44
- if (key in obj) Object.defineProperty(obj, key, {
45
- value: value,
46
- enumerable: true,
47
- configurable: true,
48
- writable: true
49
- });
50
- else obj[key] = value;
51
- return obj;
184
+ function _instanceof(left, right) {
185
+ if (null != right && "undefined" != typeof Symbol && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);
186
+ return left instanceof right;
52
187
  }
53
188
  function _iterable_to_array(iter) {
54
189
  if ("undefined" != typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) return Array.from(iter);
55
190
  }
56
- function _iterable_to_array_limit(arr, i) {
57
- var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
58
- if (null == _i) return;
59
- var _arr = [];
60
- var _n = true;
61
- var _d = false;
62
- var _s, _e;
63
- try {
64
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
65
- _arr.push(_s.value);
66
- if (i && _arr.length === i) break;
67
- }
68
- } catch (err) {
69
- _d = true;
70
- _e = err;
71
- } finally{
72
- try {
73
- if (!_n && null != _i["return"]) _i["return"]();
74
- } finally{
75
- if (_d) throw _e;
76
- }
77
- }
78
- return _arr;
79
- }
80
191
  function _non_iterable_rest() {
81
192
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
82
193
  }
83
194
  function _non_iterable_spread() {
84
195
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
85
196
  }
86
- function _object_spread(target) {
87
- for(var i = 1; i < arguments.length; i++){
88
- var source = null != arguments[i] ? arguments[i] : {};
89
- var ownKeys = Object.keys(source);
90
- if ("function" == typeof Object.getOwnPropertySymbols) ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
91
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
92
- }));
93
- ownKeys.forEach(function(key) {
94
- _define_property(target, key, source[key]);
95
- });
96
- }
97
- return target;
98
- }
99
197
  function _set_prototype_of(o, p) {
100
198
  _set_prototype_of = Object.setPrototypeOf || function(o, p) {
101
199
  o.__proto__ = p;
@@ -103,8 +201,8 @@ function _set_prototype_of(o, p) {
103
201
  };
104
202
  return _set_prototype_of(o, p);
105
203
  }
106
- function _sliced_to_array(arr, i) {
107
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
204
+ function _to_array(arr) {
205
+ return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
108
206
  }
109
207
  function _to_consumable_array(arr) {
110
208
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
@@ -128,132 +226,110 @@ function _is_native_reflect_construct() {
128
226
  return !!result;
129
227
  })();
130
228
  }
131
- var typeArrayConstructors = [
132
- Int8Array,
133
- Uint8Array,
134
- Uint8ClampedArray,
135
- Int16Array,
136
- Uint16Array,
137
- Int32Array,
138
- Uint32Array,
139
- Float32Array,
140
- Float64Array,
141
- globalThis.BigInt64Array,
142
- globalThis.BigUint64Array
143
- ].filter(Boolean);
144
- var mutableMethods = new Set([
145
- Array.prototype.push,
146
- Array.prototype.pop,
147
- Array.prototype.shift,
148
- Array.prototype.unshift,
149
- Array.prototype.splice,
150
- Array.prototype.reverse,
151
- Array.prototype.sort,
152
- Array.prototype.fill,
153
- Array.prototype.copyWithin,
154
- ArrayBuffer.prototype.slice,
155
- DataView.prototype.setInt8,
156
- DataView.prototype.setUint8,
157
- DataView.prototype.setInt16,
158
- DataView.prototype.setUint16,
159
- DataView.prototype.setInt32,
160
- DataView.prototype.setUint32,
161
- DataView.prototype.setFloat32,
162
- DataView.prototype.setFloat64
163
- ].concat(_to_consumable_array(typeArrayConstructors.flatMap(function(TypedArray) {
164
- return [
165
- TypedArray.prototype.set,
166
- TypedArray.prototype.fill,
167
- TypedArray.prototype.copyWithin,
168
- TypedArray.prototype.reverse,
169
- TypedArray.prototype.sort
170
- ];
171
- })), [
172
- Object.freeze,
173
- Object.defineProperty,
174
- Object.defineProperties,
175
- Object.preventExtensions,
176
- Object.setPrototypeOf,
177
- Object.assign,
178
- Set.prototype.add,
179
- Set.prototype["delete"],
180
- Set.prototype.clear,
181
- WeakSet.prototype.add,
182
- WeakSet.prototype["delete"],
183
- Map.prototype.set,
184
- Map.prototype["delete"],
185
- Map.prototype.clear,
186
- WeakMap.prototype.set,
187
- WeakMap.prototype["delete"],
188
- Date.prototype.setMilliseconds,
189
- Date.prototype.setMinutes,
190
- Date.prototype.setHours,
191
- Date.prototype.setDate,
192
- Date.prototype.setFullYear,
193
- Date.prototype.setUTCMinutes,
194
- Date.prototype.setUTCHours,
195
- Date.prototype.setUTCDate,
196
- Date.prototype.setUTCFullYear,
197
- Date.prototype.setTime
198
- ]));
229
+ var ERROR_MESSAGES = {
230
+ DELETE_NOT_SUPPORTED: "Delete operator is not allow",
231
+ MUTABLE_METHOD: "Mutable method is not allowed",
232
+ NEW_FUNCTION_NOT_ALLOWED: "Cannot use new with Function constructor",
233
+ NOT_A_FUNCTION: "is not a function",
234
+ PROPERTY_READ_ERROR: "Cannot read property",
235
+ VARIABLE_NOT_DEFINED: "is not defined",
236
+ FUNCTION_CONSTRUCTOR_NOT_ALLOWED: "Function constructor is not allowed",
237
+ THIS_NOT_ALLOWED: "'this' keyword is not allowed",
238
+ NOT_A_VALID_SYNTAX: "is not a valid syntax"
239
+ };
240
+ function createGlobalScope() {
241
+ var scope = Object.create(null);
242
+ var builtin = globals.builtin;
243
+ Object.keys(builtin).forEach(function(key) {
244
+ if (key in globalThis && "eval" !== key && "globalThis" !== key) {
245
+ var isWritable = builtin[key];
246
+ Object.defineProperty(scope, key, {
247
+ value: globalThis[key],
248
+ writable: isWritable,
249
+ enumerable: false,
250
+ configurable: false
251
+ });
252
+ }
253
+ });
254
+ Object.defineProperty(scope, "globalThis", {
255
+ value: scope,
256
+ writable: false,
257
+ enumerable: false,
258
+ configurable: false
259
+ });
260
+ return scope;
261
+ }
262
+ var getMutableMethods = function() {
263
+ var MUTABLE_METHODS = null;
264
+ return function() {
265
+ if (MUTABLE_METHODS) return MUTABLE_METHODS;
266
+ var set = new Set();
267
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
268
+ try {
269
+ for(var _iterator = mutableMethods[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
270
+ var path = _step.value;
271
+ var _path_split = _to_array(path.split(".")), object = _path_split[0], properties = _path_split.slice(1);
272
+ var current = globalThis[object];
273
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
274
+ try {
275
+ for(var _iterator1 = properties[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
276
+ var prop = _step1.value;
277
+ if (current && Object.hasOwn(current, prop)) current = current[prop];
278
+ else {
279
+ current = null;
280
+ break;
281
+ }
282
+ }
283
+ } catch (err) {
284
+ _didIteratorError1 = true;
285
+ _iteratorError1 = err;
286
+ } finally{
287
+ try {
288
+ if (!_iteratorNormalCompletion1 && null != _iterator1.return) _iterator1.return();
289
+ } finally{
290
+ if (_didIteratorError1) throw _iteratorError1;
291
+ }
292
+ }
293
+ if ("function" == typeof current) set.add(current);
294
+ }
295
+ } catch (err) {
296
+ _didIteratorError = true;
297
+ _iteratorError = err;
298
+ } finally{
299
+ try {
300
+ if (!_iteratorNormalCompletion && null != _iterator.return) _iterator.return();
301
+ } finally{
302
+ if (_didIteratorError) throw _iteratorError;
303
+ }
304
+ }
305
+ MUTABLE_METHODS = set;
306
+ return MUTABLE_METHODS;
307
+ };
308
+ }();
199
309
  var Evaluator_Evaluator = /*#__PURE__*/ function() {
200
310
  "use strict";
201
311
  function Evaluator() {
202
312
  var variables = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
203
313
  _class_call_check(this, Evaluator);
204
- var globalScope = Object.assign(Object.create(null), _object_spread({
205
- Infinity: 1 / 0,
206
- null: null,
207
- undefined: void 0,
208
- NaN: Number.NaN,
209
- isNaN: Number.isNaN,
210
- isFinite: Number.isFinite,
211
- parseFloat: Number.parseFloat,
212
- parseInt: Number.parseInt,
213
- encodeURI: globalThis.encodeURI,
214
- encodeURIComponent: globalThis.encodeURIComponent,
215
- decodeURI: globalThis.decodeURI,
216
- decodeURIComponent: globalThis.decodeURIComponent,
217
- Number: Number,
218
- String: String,
219
- Boolean: Boolean,
220
- BigInt: globalThis.BigInt,
221
- Symbol: globalThis.Symbol,
222
- Object: Object,
223
- Array: Array,
224
- Set: Set,
225
- WeakSet: WeakSet,
226
- Map: Map,
227
- WeakMap: WeakMap,
228
- Math: Math,
229
- JSON: JSON,
230
- Date: Date,
231
- RegExp: RegExp,
232
- Error: Error,
233
- EvalError: EvalError,
234
- RangeError: RangeError,
235
- ReferenceError: ReferenceError,
236
- SyntaxError: SyntaxError,
237
- TypeError: TypeError,
238
- URIError: URIError,
239
- Promise: Promise
240
- }, typeArrayConstructors.reduce(function(acc, obj) {
241
- acc[obj.name] = obj;
242
- return acc;
243
- }, {})));
244
314
  this.scopes = [
245
315
  variables,
246
- globalScope
316
+ createGlobalScope()
247
317
  ];
318
+ this.source = void 0;
248
319
  }
249
320
  _create_class(Evaluator, [
250
321
  {
251
322
  key: "evaluate",
252
323
  value: function(expression) {
253
- var ast = __WEBPACK_EXTERNAL_MODULE_acorn__.parse(expression, {
324
+ this.source = expression;
325
+ var ast = external_acorn_parse(expression, {
254
326
  ecmaVersion: "latest"
255
327
  });
256
- return this.execute(ast.body);
328
+ try {
329
+ return this.execute(ast.body);
330
+ } finally{
331
+ this.source = void 0;
332
+ }
257
333
  }
258
334
  },
259
335
  {
@@ -271,7 +347,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
271
347
  _iteratorError = err;
272
348
  } finally{
273
349
  try {
274
- if (!_iteratorNormalCompletion && null != _iterator["return"]) _iterator["return"]();
350
+ if (!_iteratorNormalCompletion && null != _iterator.return) _iterator.return();
275
351
  } finally{
276
352
  if (_didIteratorError) throw _iteratorError;
277
353
  }
@@ -302,6 +378,10 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
302
378
  return this.handleObjectExpression(node);
303
379
  case "ArrayExpression":
304
380
  return this.handleArrayExpression(node);
381
+ case "SpreadElement":
382
+ return this.handleSpreadElement(node);
383
+ case "ObjectExpression":
384
+ return this.handleObjectExpression(node);
305
385
  case "ArrowFunctionExpression":
306
386
  return this.handleArrowFunctionExpression(node);
307
387
  case "CallExpression":
@@ -310,68 +390,76 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
310
390
  return this.visit(node.test) ? this.visit(node.consequent) : this.visit(node.alternate);
311
391
  case "NewExpression":
312
392
  if ("Identifier" !== node.callee.type) throw new Error("Unsupported callee type '".concat(node.callee.type, "' in new expression"));
313
- if ("Function" === node.callee.name) throw new Error("Cannot use new with Function constructor");
393
+ if ("Function" === node.callee.name) throw new Error(ERROR_MESSAGES.NEW_FUNCTION_NOT_ALLOWED);
314
394
  var Constructor = this.visit(node.callee);
315
- var args = node["arguments"].map(function(arg) {
395
+ var args = node.arguments.length ? node.arguments.map(function(arg) {
316
396
  return _this.visit(arg);
317
- });
397
+ }) : [];
318
398
  return _construct(Constructor, _to_consumable_array(args));
319
399
  case "ChainExpression":
320
400
  return this.visit(node.expression);
321
401
  case "TemplateLiteral":
322
402
  return this.handleTemplateLiteral(node);
403
+ case "ThisExpression":
404
+ throw new Error(ERROR_MESSAGES.THIS_NOT_ALLOWED);
323
405
  default:
324
- throw new Error("Unsupported node type: ".concat(node.type));
406
+ var content = this.source.slice(node.start, node.end);
407
+ if (content.length > 20) content = content.slice(0, 17) + "...";
408
+ throw new Error("'".concat(content, "'") + " " + ERROR_MESSAGES.NOT_A_VALID_SYNTAX);
325
409
  }
326
410
  }
327
411
  },
328
412
  {
329
413
  key: "handleBinaryExpression",
330
414
  value: function(node) {
331
- switch(node.operator){
415
+ var op = node.operator;
416
+ var left = this.visit(node.left);
417
+ var right = this.visit(node.right);
418
+ switch(op){
332
419
  case "+":
333
- return this.visit(node.left) + this.visit(node.right);
420
+ return left + right;
334
421
  case "-":
335
- return this.visit(node.left) - this.visit(node.right);
422
+ return left - right;
336
423
  case "*":
337
- return this.visit(node.left) * this.visit(node.right);
424
+ return left * right;
338
425
  case "**":
339
- return Math.pow(this.visit(node.left), this.visit(node.right));
340
- case "/":
341
- var left = this.visit(node.left);
342
- var right = this.visit(node.right);
343
- if (0 === right) throw new Error("Division by zero");
344
- return left / right;
426
+ return Math.pow(left, right);
345
427
  case "==":
346
- return this.visit(node.left) == this.visit(node.right);
428
+ return left == right;
347
429
  case "===":
348
- return this.visit(node.left) === this.visit(node.right);
430
+ return left === right;
349
431
  case "!=":
350
- return this.visit(node.left) != this.visit(node.right);
432
+ return left != right;
351
433
  case "!==":
352
- return this.visit(node.left) !== this.visit(node.right);
434
+ return left !== right;
353
435
  case ">":
354
- return this.visit(node.left) > this.visit(node.right);
436
+ return left > right;
355
437
  case ">=":
356
- return this.visit(node.left) >= this.visit(node.right);
438
+ return left >= right;
357
439
  case "<":
358
- return this.visit(node.left) < this.visit(node.right);
440
+ return left < right;
359
441
  case "<=":
360
- return this.visit(node.left) <= this.visit(node.right);
442
+ return left <= right;
361
443
  case "%":
362
- return this.visit(node.left) % this.visit(node.right);
363
- case "&":
364
- return this.visit(node.left) & this.visit(node.right);
444
+ return left % right;
445
+ case "/":
446
+ return left / right;
365
447
  case "|":
366
- return this.visit(node.left) | this.visit(node.right);
448
+ return left | right;
449
+ case "&":
450
+ return left & right;
367
451
  case "^":
368
- return this.visit(node.left) ^ this.visit(node.right);
452
+ return left ^ right;
369
453
  case "<<":
370
- return this.visit(node.left) << this.visit(node.right);
454
+ return left << right;
371
455
  case ">>":
372
- return this.visit(node.left) >> this.visit(node.right);
456
+ return left >> right;
373
457
  case ">>>":
374
- return this.visit(node.left) >>> this.visit(node.right);
458
+ return left >>> right;
459
+ case "in":
460
+ return left in right;
461
+ case "instanceof":
462
+ return _instanceof(left, right);
375
463
  default:
376
464
  throw new Error("Unsupported operator: ".concat(node.operator));
377
465
  }
@@ -382,12 +470,14 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
382
470
  value: function(node) {
383
471
  switch(node.operator){
384
472
  case "&&":
385
- return this.visit(node.left) && this.visit(node.right);
473
+ var left = this.visit(node.left);
474
+ return left ? this.visit(node.right) : left;
386
475
  case "||":
387
- return this.visit(node.left) || this.visit(node.right);
476
+ var left1 = this.visit(node.left);
477
+ return left1 ? left1 : this.visit(node.right);
388
478
  case "??":
389
- var left = this.visit(node.left);
390
- return null != left ? left : this.visit(node.right);
479
+ var left2 = this.visit(node.left);
480
+ return null != left2 ? left2 : this.visit(node.right);
391
481
  default:
392
482
  throw new Error("Unsupported logical operator: ".concat(node.operator));
393
483
  }
@@ -410,7 +500,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
410
500
  case "void":
411
501
  return void this.visit(node.argument);
412
502
  case "delete":
413
- throw new Error("Delete operator is mutable and not supported");
503
+ throw new Error(ERROR_MESSAGES.DELETE_NOT_SUPPORTED);
414
504
  default:
415
505
  throw new Error("Unsupported unary operator: ".concat(node.operator));
416
506
  }
@@ -431,28 +521,25 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
431
521
  _iteratorError = err;
432
522
  } finally{
433
523
  try {
434
- if (!_iteratorNormalCompletion && null != _iterator["return"]) _iterator["return"]();
524
+ if (!_iteratorNormalCompletion && null != _iterator.return) _iterator.return();
435
525
  } finally{
436
526
  if (_didIteratorError) throw _iteratorError;
437
527
  }
438
528
  }
439
- throw new ReferenceError("".concat(name, " is not defined"));
529
+ throw new ReferenceError("".concat(name, " ").concat(ERROR_MESSAGES.VARIABLE_NOT_DEFINED));
440
530
  }
441
531
  },
442
532
  {
443
533
  key: "handleMemberExpression",
444
534
  value: function(node) {
445
535
  var object = this.visit(node.object);
446
- var property = "Identifier" !== node.property.type || node.computed ? this.visit(node.property) : node.property.name;
536
+ var isStaticProperty = "Identifier" === node.property.type && !node.computed;
537
+ var property = isStaticProperty ? node.property.name : this.visit(node.property);
447
538
  if (null == object) {
448
539
  if (node.optional) return;
449
- throw new TypeError("Cannot read property '".concat(property, "' of ").concat(object));
540
+ throw new TypeError("".concat(ERROR_MESSAGES.PROPERTY_READ_ERROR, " '").concat(property, "' of ").concat(object));
450
541
  }
451
- if (Object.hasOwn(object, property)) return object[property];
452
- var prototypeValue = object[property];
453
- if (mutableMethods.has(prototypeValue)) throw new Error("Cannot call mutable prototype method: ".concat(property));
454
- if ("function" == typeof prototypeValue) return prototypeValue.bind(object);
455
- return prototypeValue;
542
+ return object[property];
456
543
  }
457
544
  },
458
545
  {
@@ -463,6 +550,10 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
463
550
  try {
464
551
  for(var _iterator = node.properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
465
552
  var prop = _step.value;
553
+ if ("SpreadElement" === prop.type) {
554
+ Object.assign(obj, this.visit(prop.argument));
555
+ continue;
556
+ }
466
557
  var key = prop.key.name || prop.key.value;
467
558
  var value = this.visit(prop.value);
468
559
  obj[key] = value;
@@ -472,7 +563,7 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
472
563
  _iteratorError = err;
473
564
  } finally{
474
565
  try {
475
- if (!_iteratorNormalCompletion && null != _iterator["return"]) _iterator["return"]();
566
+ if (!_iteratorNormalCompletion && null != _iterator.return) _iterator.return();
476
567
  } finally{
477
568
  if (_didIteratorError) throw _iteratorError;
478
569
  }
@@ -483,10 +574,22 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
483
574
  {
484
575
  key: "handleArrayExpression",
485
576
  value: function(node) {
486
- var _this = this;
487
- return node.elements.map(function(element) {
488
- return _this.visit(element);
489
- });
577
+ var result = [];
578
+ for(var i = 0; i < node.elements.length; i++){
579
+ var element = node.elements.at(i);
580
+ var value = this.visit(element);
581
+ if ("SpreadElement" === element.type) {
582
+ var _result;
583
+ (_result = result).push.apply(_result, _to_consumable_array(value));
584
+ } else result.push(value);
585
+ }
586
+ return result;
587
+ }
588
+ },
589
+ {
590
+ key: "handleSpreadElement",
591
+ value: function(node) {
592
+ return this.visit(node.argument);
490
593
  }
491
594
  },
492
595
  {
@@ -496,22 +599,8 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
496
599
  return function() {
497
600
  for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++)args[_key] = arguments[_key];
498
601
  var newScope = {};
499
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
500
- try {
501
- for(var _iterator = node.params.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
502
- var _step_value = _sliced_to_array(_step.value, 2), index = _step_value[0], param = _step_value[1];
503
- newScope[param.name] = args[index];
504
- }
505
- } catch (err) {
506
- _didIteratorError = true;
507
- _iteratorError = err;
508
- } finally{
509
- try {
510
- if (!_iteratorNormalCompletion && null != _iterator["return"]) _iterator["return"]();
511
- } finally{
512
- if (_didIteratorError) throw _iteratorError;
513
- }
514
- }
602
+ var paramCount = node.params.length;
603
+ for(var i = 0; i < paramCount; i++)newScope[node.params[i].name] = args[i];
515
604
  _this.scopes.unshift(newScope);
516
605
  var result = _this.visit(node.body);
517
606
  _this.scopes.shift();
@@ -523,73 +612,269 @@ var Evaluator_Evaluator = /*#__PURE__*/ function() {
523
612
  key: "handleCallExpression",
524
613
  value: function(node) {
525
614
  var _this = this;
526
- var calledString = this.getNodeString(node.callee);
615
+ if ("MemberExpression" === node.callee.type) {
616
+ var object = this.visit(node.callee.object);
617
+ if (getMutableMethods().has(object)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
618
+ }
619
+ var calledString = getNodeString(node.callee);
527
620
  var func = this.visit(node.callee);
528
621
  if ("function" != typeof func) {
529
622
  var isOptional = node.optional || node.callee.optional;
530
623
  if (null == func && isOptional) return;
531
- throw new TypeError("".concat(calledString, " is not a function"));
624
+ throw new TypeError("".concat(calledString, " ").concat(ERROR_MESSAGES.NOT_A_FUNCTION));
532
625
  }
533
- var args = node["arguments"].map(function(arg) {
534
- return _this.visit(arg);
535
- });
536
- return func.apply(void 0, _to_consumable_array(args));
626
+ if (func === Function) throw new Error(ERROR_MESSAGES.FUNCTION_CONSTRUCTOR_NOT_ALLOWED);
627
+ var args = function() {
628
+ if (0 === node.arguments.length) return [];
629
+ var result = [];
630
+ for(var i = 0; i < node.arguments.length; i++){
631
+ var element = node.arguments.at(i);
632
+ var value = _this.visit(element);
633
+ if ("SpreadElement" === element.type) {
634
+ var _result;
635
+ (_result = result).push.apply(_result, _to_consumable_array(value));
636
+ } else result.push(value);
637
+ }
638
+ return result;
639
+ }();
640
+ if (getMutableMethods().has(func)) throw new Error(ERROR_MESSAGES.MUTABLE_METHOD);
641
+ var target = "MemberExpression" === node.callee.type ? this.visit(node.callee.object) : null;
642
+ return func.apply(target, args);
537
643
  }
538
644
  },
539
645
  {
540
646
  key: "handleTemplateLiteral",
541
647
  value: function(node) {
542
- var _this = this;
543
- return node.quasis.concat(node.expressions).filter(Boolean).sort(function(a, b) {
544
- return a.start - b.start;
545
- }).map(function(node) {
546
- if ("TemplateElement" === node.type) return node.value.raw;
547
- return _this.visit(node);
548
- }).join("");
648
+ var result = "";
649
+ var expressionCount = node.expressions.length;
650
+ for(var i = 0; i < node.quasis.length; i++){
651
+ result += node.quasis[i].value.raw;
652
+ if (i < expressionCount) result += this.visit(node.expressions[i]);
653
+ }
654
+ return result;
549
655
  }
550
- },
656
+ }
657
+ ], [
551
658
  {
552
- key: "getNodeString",
553
- value: function(node) {
554
- switch(node.type){
555
- case "Identifier":
556
- return node.name;
557
- case "Literal":
558
- return node.raw;
559
- case "ArrayExpression":
560
- return "(array)";
561
- case "ObjectExpression":
562
- return "(object)";
563
- case "MemberExpression":
564
- var accessor = this.getNodeString(node.object);
565
- if (node.computed) accessor += "[".concat(this.getNodeString(node.property), "]");
566
- else accessor += ".".concat(this.getNodeString(node.property));
567
- return accessor;
568
- default:
569
- return null;
570
- }
659
+ key: "evaluate",
660
+ value: function(expression, context) {
661
+ var evaluator = new Evaluator(context);
662
+ return evaluator.evaluate(expression);
571
663
  }
572
664
  }
573
665
  ]);
574
666
  return Evaluator;
575
667
  }();
576
- var TEMPLATE_EXPRESSION_REGEX = /\$\{\{((?:[^{}]|{[^{}]*})+)\}\}/g;
577
- function evaluatorExpression(expression, context) {
578
- var evaluator = new Evaluator_Evaluator(context);
579
- return evaluator.evaluate(expression);
668
+ function getNodeString(node) {
669
+ switch(node.type){
670
+ case "Identifier":
671
+ return node.name;
672
+ case "Literal":
673
+ return node.raw;
674
+ case "ArrayExpression":
675
+ return "[".concat(node.elements.map(function(child) {
676
+ return getNodeString(child);
677
+ }).join(","), "]");
678
+ case "ObjectExpression":
679
+ if (0 === node.properties.length) return "{}";
680
+ return "{(intermediate value)}";
681
+ case "MemberExpression":
682
+ var objectStr = getNodeString(node.object);
683
+ var propertyStr = getNodeString(node.property);
684
+ if (node.computed) return "".concat(objectStr, "[").concat(propertyStr, "]");
685
+ return "".concat(objectStr, ".").concat(propertyStr);
686
+ default:
687
+ return null;
688
+ }
689
+ }
690
+ function TemplateParser_class_call_check(instance, Constructor) {
691
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
692
+ }
693
+ function TemplateParser_defineProperties(target, props) {
694
+ for(var i = 0; i < props.length; i++){
695
+ var descriptor = props[i];
696
+ descriptor.enumerable = descriptor.enumerable || false;
697
+ descriptor.configurable = true;
698
+ if ("value" in descriptor) descriptor.writable = true;
699
+ Object.defineProperty(target, descriptor.key, descriptor);
700
+ }
701
+ }
702
+ function TemplateParser_create_class(Constructor, protoProps, staticProps) {
703
+ if (protoProps) TemplateParser_defineProperties(Constructor.prototype, protoProps);
704
+ if (staticProps) TemplateParser_defineProperties(Constructor, staticProps);
705
+ return Constructor;
706
+ }
707
+ var TemplateParser_TemplateParser = /*#__PURE__*/ function() {
708
+ "use strict";
709
+ function TemplateParser() {
710
+ var options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
711
+ TemplateParser_class_call_check(this, TemplateParser);
712
+ this.exprStart = options.expressionStart || "{{";
713
+ this.exprEnd = options.expressionEnd || "}}";
714
+ this.startLen = this.exprStart.length;
715
+ this.endLen = this.exprEnd.length;
716
+ this.preserveWhitespace = true === options.preserveWhitespace;
717
+ }
718
+ TemplateParser_create_class(TemplateParser, [
719
+ {
720
+ key: "parse",
721
+ value: function(template) {
722
+ if ("string" != typeof template) throw new TypeError("模板必须是字符串");
723
+ var tokens = [];
724
+ var length = template.length;
725
+ var state = "TEXT";
726
+ var buffer = "";
727
+ var exprStartPos = 0;
728
+ var pos = 0;
729
+ while(pos < length){
730
+ var char = template[pos];
731
+ var nextChar = template[pos + 1];
732
+ switch(state){
733
+ case "TEXT":
734
+ if (this._isMatch(pos, template, this.exprStart)) {
735
+ if (buffer.length > 0) {
736
+ tokens.push({
737
+ type: "text",
738
+ value: buffer,
739
+ start: pos - buffer.length,
740
+ end: pos
741
+ });
742
+ buffer = "";
743
+ }
744
+ state = "EXPR";
745
+ exprStartPos = pos;
746
+ pos += this.startLen;
747
+ continue;
748
+ }
749
+ buffer += char;
750
+ break;
751
+ case "EXPR":
752
+ if ("'" === char) state = "STRING_SQ";
753
+ else if ('"' === char) state = "STRING_DQ";
754
+ else if ("`" === char) state = "TEMPLATE";
755
+ else if ("\\" === char) state = "'" === nextChar ? "ESCAPE_SQ" : '"' === nextChar ? "ESCAPE_DQ" : "`" === nextChar ? "ESCAPE_TEMPLATE" : "ESCAPE_EXPR";
756
+ else if (this._isMatch(pos, template, this.exprEnd)) {
757
+ var exprValue = this.preserveWhitespace ? buffer : buffer.trim();
758
+ if (exprValue.length > 0) tokens.push({
759
+ type: "expression",
760
+ value: exprValue,
761
+ start: exprStartPos,
762
+ end: pos + this.endLen,
763
+ contentStart: exprStartPos + this.startLen,
764
+ contentEnd: pos
765
+ });
766
+ buffer = "";
767
+ state = "TEXT";
768
+ pos += this.endLen;
769
+ continue;
770
+ }
771
+ buffer += char;
772
+ break;
773
+ case "STRING_SQ":
774
+ if ("\\" === char) state = "ESCAPE_SQ";
775
+ else if ("'" === char) state = "EXPR";
776
+ buffer += char;
777
+ break;
778
+ case "STRING_DQ":
779
+ if ("\\" === char) state = "ESCAPE_DQ";
780
+ else if ('"' === char) state = "EXPR";
781
+ buffer += char;
782
+ break;
783
+ case "TEMPLATE":
784
+ if ("\\" === char) state = "ESCAPE_TEMPLATE";
785
+ else if ("`" === char) state = "EXPR";
786
+ buffer += char;
787
+ break;
788
+ case "ESCAPE_SQ":
789
+ buffer += char;
790
+ state = "STRING_SQ";
791
+ break;
792
+ case "ESCAPE_DQ":
793
+ buffer += char;
794
+ state = "STRING_DQ";
795
+ break;
796
+ case "ESCAPE_TEMPLATE":
797
+ buffer += char;
798
+ state = "TEMPLATE";
799
+ break;
800
+ case "ESCAPE_EXPR":
801
+ buffer += char;
802
+ state = "EXPR";
803
+ break;
804
+ }
805
+ pos++;
806
+ }
807
+ if (buffer.length > 0) if ("TEXT" === state) tokens.push({
808
+ type: "text",
809
+ value: buffer,
810
+ start: pos - buffer.length,
811
+ end: pos
812
+ });
813
+ else {
814
+ var incompleteExpr = this.exprStart + buffer;
815
+ tokens.push({
816
+ type: "text",
817
+ value: incompleteExpr,
818
+ start: exprStartPos,
819
+ end: pos
820
+ });
821
+ console.warn("警告:在位置 ".concat(exprStartPos, " 开始的表达式未正确结束"));
822
+ }
823
+ return tokens;
824
+ }
825
+ },
826
+ {
827
+ key: "_isMatch",
828
+ value: function(pos, template, sequence) {
829
+ if (pos + sequence.length > template.length) return false;
830
+ for(var i = 0; i < sequence.length; i++)if (template[pos + i] !== sequence[i]) return false;
831
+ return true;
832
+ }
833
+ }
834
+ ], [
835
+ {
836
+ key: "parse",
837
+ value: function(template, options) {
838
+ var parser = new TemplateParser(options);
839
+ return parser.parse(template);
840
+ }
841
+ }
842
+ ]);
843
+ return TemplateParser;
844
+ }();
845
+ function src_instanceof(left, right) {
846
+ if (null != right && "undefined" != typeof Symbol && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);
847
+ return left instanceof right;
580
848
  }
581
- function evaluatorTemplate(template, context) {
582
- var evaluator = new Evaluator_Evaluator(context);
583
- return template.replace(TEMPLATE_EXPRESSION_REGEX, function(match, expression) {
849
+ function evalExpression(expression, context) {
850
+ return Evaluator_Evaluator.evaluate(expression, context);
851
+ }
852
+ function evalTemplate(template, context, templateParserOptions) {
853
+ var result = "";
854
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
855
+ try {
856
+ for(var _iterator = TemplateParser_TemplateParser.parse(template, templateParserOptions)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
857
+ var token = _step.value;
858
+ if ("text" === token.type) result += token.value;
859
+ else if ("expression" === token.type) try {
860
+ result += Evaluator_Evaluator.evaluate(token.value, context);
861
+ } catch (error) {
862
+ if (src_instanceof(error, ReferenceError) && error.message.endsWith("is not defined")) result += "undefined";
863
+ else throw error;
864
+ }
865
+ }
866
+ } catch (err) {
867
+ _didIteratorError = true;
868
+ _iteratorError = err;
869
+ } finally{
584
870
  try {
585
- var value = evaluator.evaluate(expression.trim());
586
- return String(value);
587
- } catch (error) {
588
- if (error instanceof ReferenceError && error.message.endsWith("is not defined")) return "";
589
- throw error;
871
+ if (!_iteratorNormalCompletion && null != _iterator.return) _iterator.return();
872
+ } finally{
873
+ if (_didIteratorError) throw _iteratorError;
590
874
  }
591
- });
875
+ }
876
+ return result;
592
877
  }
593
- export { evaluatorExpression, evaluatorTemplate };
878
+ export { Evaluator_Evaluator as Evaluator, evalExpression, evalTemplate };
594
879
 
595
880
  //# sourceMappingURL=index.mjs.map