@mirascript/mirascript 0.1.18 → 0.1.20

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 (86) hide show
  1. package/dist/{chunk-5AEWM4W6.js → chunk-4T5YE7LC.js} +241 -177
  2. package/dist/chunk-4T5YE7LC.js.map +6 -0
  3. package/dist/{chunk-RLWIIOH5.js → chunk-BV5YXQYA.js} +1 -1
  4. package/dist/{chunk-RLWIIOH5.js.map → chunk-BV5YXQYA.js.map} +1 -1
  5. package/dist/chunk-OVTSNACH.js +152 -0
  6. package/dist/chunk-OVTSNACH.js.map +6 -0
  7. package/dist/{chunk-3BAOZ2P2.js → chunk-ZKL4MRMP.js} +185 -212
  8. package/dist/chunk-ZKL4MRMP.js.map +6 -0
  9. package/dist/cli/index.js +6 -5
  10. package/dist/cli/index.js.map +1 -1
  11. package/dist/compiler/emit/constants.d.ts +1 -2
  12. package/dist/compiler/emit/constants.d.ts.map +1 -1
  13. package/dist/compiler/emit/consts.d.ts +2 -2
  14. package/dist/compiler/emit/consts.d.ts.map +1 -1
  15. package/dist/compiler/emit/index.d.ts +2 -3
  16. package/dist/compiler/emit/index.d.ts.map +1 -1
  17. package/dist/compiler/emit/sourcemap.d.ts +1 -2
  18. package/dist/compiler/emit/sourcemap.d.ts.map +1 -1
  19. package/dist/compiler/emit-script.d.ts +10 -0
  20. package/dist/compiler/emit-script.d.ts.map +1 -0
  21. package/dist/compiler/generate-bytecode.d.ts +4 -2
  22. package/dist/compiler/generate-bytecode.d.ts.map +1 -1
  23. package/dist/compiler/index.d.ts +1 -6
  24. package/dist/compiler/index.d.ts.map +1 -1
  25. package/dist/compiler/load-module.d.ts +2 -0
  26. package/dist/compiler/load-module.d.ts.map +1 -0
  27. package/dist/compiler/worker.js +1 -1
  28. package/dist/helpers/constants.d.ts +2 -0
  29. package/dist/helpers/constants.d.ts.map +1 -1
  30. package/dist/helpers/serialize.d.ts.map +1 -1
  31. package/dist/helpers/utils.d.ts.map +1 -1
  32. package/dist/index.js +6 -11
  33. package/dist/subtle.d.ts +3 -1
  34. package/dist/subtle.d.ts.map +1 -1
  35. package/dist/subtle.js +3 -3
  36. package/dist/vm/checkpoint.d.ts +1 -1
  37. package/dist/vm/checkpoint.d.ts.map +1 -1
  38. package/dist/vm/helpers.d.ts +6 -1
  39. package/dist/vm/helpers.d.ts.map +1 -1
  40. package/dist/vm/lib/global/json.d.ts.map +1 -1
  41. package/dist/vm/lib/global/math-const.d.ts +24 -2
  42. package/dist/vm/lib/global/math-const.d.ts.map +1 -1
  43. package/dist/vm/lib/helpers.d.ts +5 -3
  44. package/dist/vm/lib/helpers.d.ts.map +1 -1
  45. package/dist/vm/lib/index.d.ts.map +1 -1
  46. package/dist/vm/lib/loader.d.ts +12 -5
  47. package/dist/vm/lib/loader.d.ts.map +1 -1
  48. package/dist/vm/types/context.d.ts +10 -18
  49. package/dist/vm/types/context.d.ts.map +1 -1
  50. package/dist/vm/types/extern.d.ts +1 -1
  51. package/dist/vm/types/extern.d.ts.map +1 -1
  52. package/dist/vm/types/module.d.ts +1 -1
  53. package/dist/vm/types/module.d.ts.map +1 -1
  54. package/dist/vm/types/wrapper.d.ts +4 -2
  55. package/dist/vm/types/wrapper.d.ts.map +1 -1
  56. package/package.json +4 -4
  57. package/src/compiler/emit/constants.ts +1 -2
  58. package/src/compiler/emit/consts.ts +166 -29
  59. package/src/compiler/emit/index.ts +40 -32
  60. package/src/compiler/emit/sourcemap.ts +21 -76
  61. package/src/compiler/emit-script.ts +30 -0
  62. package/src/compiler/generate-bytecode.ts +5 -8
  63. package/src/compiler/index.ts +2 -30
  64. package/src/compiler/load-module.ts +3 -0
  65. package/src/helpers/constants.ts +2 -0
  66. package/src/helpers/serialize.ts +1 -4
  67. package/src/helpers/utils.ts +2 -7
  68. package/src/subtle.ts +3 -1
  69. package/src/vm/checkpoint.ts +42 -17
  70. package/src/vm/helpers.ts +12 -5
  71. package/src/vm/lib/global/json.ts +2 -0
  72. package/src/vm/lib/global/math-const.ts +34 -2
  73. package/src/vm/lib/helpers.ts +10 -11
  74. package/src/vm/lib/index.ts +7 -3
  75. package/src/vm/lib/loader.ts +56 -19
  76. package/src/vm/types/context.ts +58 -30
  77. package/src/vm/types/extern.ts +1 -1
  78. package/src/vm/types/module.ts +1 -1
  79. package/src/vm/types/wrapper.ts +9 -5
  80. package/dist/chunk-3BAOZ2P2.js.map +0 -6
  81. package/dist/chunk-5AEWM4W6.js.map +0 -6
  82. package/dist/chunk-RIT53WVY.js +0 -1
  83. package/dist/chunk-RIT53WVY.js.map +0 -6
  84. package/dist/compiler/emit/globals.d.ts +0 -17
  85. package/dist/compiler/emit/globals.d.ts.map +0 -1
  86. package/src/compiler/emit/globals.ts +0 -39
@@ -10,9 +10,11 @@ __export(constants_exports, {
10
10
  REG_BIN: () => REG_BIN,
11
11
  REG_HEX: () => REG_HEX,
12
12
  REG_IDENTIFIER: () => REG_IDENTIFIER,
13
+ REG_IDENTIFIER_FULL: () => REG_IDENTIFIER_FULL,
13
14
  REG_NUMBER: () => REG_NUMBER,
14
15
  REG_OCT: () => REG_OCT,
15
16
  REG_ORDINAL: () => REG_ORDINAL,
17
+ REG_ORDINAL_FULL: () => REG_ORDINAL_FULL,
16
18
  REG_WHITESPACE: () => REG_WHITESPACE,
17
19
  VM_ARRAY_MAX_LENGTH: () => VM_ARRAY_MAX_LENGTH,
18
20
  VM_FUNCTION_ANONYMOUS_NAME: () => VM_FUNCTION_ANONYMOUS_NAME,
@@ -27,19 +29,21 @@ __export(constants_exports, {
27
29
  });
28
30
  var REG_IDENTIFIER = /(?:_+|@+|\$+|\p{XID_Start})\p{XID_Continue}*/u;
29
31
  var REG_ORDINAL = /(?:214748364[0-7]|21474836[0-3]\d|2147483[0-5]\d{2}|214748[0-2]\d{3}|21474[0-7]\d{4}|2147[0-3]\d{5}|214[0-6]\d{6}|21[0-3]\d{7}|20\d{8}|1\d{9}|[1-9]\d{0,8}|0)/;
32
+ var REG_IDENTIFIER_FULL = new RegExp(`^${REG_IDENTIFIER.source}$`, REG_IDENTIFIER.flags);
33
+ var REG_ORDINAL_FULL = new RegExp(`^${REG_ORDINAL.source}$`, REG_ORDINAL.flags);
30
34
  var REG_WHITESPACE = /[ \t\v\f\r\n]/u;
31
35
  var REG_HEX = /0[xX][a-fA-F0-9_]*[a-fA-F0-9]/;
32
36
  var REG_OCT = /0[oO][0-7_]*[0-7]/;
33
37
  var REG_BIN = /0[bB][01_]*[01]/;
34
38
  var REG_NUMBER = /\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?[\d_]*\d)?/u;
35
39
  var VM_ARRAY_MAX_LENGTH = 2 ** 31 - 1;
36
- var kVmScript = Symbol.for("mirascript.vm.script");
37
- var kVmFunction = Symbol.for("mirascript.vm.function");
38
- var kVmFunctionProxy = Symbol.for("mirascript.vm.function.proxy");
39
- var kVmContext = Symbol.for("mirascript.vm.context");
40
- var kVmExtern = Symbol.for("mirascript.vm.extern");
41
- var kVmModule = Symbol.for("mirascript.vm.module");
42
- var kVmWrapper = Symbol.for("mirascript.vm.wrapper");
40
+ var kVmScript = /* @__PURE__ */ Symbol.for("mirascript.vm.script");
41
+ var kVmFunction = /* @__PURE__ */ Symbol.for("mirascript.vm.function");
42
+ var kVmFunctionProxy = /* @__PURE__ */ Symbol.for("mirascript.vm.function.proxy");
43
+ var kVmContext = /* @__PURE__ */ Symbol.for("mirascript.vm.context");
44
+ var kVmExtern = /* @__PURE__ */ Symbol.for("mirascript.vm.extern");
45
+ var kVmModule = /* @__PURE__ */ Symbol.for("mirascript.vm.module");
46
+ var kVmWrapper = /* @__PURE__ */ Symbol.for("mirascript.vm.wrapper");
43
47
  var VM_SCRIPT_NAME = `<script_root>`;
44
48
  var VM_FUNCTION_ANONYMOUS_NAME = `<anonymous>`;
45
49
 
@@ -223,8 +227,6 @@ function isVmValue(value, checkDeep = false) {
223
227
  }
224
228
 
225
229
  // src/helpers/serialize.ts
226
- var REG_IDENTIFIER_FULL = new RegExp(`^${REG_IDENTIFIER.source}$`, REG_IDENTIFIER.flags);
227
- var REG_ORDINAL_FULL = new RegExp(`^${REG_ORDINAL.source}$`, REG_ORDINAL.flags);
228
230
  var DEFAULT_OPTIONS = Object.freeze({
229
231
  maxDepth: 128,
230
232
  serializeNil,
@@ -494,6 +496,22 @@ var VmError = class _VmError extends Error {
494
496
  }
495
497
  };
496
498
 
499
+ // src/compiler/generate-bytecode.ts
500
+ import { getModule, loadModule } from "@mirascript/bindings";
501
+ function generateBytecodeSync(script, options) {
502
+ const module = getModule();
503
+ const result = module.compileSync(script, options);
504
+ return [result.chunk, result.diagnostics];
505
+ }
506
+ async function generateBytecode(script, options) {
507
+ if (options == null) {
508
+ throw new TypeError("options must be provided");
509
+ }
510
+ const module = await loadModule();
511
+ const result = "compile" in module ? await module.compile(script, options) : module.compileSync(script, options);
512
+ return [result.chunk, result.diagnostics];
513
+ }
514
+
497
515
  // src/compiler/emit/index.ts
498
516
  import { OpCode } from "@mirascript/bindings";
499
517
 
@@ -540,11 +558,12 @@ function toString(value, fallback) {
540
558
  }
541
559
 
542
560
  // src/compiler/emit/consts.ts
561
+ var { stringify } = JSON;
543
562
  function toJsLiteral(value) {
544
563
  if (value === null) return "null";
545
564
  if (value === void 0) return "undefined";
546
565
  if (typeof value == "string") {
547
- return JSON.stringify(value);
566
+ return stringify(value);
548
567
  }
549
568
  if (value === 0) {
550
569
  if (1 / value === -Infinity) return "-0";
@@ -552,62 +571,172 @@ function toJsLiteral(value) {
552
571
  }
553
572
  return String(value);
554
573
  }
555
- function readConst(reader, offset) {
556
- const type = reader.getUint8(offset);
557
- switch (type) {
558
- /* c8 ignore next 2 */
559
- case 0:
560
- return [null, 1];
561
- case 1:
562
- return [true, 1];
563
- case 2:
564
- return [false, 1];
565
- case 3: {
566
- const ordinal = reader.getInt32(offset + 1, true);
567
- return [ordinal, 5];
574
+ var DECODER = new TextDecoder();
575
+ var { fromCharCode } = String;
576
+ function shortStringInJS(buf, begin, length) {
577
+ if (length < 4) {
578
+ if (length < 2) {
579
+ if (length === 0) return "";
580
+ const a3 = buf[begin];
581
+ if ((a3 & 128) > 0) {
582
+ return;
583
+ }
584
+ return fromCharCode(a3);
568
585
  }
569
- case 4: {
570
- const num = reader.getFloat64(offset + 1, true);
571
- return [num, 9];
586
+ const a2 = buf[begin++];
587
+ const b2 = buf[begin++];
588
+ if ((a2 & 128) > 0 || (b2 & 128) > 0) {
589
+ return;
572
590
  }
573
- case 5: {
574
- const len = reader.getUint32(offset + 1, true);
575
- const str = new TextDecoder().decode(new Uint8Array(reader.buffer, reader.byteOffset + offset + 5, len));
576
- return [str, 5 + len];
591
+ if (length < 3) return fromCharCode(a2, b2);
592
+ const c2 = buf[begin++];
593
+ if ((c2 & 128) > 0) {
594
+ return;
577
595
  }
578
- /* c8 ignore next 2 */
579
- default:
580
- throw new Error(`Unknown constant type: ${type}`);
596
+ return fromCharCode(a2, b2, c2);
597
+ }
598
+ const a = buf[begin++];
599
+ const b = buf[begin++];
600
+ const c = buf[begin++];
601
+ const d = buf[begin++];
602
+ if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
603
+ return;
604
+ }
605
+ if (length < 6) {
606
+ if (length === 4) return fromCharCode(a, b, c, d);
607
+ const e2 = buf[begin++];
608
+ if ((e2 & 128) > 0) {
609
+ return;
610
+ }
611
+ return fromCharCode(a, b, c, d, e2);
612
+ }
613
+ const e = buf[begin++];
614
+ const f = buf[begin++];
615
+ if ((e & 128) > 0 || (f & 128) > 0) {
616
+ return;
617
+ }
618
+ if (length < 8) {
619
+ if (length < 7) return fromCharCode(a, b, c, d, e, f);
620
+ const g2 = buf[begin++];
621
+ if ((g2 & 128) > 0) {
622
+ return;
623
+ }
624
+ return fromCharCode(a, b, c, d, e, f, g2);
625
+ }
626
+ const g = buf[begin++];
627
+ const h = buf[begin++];
628
+ if ((g & 128) > 0 || (h & 128) > 0) {
629
+ return;
630
+ }
631
+ if (length < 10) {
632
+ if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
633
+ const i2 = buf[begin++];
634
+ if ((i2 & 128) > 0) {
635
+ return;
636
+ }
637
+ return fromCharCode(a, b, c, d, e, f, g, h, i2);
638
+ }
639
+ const i = buf[begin++];
640
+ const j = buf[begin++];
641
+ if ((i & 128) > 0 || (j & 128) > 0) {
642
+ return;
643
+ }
644
+ if (length < 12) {
645
+ if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
646
+ const k2 = buf[begin++];
647
+ if ((k2 & 128) > 0) {
648
+ return;
649
+ }
650
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k2);
651
+ }
652
+ const k = buf[begin++];
653
+ const l = buf[begin++];
654
+ if ((k & 128) > 0 || (l & 128) > 0) {
655
+ return;
656
+ }
657
+ if (length < 14) {
658
+ if (length === 12) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
659
+ const m2 = buf[begin++];
660
+ if ((m2 & 128) > 0) {
661
+ begin -= 13;
662
+ return;
663
+ }
664
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m2);
581
665
  }
666
+ const m = buf[begin++];
667
+ const n = buf[begin++];
668
+ if ((m & 128) > 0 || (n & 128) > 0) {
669
+ return;
670
+ }
671
+ if (length < 15) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
672
+ const o = buf[begin++];
673
+ if ((o & 128) > 0) {
674
+ return;
675
+ }
676
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
582
677
  }
583
-
584
- // src/compiler/emit/globals.ts
585
- function readGlobal(emitter, constIdx) {
586
- const { globals } = emitter;
587
- const cached = globals.get(constIdx);
588
- if (cached) return cached;
589
- const constName = emitter.constVals[constIdx];
590
- let name, lit;
591
- if (typeof constName == "string") {
592
- name = constName;
593
- lit = emitter.constLits[constIdx];
594
- } else {
595
- name = toString(constName, void 0);
596
- lit = toJsLiteral(name);
597
- }
598
- const val = `g${globals.size + 1}`;
599
- const expr = `(${val} === undefined ? (${val} = global.get(${lit})) : ${val})`;
600
- const desc = { v: val, l: lit, e: expr, n: name };
601
- globals.set(constIdx, desc);
602
- return desc;
678
+ function decodeStr(buffer, offset, length) {
679
+ if (length < 16) {
680
+ const shortStr = shortStringInJS(buffer, offset, length);
681
+ if (shortStr !== void 0) {
682
+ return shortStr;
683
+ }
684
+ }
685
+ return DECODER.decode(buffer.subarray(offset, offset + length));
686
+ }
687
+ function readConsts(constChunk) {
688
+ const reader = new DataView(constChunk.buffer, constChunk.byteOffset, constChunk.byteLength);
689
+ const consts = [];
690
+ let offset = 0;
691
+ const length = reader.byteLength;
692
+ while (offset < length) {
693
+ const type = reader.getUint8(offset);
694
+ switch (type) {
695
+ /* c8 ignore next 2 */
696
+ case 0:
697
+ consts.push(null);
698
+ offset += 1;
699
+ break;
700
+ case 1:
701
+ consts.push(true);
702
+ offset += 1;
703
+ break;
704
+ case 2:
705
+ consts.push(false);
706
+ offset += 1;
707
+ break;
708
+ case 3: {
709
+ const ordinal = reader.getInt32(offset + 1, true);
710
+ consts.push(ordinal);
711
+ offset += 5;
712
+ break;
713
+ }
714
+ case 4: {
715
+ const num = reader.getFloat64(offset + 1, true);
716
+ consts.push(num);
717
+ offset += 9;
718
+ break;
719
+ }
720
+ case 5: {
721
+ const len = reader.getUint32(offset + 1, true);
722
+ const str = decodeStr(constChunk, offset + 5, len);
723
+ consts.push(str);
724
+ offset += 5 + len;
725
+ break;
726
+ }
727
+ /* c8 ignore next 2 */
728
+ default:
729
+ throw new Error(`Unknown constant type: ${type}`);
730
+ }
731
+ }
732
+ return consts;
603
733
  }
604
734
 
605
735
  // src/compiler/emit/sourcemap.ts
606
736
  import { SourceMapGenerator } from "source-map-js";
607
737
 
608
738
  // src/compiler/emit/constants.ts
609
- var SCRIPT_PREFIX = `'use strict'; return ((global = GlobalFallback()) => { try { CpEnter();`;
610
- var GLOBAL_HINT = `/* globals */`;
739
+ var SCRIPT_PREFIX = `return ((global = GlobalFallback()) => { try { CpEnter();`;
611
740
 
612
741
  // src/compiler/emit/sourcemap.ts
613
742
  var ORIGIN = `mira://MiraScript/`;
@@ -618,74 +747,10 @@ var { SOURCE_URL, SOURCE_MAPPING_URL } = ((source, mapping, url) => {
618
747
  SOURCE_MAPPING_URL: prefix.concat(source, mapping, url)
619
748
  };
620
749
  })(`source`, `Mapping`, `URL`);
621
- var SOURCE_OFFSET = 3;
750
+ var SOURCE_OFFSET = 4;
622
751
  var toDataUrl = typeof Buffer == "function" && typeof Buffer.from == "function" ? (s) => `data:application/json;base64,${Buffer.from(s, "utf8").toString("base64")}` : (s) => `data:application/json;charset=utf-8,${encodeURIComponent(s)}`;
623
- function addGlobalMappings(globalLine, fileName, map, globals) {
624
- let globalFile = `global;
625
- `;
626
- map.addMapping({
627
- generated: {
628
- line: 3,
629
- column: globalLine.indexOf(`global = `)
630
- },
631
- original: {
632
- line: 1,
633
- column: 0
634
- },
635
- source: fileName,
636
- name: "global"
637
- });
638
- map.addMapping({
639
- generated: {
640
- line: 3,
641
- column: SCRIPT_PREFIX.length
642
- },
643
- original: {
644
- line: 1,
645
- column: 7
646
- },
647
- source: fileName,
648
- name: ""
649
- });
650
- let i = 1;
651
- let pos = globalLine.indexOf(GLOBAL_HINT, SCRIPT_PREFIX.length) + GLOBAL_HINT.length;
652
- for (const p of globals.values()) {
653
- i++;
654
- if (pos < 0) break;
655
- const { v, n } = p;
656
- pos = globalLine.indexOf(v, pos);
657
- if (pos < 0) break;
658
- map.addMapping({
659
- generated: {
660
- line: 3,
661
- column: pos
662
- },
663
- original: {
664
- line: i,
665
- column: 0
666
- },
667
- source: fileName,
668
- name: n
669
- });
670
- globalFile += `${n};
671
- `;
672
- }
673
- map.addMapping({
674
- generated: {
675
- line: 3,
676
- column: pos
677
- },
678
- original: {
679
- line: i,
680
- column: 0
681
- },
682
- source: fileName,
683
- name: ""
684
- });
685
- map.setSourceContent(fileName, globalFile);
686
- }
687
752
  var sourceId = 1;
688
- function createSourceMap(source, sourcemaps, codeLines, globals, options) {
753
+ function createSourceMap(source, sourcemaps, codeLines, functions, options) {
689
754
  let fileName = (options.fileName ?? "").trim();
690
755
  const hasSchema = /^\w+:/.test(fileName);
691
756
  if (!hasSchema) {
@@ -734,17 +799,25 @@ function createSourceMap(source, sourcemaps, codeLines, globals, options) {
734
799
  source: fileName
735
800
  });
736
801
  }
737
- const globalLine = codeLines[0];
738
- if (globalLine?.includes(GLOBAL_HINT)) {
739
- addGlobalMappings(globalLine, `${fileName} <globals>`, map, globals);
802
+ let fnNames = `'use strict';`;
803
+ if (typeof source === "string") {
804
+ const lines = source.split(/\r?\n/);
805
+ for (let i = 0; i < functions.length; i++) {
806
+ const index = functions[i];
807
+ const mapping = sourcemaps[index];
808
+ const line = mapping ? lines[mapping.startLineNumber - 1] : void 0;
809
+ const fnName = mapping && line ? line.slice(mapping.startColumn - 1, mapping.endColumn - 1) : "";
810
+ fnNames += `var fnName_${i} = ${fnName ? toJsLiteral(fnName) : "null"};`;
811
+ }
740
812
  }
741
813
  const sourceURL = hasSchema ? fileName : `${ORIGIN}${fileName}`;
742
814
  const dataUrl = toDataUrl(map.toString());
743
- return [
815
+ codeLines.unshift(fnNames);
816
+ codeLines.push(
744
817
  // Prevent source map from being recognized as of this file
745
818
  `${SOURCE_URL}=${sourceURL}.js`,
746
819
  `${SOURCE_MAPPING_URL}=${dataUrl}`
747
- ];
820
+ );
748
821
  }
749
822
 
750
823
  // src/compiler/emit/index.ts
@@ -761,20 +834,18 @@ function createArray(length, fn) {
761
834
  }
762
835
  return result;
763
836
  }
764
- var SCRIPT_PREFIX2 = `'use strict'; return ((global = GlobalFallback()) => { try { CpEnter();`;
765
- var GLOBAL_HINT2 = `/* globals */`;
766
837
  var Emitter = class {
767
838
  constructor(source, chunk, sourcemaps, options) {
768
839
  this.source = source;
769
840
  this.chunk = chunk;
770
841
  this.sourcemaps = sourcemaps;
771
842
  this.options = options;
772
- this.constVals = [];
773
843
  this.constLits = [];
774
844
  this.codeOffset = 0;
775
845
  this.closureCounter = 0;
776
846
  this.identCounter = 0;
777
- this.globals = /* @__PURE__ */ new Map();
847
+ /** 记录函数声明所在位置 */
848
+ this.functions = [];
778
849
  this.codeLines = [];
779
850
  this.pretty = options.pretty ?? false;
780
851
  const reader = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
@@ -782,15 +853,15 @@ var Emitter = class {
782
853
  this.codeSize = reader.getUint32(4, true);
783
854
  this.constSize = reader.getUint32(8 + this.codeSize, true);
784
855
  this.codeReader = new DataView(chunk.buffer, chunk.byteOffset + 8, this.codeSize);
785
- this.constReader = new DataView(chunk.buffer, chunk.byteOffset + 12 + this.codeSize, this.constSize);
856
+ this.constVals = readConsts(
857
+ new Uint8Array(chunk.buffer, chunk.byteOffset + 12 + this.codeSize, this.constSize)
858
+ );
786
859
  }
787
860
  /** 读取常量表 */
788
861
  readConsts() {
789
- for (let i = 0, index = 0; i < this.constSize; index++) {
790
- const [constant, size] = readConst(this.constReader, i);
791
- this.constVals.push(constant);
792
- this.constLits.push(toJsLiteral(constant));
793
- i += size;
862
+ const { constVals, constLits } = this;
863
+ for (let i = 0, len = constVals.length; i < len; i++) {
864
+ constLits.push(toJsLiteral(constVals[i]));
794
865
  }
795
866
  }
796
867
  /** 制造缩进 */
@@ -799,8 +870,15 @@ var Emitter = class {
799
870
  return " ".repeat(this.identCounter + len);
800
871
  }
801
872
  /** 读取全局变量 */
802
- rg(constIdx, eager) {
803
- return readGlobal(this, constIdx).e;
873
+ rg(constIdx) {
874
+ const constName = this.constVals[constIdx];
875
+ let lit;
876
+ if (typeof constName == "string") {
877
+ lit = this.constLits[constIdx];
878
+ } else {
879
+ lit = toJsLiteral(toString(constName, void 0));
880
+ }
881
+ return `global.get(${lit})`;
804
882
  }
805
883
  /** Read variable */
806
884
  rv(i, level = 0) {
@@ -1028,14 +1106,21 @@ var Emitter = class {
1028
1106
  }
1029
1107
  return `${wv} = null`;
1030
1108
  });
1031
- const regs = createArray(regn - argn + 1, (i) => i ? this.wv(i + argn, -1) : this.wv(0, -1)).join(
1032
- ", "
1033
- );
1109
+ let regs = "_";
1110
+ for (let i = 1 + argn; i < regn + 1; i++) {
1111
+ regs += "," + this.wv(i, -1);
1112
+ }
1034
1113
  const script = startFunc && !varg && argn === 0;
1035
1114
  if (script) {
1036
- code = `${SCRIPT_PREFIX2} var ${regs};`;
1115
+ code = `${SCRIPT_PREFIX} var ${regs};`;
1037
1116
  } else {
1038
- code = `${this.wv(reg)} = Function((${args.join(", ")}) => { try { CpEnter(); var ${regs};`;
1117
+ if (!this.options.sourceMap) {
1118
+ code = `${this.wv(reg)} = Function(null ,(${args.join(", ")}) => { try { CpEnter(); var ${regs};`;
1119
+ } else {
1120
+ const index = this.functions.length;
1121
+ this.functions.push(this.codeLines.length);
1122
+ code = `${this.wv(reg)} = Function(fnName_${index} ,(${args.join(", ")}) => { try { CpEnter(); var ${regs};`;
1123
+ }
1039
1124
  }
1040
1125
  if (varg) {
1041
1126
  code += ` var ${this.wv(argn, -1)} = Vargs(vargs);`;
@@ -1115,7 +1200,7 @@ var Emitter = class {
1115
1200
  const args = createArray(n, () => read());
1116
1201
  const ns = read();
1117
1202
  const spreads = createArray(ns, () => read());
1118
- const callTarget = opcode === OpCode.Call ? this.rg(func, false) : this.rv(func);
1203
+ const callTarget = opcode === OpCode.Call ? this.rg(func) : this.rv(func);
1119
1204
  code = `${this.wv(reg)} = $Call(${callTarget}, [${args.map((a, i) => {
1120
1205
  if (spreads.includes(i)) return `...$ArraySpread(${this.rv(a)})`;
1121
1206
  else return this.rv(a);
@@ -1218,7 +1303,7 @@ var Emitter = class {
1218
1303
  case OpCode.GetGlobal: {
1219
1304
  reg = read();
1220
1305
  const i = read();
1221
- code = `${this.wv(reg)} = ${this.rg(i, false)};`;
1306
+ code = `${this.wv(reg)} = ${this.rg(i)};`;
1222
1307
  break;
1223
1308
  }
1224
1309
  case OpCode.GetGlobalDyn: {
@@ -1402,25 +1487,20 @@ var Emitter = class {
1402
1487
  read() {
1403
1488
  this.readConsts();
1404
1489
  this.readCode();
1405
- if (this.globals.size > 0) {
1406
- let globalsInit = "";
1407
- for (const { v } of this.globals.values()) {
1408
- globalsInit += globalsInit ? `, ${v}` : `var ${GLOBAL_HINT2} ${v}`;
1409
- }
1410
- this.codeLines[0] += globalsInit + ";";
1411
- }
1412
1490
  this.addSourceMap();
1413
1491
  }
1414
1492
  /** 添加源映射 */
1415
1493
  addSourceMap() {
1416
- if (!this.options.sourceMap) return;
1417
- const mapLines = createSourceMap(this.source, this.sourcemaps, this.codeLines, this.globals, this.options);
1418
- this.codeLines.push(...mapLines);
1494
+ if (this.options.sourceMap) {
1495
+ createSourceMap(this.source, this.sourcemaps, this.codeLines, this.functions, this.options);
1496
+ } else {
1497
+ this.codeLines.unshift(`'use strict';`);
1498
+ }
1419
1499
  }
1420
1500
  };
1421
1501
 
1422
1502
  // src/compiler/diagnostic.ts
1423
- import { DiagnosticCode, getModule } from "@mirascript/bindings";
1503
+ import { DiagnosticCode, getModule as getModule2 } from "@mirascript/bindings";
1424
1504
  var diagnosticMessages = /* @__PURE__ */ new Map();
1425
1505
  function getDiagnosticMessage(code) {
1426
1506
  if (code < 0 || code >= 65535 || !isSafeInteger(code)) {
@@ -1430,7 +1510,7 @@ function getDiagnosticMessage(code) {
1430
1510
  if (cached !== void 0) {
1431
1511
  return cached;
1432
1512
  }
1433
- const mod = getModule();
1513
+ const mod = getModule2();
1434
1514
  const msg = mod.getDiagnosticMessage(code);
1435
1515
  diagnosticMessages.set(code, msg);
1436
1516
  return msg;
@@ -1558,22 +1638,6 @@ function formatDiagnostics(diagnostics, source, fileName) {
1558
1638
  return messages;
1559
1639
  }
1560
1640
 
1561
- // src/compiler/generate-bytecode.ts
1562
- import { getModule as getModule2, loadModule } from "@mirascript/bindings";
1563
- function generateBytecodeSync(script, options) {
1564
- const module = getModule2();
1565
- const result = module.compileSync(script, options);
1566
- return [result.chunk, result.diagnostics];
1567
- }
1568
- async function generateBytecode(script, options) {
1569
- if (options == null) {
1570
- throw new TypeError("options must be provided");
1571
- }
1572
- const module = await loadModule();
1573
- const result = "compile" in module ? await module.compile(script, options) : module.compileSync(script, options);
1574
- return [result.chunk, result.diagnostics];
1575
- }
1576
-
1577
1641
  export {
1578
1642
  __export,
1579
1643
  VM_ARRAY_MAX_LENGTH,
@@ -1632,12 +1696,12 @@ export {
1632
1696
  VmError,
1633
1697
  innerToString,
1634
1698
  toString,
1699
+ generateBytecodeSync,
1700
+ generateBytecode,
1635
1701
  emit,
1636
1702
  DiagnosticCode,
1637
1703
  getDiagnosticMessage,
1638
1704
  parseDiagnostics,
1639
- formatDiagnostics,
1640
- generateBytecodeSync,
1641
- generateBytecode
1705
+ formatDiagnostics
1642
1706
  };
1643
- //# sourceMappingURL=chunk-5AEWM4W6.js.map
1707
+ //# sourceMappingURL=chunk-4T5YE7LC.js.map