@mirascript/mirascript 0.1.14 → 0.1.16

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 (216) hide show
  1. package/dist/{chunk-Q74RKZ7O.js → chunk-LU4ZKFF6.js} +1420 -607
  2. package/dist/chunk-LU4ZKFF6.js.map +6 -0
  3. package/dist/chunk-RIT53WVY.js +1 -0
  4. package/dist/chunk-RLWIIOH5.js +12 -0
  5. package/dist/chunk-RLWIIOH5.js.map +6 -0
  6. package/dist/{chunk-55FKP56O.js → chunk-YZGL3D7L.js} +741 -1361
  7. package/dist/chunk-YZGL3D7L.js.map +6 -0
  8. package/dist/cli/index.js +11 -67
  9. package/dist/cli/index.js.map +2 -2
  10. package/dist/compiler/compile-fast.d.ts +1 -1
  11. package/dist/compiler/compile-fast.d.ts.map +1 -1
  12. package/dist/compiler/create-script.d.ts +10 -1
  13. package/dist/compiler/create-script.d.ts.map +1 -1
  14. package/dist/compiler/diagnostic.d.ts +1 -1
  15. package/dist/compiler/diagnostic.d.ts.map +1 -1
  16. package/dist/compiler/emit/constants.d.ts +3 -0
  17. package/dist/compiler/emit/constants.d.ts.map +1 -0
  18. package/dist/compiler/emit/consts.d.ts +6 -0
  19. package/dist/compiler/emit/consts.d.ts.map +1 -0
  20. package/dist/compiler/emit/globals.d.ts +17 -0
  21. package/dist/compiler/emit/globals.d.ts.map +1 -0
  22. package/dist/compiler/emit/index.d.ts +58 -0
  23. package/dist/compiler/emit/index.d.ts.map +1 -0
  24. package/dist/compiler/emit/sourcemap.d.ts +6 -0
  25. package/dist/compiler/emit/sourcemap.d.ts.map +1 -0
  26. package/dist/compiler/index.d.ts +3 -2
  27. package/dist/compiler/index.d.ts.map +1 -1
  28. package/dist/compiler/worker.js +1 -1
  29. package/dist/helpers/constants.d.ts +15 -0
  30. package/dist/helpers/constants.d.ts.map +1 -1
  31. package/dist/helpers/convert.d.ts +12 -0
  32. package/dist/helpers/convert.d.ts.map +1 -0
  33. package/dist/{vm → helpers}/error.d.ts +1 -1
  34. package/dist/helpers/error.d.ts.map +1 -0
  35. package/dist/helpers/serialize.d.ts +14 -5
  36. package/dist/helpers/serialize.d.ts.map +1 -1
  37. package/dist/helpers/types.d.ts +54 -0
  38. package/dist/helpers/types.d.ts.map +1 -0
  39. package/dist/index.js +11 -17
  40. package/dist/subtle.d.ts +3 -2
  41. package/dist/subtle.d.ts.map +1 -1
  42. package/dist/subtle.js +21 -15
  43. package/dist/vm/checkpoint.d.ts +9 -0
  44. package/dist/vm/checkpoint.d.ts.map +1 -0
  45. package/dist/vm/helpers.d.ts +4 -10
  46. package/dist/vm/helpers.d.ts.map +1 -1
  47. package/dist/vm/index.d.ts +3 -3
  48. package/dist/vm/index.d.ts.map +1 -1
  49. package/dist/vm/lib/global/bit.d.ts +7 -7
  50. package/dist/vm/lib/global/bit.d.ts.map +1 -1
  51. package/dist/vm/lib/global/debug.d.ts +2 -2
  52. package/dist/vm/lib/global/debug.d.ts.map +1 -1
  53. package/dist/vm/lib/global/index.d.ts +0 -1
  54. package/dist/vm/lib/global/index.d.ts.map +1 -1
  55. package/dist/vm/lib/global/json.d.ts +2 -2
  56. package/dist/vm/lib/global/json.d.ts.map +1 -1
  57. package/dist/vm/lib/global/math-additional.d.ts +1 -1
  58. package/dist/vm/lib/global/math-additional.d.ts.map +1 -1
  59. package/dist/vm/lib/global/math-arr.d.ts +5 -5
  60. package/dist/vm/lib/global/math-arr.d.ts.map +1 -1
  61. package/dist/vm/lib/global/math-unary.d.ts +26 -26
  62. package/dist/vm/lib/global/math-unary.d.ts.map +1 -1
  63. package/dist/vm/lib/global/math.d.ts +3 -3
  64. package/dist/vm/lib/global/math.d.ts.map +1 -1
  65. package/dist/vm/lib/global/sequence/all-any.d.ts +2 -2
  66. package/dist/vm/lib/global/sequence/all-any.d.ts.map +1 -1
  67. package/dist/vm/lib/global/sequence/entries.d.ts +5 -5
  68. package/dist/vm/lib/global/sequence/entries.d.ts.map +1 -1
  69. package/dist/vm/lib/global/sequence/find.d.ts +3 -3
  70. package/dist/vm/lib/global/sequence/find.d.ts.map +1 -1
  71. package/dist/vm/lib/global/sequence/flatten.d.ts +1 -1
  72. package/dist/vm/lib/global/sequence/flatten.d.ts.map +1 -1
  73. package/dist/vm/lib/global/sequence/len.d.ts +1 -1
  74. package/dist/vm/lib/global/sequence/len.d.ts.map +1 -1
  75. package/dist/vm/lib/global/sequence/map-filter.d.ts +3 -3
  76. package/dist/vm/lib/global/sequence/map-filter.d.ts.map +1 -1
  77. package/dist/vm/lib/global/sequence/repeat.d.ts +1 -1
  78. package/dist/vm/lib/global/sequence/repeat.d.ts.map +1 -1
  79. package/dist/vm/lib/global/sequence/reverse.d.ts +1 -1
  80. package/dist/vm/lib/global/sequence/reverse.d.ts.map +1 -1
  81. package/dist/vm/lib/global/sequence/sort.d.ts +2 -2
  82. package/dist/vm/lib/global/sequence/sort.d.ts.map +1 -1
  83. package/dist/vm/lib/global/sequence/with.d.ts +2 -2
  84. package/dist/vm/lib/global/sequence/with.d.ts.map +1 -1
  85. package/dist/vm/lib/global/sequence/zip.d.ts +1 -1
  86. package/dist/vm/lib/global/sequence/zip.d.ts.map +1 -1
  87. package/dist/vm/lib/global/string.d.ts +10 -10
  88. package/dist/vm/lib/global/string.d.ts.map +1 -1
  89. package/dist/vm/lib/global/time.d.ts +3 -13
  90. package/dist/vm/lib/global/time.d.ts.map +1 -1
  91. package/dist/vm/lib/global/to-primitive.d.ts +4 -4
  92. package/dist/vm/lib/global/to-primitive.d.ts.map +1 -1
  93. package/dist/vm/lib/helpers.d.ts +53 -0
  94. package/dist/vm/lib/helpers.d.ts.map +1 -0
  95. package/dist/vm/lib/index.d.ts +4 -0
  96. package/dist/vm/lib/index.d.ts.map +1 -0
  97. package/dist/vm/lib/{_loader.d.ts → loader.d.ts} +5 -5
  98. package/dist/vm/lib/loader.d.ts.map +1 -0
  99. package/dist/vm/lib/mod/index.d.ts +2 -0
  100. package/dist/vm/lib/mod/index.d.ts.map +1 -0
  101. package/dist/vm/lib/mod/matrix.d.ts +15 -0
  102. package/dist/vm/lib/mod/matrix.d.ts.map +1 -0
  103. package/dist/vm/operations.d.ts +4 -6
  104. package/dist/vm/operations.d.ts.map +1 -1
  105. package/dist/vm/types/boundary.d.ts +1 -1
  106. package/dist/vm/types/boundary.d.ts.map +1 -1
  107. package/dist/vm/types/context.d.ts +18 -12
  108. package/dist/vm/types/context.d.ts.map +1 -1
  109. package/dist/vm/types/extern.d.ts +1 -3
  110. package/dist/vm/types/extern.d.ts.map +1 -1
  111. package/dist/vm/types/function.d.ts +1 -6
  112. package/dist/vm/types/function.d.ts.map +1 -1
  113. package/dist/vm/types/index.d.ts +31 -17
  114. package/dist/vm/types/index.d.ts.map +1 -1
  115. package/dist/vm/types/module.d.ts +2 -4
  116. package/dist/vm/types/module.d.ts.map +1 -1
  117. package/dist/vm/types/wrapper.d.ts +0 -2
  118. package/dist/vm/types/wrapper.d.ts.map +1 -1
  119. package/package.json +7 -3
  120. package/src/cli/index.ts +1 -1
  121. package/src/compiler/compile-fast.ts +1 -2
  122. package/src/compiler/create-script.ts +12 -2
  123. package/src/compiler/diagnostic.ts +17 -8
  124. package/src/compiler/emit/constants.ts +2 -0
  125. package/src/compiler/emit/consts.ts +47 -0
  126. package/src/compiler/emit/globals.ts +39 -0
  127. package/src/compiler/{emit.ts → emit/index.ts} +18 -202
  128. package/src/compiler/emit/sourcemap.ts +163 -0
  129. package/src/compiler/index.ts +9 -9
  130. package/src/compiler/worker.ts +1 -1
  131. package/src/helpers/constants.ts +18 -1
  132. package/src/helpers/convert.ts +128 -0
  133. package/src/{vm → helpers}/error.ts +1 -1
  134. package/src/helpers/serialize.ts +72 -25
  135. package/src/helpers/types.ts +267 -0
  136. package/src/subtle.ts +3 -1
  137. package/src/vm/checkpoint.ts +42 -0
  138. package/src/vm/helpers.ts +9 -53
  139. package/src/vm/index.ts +3 -3
  140. package/src/vm/lib/global/bit.ts +8 -9
  141. package/src/vm/lib/global/debug.ts +5 -4
  142. package/src/vm/lib/global/index.ts +0 -1
  143. package/src/vm/lib/global/json.ts +2 -2
  144. package/src/vm/lib/global/math-additional.ts +2 -4
  145. package/src/vm/lib/global/math-arr.ts +1 -1
  146. package/src/vm/lib/global/math-unary.ts +2 -4
  147. package/src/vm/lib/global/math.ts +3 -4
  148. package/src/vm/lib/global/sequence/all-any.ts +11 -6
  149. package/src/vm/lib/global/sequence/entries.ts +3 -3
  150. package/src/vm/lib/global/sequence/find.ts +8 -7
  151. package/src/vm/lib/global/sequence/flatten.ts +2 -3
  152. package/src/vm/lib/global/sequence/len.ts +1 -1
  153. package/src/vm/lib/global/sequence/map-filter.ts +7 -6
  154. package/src/vm/lib/global/sequence/repeat.ts +2 -4
  155. package/src/vm/lib/global/sequence/reverse.ts +1 -1
  156. package/src/vm/lib/global/sequence/sort.ts +6 -5
  157. package/src/vm/lib/global/sequence/with.ts +22 -21
  158. package/src/vm/lib/global/sequence/zip.ts +4 -4
  159. package/src/vm/lib/global/string.ts +16 -27
  160. package/src/vm/lib/global/time.ts +59 -30
  161. package/src/vm/lib/global/to-primitive.ts +27 -18
  162. package/src/vm/lib/{_helpers.ts → helpers.ts} +129 -41
  163. package/src/vm/lib/index.ts +16 -0
  164. package/src/vm/lib/{_loader.ts → loader.ts} +3 -11
  165. package/src/vm/lib/mod/index.ts +1 -0
  166. package/src/vm/lib/{global/mod → mod}/matrix.ts +9 -7
  167. package/src/vm/operations.ts +33 -128
  168. package/src/vm/types/boundary.ts +10 -13
  169. package/src/vm/types/context.ts +36 -28
  170. package/src/vm/types/extern.ts +8 -15
  171. package/src/vm/types/function.ts +4 -19
  172. package/src/vm/types/index.ts +47 -25
  173. package/src/vm/types/module.ts +3 -7
  174. package/src/vm/types/wrapper.ts +1 -5
  175. package/dist/chunk-35JGBXRE.js +0 -1
  176. package/dist/chunk-55FKP56O.js.map +0 -6
  177. package/dist/chunk-Q74RKZ7O.js.map +0 -6
  178. package/dist/compiler/emit.d.ts +0 -5
  179. package/dist/compiler/emit.d.ts.map +0 -1
  180. package/dist/vm/error.d.ts.map +0 -1
  181. package/dist/vm/lib/_helpers.d.ts +0 -35
  182. package/dist/vm/lib/_helpers.d.ts.map +0 -1
  183. package/dist/vm/lib/_loader.d.ts.map +0 -1
  184. package/dist/vm/lib/global/mod/index.d.ts +0 -3
  185. package/dist/vm/lib/global/mod/index.d.ts.map +0 -1
  186. package/dist/vm/lib/global/mod/matrix.d.ts +0 -15
  187. package/dist/vm/lib/global/mod/matrix.d.ts.map +0 -1
  188. package/dist/vm/types/any.d.ts +0 -10
  189. package/dist/vm/types/any.d.ts.map +0 -1
  190. package/dist/vm/types/array.d.ts +0 -12
  191. package/dist/vm/types/array.d.ts.map +0 -1
  192. package/dist/vm/types/callable.d.ts +0 -5
  193. package/dist/vm/types/callable.d.ts.map +0 -1
  194. package/dist/vm/types/const.d.ts +0 -15
  195. package/dist/vm/types/const.d.ts.map +0 -1
  196. package/dist/vm/types/immutable.d.ts +0 -15
  197. package/dist/vm/types/immutable.d.ts.map +0 -1
  198. package/dist/vm/types/primitive.d.ts +0 -7
  199. package/dist/vm/types/primitive.d.ts.map +0 -1
  200. package/dist/vm/types/record.d.ts +0 -20
  201. package/dist/vm/types/record.d.ts.map +0 -1
  202. package/dist/vm/types/script.d.ts +0 -14
  203. package/dist/vm/types/script.d.ts.map +0 -1
  204. package/dist/vm/types/value.d.ts +0 -14
  205. package/dist/vm/types/value.d.ts.map +0 -1
  206. package/src/vm/lib/global/mod/index.ts +0 -4
  207. package/src/vm/types/any.ts +0 -33
  208. package/src/vm/types/array.ts +0 -19
  209. package/src/vm/types/callable.ts +0 -10
  210. package/src/vm/types/const.ts +0 -109
  211. package/src/vm/types/immutable.ts +0 -22
  212. package/src/vm/types/primitive.ts +0 -14
  213. package/src/vm/types/record.ts +0 -53
  214. package/src/vm/types/script.ts +0 -18
  215. package/src/vm/types/value.ts +0 -22
  216. /package/dist/{chunk-35JGBXRE.js.map → chunk-RIT53WVY.js.map} +0 -0
@@ -4,86 +4,44 @@ var __export = (target, all) => {
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
5
5
  };
6
6
 
7
- // src/vm/error.ts
8
- var VmError = class _VmError extends Error {
9
- constructor(message, recovered) {
10
- super(message);
11
- this.recovered = recovered;
12
- this.name = "VmError";
13
- }
14
- /** 从其他错误构造 */
15
- static from(prefix, error, recovered) {
16
- if (prefix) {
17
- if (prefix.endsWith(":")) {
18
- prefix += " ";
19
- } else if (!prefix.endsWith(": ")) {
20
- prefix += ": ";
21
- }
22
- }
23
- let vmError;
24
- if (error instanceof Error) {
25
- vmError = new _VmError(`${prefix}${error.message}`, recovered);
26
- vmError.stack = error.stack;
27
- } else {
28
- vmError = new _VmError(`${prefix}${String(error)}`, recovered);
29
- }
30
- vmError.cause = error;
31
- return vmError;
32
- }
33
- };
34
-
35
- // src/vm/operations.ts
36
- var operations_exports = {};
37
- __export(operations_exports, {
38
- $Add: () => $Add,
39
- $Aeq: () => $Aeq,
40
- $And: () => $And,
41
- $ArraySpread: () => $ArraySpread,
42
- $AssertInit: () => $AssertInit,
43
- $AssertNonNil: () => $AssertNonNil,
44
- $Call: () => $Call,
45
- $Concat: () => $Concat,
46
- $Div: () => $Div,
47
- $Eq: () => $Eq,
48
- $Format: () => $Format,
49
- $Get: () => $Get,
50
- $Gt: () => $Gt,
51
- $Gte: () => $Gte,
52
- $Has: () => $Has,
53
- $In: () => $In,
54
- $InnerToString: () => $InnerToString,
55
- $IsArray: () => $IsArray,
56
- $IsBoolean: () => $IsBoolean,
57
- $IsNumber: () => $IsNumber,
58
- $IsRecord: () => $IsRecord,
59
- $IsString: () => $IsString,
60
- $Iterable: () => $Iterable,
61
- $Length: () => $Length,
62
- $Lt: () => $Lt,
63
- $Lte: () => $Lte,
64
- $Mod: () => $Mod,
65
- $Mul: () => $Mul,
66
- $Naeq: () => $Naeq,
67
- $Neg: () => $Neg,
68
- $Neq: () => $Neq,
69
- $Not: () => $Not,
70
- $Nsame: () => $Nsame,
71
- $Omit: () => $Omit,
72
- $Or: () => $Or,
73
- $Pick: () => $Pick,
74
- $Pos: () => $Pos,
75
- $Pow: () => $Pow,
76
- $RecordSpread: () => $RecordSpread,
77
- $Same: () => $Same,
78
- $Set: () => $Set,
79
- $Slice: () => $Slice,
80
- $SliceExclusive: () => $SliceExclusive,
81
- $Sub: () => $Sub,
82
- $ToBoolean: () => $ToBoolean,
83
- $ToNumber: () => $ToNumber,
84
- $ToString: () => $ToString,
85
- $Type: () => $Type
7
+ // src/helpers/constants.ts
8
+ var constants_exports = {};
9
+ __export(constants_exports, {
10
+ REG_BIN: () => REG_BIN,
11
+ REG_HEX: () => REG_HEX,
12
+ REG_IDENTIFIER: () => REG_IDENTIFIER,
13
+ REG_NUMBER: () => REG_NUMBER,
14
+ REG_OCT: () => REG_OCT,
15
+ REG_ORDINAL: () => REG_ORDINAL,
16
+ REG_WHITESPACE: () => REG_WHITESPACE,
17
+ VM_ARRAY_MAX_LENGTH: () => VM_ARRAY_MAX_LENGTH,
18
+ VM_FUNCTION_ANONYMOUS_NAME: () => VM_FUNCTION_ANONYMOUS_NAME,
19
+ VM_SCRIPT_NAME: () => VM_SCRIPT_NAME,
20
+ kVmContext: () => kVmContext,
21
+ kVmExtern: () => kVmExtern,
22
+ kVmFunction: () => kVmFunction,
23
+ kVmFunctionProxy: () => kVmFunctionProxy,
24
+ kVmModule: () => kVmModule,
25
+ kVmScript: () => kVmScript,
26
+ kVmWrapper: () => kVmWrapper
86
27
  });
28
+ var REG_IDENTIFIER = /(?:_+|@+|\$+|\p{XID_Start})\p{XID_Continue}*/u;
29
+ 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)/;
30
+ var REG_WHITESPACE = /[ \t\v\f\r\n]/u;
31
+ var REG_HEX = /0[xX][a-fA-F0-9_]*[a-fA-F0-9]/;
32
+ var REG_OCT = /0[oO][0-7_]*[0-7]/;
33
+ var REG_BIN = /0[bB][01_]*[01]/;
34
+ var REG_NUMBER = /\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?[\d_]*\d)?/u;
35
+ 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");
43
+ var VM_SCRIPT_NAME = `<script_root>`;
44
+ var VM_FUNCTION_ANONYMOUS_NAME = `<anonymous>`;
87
45
 
88
46
  // src/helpers/utils.ts
89
47
  var { isArray } = Array;
@@ -95,535 +53,45 @@ var hasOwnEnumerable = Function.call.bind(
95
53
  Object.prototype.propertyIsEnumerable
96
54
  );
97
55
 
98
- // src/vm/helpers.ts
99
- var helpers_exports = {};
100
- __export(helpers_exports, {
101
- ArrayRange: () => ArrayRange,
102
- ArrayRangeExclusive: () => ArrayRangeExclusive,
103
- Cp: () => Cp,
104
- CpEnter: () => CpEnter,
105
- CpExit: () => CpExit,
106
- Element: () => Element,
107
- ElementOpt: () => ElementOpt,
108
- Function: () => Function2,
109
- GlobalFallback: () => GlobalFallback,
110
- Upvalue: () => Upvalue,
111
- Vargs: () => Vargs,
112
- configCheckpoint: () => configCheckpoint
113
- });
114
-
115
- // src/vm/types/context.ts
116
- var kVmContext = Symbol.for("mirascript.vm.context");
117
- var VmSharedContext = create(null);
118
- var VmSharedContextKeys = null;
119
- function defineVmContextValue(name, value, override = false) {
120
- if (!override && name in VmSharedContext) throw new Error(`Global variable '${name}' is already defined.`);
121
- let v;
122
- if (typeof value == "function") {
123
- v = VmFunction(value, {
124
- isLib: true,
125
- fullName: `global.${name}`
126
- });
127
- } else {
128
- v = value;
129
- }
130
- VmSharedContext[name] = v ?? null;
131
- VmSharedContextKeys = null;
132
- }
133
- var DefaultVmContext = Object.freeze({
134
- [kVmContext]: true,
135
- /** @inheritdoc */
136
- keys() {
137
- VmSharedContextKeys ??= Object.freeze(keys(VmSharedContext));
138
- return VmSharedContextKeys;
139
- },
140
- /** @inheritdoc */
141
- get(key) {
142
- return VmSharedContext[key] ?? null;
143
- },
144
- /** @inheritdoc */
145
- has(key) {
146
- return key in VmSharedContext;
147
- }
148
- });
149
- var _a;
150
- _a = kVmContext;
151
- var ValueVmContext = class {
152
- constructor(env, describe) {
153
- this.env = env;
154
- this.describe = describe;
155
- this[_a] = true;
156
- this.cachedKeys = null;
157
- }
158
- /** @inheritdoc */
159
- keys() {
160
- this.cachedKeys ??= keys(this.env);
161
- return [...this.cachedKeys, ...DefaultVmContext.keys()];
162
- }
163
- /** @inheritdoc */
164
- get(key) {
165
- return this.env[key] ?? null;
166
- }
167
- /** @inheritdoc */
168
- has(key) {
169
- return key in this.env;
170
- }
171
- };
172
- var _a2;
173
- _a2 = kVmContext;
174
- var FactoryVmContext = class {
175
- constructor(getter, enumerator, describe) {
176
- this.getter = getter;
177
- this.enumerator = enumerator;
178
- this.describe = describe;
179
- this[_a2] = true;
180
- }
181
- /** @inheritdoc */
182
- keys() {
183
- if (!this.enumerator) return DefaultVmContext.keys();
184
- return [...this.enumerator(), ...DefaultVmContext.keys()];
185
- }
186
- /** @inheritdoc */
187
- get(key) {
188
- const value = this.getter(key);
189
- if (value !== void 0) return value;
190
- return DefaultVmContext.get(key);
191
- }
192
- /** @inheritdoc */
193
- has(key) {
194
- return this.getter(key) !== void 0 || DefaultVmContext.has(key);
195
- }
196
- };
197
- function createVmContext(...args) {
198
- if (args[0] == null && args[1] == null) {
199
- return { ...DefaultVmContext };
200
- }
201
- if (typeof args[0] == "function") {
202
- const [getter, enumerator, describer2] = args;
203
- return new FactoryVmContext(getter, enumerator ?? void 0, describer2 ?? void 0);
204
- }
205
- const [vmValues, externValues, describer] = args;
206
- const env = create(VmSharedContext);
207
- if (vmValues) {
208
- for (const [key, value] of entries(vmValues)) {
209
- if (!isVmAny(value, false)) continue;
210
- env[key] = value ?? null;
211
- }
212
- }
213
- if (externValues) {
214
- for (const [key, value] of entries(externValues)) {
215
- env[key] = value == null ? null : wrapToVmValue(value, null);
216
- }
217
- }
218
- return new ValueVmContext(env, describer ?? void 0);
56
+ // src/helpers/types.ts
57
+ function isVmScript(value) {
58
+ return typeof value === "function" && kVmScript in value;
219
59
  }
220
60
  function isVmContext(context) {
221
- if (context == null || typeof context != "object") return false;
222
- return context[kVmContext] === true;
61
+ return context != null && typeof context == "object" && kVmContext in context;
223
62
  }
224
-
225
- // src/vm/helpers.ts
226
- var Vargs = (varags) => {
227
- for (let i = 0, l = varags.length; i < l; i++) {
228
- const el = varags[i];
229
- if (!isVmConst(el)) {
230
- varags[i] = null;
231
- }
232
- }
233
- return varags;
234
- };
235
- var Element = (value) => {
236
- $AssertInit(value);
237
- if (!isVmConst(value)) return null;
238
- return value;
239
- };
240
- var ElementOpt = (key, value) => {
241
- $AssertInit(value);
242
- if (value == null || !isVmConst(value)) return {};
243
- return { [key]: value };
244
- };
245
- var Function2 = (fn) => {
246
- return VmFunction(fn, { isLib: false, injectCp: false });
247
- };
248
- var Upvalue = (value) => {
249
- $AssertInit(value);
250
- return value;
251
- };
252
- var assertArrayLength = (start, end) => {
253
- if (end - start > VM_ARRAY_MAX_LENGTH) {
254
- throw new RangeError(`Array length exceeds maximum limit of ${VM_ARRAY_MAX_LENGTH}`);
255
- }
256
- };
257
- var isEmptyRange = (start, end) => {
258
- return !isFinite(start) || !isFinite(end) || start > end;
259
- };
260
- var ArrayRange = (start, end) => {
261
- const s = $ToNumber(start);
262
- const e = $ToNumber(end);
263
- if (isEmptyRange(s, e)) return [];
264
- assertArrayLength(s, e);
265
- const arr = [];
266
- for (let i = s; i <= e; i++) {
267
- arr.push(i);
268
- }
269
- return arr;
270
- };
271
- var ArrayRangeExclusive = (start, end) => {
272
- const s = $ToNumber(start);
273
- const e = $ToNumber(end);
274
- if (isEmptyRange(s, e)) return [];
275
- assertArrayLength(s, e);
276
- const arr = [];
277
- for (let i = s; i < e; i++) {
278
- arr.push(i);
279
- }
280
- return arr;
281
- };
282
- var MAX_DEPTH = 128;
283
- var cpDepth = 0;
284
- var cp = Number.NaN;
285
- var cpTimeout = 100;
286
- function Cp() {
287
- if (!cp) {
288
- cp = Date.now();
289
- } else if (Date.now() - cp > cpTimeout) {
290
- throw new RangeError("Execution timeout");
291
- }
292
- }
293
- function CpEnter() {
294
- cpDepth++;
295
- if (cpDepth <= 1) {
296
- cp = Date.now();
297
- cpDepth = 1;
298
- } else if (cpDepth > MAX_DEPTH) {
299
- throw new RangeError("Maximum call depth exceeded");
300
- } else {
301
- Cp();
302
- }
303
- }
304
- function CpExit() {
305
- cpDepth--;
306
- if (cpDepth < 1) {
307
- cp = Number.NaN;
308
- cpDepth = 0;
309
- } else {
310
- Cp();
311
- }
312
- }
313
- function configCheckpoint(timeout = 100) {
314
- if (typeof timeout !== "number" || timeout <= 0 || Number.isNaN(timeout)) {
315
- throw new RangeError("Invalid timeout value");
316
- }
317
- cpTimeout = timeout;
318
- }
319
- function GlobalFallback() {
320
- return DefaultVmContext;
321
- }
322
-
323
- // src/vm/types/function.ts
324
- var kVmFunction = Symbol.for("mirascript.vm.function");
325
63
  function isVmFunction(value) {
326
- return getVmFunctionInfo(value) != null;
64
+ return typeof value == "function" && kVmFunction in value;
327
65
  }
328
66
  function getVmFunctionInfo(value) {
329
67
  if (typeof value != "function") return void 0;
330
68
  return value[kVmFunction];
331
69
  }
332
- function VmFunction(fn, option = {}) {
333
- if (typeof fn != "function") {
334
- throw new TypeError("Invalid function");
335
- }
336
- const exists = fromVmFunctionProxy(fn);
337
- if (exists) return exists;
338
- const info = {
339
- fullName: option.fullName ?? fn.name,
340
- isLib: option.isLib ?? false,
341
- summary: option.summary || void 0,
342
- params: option.params,
343
- paramsType: option.paramsType,
344
- returns: option.returns || void 0,
345
- returnsType: option.returnsType || void 0,
346
- examples: option.examples?.length ? option.examples : void 0
347
- };
348
- if (option.injectCp) {
349
- const original = fn;
350
- info.original = original;
351
- fn = ((...args) => {
352
- try {
353
- CpEnter();
354
- const ret = original(...args);
355
- return ret;
356
- } finally {
357
- CpExit();
358
- }
359
- });
360
- defineProperty(fn, "name", {
361
- value: original.name,
362
- configurable: true
363
- });
364
- }
365
- defineProperty(fn, kVmFunction, {
366
- value: Object.freeze(info)
367
- });
368
- return fn;
369
- }
370
-
371
- // src/vm/types/wrapper.ts
372
- var VmWrapper = class {
373
- constructor(value) {
374
- this.value = value;
375
- }
376
- /** Convert the object to JSON */
377
- toJSON() {
378
- return void 0;
379
- }
380
- /** 转为字符串 */
381
- toString(useBraces) {
382
- const { type, describe } = this;
383
- if (!describe) return `<${type}>`;
384
- return `<${type} ${describe}>`;
385
- }
386
- };
387
- var kVmWrapper = Symbol.for("mirascript.vm.wrapper");
388
- Object.defineProperty(VmWrapper.prototype, kVmWrapper, { value: true });
389
70
  function isVmWrapper(value) {
390
71
  return value != null && typeof value == "object" && kVmWrapper in value;
391
72
  }
392
-
393
- // src/vm/types/extern.ts
394
- var ObjectPrototype = Object.prototype;
395
- var ObjectToString = ObjectPrototype.toString;
396
- var FunctionToString = Function.prototype.toString;
397
- var ArrayToString = Array.prototype.toString;
398
- var ArrayMap = Array.prototype.map;
399
- var VmExtern = class extends VmWrapper {
400
- constructor(value, thisArg = null) {
401
- super(value);
402
- this.thisArg = thisArg;
403
- }
404
- /** Check if the object has a property */
405
- access(key, read) {
406
- if (key.startsWith("_")) return false;
407
- if (typeof this.value == "function" && (key === "prototype" || key === "arguments" || key === "caller"))
408
- return false;
409
- if (hasOwn(this.value, key)) return true;
410
- if (!read) return true;
411
- if (!(key in this.value)) return false;
412
- if (key === "constructor") return false;
413
- const prop = this.value[key];
414
- if (key in Function.prototype && prop === Function.prototype[key]) return false;
415
- if (key in Array.prototype && prop === Array.prototype[key]) return false;
416
- if (key in Object.prototype && prop === Object.prototype[key]) return false;
417
- return true;
418
- }
419
- /** 决定是否对属性进行包装 */
420
- assumeVmValue(value, key) {
421
- return false;
422
- }
423
- /** @inheritdoc */
424
- has(key) {
425
- return this.access(key, true);
426
- }
427
- /** @inheritdoc */
428
- get(key) {
429
- if (!this.has(key)) return void 0;
430
- const prop = this.value[key];
431
- return wrapToVmValue(prop, this, (v) => this.assumeVmValue(v, key));
432
- }
433
- /** Set a property on the object */
434
- set(key, value) {
435
- if (!this.access(key, false)) return false;
436
- const prop = unwrapFromVmValue(value);
437
- this.value[key] = prop;
438
- return true;
439
- }
440
- /** Call extern value */
441
- call(args) {
442
- const { value } = this;
443
- if (typeof value != "function") {
444
- throw VmError.from(`Not a callable extern`, null, null);
445
- }
446
- const caller = this.thisArg?.value ?? null;
447
- const unwrappedArgs = args.map(unwrapFromVmValue);
448
- let ret;
449
- try {
450
- ret = apply(value, caller, unwrappedArgs);
451
- } catch (ex) {
452
- throw VmError.from(`Callable extern`, ex, null);
453
- }
454
- return wrapToVmValue(ret, null, (obj) => this.assumeVmValue(obj, void 0));
455
- }
456
- /** @inheritdoc */
457
- keys() {
458
- const keys2 = [];
459
- for (const key in this.value) {
460
- if (this.has(key)) keys2.push(key);
461
- }
462
- return keys2;
463
- }
464
- /** @inheritdoc */
465
- same(other) {
466
- if (!isVmExtern(other)) return false;
467
- return this.value === other.value && this.thisArg === other.thisArg;
468
- }
469
- /** @inheritdoc */
470
- toString(useBraces) {
471
- const { toString } = this.value;
472
- if (typeof toString != "function" || toString === ObjectToString || toString === FunctionToString) {
473
- return super.toString(useBraces);
474
- }
475
- if (toString === ArrayToString && isArray(this.value)) {
476
- const mapped = ArrayMap.call(this.value, (item) => {
477
- if (item === void 0) return "";
478
- return $InnerToString(wrapToVmValue(item ?? null, null), true);
479
- });
480
- const str = mapped.join(", ");
481
- if (useBraces) return `[${str}]`;
482
- return str;
483
- }
484
- try {
485
- return String(this.value);
486
- } catch {
487
- return super.toString(useBraces);
488
- }
489
- }
490
- /** @inheritdoc */
491
- get type() {
492
- return "extern";
493
- }
494
- /** @inheritdoc */
495
- get describe() {
496
- const tag = ObjectToString.call(this.value).slice(8, -1);
497
- if (isArray(this.value)) {
498
- return `${tag}(${this.value.length})`;
499
- } else if (tag === "Object") {
500
- const proto = getPrototypeOf(this.value);
501
- if (proto === ObjectPrototype) {
502
- return "Object";
503
- }
504
- if (proto == null) {
505
- return "Object: null prototype";
506
- }
507
- if (typeof proto.constructor === "function" && proto.constructor.name) {
508
- return proto.constructor.name;
509
- }
510
- } else if (tag === "Function" && "prototype" in this.value && typeof this.value.prototype == "object") {
511
- const { name } = this.value;
512
- return `class ${name || "<anonymous>"}`;
513
- } else if (tag === "Function") {
514
- const { name } = this.value;
515
- return `function ${name || "<anonymous>"}()`;
516
- } else if (tag === "AsyncFunction") {
517
- const { name } = this.value;
518
- return `async function ${name || "<anonymous>"}()`;
519
- } else if (tag === "GeneratorFunction") {
520
- const { name } = this.value;
521
- return `function* ${name || "<anonymous>"}()`;
522
- } else if (tag === "AsyncGeneratorFunction") {
523
- const { name } = this.value;
524
- return `async function* ${name || "<anonymous>"}()`;
525
- }
526
- return tag;
527
- }
528
- };
529
- var kVmExtern = Symbol.for("mirascript.vm.extern");
530
- Object.defineProperty(VmExtern.prototype, kVmExtern, { value: true });
73
+ function isVmModule(value) {
74
+ return value != null && typeof value == "object" && kVmModule in value;
75
+ }
531
76
  function isVmExtern(value) {
532
77
  return value != null && typeof value == "object" && kVmExtern in value;
533
78
  }
534
-
535
- // src/vm/types/boundary.ts
536
- var kProxy = Symbol.for("mirascript.vm.function.proxy");
537
- function toVmFunctionProxy(fn) {
538
- if (!isVmFunction(fn)) return fn;
539
- const cached = fn[kProxy];
540
- if (cached != null) return cached;
541
- const proxy = (...args) => {
542
- const ret = $Call(
543
- fn,
544
- // 作为函数参数传入的值一定丢失了它的 this
545
- args.map((v) => wrapToVmValue(v, null))
546
- );
547
- return unwrapFromVmValue(ret);
548
- };
549
- defineProperty(fn, kProxy, { value: proxy });
550
- defineProperty(proxy, kProxy, { value: fn });
551
- defineProperty(proxy, "name", {
552
- value: fn.name,
553
- configurable: true
554
- });
555
- return proxy;
556
- }
557
- function fromVmFunctionProxy(fn) {
558
- if (isVmFunction(fn)) return fn;
559
- const original = fn[kProxy];
560
- if (original && isVmFunction(original)) return original;
561
- return void 0;
562
- }
563
- function wrapToVmValue(value, thisArg = null, assumeVmValue) {
564
- if (value == null) return null;
565
- switch (typeof value) {
566
- case "function": {
567
- const unwrapped = fromVmFunctionProxy(value);
568
- if (unwrapped) return unwrapped;
569
- return new VmExtern(value, thisArg);
570
- }
571
- case "object": {
572
- if (isVmWrapper(value)) return value;
573
- if (value instanceof Date) return value.valueOf();
574
- if (assumeVmValue?.(value)) return value;
575
- return new VmExtern(value);
576
- }
577
- case "string":
578
- case "number":
579
- case "boolean":
580
- return value;
581
- case "bigint":
582
- return Number(value);
583
- case "symbol":
584
- case "undefined":
585
- default:
586
- return null;
587
- }
588
- }
589
- function unwrapFromVmValue(value) {
590
- if (typeof value == "function") {
591
- return toVmFunctionProxy(value);
592
- }
593
- if (value == null || typeof value != "object") return value;
594
- if (!isVmExtern(value)) return value;
595
- if (value.thisArg == null || typeof value.value != "function") {
596
- return value.value;
597
- }
598
- const f = value;
599
- const caller = f.thisArg.value;
600
- return new Proxy(f.value, {
601
- apply(target, thisArg, args) {
602
- return apply(target, caller, args);
603
- }
604
- });
605
- }
606
-
607
- // src/vm/types/script.ts
608
- var kVmScript = Symbol.for("mirascript.vm.script");
609
- function isVmScript(value) {
610
- return typeof value === "function" && value[kVmScript] === true;
611
- }
612
-
613
- // src/vm/types/callable.ts
614
79
  function isVmCallable(value) {
615
80
  return isVmFunction(value) || isVmExtern(value) && typeof value.value == "function";
616
81
  }
617
-
618
- // src/vm/types/array.ts
619
- var VM_ARRAY_MAX_LENGTH = 2 ** 31 - 1;
82
+ function isVmPrimitive(value) {
83
+ if (value === null || typeof value == "number" || typeof value == "string" || typeof value == "boolean") {
84
+ value;
85
+ value;
86
+ return true;
87
+ }
88
+ return false;
89
+ }
620
90
  function isVmArray(value) {
621
91
  if (!isArray(value)) return false;
622
92
  value;
623
93
  return true;
624
94
  }
625
-
626
- // src/vm/types/record.ts
627
95
  function isVmRecord(value) {
628
96
  if (value == null || typeof value !== "object") return false;
629
97
  if (isVmWrapper(value)) return false;
@@ -654,19 +122,7 @@ function isVmArrayLikeRecordByKeys(keys2) {
654
122
  function isVmArrayLikeRecord(value) {
655
123
  return isVmArrayLikeRecordByKeys(keys(value));
656
124
  }
657
-
658
- // src/vm/types/primitive.ts
659
- function isVmPrimitive(value) {
660
- if (value === null || typeof value == "number" || typeof value == "string" || typeof value == "boolean") {
661
- value;
662
- value;
663
- return true;
664
- }
665
- return false;
666
- }
667
-
668
- // src/vm/types/const.ts
669
- var MAX_DEPTH2 = 32;
125
+ var MAX_DEPTH = 16;
670
126
  function isVmArrayDeep(value, depth) {
671
127
  const proto1 = getPrototypeOf(value);
672
128
  if (!isArray(proto1)) return false;
@@ -691,7 +147,7 @@ function isVmRecordDeep(value, depth) {
691
147
  return values(value).every((value2) => isVmConstInner(value2, depth));
692
148
  }
693
149
  function isVmConstInner(value, depth) {
694
- if (depth++ >= MAX_DEPTH2) return false;
150
+ if (depth++ >= MAX_DEPTH) return false;
695
151
  switch (typeof value) {
696
152
  case "string":
697
153
  case "number":
@@ -739,51 +195,9 @@ function isVmConst(value, checkDeep = false) {
739
195
  return false;
740
196
  }
741
197
  }
742
-
743
- // src/vm/types/module.ts
744
- var VmModule = class extends VmWrapper {
745
- constructor(name, value) {
746
- super(value);
747
- this.name = name;
748
- }
749
- /** @inheritdoc */
750
- has(key) {
751
- return hasOwnEnumerable(this.value, key);
752
- }
753
- /** @inheritdoc */
754
- get(key) {
755
- if (!this.has(key)) return void 0;
756
- return this.value[key] ?? null;
757
- }
758
- /** @inheritdoc */
759
- keys() {
760
- return keys(this.value);
761
- }
762
- /** @inheritdoc */
763
- same(other) {
764
- return this === other;
765
- }
766
- /** @inheritdoc */
767
- get type() {
768
- return "module";
769
- }
770
- /** @inheritdoc */
771
- get describe() {
772
- return this.name;
773
- }
774
- };
775
- var kVmModule = Symbol.for("mirascript.vm.module");
776
- Object.defineProperty(VmModule.prototype, kVmModule, { value: true });
777
- function isVmModule(value) {
778
- return value != null && typeof value == "object" && kVmModule in value;
779
- }
780
-
781
- // src/vm/types/immutable.ts
782
198
  function isVmImmutable(value, checkDeep = false) {
783
199
  return isVmModule(value) || isVmFunction(value) || isVmConst(value, checkDeep);
784
200
  }
785
-
786
- // src/vm/types/any.ts
787
201
  function isVmAny(value, checkDeep) {
788
202
  switch (typeof value) {
789
203
  case "string":
@@ -803,585 +217,412 @@ function isVmAny(value, checkDeep) {
803
217
  return false;
804
218
  }
805
219
  }
806
-
807
- // src/vm/types/value.ts
808
220
  function isVmValue(value, checkDeep = false) {
809
221
  if (value === void 0) return false;
810
222
  return isVmAny(value, checkDeep);
811
223
  }
812
224
 
813
- // src/vm/operations.ts
814
- var { abs, min, trunc, ceil } = Math;
815
- var { slice, at } = Array.prototype;
816
- var { POSITIVE_INFINITY, NEGATIVE_INFINITY } = Number;
817
- var isSame = (a, b) => {
818
- if (typeof a == "number" && typeof b == "number") return a === b || isNaN(a) && isNaN(b);
819
- if (a === b) return true;
820
- if (a == null || typeof a != "object" || b == null || typeof b != "object") return false;
821
- if (isVmWrapper(a)) return a.same(b);
822
- if (isVmWrapper(b)) return b.same(a);
823
- if (isVmArray(a) && isVmArray(b)) {
824
- const len = a.length;
825
- if (len !== b.length) {
826
- return false;
827
- }
828
- for (let i = 0; i < len; i++) {
829
- if (!isSame(a[i] ?? null, b[i] ?? null)) {
830
- return false;
225
+ // 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
+ var DEFAULT_OPTIONS = Object.freeze({
229
+ maxDepth: 128,
230
+ serializeNil,
231
+ serializeBoolean,
232
+ serializeNumber,
233
+ serializeString: serializeStringImpl,
234
+ serializeStringQuote: (value) => value,
235
+ serializeStringEscape: (value) => value,
236
+ serializeStringContent: (value) => value,
237
+ serializeArray,
238
+ serializeRecord,
239
+ serializePropName: String,
240
+ serializeFunction: serializeNil,
241
+ serializeModule: serializeNil,
242
+ serializeExtern: serializeNil
243
+ });
244
+ function isDefaultOptions(options) {
245
+ return options == null || options === DEFAULT_OPTIONS;
246
+ }
247
+ function mergeOptions(base, options) {
248
+ if (options == null) return base;
249
+ let opt = null;
250
+ for (const key in options) {
251
+ if (!hasOwn(options, key) || !hasOwn(base, key)) continue;
252
+ const el = options[key];
253
+ if (el == null) continue;
254
+ opt ??= { ...base };
255
+ opt[key] = el;
256
+ }
257
+ return opt ? Object.freeze(opt) : base;
258
+ }
259
+ function getSerializeOptions(options) {
260
+ if (isDefaultOptions(options)) return DEFAULT_OPTIONS;
261
+ return mergeOptions(DEFAULT_OPTIONS, options);
262
+ }
263
+ function serializeStringImpl(value, options) {
264
+ if (!/[\p{C}'"`$\\]/u.test(value)) {
265
+ const oq = options.serializeStringQuote(`'`, true, options);
266
+ const cq = options.serializeStringQuote(`'`, false, options);
267
+ const c = options.serializeStringContent(value, options);
268
+ return oq + c + cq;
269
+ }
270
+ let ret = options.serializeStringQuote(`'`, true, options);
271
+ for (const char of value) {
272
+ if (char === "'") {
273
+ ret += options.serializeStringEscape(String.raw`\'`, options);
274
+ } else if (char === "\0") {
275
+ ret += options.serializeStringEscape(String.raw`\0`, options);
276
+ } else if (char === "\n") {
277
+ ret += options.serializeStringEscape(String.raw`\n`, options);
278
+ } else if (char === "\r") {
279
+ ret += options.serializeStringEscape(String.raw`\r`, options);
280
+ } else if (char === " ") {
281
+ ret += options.serializeStringEscape(String.raw`\t`, options);
282
+ } else if (char === "\b") {
283
+ ret += options.serializeStringEscape(String.raw`\b`, options);
284
+ } else if (char === "\f") {
285
+ ret += options.serializeStringEscape(String.raw`\f`, options);
286
+ } else if (char === "\v") {
287
+ ret += options.serializeStringEscape(String.raw`\v`, options);
288
+ } else if (char === "\\") {
289
+ ret += options.serializeStringEscape(String.raw`\\`, options);
290
+ } else if (char === "$") {
291
+ ret += options.serializeStringEscape(String.raw`\$`, options);
292
+ } else if (/\p{C}/u.test(char)) {
293
+ const code = char.codePointAt(0);
294
+ if (code <= 127) {
295
+ ret += options.serializeStringEscape(String.raw`\x${code.toString(16).padStart(2, "0")}`, options);
296
+ } else if (code >= 55296 && code <= 57343) {
297
+ ret += options.serializeStringContent("�", options);
298
+ } else {
299
+ ret += options.serializeStringEscape(String.raw`\u{${code.toString(16)}}`, options);
831
300
  }
301
+ } else {
302
+ ret += options.serializeStringContent(char, options);
832
303
  }
833
- return true;
834
304
  }
835
- if (!isVmArray(a) && !isVmArray(b)) {
836
- const aKeys = keys(a);
837
- const bKeys = keys(b);
838
- if (aKeys.length !== bKeys.length) {
839
- return false;
840
- }
841
- for (const key of aKeys) {
842
- if (!hasOwnEnumerable(b, key)) {
843
- return false;
844
- }
845
- if (!isSame(a[key] ?? null, b[key] ?? null)) {
846
- return false;
847
- }
848
- }
849
- return true;
305
+ ret += options.serializeStringQuote(`'`, false, options);
306
+ return ret;
307
+ }
308
+ function serializeString(value, options) {
309
+ return serializeStringImpl(value, getSerializeOptions(options));
310
+ }
311
+ function serializeRecordKeyDefault(key) {
312
+ if (REG_ORDINAL_FULL.test(key) || REG_IDENTIFIER_FULL.test(key)) {
313
+ return key;
850
314
  }
851
- return false;
852
- };
853
- var overloadNumberString = (a, b) => {
854
- if (typeof a == "number" || typeof b == "number") return true;
855
- if (typeof a == "string" || typeof b == "string") return false;
856
- return true;
857
- };
858
- var $Add = (a, b) => {
859
- return $ToNumber(a) + $ToNumber(b);
860
- };
861
- var $Sub = (a, b) => {
862
- return $ToNumber(a) - $ToNumber(b);
863
- };
864
- var $Mul = (a, b) => {
865
- return $ToNumber(a) * $ToNumber(b);
866
- };
867
- var $Div = (a, b) => {
868
- return $ToNumber(a) / $ToNumber(b);
869
- };
870
- var $Mod = (a, b) => {
871
- return $ToNumber(a) % $ToNumber(b);
872
- };
873
- var $Pow = (a, b) => {
874
- return $ToNumber(a) ** $ToNumber(b);
875
- };
876
- var $And = (a, b) => {
877
- return $ToBoolean(a) && $ToBoolean(b);
878
- };
879
- var $Or = (a, b) => {
880
- return $ToBoolean(a) || $ToBoolean(b);
881
- };
882
- var $Gt = (a, b) => {
883
- if (overloadNumberString(a, b)) {
884
- return $ToNumber(a) > $ToNumber(b);
885
- } else {
886
- return $ToString(a) > $ToString(b);
315
+ return serializeStringImpl(key, DEFAULT_OPTIONS);
316
+ }
317
+ function serializeRecordKeyOpt(value, options) {
318
+ if (isDefaultOptions(options)) {
319
+ return serializeRecordKeyDefault(value);
887
320
  }
888
- };
889
- var $Gte = (a, b) => {
890
- if (overloadNumberString(a, b)) {
891
- return $ToNumber(a) >= $ToNumber(b);
892
- } else {
893
- return $ToString(a) >= $ToString(b);
321
+ if (REG_ORDINAL_FULL.test(value)) {
322
+ return options.serializePropName(Number(value), options);
894
323
  }
895
- };
896
- var $Lt = (a, b) => {
897
- if (overloadNumberString(a, b)) {
898
- return $ToNumber(a) < $ToNumber(b);
899
- } else {
900
- return $ToString(a) < $ToString(b);
324
+ if (REG_IDENTIFIER_FULL.test(value)) {
325
+ return options.serializePropName(value, options);
901
326
  }
902
- };
903
- var $Lte = (a, b) => {
904
- if (overloadNumberString(a, b)) {
905
- return $ToNumber(a) <= $ToNumber(b);
906
- } else {
907
- return $ToString(a) <= $ToString(b);
327
+ return options.serializeString(value, options);
328
+ }
329
+ function serializeRecordKey(key, options) {
330
+ if (isDefaultOptions(options)) {
331
+ return serializeRecordKeyDefault(key);
908
332
  }
909
- };
910
- var $Eq = (a, b) => {
911
- $AssertInit(a);
912
- $AssertInit(b);
913
- if (typeof a == "number" && typeof b == "number") return a === b;
914
- return isSame(a, b);
915
- };
916
- var $Neq = (a, b) => {
917
- return !$Eq(a, b);
918
- };
919
- var $Aeq = (a, b) => {
920
- if (overloadNumberString(a, b)) {
921
- const an = $ToNumber(a);
922
- const bn = $ToNumber(b);
923
- const EPS = 1e-15;
924
- if (isNaN(an) || isNaN(bn)) return false;
925
- if (an === bn) return true;
926
- const absoluteDifference = abs(an - bn);
927
- if (absoluteDifference < EPS) return true;
928
- const base = min(abs(an), abs(bn));
929
- return absoluteDifference < base * EPS;
930
- } else {
931
- const as = $ToString(a);
932
- const bs = $ToString(b);
933
- if (as === bs) return true;
934
- const ai = as.toLowerCase();
935
- const bi = bs.toLowerCase();
936
- if (ai === bi) return true;
937
- const an = ai.normalize("NFC");
938
- const bn = bi.normalize("NFC");
939
- return an === bn;
333
+ return serializeRecordKeyOpt(key, getSerializeOptions(options));
334
+ }
335
+ function serializeNil() {
336
+ return "nil";
337
+ }
338
+ function serializeBoolean(value) {
339
+ return value ? "true" : "false";
340
+ }
341
+ function serializeNumber(value) {
342
+ if (isNaN(value)) return "nan";
343
+ if (!isFinite(value)) return value < 0 ? "-inf" : "inf";
344
+ if (value === 0) {
345
+ if (1 / value < 0) return "-0";
346
+ return "0";
940
347
  }
941
- };
942
- var $Naeq = (a, b) => {
943
- return !$Aeq(a, b);
944
- };
945
- var $Same = (a, b) => {
946
- $AssertInit(a);
947
- $AssertInit(b);
948
- return isSame(a, b);
949
- };
950
- var $Nsame = (a, b) => {
951
- return !$Same(a, b);
952
- };
953
- var $In = (value, iterable) => {
954
- $AssertInit(value);
955
- $AssertInit(iterable);
956
- if (iterable == null) return false;
957
- if (isVmArray(iterable)) {
958
- if (value == null) {
959
- for (const item of iterable) {
960
- if (item == null) return true;
961
- }
962
- return false;
963
- }
964
- if (isVmPrimitive(value)) return iterable.includes(value);
965
- value;
966
- return iterable.some((item = null) => isSame(item, value));
348
+ return String(value);
349
+ }
350
+ function serializeArray(value, depth, options) {
351
+ if (depth > options.maxDepth) return `[]`;
352
+ if (value.length === 0) return "[]";
353
+ let str = "[";
354
+ for (let i = 0; i < value.length; i++) {
355
+ if (i > 0) str += ", ";
356
+ str += serializeImpl(value[i], depth, options);
357
+ }
358
+ str += "]";
359
+ return str;
360
+ }
361
+ var { valueOf } = Object.prototype;
362
+ function customValueOf(value) {
363
+ const thisValueOf = value.valueOf;
364
+ if (typeof thisValueOf != "function" || thisValueOf === valueOf) {
365
+ return void 0;
967
366
  }
968
- if (isVmWrapper(iterable)) return iterable.has($ToString(value));
969
- if (typeof iterable == "object") return hasOwnEnumerable(iterable, $ToString(value));
970
- iterable;
971
- return false;
972
- };
973
- var $Concat = (...args) => {
974
- return args.map($Format).join("");
975
- };
976
- var $Pos = (a) => {
977
- return $ToNumber(a);
978
- };
979
- var $Neg = (a) => {
980
- return -$ToNumber(a);
981
- };
982
- var $Not = (a) => {
983
- return !$ToBoolean(a);
984
- };
985
- var $Length = (value) => {
986
- $AssertInit(value);
987
- if (isVmArray(value)) return value.length;
988
- if (isVmRecord(value)) return keys(value).length;
989
- if (isVmWrapper(value)) {
990
- return value.keys().length;
991
- }
992
- return Number.NaN;
993
- };
994
- var $Omit = (value, omitted) => {
995
- $AssertInit(value);
996
- if (value == null || !isVmRecord(value)) return {};
997
- const result = {};
998
- const valueKeys = keys(value);
999
- const omittedSet = new Set(omitted.map($ToString));
1000
- for (const key of valueKeys) {
1001
- if (!omittedSet.has(key)) {
1002
- result[key] = value[key] ?? null;
367
+ const customValue = thisValueOf.call(value);
368
+ if (customValue === value) return void 0;
369
+ return customValue;
370
+ }
371
+ function serializeRecord(value, depth, options) {
372
+ const customValue = customValueOf(value);
373
+ if (customValue !== void 0) {
374
+ return serializeImpl(customValue, depth - 1, options);
375
+ }
376
+ if (depth > options.maxDepth) return `()`;
377
+ const e = entries(value);
378
+ if (e.length === 0) return "()";
379
+ if (e.length === 1) {
380
+ const [k, v] = e[0];
381
+ if (k === "0") {
382
+ return `(${serializeImpl(v, depth, options)},)`;
1003
383
  }
1004
- }
1005
- return result;
1006
- };
1007
- var $Pick = (value, picked) => {
1008
- $AssertInit(value);
1009
- if (value == null || !isVmRecord(value)) return {};
1010
- const result = {};
1011
- for (const key of picked) {
1012
- const k = $ToString(key);
1013
- if (hasOwnEnumerable(value, k)) {
1014
- result[k] = value[k] ?? null;
384
+ return `(${serializeRecordKeyOpt(k, options)}: ${serializeImpl(v, depth, options)})`;
385
+ }
386
+ const omitKey = isVmArrayLikeRecordByEntires(e);
387
+ let str = "(";
388
+ for (const [key, val] of e) {
389
+ if (str.length > 1) str += ", ";
390
+ if (omitKey) {
391
+ str += serializeImpl(val, depth, options);
392
+ } else {
393
+ str += `${serializeRecordKeyOpt(key, options)}: ${serializeImpl(val, depth, options)}`;
1015
394
  }
1016
395
  }
1017
- return result;
1018
- };
1019
- var sliceCore = (value, start, end, exclusive) => {
1020
- const { length } = value;
1021
- if (isNaN(start)) start = 0;
1022
- else if (start < 0) start = length + start;
1023
- if (isNaN(end)) end = exclusive ? length : length - 1;
1024
- else if (end < 0) end = length + end;
1025
- start = ceil(start);
1026
- if (exclusive || !isSafeInteger(end)) {
1027
- end = ceil(end);
1028
- } else {
1029
- end = end + 1;
396
+ str += ")";
397
+ return str;
398
+ }
399
+ function serializeImpl(value, depth, options) {
400
+ if (value == null) {
401
+ return options.serializeNil(options);
1030
402
  }
1031
- return slice.call(value, start, end);
1032
- };
1033
- var $Slice = (value, start, end) => {
1034
- $AssertInit(value);
1035
- if (!isVmArray(value)) throw new VmError(`Expected array, got ${$Type(value)}`, []);
1036
- const s = start != null ? $ToNumber(start) : 0;
1037
- const e = end != null ? $ToNumber(end) : value.length - 1;
1038
- return sliceCore(value, s, e, false);
1039
- };
1040
- var $SliceExclusive = (value, start, end) => {
1041
- $AssertInit(value);
1042
- if (!isVmArray(value)) throw new VmError(`Expected array, got ${$Type(value)}`, []);
1043
- const s = start != null ? $ToNumber(start) : 0;
1044
- const e = end != null ? $ToNumber(end) : value.length;
1045
- return sliceCore(value, s, e, true);
1046
- };
1047
- var $AssertInit = (value) => {
1048
- if (value === void 0) throw new VmError(`Uninitialized value`, null);
1049
- };
1050
- var $Call = (func, args) => {
1051
- for (const a of args) {
1052
- $AssertInit(a);
403
+ if (typeof value == "boolean") {
404
+ return options.serializeBoolean(value, options);
1053
405
  }
1054
- if (isVmExtern(func)) {
1055
- return func.call(args) ?? null;
406
+ if (typeof value == "number") {
407
+ return options.serializeNumber(value, options);
1056
408
  }
1057
- if (isVmFunction(func)) {
1058
- return func(...args) ?? null;
409
+ if (typeof value == "string") {
410
+ return options.serializeString(value, options);
1059
411
  }
1060
- throw new VmError(`Expected callable, got ${$Type(func)}`, null);
1061
- };
1062
- var $Type = (value) => {
1063
- if (value === void 0 || value === null) return "nil";
1064
- if (isVmExtern(value)) return "extern";
1065
- if (isVmModule(value)) return "module";
1066
- if (isVmArray(value)) return "array";
1067
- if (typeof value == "object") return "record";
1068
- return typeof value;
1069
- };
1070
- var $ToBoolean = (value) => {
1071
- $AssertInit(value);
1072
- return value != null && value !== false;
1073
- };
1074
- function numberToString(value) {
1075
- if (isNaN(value)) return "nan";
1076
- if (value === Infinity) return "inf";
1077
- if (value === -Infinity) return "-inf";
1078
- return String(value);
1079
- }
1080
- function $InnerToString(value, useBraces) {
1081
- if (value == null) return "nil";
1082
- if (isVmWrapper(value)) return value.toString(useBraces);
1083
- if (typeof value == "function") {
1084
- const name = getVmFunctionInfo(value)?.fullName;
1085
- return name ? `<function ${name}>` : `<function>`;
412
+ if (isVmFunction(value)) {
413
+ return options.serializeFunction(value, options);
1086
414
  }
1087
- if (isVmArray(value)) {
1088
- const strings = [];
1089
- for (const item of value) {
1090
- strings.push($InnerToString(item, true));
1091
- }
1092
- const results = strings.join(", ");
1093
- if (!useBraces) return results;
1094
- return `[${results}]`;
415
+ if (isVmModule(value)) {
416
+ return options.serializeModule(value, depth + 1, options);
1095
417
  }
1096
- if (typeof value == "object") {
1097
- const entries2 = keys(value).map((key) => `${key}: ${$InnerToString(value[key], true)}`).join(", ");
1098
- if (!useBraces) return entries2;
1099
- return `(${entries2})`;
418
+ if (isVmExtern(value)) {
419
+ return options.serializeExtern(value, depth + 1, options);
1100
420
  }
1101
- if (typeof value == "number") {
1102
- return numberToString(value);
421
+ if (isVmArray(value)) {
422
+ return options.serializeArray(value, depth + 1, options);
1103
423
  }
1104
- return String(value);
424
+ if (isVmRecord(value)) {
425
+ return options.serializeRecord(value, depth + 1, options);
426
+ }
427
+ value;
428
+ return options.serializeNil(options);
1105
429
  }
1106
- var $ToString = (value) => {
1107
- $AssertInit(value);
1108
- if (typeof value == "string") return value;
1109
- if (value === null) return "";
1110
- return $InnerToString(value, false);
1111
- };
1112
- var $ToNumber = (value) => {
1113
- $AssertInit(value);
1114
- if (typeof value == "number") return value;
1115
- if (value == null) return 0;
1116
- if (typeof value == "string") {
1117
- value = value.trim();
1118
- if (value === "") return 0;
1119
- if (value === "inf" || value === "+inf" || value === "Infinity" || value === "+Infinity") {
1120
- return POSITIVE_INFINITY;
1121
- }
1122
- if (value === "-inf" || value === "-Infinity") {
1123
- return NEGATIVE_INFINITY;
1124
- }
1125
- return Number(value);
430
+ function serialize(value, options) {
431
+ return serializeImpl(value, 0, getSerializeOptions(options));
432
+ }
433
+ function displayFunction(value) {
434
+ try {
435
+ const name = getVmFunctionInfo(value)?.fullName;
436
+ return name ? `<function ${name}>` : `<function>`;
437
+ } catch {
438
+ return `<function>`;
1126
439
  }
1127
- if (typeof value == "boolean") return value ? 1 : 0;
1128
- return Number.NaN;
1129
- };
1130
- var $IsBoolean = (value) => {
1131
- $AssertInit(value);
1132
- return typeof value == "boolean";
1133
- };
1134
- var $IsNumber = (value) => {
1135
- $AssertInit(value);
1136
- return typeof value == "number";
1137
- };
1138
- var $IsString = (value) => {
1139
- $AssertInit(value);
1140
- return typeof value == "string";
1141
- };
1142
- var $IsRecord = (value) => {
1143
- $AssertInit(value);
1144
- return isVmRecord(value);
1145
- };
1146
- var $IsArray = (value) => {
1147
- $AssertInit(value);
1148
- return isVmArray(value);
1149
- };
1150
- var $AssertNonNil = (value) => {
1151
- $AssertInit(value);
1152
- if (value !== null) return;
1153
- throw new VmError("Expected non-nil value", null);
1154
- };
1155
- var $Has = (obj, key) => {
1156
- $AssertInit(obj);
1157
- const pk = $ToString(key);
1158
- if (obj == null || typeof obj != "object") return false;
1159
- if (isVmWrapper(obj)) return obj.has(pk);
1160
- return hasOwnEnumerable(obj, pk);
1161
- };
1162
- var $Get = (obj, key) => {
1163
- $AssertInit(obj);
1164
- if (isVmArray(obj)) {
1165
- const index = $ToNumber(key);
1166
- if (isNaN(index)) return null;
1167
- return at.call(obj, trunc(index)) ?? null;
1168
- }
1169
- const pk = $ToString(key);
1170
- if (obj == null || typeof obj != "object") return null;
1171
- if (isVmWrapper(obj)) return obj.get(pk) ?? null;
1172
- if (!hasOwnEnumerable(obj, pk)) return null;
1173
- return obj[pk] ?? null;
1174
- };
1175
- var $Set = (obj, key, value) => {
1176
- $AssertInit(obj);
1177
- $AssertInit(value);
1178
- const pk = $ToString(key);
1179
- if (obj == null) return;
1180
- if (!isVmExtern(obj)) throw new VmError(`Expected extern, got ${$Type(obj)}`, void 0);
1181
- obj.set(pk, value);
1182
- };
1183
- var $Iterable = (value) => {
1184
- $AssertInit(value);
1185
- if (isVmWrapper(value)) return value.keys();
1186
- if (isVmArray(value)) return value;
1187
- if (value != null && typeof value == "object") return keys(value);
1188
- throw new VmError(`Value is not iterable`, isVmFunction(value) ? [] : [value]);
1189
- };
1190
- var $RecordSpread = (record) => {
1191
- $AssertInit(record);
1192
- if (record == null || isVmRecord(record)) return record;
1193
- if (isVmArray(record)) {
1194
- const result = {};
1195
- const len = record.length;
1196
- for (let i = 0; i < len; i++) {
1197
- const item = record[i];
1198
- result[i] = item ?? null;
1199
- }
1200
- return result;
440
+ }
441
+ function displayWrapper(value, useBraces, fallback) {
442
+ try {
443
+ return value.toString(useBraces);
444
+ } catch {
445
+ return fallback;
1201
446
  }
1202
- if (isVmExtern(record)) {
1203
- const result = create(null);
1204
- for (const key of record.keys()) {
1205
- const value = record.get(key) ?? null;
1206
- if (isVmPrimitive(value)) {
1207
- result[key] = value;
1208
- }
1209
- }
1210
- return result;
447
+ }
448
+ var DISPLAY_OPTIONS = Object.freeze({
449
+ maxDepth: 3,
450
+ serializeNil,
451
+ serializeBoolean,
452
+ serializeNumber,
453
+ serializeString: serializeStringImpl,
454
+ serializeStringQuote: (value) => value,
455
+ serializeStringEscape: (value) => value,
456
+ serializeStringContent: (value) => value,
457
+ serializeArray,
458
+ serializeRecord,
459
+ serializePropName: String,
460
+ serializeFunction: displayFunction,
461
+ serializeModule: (value) => displayWrapper(value, true, "<module>"),
462
+ serializeExtern: (value) => displayWrapper(value, true, "<extern>")
463
+ });
464
+ function display(value, options) {
465
+ const opt = mergeOptions(DISPLAY_OPTIONS, options);
466
+ return serializeImpl(value, 0, opt);
467
+ }
468
+
469
+ // src/helpers/error.ts
470
+ var VmError = class _VmError extends Error {
471
+ constructor(message, recovered) {
472
+ super(message);
473
+ this.recovered = recovered;
474
+ this.name = "VmError";
1211
475
  }
1212
- throw new VmError(`Expected record, array, extern or nil, got ${$Type(record)}`, null);
1213
- };
1214
- var $ArraySpread = (array) => {
1215
- $AssertInit(array);
1216
- if (array == null) return [];
1217
- if (isVmArray(array)) return array;
1218
- if (isVmExtern(array) && typeof array.value[Symbol.iterator] == "function") {
1219
- const result = [];
1220
- for (const item of array.value) {
1221
- if (isVmPrimitive(item)) {
1222
- result.push(item);
1223
- } else {
1224
- result.push(null);
476
+ /** 从其他错误构造 */
477
+ static from(prefix, error, recovered) {
478
+ if (prefix) {
479
+ if (prefix.endsWith(":")) {
480
+ prefix += " ";
481
+ } else if (!prefix.endsWith(": ")) {
482
+ prefix += ": ";
1225
483
  }
1226
484
  }
1227
- return result;
1228
- }
1229
- throw new VmError(`Expected array, iterable extern or nil, got ${$Type(array)}`, []);
1230
- };
1231
- function formatNumber(value) {
1232
- if (!Number.isFinite(value)) return numberToString(value);
1233
- if (value === 0) return "0";
1234
- const s = value.toString();
1235
- let ps;
1236
- const abs2 = Math.abs(value);
1237
- if (abs2 >= 1e3 || abs2 < 1e-3) {
1238
- const ps1 = value.toExponential();
1239
- const ps2 = value.toExponential(5);
1240
- ps = ps1.length < ps2.length ? ps1 : ps2;
1241
- } else {
1242
- ps = value.toPrecision(6);
1243
- }
1244
- return ps.length < s.length ? ps : s;
1245
- }
1246
- var $Format = (value, format) => {
1247
- $AssertInit(value);
1248
- const f = format == null ? "" : typeof format == "string" ? format.trim() : $ToString(format);
1249
- if (typeof value == "number") {
1250
- if (/^\.\d+$/.test(f)) {
1251
- let digits = Math.trunc(Number(f.slice(1)));
1252
- if (!(digits <= 100)) digits = 100;
1253
- return value.toFixed(digits);
485
+ let vmError;
486
+ if (error instanceof Error) {
487
+ vmError = new _VmError(`${prefix}${error.message}`, recovered);
488
+ vmError.stack = error.stack;
1254
489
  } else {
1255
- return formatNumber(value);
490
+ vmError = new _VmError(`${prefix}${String(error)}`, recovered);
1256
491
  }
492
+ vmError.cause = error;
493
+ return vmError;
1257
494
  }
1258
- return $ToString(value);
1259
495
  };
1260
496
 
1261
- // src/compiler/diagnostic.ts
1262
- import { DiagnosticCode, getModule } from "@mirascript/bindings";
1263
- var diagnosticMessages = /* @__PURE__ */ new Map();
1264
- function getDiagnosticMessage(code) {
1265
- if (!isSafeInteger(code) || code < 0 || code >= 65535) {
1266
- throw new RangeError(`Invalid DiagnosticCode: ${code}`);
1267
- }
1268
- if (diagnosticMessages.has(code)) {
1269
- return diagnosticMessages.get(code) || null;
497
+ // src/helpers/convert.ts
498
+ var convert_exports = {};
499
+ __export(convert_exports, {
500
+ innerToString: () => innerToString,
501
+ toBoolean: () => toBoolean,
502
+ toFormat: () => toFormat,
503
+ toNumber: () => toNumber,
504
+ toString: () => toString
505
+ });
506
+ var { POSITIVE_INFINITY, NEGATIVE_INFINITY } = Number;
507
+ function toNumber(value, fallback) {
508
+ if (typeof value === "number") {
509
+ return value;
1270
510
  }
1271
- const mod = getModule();
1272
- const msg = mod.getDiagnosticMessage(code);
1273
- diagnosticMessages.set(code, msg);
1274
- return msg;
1275
- }
1276
- function parseDiagnostics(source, diagnostics, filter) {
1277
- const parsed = [];
1278
- const bufLen = diagnostics.length;
1279
- for (let i = 0; i < bufLen; i += 5) {
1280
- const code = diagnostics[i + 4];
1281
- if (filter && !filter(code)) {
1282
- continue;
1283
- }
1284
- const startLineNumber = diagnostics[i];
1285
- const startColumn = diagnostics[i + 1];
1286
- const endLineNumber = diagnostics[i + 2];
1287
- const endColumn = diagnostics[i + 3];
1288
- parsed.push({
1289
- code,
1290
- range: {
1291
- startLineNumber,
1292
- startColumn,
1293
- endLineNumber,
1294
- endColumn
1295
- }
1296
- });
511
+ if (typeof value == "boolean") {
512
+ return value ? 1 : 0;
1297
513
  }
1298
- const _errors = [];
1299
- const _warnings = [];
1300
- const _infos = [];
1301
- const _hints = [];
1302
- const _tags = [];
1303
- const _references = [];
1304
- const _tagsReferences = [];
1305
- const _sourcemaps = [];
1306
- for (let i = 0; i < parsed.length; i++) {
1307
- const diagnostic = parsed[i];
1308
- const { code } = diagnostic;
1309
- if (code > DiagnosticCode.ErrorStart && code < DiagnosticCode.ErrorEnd) {
1310
- _errors.push(diagnostic);
1311
- } else if (code > DiagnosticCode.WarningStart && code < DiagnosticCode.WarningEnd) {
1312
- _warnings.push(diagnostic);
1313
- } else if (code > DiagnosticCode.InfoStart && code < DiagnosticCode.InfoEnd) {
1314
- _infos.push(diagnostic);
1315
- } else if (code > DiagnosticCode.HintStart && code < DiagnosticCode.HintEnd) {
1316
- _hints.push(diagnostic);
1317
- } else if (code > DiagnosticCode.TagStart && code < DiagnosticCode.TagEnd) {
1318
- _tags.push(diagnostic);
1319
- } else if (code === DiagnosticCode.SourceMap) {
1320
- _sourcemaps.push(diagnostic.range);
1321
- continue;
1322
- } else {
1323
- continue;
1324
- }
1325
- diagnostic.references = [];
1326
- while (i + 1 < parsed.length) {
1327
- const ref = parsed[i + 1];
1328
- let isRef = false;
1329
- if (ref.code > DiagnosticCode.TagRefStart && ref.code < DiagnosticCode.TagRefEnd) {
1330
- isRef = true;
1331
- _tagsReferences.push(ref);
514
+ if (typeof value == "string") {
515
+ value = value.trim();
516
+ if (value !== "") {
517
+ if (value === "inf" || value === "+inf" || value === "Infinity" || value === "+Infinity") {
518
+ return POSITIVE_INFINITY;
1332
519
  }
1333
- if (ref.code > DiagnosticCode.ReferenceStart && ref.code < DiagnosticCode.ReferenceEnd) {
1334
- isRef = true;
1335
- _references.push(ref);
520
+ if (value === "-inf" || value === "-Infinity") {
521
+ return NEGATIVE_INFINITY;
1336
522
  }
1337
- if (!isRef) {
1338
- break;
523
+ if (value === "nan" || value === "NaN") {
524
+ return Number.NaN;
1339
525
  }
1340
- i++;
1341
- ref.diagnostic = diagnostic;
1342
- diagnostic.references.push(ref);
526
+ const num = Number(value);
527
+ if (!isNaN(num)) return num;
1343
528
  }
1344
529
  }
1345
- return {
1346
- errors: _errors,
1347
- warnings: _warnings,
1348
- infos: _infos,
1349
- hints: _hints,
1350
- tags: _tags,
1351
- references: _references,
1352
- tagsReferences: _tagsReferences,
1353
- sourcemaps: _sourcemaps
1354
- };
530
+ if (fallback === void 0) {
531
+ throw new VmError(`Failed to convert value to number: ${display(value)}`, Number.NaN);
532
+ }
533
+ return fallback;
1355
534
  }
1356
- function formatRange(range) {
1357
- if (range.startLineNumber === range.endLineNumber) {
1358
- if (range.startColumn === range.endColumn) {
1359
- return `${range.startLineNumber}:${range.startColumn}`;
535
+ function toBoolean(value, fallback) {
536
+ if (typeof value === "boolean") return value;
537
+ if (fallback === void 0) {
538
+ throw new VmError(`Failed to convert value to boolean: ${display(value)}`, false);
539
+ }
540
+ return fallback;
541
+ }
542
+ function numberToString(value) {
543
+ if (isNaN(value)) return "nan";
544
+ if (value === Infinity) return "inf";
545
+ if (value === -Infinity) return "-inf";
546
+ return String(value);
547
+ }
548
+ function innerToString(value, useBraces) {
549
+ if (value == null) return "nil";
550
+ if (typeof value == "number") return numberToString(value);
551
+ if (typeof value == "string" || typeof value == "boolean") return String(value);
552
+ if (typeof value == "function") return displayFunction(value);
553
+ if (isVmWrapper(value)) return value.toString(useBraces);
554
+ if (isVmArray(value)) {
555
+ const strings = [];
556
+ for (const item of value) {
557
+ strings.push(innerToString(item, true));
1360
558
  }
1361
- return `${range.startLineNumber}:${range.startColumn}-${range.endColumn}`;
559
+ const results = strings.join(", ");
560
+ if (!useBraces) return results;
561
+ return `[${results}]`;
562
+ } else {
563
+ const entries2 = keys(value).map((key) => `${key}: ${innerToString(value[key], true)}`).join(", ");
564
+ if (!useBraces) return entries2;
565
+ return `(${entries2})`;
1362
566
  }
1363
- return `${range.startLineNumber}:${range.startColumn}-${range.endLineNumber}:${range.endColumn}`;
1364
567
  }
1365
- function formatDiagnostic(diagnostic) {
1366
- const range = formatRange(diagnostic.range);
1367
- const codeName = DiagnosticCode[diagnostic.code] || `Unknown(${diagnostic.code})`;
1368
- let message = getDiagnosticMessage(diagnostic.code);
1369
- for (const ref of diagnostic.references) {
1370
- const refRange = formatRange(ref.range);
1371
- message += `
1372
- (${refRange}): ${getDiagnosticMessage(ref.code)}`;
568
+ function toString(value, fallback) {
569
+ if (typeof value === "string") return value;
570
+ if (value == null) return "";
571
+ try {
572
+ return innerToString(value, false);
573
+ } catch (ex) {
574
+ if (fallback === void 0) {
575
+ const e = new VmError(`Failed to convert value to string: ${display(value)}`, "");
576
+ e.cause = ex;
577
+ throw e;
578
+ }
579
+ return fallback;
580
+ }
581
+ }
582
+ function formatNumber(value) {
583
+ if (!isFinite(value)) return toString(value, void 0);
584
+ if (value === 0) return "0";
585
+ const s = value.toString();
586
+ let ps;
587
+ const abs = Math.abs(value);
588
+ if (abs >= 1e3 || abs < 1e-3) {
589
+ const ps1 = value.toExponential();
590
+ const ps2 = value.toExponential(5);
591
+ ps = ps1.length < ps2.length ? ps1 : ps2;
592
+ } else {
593
+ ps = value.toPrecision(6);
594
+ }
595
+ return ps.length < s.length ? ps : s;
596
+ }
597
+ function toFormat(value, format) {
598
+ const f = format == null ? "" : format.trim();
599
+ if (typeof value == "number") {
600
+ if (/^\.\d+$/.test(f)) {
601
+ let digits = Math.trunc(Number(f.slice(1)));
602
+ if (!(digits <= 100)) digits = 100;
603
+ return value.toFixed(digits);
604
+ } else {
605
+ return formatNumber(value);
606
+ }
1373
607
  }
1374
- return ` ${codeName}(${range}): ${message}`;
608
+ return toString(value, void 0);
1375
609
  }
1376
610
 
1377
- // src/compiler/emit.ts
611
+ // src/compiler/emit/index.ts
1378
612
  import { OpCode } from "@mirascript/bindings";
1379
- import { SourceMapGenerator } from "source-map-js";
1380
- function emit(source, chunk, sourcemaps, options) {
1381
- const gen = new Emitter(source, chunk, sourcemaps, options);
1382
- gen.read();
1383
- const code = gen.codeLines.join("\n");
1384
- return code;
613
+
614
+ // src/compiler/emit/consts.ts
615
+ function toJsLiteral(value) {
616
+ if (value === null) return "null";
617
+ if (value === void 0) return "undefined";
618
+ if (typeof value == "string") {
619
+ return JSON.stringify(value);
620
+ }
621
+ if (value === 0) {
622
+ if (1 / value === -Infinity) return "-0";
623
+ return "0";
624
+ }
625
+ return String(value);
1385
626
  }
1386
627
  function readConst(reader, offset) {
1387
628
  const type = reader.getUint8(offset);
@@ -1411,20 +652,176 @@ function readConst(reader, offset) {
1411
652
  throw new Error(`Unknown constant type: ${type}`);
1412
653
  }
1413
654
  }
1414
- function toJavascript(value) {
1415
- if (value === null) return "null";
1416
- if (value === void 0) return "undefined";
1417
- if (typeof value == "object" || typeof value == "string") {
1418
- return JSON.stringify(value);
1419
- }
1420
- if (value === 0) {
1421
- if (1 / value === -Infinity) return "-0";
1422
- return "0";
1423
- }
1424
- return String(value);
655
+
656
+ // src/compiler/emit/globals.ts
657
+ function readGlobal(emitter, constIdx) {
658
+ const { globals } = emitter;
659
+ const cached = globals.get(constIdx);
660
+ if (cached) return cached;
661
+ const constName = emitter.constVals[constIdx];
662
+ let name, lit;
663
+ if (typeof constName == "string") {
664
+ name = constName;
665
+ lit = emitter.constLits[constIdx];
666
+ } else {
667
+ name = toString(constName, void 0);
668
+ lit = toJsLiteral(name);
669
+ }
670
+ const val = `g${globals.size + 1}`;
671
+ const expr = `(${val} === undefined ? (${val} = global.get(${lit})) : ${val})`;
672
+ const desc = { v: val, l: lit, e: expr, n: name };
673
+ globals.set(constIdx, desc);
674
+ return desc;
1425
675
  }
676
+
677
+ // src/compiler/emit/sourcemap.ts
678
+ import { SourceMapGenerator } from "source-map-js";
679
+
680
+ // src/compiler/emit/constants.ts
681
+ var SCRIPT_PREFIX = `'use strict'; return ((global = GlobalFallback()) => { try { CpEnter();`;
682
+ var GLOBAL_HINT = `/* globals */`;
683
+
684
+ // src/compiler/emit/sourcemap.ts
1426
685
  var ORIGIN = `mira://MiraScript/`;
686
+ var PREFIX = "//# ";
687
+ var SOURCE_URL = `${PREFIX}sourceURL`;
688
+ var SOURCE_MAPPING_URL = `${PREFIX}sourceMappingURL`;
689
+ var SOURCE_OFFSET = 3;
690
+ 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)}`;
691
+ function addGlobalMappings(globalLine, fileName, map, globals) {
692
+ let globalFile = `global;
693
+ `;
694
+ map.addMapping({
695
+ generated: {
696
+ line: 3,
697
+ column: globalLine.indexOf(`global = `)
698
+ },
699
+ original: {
700
+ line: 1,
701
+ column: 0
702
+ },
703
+ source: fileName,
704
+ name: "global"
705
+ });
706
+ map.addMapping({
707
+ generated: {
708
+ line: 3,
709
+ column: SCRIPT_PREFIX.length
710
+ },
711
+ original: {
712
+ line: 1,
713
+ column: 7
714
+ },
715
+ source: fileName,
716
+ name: ""
717
+ });
718
+ let i = 1;
719
+ let pos = globalLine.indexOf(GLOBAL_HINT, SCRIPT_PREFIX.length) + GLOBAL_HINT.length;
720
+ for (const p of globals.values()) {
721
+ i++;
722
+ if (pos < 0) break;
723
+ const { v, n } = p;
724
+ pos = globalLine.indexOf(v, pos);
725
+ if (pos < 0) break;
726
+ map.addMapping({
727
+ generated: {
728
+ line: 3,
729
+ column: pos
730
+ },
731
+ original: {
732
+ line: i,
733
+ column: 0
734
+ },
735
+ source: fileName,
736
+ name: n
737
+ });
738
+ globalFile += `${n};
739
+ `;
740
+ }
741
+ map.addMapping({
742
+ generated: {
743
+ line: 3,
744
+ column: pos
745
+ },
746
+ original: {
747
+ line: i,
748
+ column: 0
749
+ },
750
+ source: fileName,
751
+ name: ""
752
+ });
753
+ map.setSourceContent(fileName, globalFile);
754
+ }
1427
755
  var sourceId = 1;
756
+ function createSourceMap(source, sourcemaps, codeLines, globals, options) {
757
+ let fileName = (options.fileName ?? "").trim();
758
+ const hasSchema = /^\w+:/.test(fileName);
759
+ if (!hasSchema) {
760
+ if (fileName.startsWith("/")) {
761
+ fileName = fileName.replace(/^\/+\s*/, "");
762
+ }
763
+ if (!fileName) {
764
+ fileName = `${sourceId++}.${options.input_mode === "Template" ? "miratpl" : "mira"}`;
765
+ }
766
+ }
767
+ const map = new SourceMapGenerator({
768
+ file: fileName + ".js"
769
+ });
770
+ if (typeof source === "string") {
771
+ map.setSourceContent(fileName, source);
772
+ }
773
+ let hasStartMap = false;
774
+ for (let i = 1; i < sourcemaps.length; i++) {
775
+ const range = sourcemaps[i];
776
+ if (!range) break;
777
+ if (!hasStartMap && range.startLineNumber === 1 && range.startColumn === 1) {
778
+ hasStartMap = true;
779
+ }
780
+ map.addMapping({
781
+ generated: {
782
+ line: i + SOURCE_OFFSET,
783
+ column: 0
784
+ },
785
+ original: {
786
+ line: range.startLineNumber,
787
+ column: range.startColumn - 1
788
+ },
789
+ source: fileName
790
+ });
791
+ }
792
+ if (!hasStartMap) {
793
+ map.addMapping({
794
+ generated: {
795
+ line: SOURCE_OFFSET,
796
+ column: SCRIPT_PREFIX.length - "CpEnter();".length
797
+ },
798
+ original: {
799
+ line: 1,
800
+ column: 0
801
+ },
802
+ source: fileName
803
+ });
804
+ }
805
+ const globalLine = codeLines[0];
806
+ if (globalLine?.includes(GLOBAL_HINT)) {
807
+ addGlobalMappings(globalLine, `${fileName} <globals>`, map, globals);
808
+ }
809
+ const sourceURL = hasSchema ? fileName : `${ORIGIN}${fileName}`;
810
+ const dataUrl = toDataUrl(map.toString());
811
+ return [
812
+ // Prevent source map from being recognized as of this file
813
+ `${SOURCE_URL}=${sourceURL}.js`,
814
+ `${SOURCE_MAPPING_URL}=${dataUrl}`
815
+ ];
816
+ }
817
+
818
+ // src/compiler/emit/index.ts
819
+ function emit(source, chunk, sourcemaps, options) {
820
+ const gen = new Emitter(source, chunk, sourcemaps, options);
821
+ gen.read();
822
+ const code = gen.codeLines.join("\n");
823
+ return code;
824
+ }
1428
825
  function createArray(length, fn) {
1429
826
  const result = [];
1430
827
  for (let i = 0; i < length; i++) {
@@ -1432,8 +829,8 @@ function createArray(length, fn) {
1432
829
  }
1433
830
  return result;
1434
831
  }
1435
- var SCRIPT_PREFIX = `'use strict'; return ((global = GlobalFallback()) => { try { CpEnter();`;
1436
- var GLOBAL_HINT = `/* globals */`;
832
+ var SCRIPT_PREFIX2 = `'use strict'; return ((global = GlobalFallback()) => { try { CpEnter();`;
833
+ var GLOBAL_HINT2 = `/* globals */`;
1437
834
  var Emitter = class {
1438
835
  constructor(source, chunk, sourcemaps, options) {
1439
836
  this.source = source;
@@ -1460,7 +857,7 @@ var Emitter = class {
1460
857
  for (let i = 0, index = 0; i < this.constSize; index++) {
1461
858
  const [constant, size] = readConst(this.constReader, i);
1462
859
  this.constVals.push(constant);
1463
- this.constLits.push(toJavascript(constant));
860
+ this.constLits.push(toJsLiteral(constant));
1464
861
  i += size;
1465
862
  }
1466
863
  }
@@ -1471,21 +868,7 @@ var Emitter = class {
1471
868
  }
1472
869
  /** 读取全局变量 */
1473
870
  rg(constIdx, eager) {
1474
- const cached = this.globals.get(constIdx);
1475
- if (cached != null) {
1476
- if (eager && !cached[2]) {
1477
- cached[2] = true;
1478
- cached[3] = cached[0];
1479
- }
1480
- return cached[3];
1481
- }
1482
- const constName = this.constVals[constIdx];
1483
- const name = $ToString(constName);
1484
- const lit = typeof constName == "string" ? this.constLits[constIdx] : JSON.stringify(name);
1485
- const val = `g${this.globals.size + 1}`;
1486
- const expr = eager ? val : `(${val} === undefined ? (${val} = global.get(${lit})) : ${val})`;
1487
- this.globals.set(constIdx, [val, lit, eager, expr, name]);
1488
- return expr;
871
+ return readGlobal(this, constIdx).e;
1489
872
  }
1490
873
  /** Read variable */
1491
874
  rv(i, level = 0) {
@@ -1718,7 +1101,7 @@ var Emitter = class {
1718
1101
  );
1719
1102
  const script = startFunc && !varg && argn === 0;
1720
1103
  if (script) {
1721
- code = `${SCRIPT_PREFIX} var ${regs};`;
1104
+ code = `${SCRIPT_PREFIX2} var ${regs};`;
1722
1105
  } else {
1723
1106
  code = `${this.wv(reg)} = Function((${args.join(", ")}) => { try { CpEnter(); var ${regs};`;
1724
1107
  }
@@ -1800,7 +1183,7 @@ var Emitter = class {
1800
1183
  const args = createArray(n, () => read());
1801
1184
  const ns = read();
1802
1185
  const spreads = createArray(ns, () => read());
1803
- const callTarget = opcode === OpCode.Call ? this.rg(func, this.identCounter <= 1) : this.rv(func);
1186
+ const callTarget = opcode === OpCode.Call ? this.rg(func, false) : this.rv(func);
1804
1187
  code = `${this.wv(reg)} = $Call(${callTarget}, [${args.map((a, i) => {
1805
1188
  if (spreads.includes(i)) return `...$ArraySpread(${this.rv(a)})`;
1806
1189
  else return this.rv(a);
@@ -1903,7 +1286,7 @@ var Emitter = class {
1903
1286
  case OpCode.GetGlobal: {
1904
1287
  reg = read();
1905
1288
  const i = read();
1906
- code = `${this.wv(reg)} = ${this.rg(i, this.identCounter <= 1)};`;
1289
+ code = `${this.wv(reg)} = ${this.rg(i, false)};`;
1907
1290
  break;
1908
1291
  }
1909
1292
  case OpCode.GetGlobalDyn: {
@@ -1976,12 +1359,12 @@ var Emitter = class {
1976
1359
  }
1977
1360
  case OpCode.If: {
1978
1361
  const cond = read();
1979
- code = `if (${this.rv(cond)} !== false && $ToBoolean(${this.rv(cond)}) !== false) {`;
1362
+ code = `if ($ToBoolean(${this.rv(cond)})) {`;
1980
1363
  break;
1981
1364
  }
1982
1365
  case OpCode.IfNot: {
1983
1366
  const cond = read();
1984
- code = `if (${this.rv(cond)} !== true && $ToBoolean(${this.rv(cond)}) !== true) {`;
1367
+ code = `if (!$ToBoolean(${this.rv(cond)})) {`;
1985
1368
  break;
1986
1369
  }
1987
1370
  case OpCode.IfInit: {
@@ -2089,9 +1472,8 @@ var Emitter = class {
2089
1472
  this.readCode();
2090
1473
  if (this.globals.size > 0) {
2091
1474
  let globalsInit = "";
2092
- for (const [val, lit, eager] of this.globals.values()) {
2093
- const expr = eager ? `${val} = global.get(${lit})` : val;
2094
- globalsInit += globalsInit ? `, ${expr}` : `var ${GLOBAL_HINT} ${expr}`;
1475
+ for (const { v } of this.globals.values()) {
1476
+ globalsInit += globalsInit ? `, ${v}` : `var ${GLOBAL_HINT2} ${v}`;
2095
1477
  }
2096
1478
  this.codeLines[0] += globalsInit + ";";
2097
1479
  }
@@ -2100,133 +1482,131 @@ var Emitter = class {
2100
1482
  /** 添加源映射 */
2101
1483
  addSourceMap() {
2102
1484
  if (!this.options.sourceMap) return;
2103
- let fileName = (this.options.fileName ?? "").trim();
2104
- const hasSchema = /^\w+:/.test(fileName);
2105
- if (!hasSchema) {
2106
- if (fileName.startsWith("/")) {
2107
- fileName = fileName.replace(/^\/+\s*/, "");
2108
- }
2109
- if (!fileName) {
2110
- fileName = `${sourceId++}.${this.options.input_mode === "Template" ? "miratpl" : "mira"}`;
2111
- }
1485
+ const mapLines = createSourceMap(this.source, this.sourcemaps, this.codeLines, this.globals, this.options);
1486
+ this.codeLines.push(...mapLines);
1487
+ }
1488
+ };
1489
+
1490
+ // src/compiler/diagnostic.ts
1491
+ import { DiagnosticCode, getModule } from "@mirascript/bindings";
1492
+ var diagnosticMessages = /* @__PURE__ */ new Map();
1493
+ function getDiagnosticMessage(code) {
1494
+ if (!isSafeInteger(code) || code < 0 || code >= 65535) {
1495
+ throw new RangeError(`Invalid DiagnosticCode: ${code}`);
1496
+ }
1497
+ if (diagnosticMessages.has(code)) {
1498
+ return diagnosticMessages.get(code) || null;
1499
+ }
1500
+ const mod = getModule();
1501
+ const msg = mod.getDiagnosticMessage(code);
1502
+ diagnosticMessages.set(code, msg);
1503
+ return msg;
1504
+ }
1505
+ function parseDiagnostics(source, diagnostics, filter) {
1506
+ const parsed = [];
1507
+ const bufLen = diagnostics.length;
1508
+ for (let i = 0; i < bufLen; i += 5) {
1509
+ const code = diagnostics[i + 4];
1510
+ if (filter && !filter(code)) {
1511
+ continue;
2112
1512
  }
2113
- const map = new SourceMapGenerator({
2114
- file: fileName + ".js"
1513
+ const startLineNumber = diagnostics[i];
1514
+ const startColumn = diagnostics[i + 1];
1515
+ const endLineNumber = diagnostics[i + 2];
1516
+ const endColumn = diagnostics[i + 3];
1517
+ parsed.push({
1518
+ code,
1519
+ range: {
1520
+ startLineNumber,
1521
+ startColumn,
1522
+ endLineNumber,
1523
+ endColumn
1524
+ }
2115
1525
  });
2116
- if (typeof this.source === "string") {
2117
- map.setSourceContent(fileName, this.source);
1526
+ }
1527
+ const _errors = [];
1528
+ const _warnings = [];
1529
+ const _infos = [];
1530
+ const _hints = [];
1531
+ const _tags = [];
1532
+ const _references = [];
1533
+ const _tagsReferences = [];
1534
+ const _sourcemaps = [];
1535
+ for (let i = 0; i < parsed.length; i++) {
1536
+ const diagnostic = parsed[i];
1537
+ const { code } = diagnostic;
1538
+ if (code > DiagnosticCode.ErrorStart && code < DiagnosticCode.ErrorEnd) {
1539
+ _errors.push(diagnostic);
1540
+ } else if (code > DiagnosticCode.WarningStart && code < DiagnosticCode.WarningEnd) {
1541
+ _warnings.push(diagnostic);
1542
+ } else if (code > DiagnosticCode.InfoStart && code < DiagnosticCode.InfoEnd) {
1543
+ _infos.push(diagnostic);
1544
+ } else if (code > DiagnosticCode.HintStart && code < DiagnosticCode.HintEnd) {
1545
+ _hints.push(diagnostic);
1546
+ } else if (code > DiagnosticCode.TagStart && code < DiagnosticCode.TagEnd) {
1547
+ _tags.push(diagnostic);
1548
+ } else if (code === DiagnosticCode.SourceMap) {
1549
+ _sourcemaps.push(diagnostic.range);
1550
+ continue;
1551
+ } else {
1552
+ continue;
2118
1553
  }
2119
- let hasStartMap = false;
2120
- for (let i = 1; i < this.sourcemaps.length; i++) {
2121
- const range = this.sourcemaps[i];
2122
- if (!range) break;
2123
- if (!hasStartMap && range.startLineNumber === 1 && range.startColumn === 1) {
2124
- hasStartMap = true;
2125
- }
2126
- map.addMapping({
2127
- generated: {
2128
- // 前两行固定为:
2129
- // (function anonymous($Add,$Aeq, ...
2130
- // ) {
2131
- line: i + 3,
2132
- column: 0
2133
- },
2134
- original: {
2135
- line: range.startLineNumber,
2136
- column: range.startColumn - 1
2137
- },
2138
- source: fileName
2139
- });
1554
+ diagnostic.references = [];
1555
+ while (i + 1 < parsed.length) {
1556
+ const ref = parsed[i + 1];
1557
+ let isRef = false;
1558
+ if (ref.code > DiagnosticCode.TagRefStart && ref.code < DiagnosticCode.TagRefEnd) {
1559
+ isRef = true;
1560
+ _tagsReferences.push(ref);
1561
+ }
1562
+ if (ref.code > DiagnosticCode.ReferenceStart && ref.code < DiagnosticCode.ReferenceEnd) {
1563
+ isRef = true;
1564
+ _references.push(ref);
1565
+ }
1566
+ if (!isRef) {
1567
+ break;
1568
+ }
1569
+ i++;
1570
+ ref.diagnostic = diagnostic;
1571
+ diagnostic.references.push(ref);
2140
1572
  }
2141
- if (!hasStartMap) {
2142
- map.addMapping({
2143
- generated: {
2144
- line: 3,
2145
- column: SCRIPT_PREFIX.length - "CpEnter();".length
2146
- },
2147
- original: {
2148
- line: 1,
2149
- column: 0
2150
- },
2151
- source: fileName
2152
- });
1573
+ }
1574
+ return {
1575
+ errors: _errors,
1576
+ warnings: _warnings,
1577
+ infos: _infos,
1578
+ hints: _hints,
1579
+ tags: _tags,
1580
+ references: _references,
1581
+ tagsReferences: _tagsReferences,
1582
+ sourcemaps: _sourcemaps
1583
+ };
1584
+ }
1585
+ function formatRange(range) {
1586
+ if (range.startLineNumber === range.endLineNumber) {
1587
+ if (range.startColumn === range.endColumn) {
1588
+ return `${range.startLineNumber}:${range.startColumn}`;
2153
1589
  }
2154
- {
2155
- const line0 = this.codeLines[0];
2156
- const file = `${fileName} <globals>`;
2157
- let globals = `global;
2158
- `;
2159
- map.addMapping({
2160
- generated: {
2161
- line: 3,
2162
- column: line0.indexOf(`global = `)
2163
- },
2164
- original: {
2165
- line: 1,
2166
- column: 0
2167
- },
2168
- source: file,
2169
- name: "global"
2170
- });
2171
- map.addMapping({
2172
- generated: {
2173
- line: 3,
2174
- column: SCRIPT_PREFIX.length
2175
- },
2176
- original: {
2177
- line: 1,
2178
- column: 7
2179
- },
2180
- source: file,
2181
- name: ""
2182
- });
2183
- let i = 1;
2184
- let pos = line0.indexOf(GLOBAL_HINT, SCRIPT_PREFIX.length) + GLOBAL_HINT.length;
2185
- for (const p of this.globals.values()) {
2186
- i++;
2187
- if (pos < 0) break;
2188
- const val = p[0];
2189
- pos = line0.indexOf(val, pos);
2190
- if (pos < 0) break;
2191
- const name = p[4];
2192
- map.addMapping({
2193
- generated: {
2194
- line: 3,
2195
- column: pos
2196
- },
2197
- original: {
2198
- line: i,
2199
- column: 0
2200
- },
2201
- source: file,
2202
- name
2203
- });
2204
- globals += `${name};
2205
- `;
2206
- }
2207
- map.addMapping({
2208
- generated: {
2209
- line: 3,
2210
- column: pos
2211
- },
2212
- original: {
2213
- line: i,
2214
- column: 0
2215
- },
2216
- source: file,
2217
- name: ""
2218
- });
2219
- map.setSourceContent(file, globals);
1590
+ return `${range.startLineNumber}:${range.startColumn}-${range.endColumn}`;
1591
+ }
1592
+ return `${range.startLineNumber}:${range.startColumn}-${range.endLineNumber}:${range.endColumn}`;
1593
+ }
1594
+ function formatDiagnostics(diagnostics, source, fileName) {
1595
+ const rangePrefix = fileName ? `${fileName}:` : "";
1596
+ const messages = [];
1597
+ for (const diagnostic of diagnostics) {
1598
+ const range = formatRange(diagnostic.range);
1599
+ const codeName = DiagnosticCode[diagnostic.code] || `Unknown(${diagnostic.code})`;
1600
+ let message = getDiagnosticMessage(diagnostic.code);
1601
+ for (const ref of diagnostic.references) {
1602
+ const refRange = formatRange(ref.range);
1603
+ message += `
1604
+ (${refRange}): ${getDiagnosticMessage(ref.code)}`;
2220
1605
  }
2221
- const prefix = "//# ";
2222
- const sourceURL = hasSchema ? fileName : `${ORIGIN}${fileName}`;
2223
- this.codeLines.push(
2224
- // Prevent source map from being recognized as of this file
2225
- `${prefix}sourceURL=${sourceURL}.js`,
2226
- `${prefix}sourceMappingURL=data:application/json,${encodeURIComponent(map.toString())}`
2227
- );
1606
+ messages.push(` ${codeName}(${rangePrefix}${range}): ${message}`);
2228
1607
  }
2229
- };
1608
+ return messages;
1609
+ }
2230
1610
 
2231
1611
  // src/compiler/generate-bytecode.ts
2232
1612
  import { getModule as getModule2, loadModule } from "@mirascript/bindings";
@@ -2246,6 +1626,17 @@ async function generateBytecode(script, options) {
2246
1626
 
2247
1627
  export {
2248
1628
  __export,
1629
+ VM_ARRAY_MAX_LENGTH,
1630
+ kVmScript,
1631
+ kVmFunction,
1632
+ kVmFunctionProxy,
1633
+ kVmContext,
1634
+ kVmExtern,
1635
+ kVmModule,
1636
+ kVmWrapper,
1637
+ VM_SCRIPT_NAME,
1638
+ VM_FUNCTION_ANONYMOUS_NAME,
1639
+ constants_exports,
2249
1640
  isArray,
2250
1641
  isFinite,
2251
1642
  isNaN,
@@ -2256,62 +1647,51 @@ export {
2256
1647
  values,
2257
1648
  entries,
2258
1649
  create,
1650
+ getPrototypeOf,
2259
1651
  fromEntries,
2260
1652
  defineProperty,
2261
- VmError,
2262
- $Add,
2263
- $Sub,
2264
- $Mul,
2265
- $Div,
2266
- $Same,
2267
- $Call,
2268
- $Type,
2269
- $ToBoolean,
2270
- $ToString,
2271
- $ToNumber,
2272
- $Format,
2273
- operations_exports,
2274
- VmSharedContext,
2275
- defineVmContextValue,
2276
- DefaultVmContext,
2277
- createVmContext,
1653
+ apply,
1654
+ hasOwnEnumerable,
1655
+ isVmScript,
2278
1656
  isVmContext,
2279
- Element,
2280
- Cp,
2281
- configCheckpoint,
2282
- GlobalFallback,
2283
- helpers_exports,
2284
1657
  isVmFunction,
2285
1658
  getVmFunctionInfo,
2286
- VmFunction,
2287
1659
  isVmWrapper,
2288
- VmExtern,
1660
+ isVmModule,
2289
1661
  isVmExtern,
2290
- toVmFunctionProxy,
2291
- fromVmFunctionProxy,
2292
- wrapToVmValue,
2293
- unwrapFromVmValue,
2294
- isVmScript,
2295
1662
  isVmCallable,
2296
- VM_ARRAY_MAX_LENGTH,
1663
+ isVmPrimitive,
2297
1664
  isVmArray,
2298
1665
  isVmRecord,
2299
1666
  isVmArrayLikeRecordByEntires,
2300
1667
  isVmArrayLikeRecordByKeys,
2301
1668
  isVmArrayLikeRecord,
2302
- isVmPrimitive,
2303
1669
  isVmConst,
2304
- VmModule,
2305
- isVmModule,
2306
1670
  isVmImmutable,
2307
1671
  isVmAny,
2308
1672
  isVmValue,
1673
+ serializeString,
1674
+ serializeRecordKey,
1675
+ serializeNil,
1676
+ serializeBoolean,
1677
+ serializeNumber,
1678
+ serializeArray,
1679
+ serializeRecord,
1680
+ serialize,
1681
+ display,
1682
+ VmError,
1683
+ toNumber,
1684
+ toBoolean,
1685
+ innerToString,
1686
+ toString,
1687
+ toFormat,
1688
+ convert_exports,
1689
+ emit,
2309
1690
  DiagnosticCode,
2310
1691
  getDiagnosticMessage,
2311
1692
  parseDiagnostics,
2312
- formatDiagnostic,
2313
- emit,
1693
+ formatDiagnostics,
2314
1694
  generateBytecodeSync,
2315
1695
  generateBytecode
2316
1696
  };
2317
- //# sourceMappingURL=chunk-55FKP56O.js.map
1697
+ //# sourceMappingURL=chunk-YZGL3D7L.js.map