@mirascript/mirascript 0.1.86 → 0.1.88

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.
Files changed (47) hide show
  1. package/dist/{chunk-LSPCAN32.js → chunk-HHKY7Y7L.js} +125 -756
  2. package/dist/chunk-HHKY7Y7L.js.map +6 -0
  3. package/dist/{chunk-A425FGG3.js → chunk-Y7S6AHE7.js} +689 -32
  4. package/dist/chunk-Y7S6AHE7.js.map +6 -0
  5. package/dist/compiler/{emit → bytecode}/consts.d.ts +0 -2
  6. package/dist/compiler/bytecode/consts.d.ts.map +1 -0
  7. package/dist/compiler/bytecode/index.d.ts +28 -0
  8. package/dist/compiler/bytecode/index.d.ts.map +1 -0
  9. package/dist/compiler/emit/index.d.ts +2 -13
  10. package/dist/compiler/emit/index.d.ts.map +1 -1
  11. package/dist/compiler/emit/utils.d.ts +6 -0
  12. package/dist/compiler/emit/utils.d.ts.map +1 -0
  13. package/dist/compiler/generate-bytecode.d.ts +1 -1
  14. package/dist/compiler/generate-bytecode.d.ts.map +1 -1
  15. package/dist/compiler/index.d.ts +1 -1
  16. package/dist/compiler/index.d.ts.map +1 -1
  17. package/dist/compiler/options.d.ts +5 -0
  18. package/dist/compiler/options.d.ts.map +1 -0
  19. package/dist/compiler/verbose.d.ts +11 -0
  20. package/dist/compiler/verbose.d.ts.map +1 -0
  21. package/dist/compiler/worker.d.ts +1 -1
  22. package/dist/compiler/worker.js +3 -3
  23. package/dist/compiler/worker.js.map +1 -1
  24. package/dist/index.js +17 -51
  25. package/dist/index.js.map +2 -2
  26. package/dist/subtle.d.ts +3 -1
  27. package/dist/subtle.d.ts.map +1 -1
  28. package/dist/subtle.js +18 -14
  29. package/dist/subtle.js.map +1 -1
  30. package/dist/vm/lib/index.d.ts +9 -1
  31. package/dist/vm/lib/index.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/compiler/{emit → bytecode}/consts.ts +0 -16
  34. package/src/compiler/bytecode/index.ts +58 -0
  35. package/src/compiler/emit/index.ts +19 -72
  36. package/src/compiler/emit/sourcemap.ts +1 -1
  37. package/src/compiler/emit/utils.ts +28 -0
  38. package/src/compiler/generate-bytecode.ts +3 -3
  39. package/src/compiler/index.ts +9 -29
  40. package/src/compiler/options.ts +16 -0
  41. package/src/compiler/verbose.ts +32 -0
  42. package/src/compiler/worker.ts +2 -2
  43. package/src/subtle.ts +3 -1
  44. package/src/vm/lib/index.ts +10 -1
  45. package/dist/chunk-A425FGG3.js.map +0 -6
  46. package/dist/chunk-LSPCAN32.js.map +0 -6
  47. package/dist/compiler/emit/consts.d.ts.map +0 -1
@@ -16,642 +16,6 @@ var __copyProps = (to, from, except, desc) => {
16
16
  };
17
17
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
18
 
19
- // src/helpers/constants.ts
20
- var constants_exports = {};
21
- __export(constants_exports, {
22
- VM_ARRAY_MAX_LENGTH: () => VM_ARRAY_MAX_LENGTH,
23
- VM_FUNCTION_ANONYMOUS_NAME: () => VM_FUNCTION_ANONYMOUS_NAME,
24
- VM_SCRIPT_NAME: () => VM_SCRIPT_NAME,
25
- kVmContext: () => kVmContext,
26
- kVmExtern: () => kVmExtern,
27
- kVmFunction: () => kVmFunction,
28
- kVmFunctionProxy: () => kVmFunctionProxy,
29
- kVmModule: () => kVmModule,
30
- kVmScript: () => kVmScript,
31
- kVmWrapper: () => kVmWrapper
32
- });
33
- __reExport(constants_exports, constants_star);
34
- import * as constants_star from "@mirascript/constants";
35
- var VM_ARRAY_MAX_LENGTH = 16777216;
36
- var kVmScript = /* @__PURE__ */ Symbol.for("mirascript.vm.script");
37
- var kVmFunction = /* @__PURE__ */ Symbol.for("mirascript.vm.function");
38
- var kVmFunctionProxy = /* @__PURE__ */ Symbol.for("mirascript.vm.function.proxy");
39
- var kVmContext = /* @__PURE__ */ Symbol.for("mirascript.vm.context");
40
- var kVmExtern = /* @__PURE__ */ Symbol.for("mirascript.vm.extern");
41
- var kVmModule = /* @__PURE__ */ Symbol.for("mirascript.vm.module");
42
- var kVmWrapper = /* @__PURE__ */ Symbol.for("mirascript.vm.wrapper");
43
- var VM_SCRIPT_NAME = `<script_root>`;
44
- var VM_FUNCTION_ANONYMOUS_NAME = `<anonymous>`;
45
-
46
- // src/helpers/utils.ts
47
- var { isArray } = Array;
48
- var {
49
- isFinite,
50
- isNaN,
51
- isInteger,
52
- isSafeInteger,
53
- NaN: NotNumber,
54
- POSITIVE_INFINITY: PositiveInfinity,
55
- NEGATIVE_INFINITY: NegativeInfinity
56
- } = Number;
57
- var { hasOwn, keys, values, entries, create, fromEntries, defineProperty, getOwnPropertyNames, freeze } = Object;
58
- var { apply, getPrototypeOf } = Reflect;
59
- var keysLength = "keysLength" in Object && typeof Object.keysLength == "function" ? Object.keysLength : (o) => keys(o).length;
60
- var hasOwnEnumerable = Function.call.bind(
61
- // eslint-disable-next-line @typescript-eslint/unbound-method
62
- Object.prototype.propertyIsEnumerable
63
- );
64
- var SPECIAL_KEYS = new Set(getOwnPropertyNames(Object.prototype));
65
- var setRecord = (obj, key, value) => {
66
- if (!SPECIAL_KEYS.has(key)) {
67
- obj[key] = value ?? null;
68
- } else {
69
- defineProperty(obj, key, {
70
- __proto__: null,
71
- value: value ?? null,
72
- configurable: true,
73
- writable: true,
74
- enumerable: true
75
- });
76
- }
77
- };
78
- var getRecord = (obj, key) => {
79
- if (!hasOwnEnumerable(obj, key)) return void 0;
80
- return obj[key] ?? null;
81
- };
82
-
83
- // src/helpers/types/basic.ts
84
- function isVmScript(value) {
85
- return typeof value === "function" && kVmScript in value;
86
- }
87
- function isVmContext(context) {
88
- return context != null && typeof context == "object" && kVmContext in context;
89
- }
90
- function isVmFunction(value) {
91
- return typeof value == "function" && kVmFunction in value;
92
- }
93
- function getVmFunctionInfo(value) {
94
- if (typeof value != "function") return void 0;
95
- return value[kVmFunction];
96
- }
97
- function isVmWrapper(value) {
98
- return value != null && typeof value == "object" && kVmWrapper in value;
99
- }
100
- function isVmModule(value) {
101
- return value != null && typeof value == "object" && kVmModule in value;
102
- }
103
- function isVmExtern(value) {
104
- return value != null && typeof value == "object" && kVmExtern in value;
105
- }
106
- function isVmCallable(value) {
107
- return isVmFunction(value) || isVmExtern(value) && typeof value.value == "function";
108
- }
109
- function isVmPrimitive(value) {
110
- if (value === null || typeof value == "number" || typeof value == "string" || typeof value == "boolean") {
111
- value;
112
- value;
113
- return true;
114
- }
115
- return false;
116
- }
117
- function isVmArray(value) {
118
- if (!isArray(value)) return false;
119
- value;
120
- return true;
121
- }
122
- function isVmRecord(value) {
123
- if (value == null || typeof value != "object") return false;
124
- if (isVmWrapper(value) || isVmArray(value)) return false;
125
- value;
126
- return true;
127
- }
128
-
129
- // src/helpers/types/proto.ts
130
- function isPlainArray(value) {
131
- const proto = getPrototypeOf(value);
132
- return isArray(proto) && hasOwn(proto, "push");
133
- }
134
- function isPlainObject(value) {
135
- const proto1 = getPrototypeOf(value);
136
- if (proto1 == null) return true;
137
- const proto2 = getPrototypeOf(proto1);
138
- if (proto2 != null) return false;
139
- return hasOwn(proto1, "hasOwnProperty");
140
- }
141
-
142
- // src/helpers/types/const.ts
143
- var MAX_DEPTH = 16;
144
- function isVmArrayDeep(value, depth) {
145
- if (!isPlainArray(value)) return false;
146
- if (depth <= 0) return true;
147
- const inner_depth = depth - 1;
148
- return value.every((item) => isVmConstInner(item, inner_depth));
149
- }
150
- function isVmRecordDeep(value, depth) {
151
- if (!isPlainObject(value)) return false;
152
- if (depth <= 0) return true;
153
- const inner_depth = depth - 1;
154
- return values(value).every((value2) => isVmConstInner(value2, inner_depth));
155
- }
156
- function isVmConstInner(value, depth) {
157
- if (value == null) return true;
158
- if (typeof value == "string" || typeof value == "number" || typeof value == "boolean") return true;
159
- if (typeof value == "object") {
160
- if (isVmWrapper(value)) return false;
161
- if (isArray(value)) {
162
- return isVmArrayDeep(value, depth);
163
- } else {
164
- return isVmRecordDeep(value, depth);
165
- }
166
- }
167
- return false;
168
- }
169
- function isVmConst(value, checkDeep = false) {
170
- if (value === void 0) return false;
171
- return isVmConstInner(value, checkDeep ? MAX_DEPTH : 0);
172
- }
173
-
174
- // src/helpers/types/get-type.ts
175
- function getVmType(value) {
176
- if (value === void 0) return "uninitialized";
177
- if (value === null) return "nil";
178
- if (isVmWrapper(value)) return value.type;
179
- if (isVmArray(value)) return "array";
180
- if (typeof value == "object") return "record";
181
- return typeof value;
182
- }
183
-
184
- // src/helpers/types/array-like.ts
185
- function isVmArrayLikeRecordByEntries(entries2) {
186
- const { length } = entries2;
187
- if (length === 0) return true;
188
- if (length > VM_ARRAY_MAX_LENGTH) return false;
189
- const firstKey = entries2[0][0];
190
- if (firstKey !== "0") return false;
191
- const lastKey = entries2[length - 1][0];
192
- if (lastKey !== String(length - 1)) return false;
193
- return true;
194
- }
195
- function isVmArrayLikeRecordByKeys(keys2) {
196
- const { length } = keys2;
197
- if (length === 0) return true;
198
- if (length > VM_ARRAY_MAX_LENGTH) return false;
199
- const firstKey = keys2[0];
200
- if (firstKey !== "0") return false;
201
- const lastKey = keys2[length - 1];
202
- if (lastKey !== String(length - 1)) return false;
203
- return true;
204
- }
205
- function isVmArrayLikeRecord(value) {
206
- return isVmArrayLikeRecordByKeys(keys(value));
207
- }
208
-
209
- // src/helpers/types/composed.ts
210
- function isVmImmutable(value, checkDeep = false) {
211
- return isVmModule(value) || isVmFunction(value) || isVmConst(value, checkDeep);
212
- }
213
- function isVmValue(value, checkDeep = false) {
214
- return isVmWrapper(value) || isVmFunction(value) || isVmConst(value, checkDeep);
215
- }
216
- function isVmAny(value, checkDeep) {
217
- if (value == null) return true;
218
- return isVmValue(value, checkDeep);
219
- }
220
-
221
- // src/helpers/error.ts
222
- var VmError = class _VmError extends Error {
223
- constructor(message, recovered) {
224
- super(message);
225
- this.recovered = recovered;
226
- this.name = "VmError";
227
- }
228
- /** 从其他错误构造 */
229
- static from(prefix, error, recovered) {
230
- if (prefix) {
231
- if (prefix.endsWith(":")) {
232
- prefix += " ";
233
- } else if (!prefix.endsWith(": ")) {
234
- prefix += ": ";
235
- }
236
- }
237
- let vmError;
238
- if (error instanceof Error) {
239
- vmError = new _VmError(`${prefix}${error.message}`, recovered);
240
- vmError.stack = error.stack;
241
- } else {
242
- vmError = new _VmError(`${prefix}${String(error)}`, recovered);
243
- }
244
- vmError.cause = error;
245
- return vmError;
246
- }
247
- };
248
-
249
- // src/helpers/serialize.ts
250
- import { REG_IDENTIFIER_FULL, REG_ORDINAL_FULL } from "@mirascript/constants";
251
-
252
- // src/helpers/convert/string.ts
253
- function numberToString(value, minusZero) {
254
- if (isNaN(value)) return "nan";
255
- if (value === Infinity) return "inf";
256
- if (value === -Infinity) return "-inf";
257
- if (minusZero && value === 0 && 1 / value < 0) return "-0";
258
- return String(value);
259
- }
260
- function innerToString(value, useBraces) {
261
- if (value == null) return "nil";
262
- if (typeof value == "number") return numberToString(value, false);
263
- if (typeof value == "string" || typeof value == "boolean") return String(value);
264
- if (typeof value == "function") return displayFunction(value);
265
- if (isVmWrapper(value)) return value.toString(useBraces);
266
- if (isVmArray(value)) {
267
- const strings = [];
268
- for (const item of value) {
269
- strings.push(innerToString(item, true));
270
- }
271
- const results = strings.join(", ");
272
- if (!useBraces) return results;
273
- return `[${results}]`;
274
- } else {
275
- const entries2 = keys(value).map((key) => `${key}: ${innerToString(value[key], true)}`).join(", ");
276
- if (!useBraces) return entries2;
277
- return `(${entries2})`;
278
- }
279
- }
280
- function toString(value, fallback) {
281
- if (typeof value == "string") return value;
282
- if (value == null) return "";
283
- try {
284
- return innerToString(value, false);
285
- } catch (ex) {
286
- if (fallback === void 0) {
287
- const e = new VmError(`Failed to convert value to string: ${display(value)}`, "");
288
- e.cause = ex;
289
- throw e;
290
- }
291
- return fallback;
292
- }
293
- }
294
-
295
- // src/helpers/serialize.ts
296
- function serializeNil() {
297
- return "nil";
298
- }
299
- function serializeBoolean(value) {
300
- return value ? "true" : "false";
301
- }
302
- function serializeNumber(value) {
303
- return numberToString(value, true);
304
- }
305
- function serializeStringEscaped(escaped, options) {
306
- return options.serializeStringEscape("\\" + escaped, options);
307
- }
308
- function serializeStringContent(value, options) {
309
- return options.serializeStringContent(value, options);
310
- }
311
- var STRING_QUOTE = `'`;
312
- var STRING_REG = new RegExp(String.raw`[${STRING_QUOTE}\\\$\p{C}\u2028\u2029]`, "gu");
313
- var STRING_MARK = /^[\p{M}]$/u;
314
- function serializeSpecialChar(char, options) {
315
- if (char === STRING_QUOTE) {
316
- return serializeStringEscaped(STRING_QUOTE, options);
317
- } else if (char === "\0") {
318
- return serializeStringEscaped(`0`, options);
319
- } else if (char === "\n") {
320
- return serializeStringEscaped(`n`, options);
321
- } else if (char === "\r") {
322
- return serializeStringEscaped(`r`, options);
323
- } else if (char === " ") {
324
- return serializeStringEscaped(`t`, options);
325
- } else if (char === "\b") {
326
- return serializeStringEscaped(`b`, options);
327
- } else if (char === "\f") {
328
- return serializeStringEscaped(`f`, options);
329
- } else if (char === "\v") {
330
- return serializeStringEscaped(`v`, options);
331
- } else if (char === "\\") {
332
- return serializeStringEscaped(`\\`, options);
333
- } else if (char === "$") {
334
- return serializeStringEscaped(`$`, options);
335
- } else {
336
- const code = char.codePointAt(0);
337
- if (code <= 127) {
338
- return serializeStringEscaped(`x${code.toString(16).padStart(2, "0")}`, options);
339
- } else if (code >= 55296 && code <= 57343) {
340
- return serializeStringContent("�", options);
341
- } else {
342
- return serializeStringEscaped(`u{${code.toString(16)}}`, options);
343
- }
344
- }
345
- }
346
- function serializeStringImpl(value, options) {
347
- const oq = options.serializeStringQuote(STRING_QUOTE, true, options);
348
- const cq = options.serializeStringQuote(STRING_QUOTE, false, options);
349
- if (value.length === 0) {
350
- return oq + cq;
351
- }
352
- let ret = oq;
353
- let lastIndex = 0;
354
- while (value.length > lastIndex) {
355
- const cp = value.codePointAt(lastIndex);
356
- const ch = String.fromCodePoint(cp);
357
- if (!STRING_MARK.test(ch)) break;
358
- ret += serializeStringEscaped(`u{${cp.toString(16)}}`, options);
359
- lastIndex += ch.length;
360
- }
361
- STRING_REG.lastIndex = lastIndex;
362
- let match;
363
- while ((match = STRING_REG.exec(value)) !== null) {
364
- ret += serializeStringContent(value.slice(lastIndex, match.index), options);
365
- lastIndex = STRING_REG.lastIndex;
366
- ret += serializeSpecialChar(match[0], options);
367
- }
368
- ret += serializeStringContent(value.slice(lastIndex), options);
369
- ret += cq;
370
- return ret;
371
- }
372
- function serializeString(value, options) {
373
- return serializeStringImpl(value, getSerializeOptions(options));
374
- }
375
- function serializeRecordKeyDefault(key) {
376
- if (REG_ORDINAL_FULL.test(key) || REG_IDENTIFIER_FULL.test(key)) {
377
- return key;
378
- }
379
- return serializeStringImpl(key, DEFAULT_OPTIONS);
380
- }
381
- function serializeRecordKeyOpt(value, options) {
382
- if (isDefaultOptions(options)) {
383
- return serializeRecordKeyDefault(value);
384
- }
385
- if (REG_ORDINAL_FULL.test(value)) {
386
- return options.serializePropName(Number(value), options);
387
- }
388
- if (REG_IDENTIFIER_FULL.test(value)) {
389
- return options.serializePropName(value, options);
390
- }
391
- return options.serializeString(value, options);
392
- }
393
- function serializeRecordKey(key, options) {
394
- if (isDefaultOptions(options)) {
395
- return serializeRecordKeyDefault(key);
396
- }
397
- return serializeRecordKeyOpt(key, getSerializeOptions(options));
398
- }
399
- function serializeArray(value, depth, options) {
400
- if (depth > options.maxDepth) return `[]`;
401
- if (value.length === 0) return "[]";
402
- let result = "[";
403
- for (let i = 0; i < value.length; i++) {
404
- if (i > 0) result += ", ";
405
- result += serializeImpl(value[i], depth, options);
406
- }
407
- result += "]";
408
- return result;
409
- }
410
- var { valueOf } = Object.prototype;
411
- function customValueOf(value) {
412
- const thisValueOf = value.valueOf;
413
- if (typeof thisValueOf !== "function" || thisValueOf === valueOf) {
414
- return void 0;
415
- }
416
- const customValue = thisValueOf.call(value);
417
- if (customValue === value) return void 0;
418
- return customValue;
419
- }
420
- function serializeRecord(value, depth, options) {
421
- const customValue = customValueOf(value);
422
- if (customValue !== void 0) {
423
- return serializeImpl(customValue, depth - 1, options);
424
- }
425
- if (depth > options.maxDepth) return `()`;
426
- const e = entries(value);
427
- if (e.length === 0) return "()";
428
- if (e.length === 1) {
429
- const [k, v] = e[0];
430
- if (k === "0") {
431
- return `(${serializeImpl(v, depth, options)},)`;
432
- }
433
- return `(${serializeRecordKeyOpt(k, options)}: ${serializeImpl(v, depth, options)})`;
434
- }
435
- const omitKey = isVmArrayLikeRecordByEntries(e);
436
- let result = "(";
437
- for (const [key, val] of e) {
438
- if (result.length > 1) result += ", ";
439
- if (omitKey) {
440
- result += serializeImpl(val, depth, options);
441
- } else {
442
- result += `${serializeRecordKeyOpt(key, options)}: ${serializeImpl(val, depth, options)}`;
443
- }
444
- }
445
- result += ")";
446
- return result;
447
- }
448
- function serializeImpl(value, depth, options) {
449
- if (value == null) {
450
- return options.serializeNil(options);
451
- }
452
- if (typeof value == "boolean") {
453
- return options.serializeBoolean(value, options);
454
- }
455
- if (typeof value == "number") {
456
- return options.serializeNumber(value, options);
457
- }
458
- if (typeof value == "string") {
459
- return options.serializeString(value, options);
460
- }
461
- if (isVmFunction(value)) {
462
- return options.serializeFunction(value, options);
463
- }
464
- if (isVmModule(value)) {
465
- return options.serializeModule(value, depth + 1, options);
466
- }
467
- if (isVmExtern(value)) {
468
- return options.serializeExtern(value, depth + 1, options);
469
- }
470
- if (isVmArray(value)) {
471
- return options.serializeArray(value, depth + 1, options);
472
- }
473
- if (isVmRecord(value)) {
474
- return options.serializeRecord(value, depth + 1, options);
475
- }
476
- value;
477
- return options.serializeNil(options);
478
- }
479
- function serialize(value, options) {
480
- return serializeImpl(value, 0, getSerializeOptions(options));
481
- }
482
- function displayFunction(value) {
483
- try {
484
- const name = getVmFunctionInfo(value)?.fullName;
485
- return name ? `<function ${name}>` : `<function>`;
486
- } catch {
487
- return `<function>`;
488
- }
489
- }
490
- function displayModule(value) {
491
- try {
492
- return value.toString(true);
493
- } catch {
494
- return `<module>`;
495
- }
496
- }
497
- function displayExtern(value) {
498
- try {
499
- const tag = `<extern ${value.tag}>`;
500
- const rep = value.toString(true);
501
- if (rep === tag || rep.length > 50) {
502
- return tag;
503
- }
504
- return `${tag} ${rep}`;
505
- } catch {
506
- return `<extern>`;
507
- }
508
- }
509
- function display(value, options) {
510
- const opt = mergeOptions(DISPLAY_OPTIONS, options);
511
- return serializeImpl(value, 0, opt);
512
- }
513
- function isDefaultOptions(options) {
514
- return options == null || options === DEFAULT_OPTIONS;
515
- }
516
- function mergeOptions(base, options) {
517
- if (options == null) return base;
518
- let opt = null;
519
- for (const key in options) {
520
- if (!hasOwn(options, key) || !hasOwn(base, key)) continue;
521
- const el = options[key];
522
- if (el == null) continue;
523
- opt ??= { ...base };
524
- opt[key] = el;
525
- }
526
- return opt ? Object.freeze(opt) : base;
527
- }
528
- function getSerializeOptions(options) {
529
- if (isDefaultOptions(options)) return DEFAULT_OPTIONS;
530
- return mergeOptions(DEFAULT_OPTIONS, options);
531
- }
532
- var DEFAULT_OPTIONS = Object.freeze({
533
- maxDepth: 128,
534
- serializeNil,
535
- serializeBoolean,
536
- serializeNumber,
537
- serializeString: serializeStringImpl,
538
- serializeStringQuote: (value) => value,
539
- serializeStringEscape: (value) => value,
540
- serializeStringContent: (value) => value,
541
- serializeArray,
542
- serializeRecord,
543
- serializePropName: String,
544
- serializeFunction: serializeNil,
545
- serializeModule: serializeNil,
546
- serializeExtern: serializeNil
547
- });
548
- var DISPLAY_OPTIONS = Object.freeze({
549
- maxDepth: 3,
550
- serializeNil,
551
- serializeBoolean,
552
- serializeNumber,
553
- serializeString: serializeStringImpl,
554
- serializeStringQuote: (value) => value,
555
- serializeStringEscape: (value) => value,
556
- serializeStringContent: (value) => value,
557
- serializeArray,
558
- serializeRecord,
559
- serializePropName: String,
560
- serializeFunction: displayFunction,
561
- serializeModule: displayModule,
562
- serializeExtern: displayExtern
563
- });
564
-
565
- // src/helpers/convert/index.ts
566
- var convert_exports = {};
567
- __export(convert_exports, {
568
- toBoolean: () => toBoolean,
569
- toFormat: () => toFormat,
570
- toNumber: () => toNumber,
571
- toString: () => toString
572
- });
573
-
574
- // src/helpers/convert/number.ts
575
- var parseInt = (value, radix) => Number.parseInt(value.slice(2), radix);
576
- function parseNumericLiteral(value) {
577
- const ch0 = value.charAt(0);
578
- if (ch0 < "0" || ch0 > "9") return null;
579
- if (/^0[bB][01]+$/.test(value)) return parseInt(value, 2);
580
- if (/^0[oO][0-7]+$/.test(value)) return parseInt(value, 8);
581
- if (/^0[xX][0-9a-fA-F]+$/.test(value)) return parseInt(value, 16);
582
- if (/^[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?$/.test(value)) return Number.parseFloat(value);
583
- return null;
584
- }
585
- function stringToNumber(value) {
586
- value = value.trim();
587
- if (value === "") return null;
588
- if (value === "inf" || value === "+inf" || value === "Infinity" || value === "+Infinity") return PositiveInfinity;
589
- if (value === "-inf" || value === "-Infinity") return NegativeInfinity;
590
- if (value === "nan" || value === "NaN") return NotNumber;
591
- if (value.startsWith("-")) {
592
- const num = parseNumericLiteral(value.slice(1));
593
- if (num != null) return -num;
594
- return null;
595
- } else if (value.startsWith("+")) {
596
- return parseNumericLiteral(value.slice(1));
597
- } else {
598
- return parseNumericLiteral(value);
599
- }
600
- }
601
- function toNumber(value, fallback) {
602
- if (typeof value == "number") return value;
603
- if (typeof value == "boolean") return value ? 1 : 0;
604
- if (typeof value == "string") {
605
- const num = stringToNumber(value);
606
- if (num !== null) return num;
607
- }
608
- if (fallback === void 0) {
609
- throw new VmError(`Failed to convert value to number: ${display(value)}`, NotNumber);
610
- }
611
- return fallback;
612
- }
613
-
614
- // src/helpers/convert/format.ts
615
- function formatNumber(finite) {
616
- if (finite === 0) return "0";
617
- const s = finite.toString();
618
- let ps;
619
- const abs = Math.abs(finite);
620
- if (abs >= 1e3 || abs < 1e-3) {
621
- const ps1 = finite.toExponential();
622
- const ps2 = finite.toExponential(5);
623
- ps = ps1.length < ps2.length ? ps1 : ps2;
624
- } else {
625
- ps = finite.toPrecision(6);
626
- }
627
- return ps.length < s.length ? ps : s;
628
- }
629
- function toFormat(value, format) {
630
- const f = format == null ? "" : format.trim();
631
- if (typeof value == "number") {
632
- if (!isFinite(value)) {
633
- return toString(value, void 0);
634
- }
635
- if (/^\.\d+$/.test(f)) {
636
- let digits = Math.trunc(Number(f.slice(1)));
637
- if (!(digits <= 100)) digits = 100;
638
- return value.toFixed(digits);
639
- } else {
640
- return formatNumber(value);
641
- }
642
- }
643
- return toString(value, void 0);
644
- }
645
-
646
- // src/helpers/convert/boolean.ts
647
- function toBoolean(value, fallback) {
648
- if (typeof value == "boolean") return value;
649
- if (fallback === void 0) {
650
- throw new VmError(`Failed to convert value to boolean: ${display(value)}`, false);
651
- }
652
- return fallback;
653
- }
654
-
655
19
  // src/compiler/generate-bytecode.ts
656
20
  import { getModule, loadModule } from "@mirascript/bindings";
657
21
  function generateBytecodeSync(script, options) {
@@ -668,23 +32,7 @@ async function generateBytecode(script, options) {
668
32
  return [result.chunk, result.diagnostics];
669
33
  }
670
34
 
671
- // src/compiler/emit/index.ts
672
- import { OpCode } from "@mirascript/constants";
673
-
674
- // src/compiler/emit/consts.ts
675
- var { stringify } = JSON;
676
- function toJsLiteral(value) {
677
- if (value === null) return "null";
678
- if (value === void 0) return "undefined";
679
- if (typeof value == "string") {
680
- return stringify(value);
681
- }
682
- if (value === 0) {
683
- if (1 / value === -Infinity) return "-0";
684
- return "0";
685
- }
686
- return String(value);
687
- }
35
+ // src/compiler/bytecode/consts.ts
688
36
  var DECODER = new TextDecoder();
689
37
  var { fromCharCode } = String;
690
38
  function shortStringInJS(buf, begin, length) {
@@ -845,8 +193,78 @@ function readConsts(constChunk) {
845
193
  return consts;
846
194
  }
847
195
 
196
+ // src/compiler/bytecode/index.ts
197
+ var BytecodeReader = class {
198
+ constructor(chunk) {
199
+ this.chunk = chunk;
200
+ this.codeOffset = 0;
201
+ const reader = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
202
+ this.chunkSize = reader.getUint32(0, true);
203
+ this.codeSize = reader.getUint32(4, true);
204
+ this.constSize = reader.getUint32(8 + this.codeSize, true);
205
+ this.codeReader = new DataView(chunk.buffer, chunk.byteOffset + 8, this.codeSize);
206
+ this.constVals = readConsts(
207
+ new Uint8Array(chunk.buffer, chunk.byteOffset + 12 + this.codeSize, this.constSize)
208
+ );
209
+ }
210
+ /** 是否仍有指令可读。 */
211
+ get hasCode() {
212
+ return this.codeOffset < this.codeSize;
213
+ }
214
+ /** 读取操作码。 */
215
+ readOpcode() {
216
+ const offset = this.codeOffset;
217
+ const raw = this.codeReader.getUint8(this.codeOffset++);
218
+ return { opcode: raw & 127, wide: raw >= 128, offset };
219
+ }
220
+ /** 读取操作码。 */
221
+ peekOpcode() {
222
+ const raw = this.codeReader.getUint8(this.codeOffset);
223
+ return raw & 127;
224
+ }
225
+ /** 读取无符号参数。 */
226
+ readParam(wide) {
227
+ const value = wide ? this.codeReader.getUint32(this.codeOffset, true) : this.codeReader.getUint8(this.codeOffset);
228
+ this.codeOffset += wide ? 4 : 1;
229
+ return value;
230
+ }
231
+ /** 读取有符号索引。 */
232
+ readIndex(wide) {
233
+ const value = wide ? this.codeReader.getInt32(this.codeOffset, true) : this.codeReader.getInt8(this.codeOffset);
234
+ this.codeOffset += wide ? 4 : 1;
235
+ return value;
236
+ }
237
+ };
238
+
239
+ // src/compiler/emit/index.ts
240
+ import { OpCode } from "@mirascript/constants";
241
+
848
242
  // src/compiler/emit/sourcemap.ts
849
243
  import { SourceMapGenerator } from "source-map-js";
244
+
245
+ // src/compiler/emit/utils.ts
246
+ var { stringify } = JSON;
247
+ function toJsLiteral(value) {
248
+ if (value === null) return "null";
249
+ if (value === void 0) return "undefined";
250
+ if (typeof value == "string") {
251
+ return stringify(value);
252
+ }
253
+ if (value === 0) {
254
+ if (1 / value === -Infinity) return "-0";
255
+ return "0";
256
+ }
257
+ return String(value);
258
+ }
259
+ function createArray(length, fn) {
260
+ const result = [];
261
+ for (let i = 0; i < length; i++) {
262
+ result.push(fn(i));
263
+ }
264
+ return result;
265
+ }
266
+
267
+ // src/compiler/emit/sourcemap.ts
850
268
  var ORIGIN = `mira://MiraScript/`;
851
269
  var { SOURCE_URL, SOURCE_MAPPING_URL } = ((source, mapping, url) => {
852
270
  const prefix = "//# ";
@@ -938,21 +356,13 @@ function emit(source, chunk, sourcemaps, options) {
938
356
  const code = gen.codeLines.join("\n");
939
357
  return code;
940
358
  }
941
- function createArray(length, fn) {
942
- const result = [];
943
- for (let i = 0; i < length; i++) {
944
- result.push(fn(i));
945
- }
946
- return result;
947
- }
948
- var Emitter = class {
359
+ var Emitter = class extends BytecodeReader {
949
360
  constructor(source, chunk, sourcemaps, options) {
361
+ super(chunk);
950
362
  this.source = source;
951
- this.chunk = chunk;
952
363
  this.sourcemaps = sourcemaps;
953
364
  this.options = options;
954
365
  this.constLits = [];
955
- this.codeOffset = 0;
956
366
  this.closureCounter = 0;
957
367
  this.identCounter = 0;
958
368
  /** 记录函数声明所在位置 */
@@ -961,14 +371,6 @@ var Emitter = class {
961
371
  this.useGlobal = false;
962
372
  this.codeLines = [];
963
373
  this.pretty = options.pretty ?? false;
964
- const reader = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
965
- this.chunkSize = reader.getUint32(0, true);
966
- this.codeSize = reader.getUint32(4, true);
967
- this.constSize = reader.getUint32(8 + this.codeSize, true);
968
- this.codeReader = new DataView(chunk.buffer, chunk.byteOffset + 8, this.codeSize);
969
- this.constVals = readConsts(
970
- new Uint8Array(chunk.buffer, chunk.byteOffset + 12 + this.codeSize, this.constSize)
971
- );
972
374
  }
973
375
  /** 读取常量表 */
974
376
  readConsts() {
@@ -984,13 +386,7 @@ var Emitter = class {
984
386
  }
985
387
  /** 读取全局变量 */
986
388
  rg(constIdx) {
987
- const constName = this.constVals[constIdx];
988
- let lit;
989
- if (typeof constName == "string") {
990
- lit = this.constLits[constIdx];
991
- } else {
992
- lit = toJsLiteral(toString(constName, void 0));
993
- }
389
+ const lit = this.constLits[constIdx];
994
390
  this.useGlobal = true;
995
391
  return `global.get(${lit})`;
996
392
  }
@@ -1005,25 +401,12 @@ var Emitter = class {
1005
401
  if (!i) return "_";
1006
402
  return this.rv(i, level);
1007
403
  }
1008
- /** 读取 code param */
1009
- readParam(wide) {
1010
- const value = wide ? this.codeReader.getUint32(this.codeOffset, true) : this.codeReader.getUint8(this.codeOffset);
1011
- this.codeOffset += wide ? 4 : 1;
1012
- return value;
1013
- }
1014
- /** 读取 code param */
1015
- readIndex(wide) {
1016
- const value = wide ? this.codeReader.getInt32(this.codeOffset, true) : this.codeReader.getInt8(this.codeOffset);
1017
- this.codeOffset += wide ? 4 : 1;
1018
- return value;
1019
- }
1020
404
  /** 读取闭包 */
1021
405
  readClosure() {
1022
406
  this.closureCounter++;
1023
407
  this.identCounter++;
1024
408
  while (this.codeOffset < this.codeSize) {
1025
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
1026
- const opcode = opcode_raw & 127;
409
+ const opcode = this.peekOpcode();
1027
410
  if (opcode !== OpCode.FuncEnd) {
1028
411
  this.readCode();
1029
412
  continue;
@@ -1039,8 +422,7 @@ var Emitter = class {
1039
422
  /** 读取块结束 */
1040
423
  readBlockEnd(end) {
1041
424
  while (this.codeOffset < this.codeSize) {
1042
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
1043
- const opcode = opcode_raw & 127;
425
+ const opcode = this.peekOpcode();
1044
426
  if (opcode !== end) {
1045
427
  this.readCode();
1046
428
  continue;
@@ -1059,8 +441,7 @@ var Emitter = class {
1059
441
  readIfElse() {
1060
442
  this.identCounter++;
1061
443
  while (this.codeOffset < this.codeSize) {
1062
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
1063
- const opcode = opcode_raw & 127;
444
+ const opcode = this.peekOpcode();
1064
445
  if (opcode === OpCode.IfEnd) {
1065
446
  return this.readBlockEnd(OpCode.IfEnd);
1066
447
  }
@@ -1078,9 +459,7 @@ var Emitter = class {
1078
459
  readModule(obj) {
1079
460
  this.identCounter++;
1080
461
  while (this.codeOffset < this.codeSize) {
1081
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1082
- const opcode = opcode_raw & 127;
1083
- const wide = opcode_raw >= 128;
462
+ const { opcode, wide } = this.readOpcode();
1084
463
  const read = () => this.readParam(wide);
1085
464
  let code;
1086
465
  switch (opcode) {
@@ -1115,9 +494,7 @@ var Emitter = class {
1115
494
  readRecord(obj) {
1116
495
  this.identCounter++;
1117
496
  while (this.codeOffset < this.codeSize) {
1118
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1119
- const opcode = opcode_raw & 127;
1120
- const wide = opcode_raw >= 128;
497
+ const { opcode, wide } = this.readOpcode();
1121
498
  const read = () => this.readParam(wide);
1122
499
  let code;
1123
500
  switch (opcode) {
@@ -1175,9 +552,7 @@ var Emitter = class {
1175
552
  readArray(arr) {
1176
553
  this.identCounter++;
1177
554
  while (this.codeOffset < this.codeSize) {
1178
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1179
- const opcode = opcode_raw & 127;
1180
- const wide = opcode_raw >= 128;
555
+ const { opcode, wide } = this.readOpcode();
1181
556
  const read = () => this.readParam(wide);
1182
557
  let code;
1183
558
  switch (opcode) {
@@ -1228,9 +603,7 @@ var Emitter = class {
1228
603
  }
1229
604
  /** 读取代码 */
1230
605
  readCode() {
1231
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1232
- const opcode = opcode_raw & 127;
1233
- const wide = opcode_raw >= 128;
606
+ const { opcode, wide } = this.readOpcode();
1234
607
  const read = () => this.readParam(wide);
1235
608
  const readIndex = () => this.readIndex(wide);
1236
609
  const ident = this.ident();
@@ -1516,7 +889,7 @@ var Emitter = class {
1516
889
  }
1517
890
  case OpCode.Module: {
1518
891
  reg = read();
1519
- const nameIdx = readIndex();
892
+ const nameIdx = read();
1520
893
  const name = this.constLits[nameIdx];
1521
894
  code = `${this.wv(reg)} = $Module(${name}, {`;
1522
895
  break;
@@ -1662,6 +1035,45 @@ var Emitter = class {
1662
1035
 
1663
1036
  // src/compiler/diagnostic.ts
1664
1037
  import { DIAGNOSTIC_MESSAGES, DiagnosticCode } from "@mirascript/constants";
1038
+
1039
+ // src/helpers/utils.ts
1040
+ var { isArray } = Array;
1041
+ var {
1042
+ isFinite,
1043
+ isNaN,
1044
+ isInteger,
1045
+ isSafeInteger,
1046
+ NaN: NotNumber,
1047
+ POSITIVE_INFINITY: PositiveInfinity,
1048
+ NEGATIVE_INFINITY: NegativeInfinity
1049
+ } = Number;
1050
+ var { hasOwn, keys, values, entries, create, fromEntries, defineProperty, getOwnPropertyNames, freeze } = Object;
1051
+ var { apply, getPrototypeOf } = Reflect;
1052
+ var keysLength = "keysLength" in Object && typeof Object.keysLength == "function" ? Object.keysLength : (o) => keys(o).length;
1053
+ var hasOwnEnumerable = Function.call.bind(
1054
+ // eslint-disable-next-line @typescript-eslint/unbound-method
1055
+ Object.prototype.propertyIsEnumerable
1056
+ );
1057
+ var SPECIAL_KEYS = new Set(getOwnPropertyNames(Object.prototype));
1058
+ var setRecord = (obj, key, value) => {
1059
+ if (!SPECIAL_KEYS.has(key)) {
1060
+ obj[key] = value ?? null;
1061
+ } else {
1062
+ defineProperty(obj, key, {
1063
+ __proto__: null,
1064
+ value: value ?? null,
1065
+ configurable: true,
1066
+ writable: true,
1067
+ enumerable: true
1068
+ });
1069
+ }
1070
+ };
1071
+ var getRecord = (obj, key) => {
1072
+ if (!hasOwnEnumerable(obj, key)) return void 0;
1073
+ return obj[key] ?? null;
1074
+ };
1075
+
1076
+ // src/compiler/diagnostic.ts
1665
1077
  function getDiagnosticMessage(code) {
1666
1078
  if (code < 0 || code >= 65535 || !isSafeInteger(code)) {
1667
1079
  throw new RangeError(`Invalid DiagnosticCode: ${code}`);
@@ -1811,6 +1223,7 @@ function formatDiagnostics(diagnostics, source, fileName) {
1811
1223
 
1812
1224
  export {
1813
1225
  __export,
1226
+ __reExport,
1814
1227
  isArray,
1815
1228
  isFinite,
1816
1229
  isNaN,
@@ -1818,6 +1231,7 @@ export {
1818
1231
  isSafeInteger,
1819
1232
  NotNumber,
1820
1233
  PositiveInfinity,
1234
+ NegativeInfinity,
1821
1235
  hasOwn,
1822
1236
  keys,
1823
1237
  values,
@@ -1833,54 +1247,9 @@ export {
1833
1247
  hasOwnEnumerable,
1834
1248
  setRecord,
1835
1249
  getRecord,
1836
- VM_ARRAY_MAX_LENGTH,
1837
- kVmScript,
1838
- kVmFunction,
1839
- kVmFunctionProxy,
1840
- kVmContext,
1841
- kVmExtern,
1842
- kVmModule,
1843
- kVmWrapper,
1844
- VM_SCRIPT_NAME,
1845
- VM_FUNCTION_ANONYMOUS_NAME,
1846
- constants_exports,
1847
- isVmScript,
1848
- isVmContext,
1849
- isVmFunction,
1850
- getVmFunctionInfo,
1851
- isVmWrapper,
1852
- isVmModule,
1853
- isVmExtern,
1854
- isVmCallable,
1855
- isVmPrimitive,
1856
- isVmArray,
1857
- isVmRecord,
1858
- isVmConst,
1859
- getVmType,
1860
- isVmArrayLikeRecordByEntries,
1861
- isVmArrayLikeRecordByKeys,
1862
- isVmArrayLikeRecord,
1863
- isVmImmutable,
1864
- isVmValue,
1865
- isVmAny,
1866
- VmError,
1867
- innerToString,
1868
- toString,
1869
- serializeNil,
1870
- serializeBoolean,
1871
- serializeNumber,
1872
- serializeString,
1873
- serializeRecordKey,
1874
- serializeArray,
1875
- serializeRecord,
1876
- serialize,
1877
- display,
1878
- toNumber,
1879
- toFormat,
1880
- toBoolean,
1881
- convert_exports,
1882
1250
  generateBytecodeSync,
1883
1251
  generateBytecode,
1252
+ BytecodeReader,
1884
1253
  emit,
1885
1254
  DiagnosticCode,
1886
1255
  getDiagnosticMessage,
@@ -1889,4 +1258,4 @@ export {
1889
1258
  parseDiagnostics,
1890
1259
  formatDiagnostics
1891
1260
  };
1892
- //# sourceMappingURL=chunk-LSPCAN32.js.map
1261
+ //# sourceMappingURL=chunk-HHKY7Y7L.js.map