@mirascript/mirascript 0.1.61 → 0.1.62-alpha.7

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 (176) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +4 -4
  3. package/dist/chunk-6CAGNGY6.js +0 -3980
  4. package/dist/chunk-6CAGNGY6.js.map +0 -6
  5. package/dist/chunk-VRGQ23AK.js +0 -1815
  6. package/dist/chunk-VRGQ23AK.js.map +0 -6
  7. package/dist/compiler/compile-fast.d.ts +0 -7
  8. package/dist/compiler/compile-fast.d.ts.map +0 -1
  9. package/dist/compiler/create-script.d.ts +0 -20
  10. package/dist/compiler/create-script.d.ts.map +0 -1
  11. package/dist/compiler/diagnostic.d.ts +0 -71
  12. package/dist/compiler/diagnostic.d.ts.map +0 -1
  13. package/dist/compiler/emit/constants.d.ts +0 -5
  14. package/dist/compiler/emit/constants.d.ts.map +0 -1
  15. package/dist/compiler/emit/consts.d.ts +0 -6
  16. package/dist/compiler/emit/consts.d.ts.map +0 -1
  17. package/dist/compiler/emit/index.d.ts +0 -62
  18. package/dist/compiler/emit/index.d.ts.map +0 -1
  19. package/dist/compiler/emit/sourcemap.d.ts +0 -6
  20. package/dist/compiler/emit/sourcemap.d.ts.map +0 -1
  21. package/dist/compiler/emit-script.d.ts +0 -10
  22. package/dist/compiler/emit-script.d.ts.map +0 -1
  23. package/dist/compiler/generate-bytecode.d.ts +0 -12
  24. package/dist/compiler/generate-bytecode.d.ts.map +0 -1
  25. package/dist/compiler/index.d.ts +0 -14
  26. package/dist/compiler/index.d.ts.map +0 -1
  27. package/dist/compiler/load-module.d.ts +0 -2
  28. package/dist/compiler/load-module.d.ts.map +0 -1
  29. package/dist/compiler/types.d.ts +0 -16
  30. package/dist/compiler/types.d.ts.map +0 -1
  31. package/dist/compiler/worker-manager.d.ts +0 -11
  32. package/dist/compiler/worker-manager.d.ts.map +0 -1
  33. package/dist/compiler/worker.d.ts +0 -10
  34. package/dist/compiler/worker.d.ts.map +0 -1
  35. package/dist/compiler/worker.js +0 -31
  36. package/dist/compiler/worker.js.map +0 -6
  37. package/dist/helpers/analyze.d.ts +0 -8
  38. package/dist/helpers/analyze.d.ts.map +0 -1
  39. package/dist/helpers/constants.d.ts +0 -20
  40. package/dist/helpers/constants.d.ts.map +0 -1
  41. package/dist/helpers/convert/index.d.ts +0 -5
  42. package/dist/helpers/convert/index.d.ts.map +0 -1
  43. package/dist/helpers/convert/to-boolean.d.ts +0 -4
  44. package/dist/helpers/convert/to-boolean.d.ts.map +0 -1
  45. package/dist/helpers/convert/to-format.d.ts +0 -4
  46. package/dist/helpers/convert/to-format.d.ts.map +0 -1
  47. package/dist/helpers/convert/to-number.d.ts +0 -4
  48. package/dist/helpers/convert/to-number.d.ts.map +0 -1
  49. package/dist/helpers/convert/to-string.d.ts +0 -6
  50. package/dist/helpers/convert/to-string.d.ts.map +0 -1
  51. package/dist/helpers/error.d.ts +0 -11
  52. package/dist/helpers/error.d.ts.map +0 -1
  53. package/dist/helpers/serialize.d.ts +0 -63
  54. package/dist/helpers/serialize.d.ts.map +0 -1
  55. package/dist/helpers/types.d.ts +0 -54
  56. package/dist/helpers/types.d.ts.map +0 -1
  57. package/dist/helpers/utils.d.ts +0 -37
  58. package/dist/helpers/utils.d.ts.map +0 -1
  59. package/dist/index.d.ts +0 -5
  60. package/dist/index.d.ts.map +0 -1
  61. package/dist/index.js +0 -243
  62. package/dist/index.js.map +0 -6
  63. package/dist/subtle.d.ts +0 -14
  64. package/dist/subtle.d.ts.map +0 -1
  65. package/dist/subtle.js +0 -122
  66. package/dist/subtle.js.map +0 -6
  67. package/dist/vm/checkpoint.d.ts +0 -9
  68. package/dist/vm/checkpoint.d.ts.map +0 -1
  69. package/dist/vm/index.d.ts +0 -5
  70. package/dist/vm/index.d.ts.map +0 -1
  71. package/dist/vm/lib/global/bit.d.ts +0 -8
  72. package/dist/vm/lib/global/bit.d.ts.map +0 -1
  73. package/dist/vm/lib/global/debug.d.ts +0 -38
  74. package/dist/vm/lib/global/debug.d.ts.map +0 -1
  75. package/dist/vm/lib/global/index.d.ts +0 -9
  76. package/dist/vm/lib/global/index.d.ts.map +0 -1
  77. package/dist/vm/lib/global/json.d.ts +0 -3
  78. package/dist/vm/lib/global/json.d.ts.map +0 -1
  79. package/dist/vm/lib/global/math/arr.d.ts +0 -7
  80. package/dist/vm/lib/global/math/arr.d.ts.map +0 -1
  81. package/dist/vm/lib/global/math/const.d.ts +0 -31
  82. package/dist/vm/lib/global/math/const.d.ts.map +0 -1
  83. package/dist/vm/lib/global/math/gamma.d.ts +0 -3
  84. package/dist/vm/lib/global/math/gamma.d.ts.map +0 -1
  85. package/dist/vm/lib/global/math/index.d.ts +0 -9
  86. package/dist/vm/lib/global/math/index.d.ts.map +0 -1
  87. package/dist/vm/lib/global/math/round.d.ts +0 -6
  88. package/dist/vm/lib/global/math/round.d.ts.map +0 -1
  89. package/dist/vm/lib/global/math/unary.d.ts +0 -24
  90. package/dist/vm/lib/global/math/unary.d.ts.map +0 -1
  91. package/dist/vm/lib/global/sequence/all-any.d.ts +0 -3
  92. package/dist/vm/lib/global/sequence/all-any.d.ts.map +0 -1
  93. package/dist/vm/lib/global/sequence/entries.d.ts +0 -11
  94. package/dist/vm/lib/global/sequence/entries.d.ts.map +0 -1
  95. package/dist/vm/lib/global/sequence/find.d.ts +0 -9
  96. package/dist/vm/lib/global/sequence/find.d.ts.map +0 -1
  97. package/dist/vm/lib/global/sequence/flatten.d.ts +0 -2
  98. package/dist/vm/lib/global/sequence/flatten.d.ts.map +0 -1
  99. package/dist/vm/lib/global/sequence/index.d.ts +0 -14
  100. package/dist/vm/lib/global/sequence/index.d.ts.map +0 -1
  101. package/dist/vm/lib/global/sequence/len.d.ts +0 -2
  102. package/dist/vm/lib/global/sequence/len.d.ts.map +0 -1
  103. package/dist/vm/lib/global/sequence/map-filter.d.ts +0 -8
  104. package/dist/vm/lib/global/sequence/map-filter.d.ts.map +0 -1
  105. package/dist/vm/lib/global/sequence/new.d.ts +0 -4
  106. package/dist/vm/lib/global/sequence/new.d.ts.map +0 -1
  107. package/dist/vm/lib/global/sequence/repeat.d.ts +0 -3
  108. package/dist/vm/lib/global/sequence/repeat.d.ts.map +0 -1
  109. package/dist/vm/lib/global/sequence/reverse.d.ts +0 -2
  110. package/dist/vm/lib/global/sequence/reverse.d.ts.map +0 -1
  111. package/dist/vm/lib/global/sequence/sort.d.ts +0 -4
  112. package/dist/vm/lib/global/sequence/sort.d.ts.map +0 -1
  113. package/dist/vm/lib/global/sequence/unique.d.ts +0 -4
  114. package/dist/vm/lib/global/sequence/unique.d.ts.map +0 -1
  115. package/dist/vm/lib/global/sequence/with.d.ts +0 -4
  116. package/dist/vm/lib/global/sequence/with.d.ts.map +0 -1
  117. package/dist/vm/lib/global/sequence/zip.d.ts +0 -3
  118. package/dist/vm/lib/global/sequence/zip.d.ts.map +0 -1
  119. package/dist/vm/lib/global/string/case.d.ts +0 -3
  120. package/dist/vm/lib/global/string/case.d.ts.map +0 -1
  121. package/dist/vm/lib/global/string/index.d.ts +0 -8
  122. package/dist/vm/lib/global/string/index.d.ts.map +0 -1
  123. package/dist/vm/lib/global/string/search.d.ts +0 -4
  124. package/dist/vm/lib/global/string/search.d.ts.map +0 -1
  125. package/dist/vm/lib/global/string/trim.d.ts +0 -4
  126. package/dist/vm/lib/global/string/trim.d.ts.map +0 -1
  127. package/dist/vm/lib/global/time.d.ts +0 -4
  128. package/dist/vm/lib/global/time.d.ts.map +0 -1
  129. package/dist/vm/lib/global/to-primitive.d.ts +0 -4
  130. package/dist/vm/lib/global/to-primitive.d.ts.map +0 -1
  131. package/dist/vm/lib/helpers.d.ts +0 -57
  132. package/dist/vm/lib/helpers.d.ts.map +0 -1
  133. package/dist/vm/lib/index.d.ts +0 -4
  134. package/dist/vm/lib/index.d.ts.map +0 -1
  135. package/dist/vm/lib/loader.d.ts +0 -23
  136. package/dist/vm/lib/loader.d.ts.map +0 -1
  137. package/dist/vm/lib/mod/index.d.ts +0 -2
  138. package/dist/vm/lib/mod/index.d.ts.map +0 -1
  139. package/dist/vm/lib/mod/matrix.d.ts +0 -15
  140. package/dist/vm/lib/mod/matrix.d.ts.map +0 -1
  141. package/dist/vm/operations/call.d.ts +0 -9
  142. package/dist/vm/operations/call.d.ts.map +0 -1
  143. package/dist/vm/operations/common.d.ts +0 -4
  144. package/dist/vm/operations/common.d.ts.map +0 -1
  145. package/dist/vm/operations/compound.d.ts +0 -22
  146. package/dist/vm/operations/compound.d.ts.map +0 -1
  147. package/dist/vm/operations/convert.d.ts +0 -10
  148. package/dist/vm/operations/convert.d.ts.map +0 -1
  149. package/dist/vm/operations/cp.d.ts +0 -2
  150. package/dist/vm/operations/cp.d.ts.map +0 -1
  151. package/dist/vm/operations/helpers.d.ts +0 -24
  152. package/dist/vm/operations/helpers.d.ts.map +0 -1
  153. package/dist/vm/operations/index.d.ts +0 -10
  154. package/dist/vm/operations/index.d.ts.map +0 -1
  155. package/dist/vm/operations/operator.d.ts +0 -46
  156. package/dist/vm/operations/operator.d.ts.map +0 -1
  157. package/dist/vm/operations/slice.d.ts +0 -6
  158. package/dist/vm/operations/slice.d.ts.map +0 -1
  159. package/dist/vm/operations/type-check.d.ts +0 -16
  160. package/dist/vm/operations/type-check.d.ts.map +0 -1
  161. package/dist/vm/operations/utils.d.ts +0 -11
  162. package/dist/vm/operations/utils.d.ts.map +0 -1
  163. package/dist/vm/types/boundary.d.ts +0 -13
  164. package/dist/vm/types/boundary.d.ts.map +0 -1
  165. package/dist/vm/types/context.d.ts +0 -33
  166. package/dist/vm/types/context.d.ts.map +0 -1
  167. package/dist/vm/types/extern.d.ts +0 -44
  168. package/dist/vm/types/extern.d.ts.map +0 -1
  169. package/dist/vm/types/function.d.ts +0 -50
  170. package/dist/vm/types/function.d.ts.map +0 -1
  171. package/dist/vm/types/index.d.ts +0 -58
  172. package/dist/vm/types/index.d.ts.map +0 -1
  173. package/dist/vm/types/module.d.ts +0 -25
  174. package/dist/vm/types/module.d.ts.map +0 -1
  175. package/dist/vm/types/wrapper.d.ts +0 -25
  176. package/dist/vm/types/wrapper.d.ts.map +0 -1
@@ -1,1815 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
-
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_IDENTIFIER_FULL: () => REG_IDENTIFIER_FULL,
14
- REG_NUMBER: () => REG_NUMBER,
15
- REG_OCT: () => REG_OCT,
16
- REG_ORDINAL: () => REG_ORDINAL,
17
- REG_ORDINAL_FULL: () => REG_ORDINAL_FULL,
18
- REG_WHITESPACE: () => REG_WHITESPACE,
19
- VM_ARRAY_MAX_LENGTH: () => VM_ARRAY_MAX_LENGTH,
20
- VM_FUNCTION_ANONYMOUS_NAME: () => VM_FUNCTION_ANONYMOUS_NAME,
21
- VM_SCRIPT_NAME: () => VM_SCRIPT_NAME,
22
- kVmContext: () => kVmContext,
23
- kVmExtern: () => kVmExtern,
24
- kVmFunction: () => kVmFunction,
25
- kVmFunctionProxy: () => kVmFunctionProxy,
26
- kVmModule: () => kVmModule,
27
- kVmScript: () => kVmScript,
28
- kVmWrapper: () => kVmWrapper
29
- });
30
- var REG_IDENTIFIER = /(?:_+|@+|\$+|\p{XID_Start})\p{XID_Continue}*/u;
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);
34
- var REG_WHITESPACE = /[ \t\v\f\r\n]/u;
35
- var REG_HEX = /0[xX][a-fA-F0-9_]*[a-fA-F0-9]/;
36
- var REG_OCT = /0[oO][0-7_]*[0-7]/;
37
- var REG_BIN = /0[bB][01_]*[01]/;
38
- var REG_NUMBER = /\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?[\d_]*\d)?/u;
39
- var VM_ARRAY_MAX_LENGTH = 134217728;
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");
47
- var VM_SCRIPT_NAME = `<script_root>`;
48
- var VM_FUNCTION_ANONYMOUS_NAME = `<anonymous>`;
49
-
50
- // src/helpers/utils.ts
51
- var { isArray } = Array;
52
- var {
53
- isFinite,
54
- isNaN,
55
- isInteger,
56
- isSafeInteger,
57
- NaN: NotNumber,
58
- POSITIVE_INFINITY: PositiveInfinity,
59
- NEGATIVE_INFINITY: NegativeInfinity
60
- } = Number;
61
- var { hasOwn, keys, values, entries, create, fromEntries, defineProperty, getOwnPropertyNames } = Object;
62
- var { apply, getPrototypeOf } = Reflect;
63
- var hasOwnEnumerable = Function.call.bind(
64
- // eslint-disable-next-line @typescript-eslint/unbound-method
65
- Object.prototype.propertyIsEnumerable
66
- );
67
-
68
- // src/helpers/types.ts
69
- function isVmScript(value) {
70
- return typeof value === "function" && kVmScript in value;
71
- }
72
- function isVmContext(context) {
73
- return context != null && typeof context == "object" && kVmContext in context;
74
- }
75
- function isVmFunction(value) {
76
- return typeof value == "function" && kVmFunction in value;
77
- }
78
- function getVmFunctionInfo(value) {
79
- if (typeof value != "function") return void 0;
80
- return value[kVmFunction];
81
- }
82
- function isVmWrapper(value) {
83
- return value != null && typeof value == "object" && kVmWrapper in value;
84
- }
85
- function isVmModule(value) {
86
- return value != null && typeof value == "object" && kVmModule in value;
87
- }
88
- function isVmExtern(value) {
89
- return value != null && typeof value == "object" && kVmExtern in value;
90
- }
91
- function isVmCallable(value) {
92
- return isVmFunction(value) || isVmExtern(value) && typeof value.value == "function";
93
- }
94
- function isVmPrimitive(value) {
95
- if (value === null || typeof value == "number" || typeof value == "string" || typeof value == "boolean") {
96
- value;
97
- value;
98
- return true;
99
- }
100
- return false;
101
- }
102
- function isVmArray(value) {
103
- if (!isArray(value)) return false;
104
- value;
105
- return true;
106
- }
107
- function isVmRecord(value) {
108
- if (value == null || typeof value !== "object") return false;
109
- if (isVmWrapper(value)) return false;
110
- if (isVmArray(value)) return false;
111
- value;
112
- return true;
113
- }
114
- function isVmArrayLikeRecordByEntires(entries2) {
115
- const { length } = entries2;
116
- if (length === 0) return true;
117
- if (length > VM_ARRAY_MAX_LENGTH) return false;
118
- const firstKey = entries2[0][0];
119
- if (firstKey !== "0") return false;
120
- const lastKey = entries2[length - 1][0];
121
- if (lastKey !== String(length - 1)) return false;
122
- return true;
123
- }
124
- function isVmArrayLikeRecordByKeys(keys2) {
125
- const { length } = keys2;
126
- if (length === 0) return true;
127
- if (length > VM_ARRAY_MAX_LENGTH) return false;
128
- const firstKey = keys2[0];
129
- if (firstKey !== "0") return false;
130
- const lastKey = keys2[length - 1];
131
- if (lastKey !== String(length - 1)) return false;
132
- return true;
133
- }
134
- function isVmArrayLikeRecord(value) {
135
- return isVmArrayLikeRecordByKeys(keys(value));
136
- }
137
- var MAX_DEPTH = 16;
138
- function isVmArrayDeep(value, depth) {
139
- const proto1 = getPrototypeOf(value);
140
- if (!isArray(proto1)) return false;
141
- if (!depth) return true;
142
- return value.every((item) => isVmConstInner(item, depth));
143
- }
144
- function isVmRecordDeep(value, depth) {
145
- let isRecord;
146
- const proto1 = getPrototypeOf(value);
147
- if (proto1 == null) {
148
- isRecord = true;
149
- } else {
150
- const proto2 = getPrototypeOf(proto1);
151
- if (proto2 != null) {
152
- isRecord = false;
153
- } else {
154
- isRecord = "hasOwnProperty" in value;
155
- }
156
- }
157
- if (!isRecord) return false;
158
- if (!depth) return true;
159
- return values(value).every((value2) => isVmConstInner(value2, depth));
160
- }
161
- function isVmConstInner(value, depth) {
162
- if (depth++ >= MAX_DEPTH) return false;
163
- switch (typeof value) {
164
- case "string":
165
- case "number":
166
- case "boolean":
167
- case "undefined":
168
- return true;
169
- case "object":
170
- if (value == null) return true;
171
- if (isVmWrapper(value)) return false;
172
- if (isArray(value)) {
173
- return isVmArrayDeep(value, depth);
174
- } else {
175
- return isVmRecordDeep(value, depth);
176
- }
177
- case "function":
178
- case "bigint":
179
- case "symbol":
180
- default:
181
- return false;
182
- }
183
- }
184
- function isVmConst(value, checkDeep = false) {
185
- switch (typeof value) {
186
- case "string":
187
- case "number":
188
- case "boolean":
189
- return true;
190
- case "object":
191
- if (value == null) return true;
192
- if (isVmWrapper(value)) return false;
193
- if (!checkDeep) {
194
- if (isArray(value)) {
195
- return isVmArrayDeep(value, 0);
196
- } else {
197
- return isVmRecordDeep(value, 0);
198
- }
199
- } else {
200
- return isVmConstInner(value, 1);
201
- }
202
- case "undefined":
203
- case "function":
204
- case "bigint":
205
- case "symbol":
206
- default:
207
- return false;
208
- }
209
- }
210
- function isVmImmutable(value, checkDeep = false) {
211
- return isVmModule(value) || isVmFunction(value) || isVmConst(value, checkDeep);
212
- }
213
- function isVmAny(value, checkDeep) {
214
- switch (typeof value) {
215
- case "string":
216
- case "number":
217
- case "boolean":
218
- case "undefined":
219
- return true;
220
- case "object":
221
- if (value == null) return true;
222
- if (isVmWrapper(value)) return true;
223
- return isVmConst(value, checkDeep);
224
- case "function":
225
- return isVmFunction(value);
226
- case "bigint":
227
- case "symbol":
228
- default:
229
- return false;
230
- }
231
- }
232
- function isVmValue(value, checkDeep = false) {
233
- if (value === void 0) return false;
234
- return isVmAny(value, checkDeep);
235
- }
236
-
237
- // src/helpers/serialize.ts
238
- var DEFAULT_OPTIONS = Object.freeze({
239
- maxDepth: 128,
240
- serializeNil,
241
- serializeBoolean,
242
- serializeNumber,
243
- serializeString: serializeStringImpl,
244
- serializeStringQuote: (value) => value,
245
- serializeStringEscape: (value) => value,
246
- serializeStringContent: (value) => value,
247
- serializeArray,
248
- serializeRecord,
249
- serializePropName: String,
250
- serializeFunction: serializeNil,
251
- serializeModule: serializeNil,
252
- serializeExtern: serializeNil
253
- });
254
- function isDefaultOptions(options) {
255
- return options == null || options === DEFAULT_OPTIONS;
256
- }
257
- function mergeOptions(base, options) {
258
- if (options == null) return base;
259
- let opt = null;
260
- for (const key in options) {
261
- if (!hasOwn(options, key) || !hasOwn(base, key)) continue;
262
- const el = options[key];
263
- if (el == null) continue;
264
- opt ??= { ...base };
265
- opt[key] = el;
266
- }
267
- return opt ? Object.freeze(opt) : base;
268
- }
269
- function getSerializeOptions(options) {
270
- if (isDefaultOptions(options)) return DEFAULT_OPTIONS;
271
- return mergeOptions(DEFAULT_OPTIONS, options);
272
- }
273
- function serializeStringEscaped(escaped, options) {
274
- return options.serializeStringEscape("\\" + escaped, options);
275
- }
276
- function serializeStringContent(value, options) {
277
- return options.serializeStringContent(value, options);
278
- }
279
- var STRING_QUOTE = `'`;
280
- var STRING_REG = new RegExp(String.raw`[${STRING_QUOTE}\\\$\p{C}\u2028\u2029]`, "gu");
281
- function serializeStringImpl(value, options) {
282
- const oq = options.serializeStringQuote(STRING_QUOTE, true, options);
283
- const cq = options.serializeStringQuote(STRING_QUOTE, false, options);
284
- if (value.length === 0) {
285
- return oq + cq;
286
- }
287
- let ret = oq;
288
- let lastIndex = 0;
289
- while (value.length > lastIndex && /^[\p{M}]$/u.test(value[lastIndex])) {
290
- const c = serializeStringEscaped(`u{${value.codePointAt(lastIndex).toString(16)}}`, options);
291
- ret += c;
292
- lastIndex += 1;
293
- }
294
- STRING_REG.lastIndex = lastIndex;
295
- let match;
296
- while ((match = STRING_REG.exec(value)) !== null) {
297
- const char = match[0];
298
- ret += serializeStringContent(value.slice(lastIndex, match.index), options);
299
- lastIndex = STRING_REG.lastIndex;
300
- if (char === STRING_QUOTE) {
301
- ret += serializeStringEscaped(STRING_QUOTE, options);
302
- } else if (char === "\0") {
303
- ret += serializeStringEscaped(`0`, options);
304
- } else if (char === "\n") {
305
- ret += serializeStringEscaped(`n`, options);
306
- } else if (char === "\r") {
307
- ret += serializeStringEscaped(`r`, options);
308
- } else if (char === " ") {
309
- ret += serializeStringEscaped(`t`, options);
310
- } else if (char === "\b") {
311
- ret += serializeStringEscaped(`b`, options);
312
- } else if (char === "\f") {
313
- ret += serializeStringEscaped(`f`, options);
314
- } else if (char === "\v") {
315
- ret += serializeStringEscaped(`v`, options);
316
- } else if (char === "\\") {
317
- ret += serializeStringEscaped(`\\`, options);
318
- } else if (char === "$") {
319
- ret += serializeStringEscaped(`$`, options);
320
- } else {
321
- const code = char.codePointAt(0);
322
- if (code <= 127) {
323
- ret += serializeStringEscaped(`x${code.toString(16).padStart(2, "0")}`, options);
324
- } else if (code >= 55296 && code <= 57343) {
325
- ret += serializeStringContent("�", options);
326
- } else {
327
- ret += serializeStringEscaped(`u{${code.toString(16)}}`, options);
328
- }
329
- }
330
- }
331
- ret += serializeStringContent(value.slice(lastIndex), options);
332
- ret += cq;
333
- return ret;
334
- }
335
- function serializeString(value, options) {
336
- return serializeStringImpl(value, getSerializeOptions(options));
337
- }
338
- function serializeRecordKeyDefault(key) {
339
- if (REG_ORDINAL_FULL.test(key) || REG_IDENTIFIER_FULL.test(key)) {
340
- return key;
341
- }
342
- return serializeStringImpl(key, DEFAULT_OPTIONS);
343
- }
344
- function serializeRecordKeyOpt(value, options) {
345
- if (isDefaultOptions(options)) {
346
- return serializeRecordKeyDefault(value);
347
- }
348
- if (REG_ORDINAL_FULL.test(value)) {
349
- return options.serializePropName(Number(value), options);
350
- }
351
- if (REG_IDENTIFIER_FULL.test(value)) {
352
- return options.serializePropName(value, options);
353
- }
354
- return options.serializeString(value, options);
355
- }
356
- function serializeRecordKey(key, options) {
357
- if (isDefaultOptions(options)) {
358
- return serializeRecordKeyDefault(key);
359
- }
360
- return serializeRecordKeyOpt(key, getSerializeOptions(options));
361
- }
362
- function serializeNil() {
363
- return "nil";
364
- }
365
- function serializeBoolean(value) {
366
- return value ? "true" : "false";
367
- }
368
- function serializeNumber(value) {
369
- if (isNaN(value)) return "nan";
370
- if (!isFinite(value)) return value < 0 ? "-inf" : "inf";
371
- if (value === 0) {
372
- if (1 / value < 0) return "-0";
373
- return "0";
374
- }
375
- return String(value);
376
- }
377
- function serializeArray(value, depth, options) {
378
- if (depth > options.maxDepth) return `[]`;
379
- if (value.length === 0) return "[]";
380
- let str = "[";
381
- for (let i = 0; i < value.length; i++) {
382
- if (i > 0) str += ", ";
383
- str += serializeImpl(value[i], depth, options);
384
- }
385
- str += "]";
386
- return str;
387
- }
388
- var { valueOf } = Object.prototype;
389
- function customValueOf(value) {
390
- const thisValueOf = value.valueOf;
391
- if (typeof thisValueOf != "function" || thisValueOf === valueOf) {
392
- return void 0;
393
- }
394
- const customValue = thisValueOf.call(value);
395
- if (customValue === value) return void 0;
396
- return customValue;
397
- }
398
- function serializeRecord(value, depth, options) {
399
- const customValue = customValueOf(value);
400
- if (customValue !== void 0) {
401
- return serializeImpl(customValue, depth - 1, options);
402
- }
403
- if (depth > options.maxDepth) return `()`;
404
- const e = entries(value);
405
- if (e.length === 0) return "()";
406
- if (e.length === 1) {
407
- const [k, v] = e[0];
408
- if (k === "0") {
409
- return `(${serializeImpl(v, depth, options)},)`;
410
- }
411
- return `(${serializeRecordKeyOpt(k, options)}: ${serializeImpl(v, depth, options)})`;
412
- }
413
- const omitKey = isVmArrayLikeRecordByEntires(e);
414
- let str = "(";
415
- for (const [key, val] of e) {
416
- if (str.length > 1) str += ", ";
417
- if (omitKey) {
418
- str += serializeImpl(val, depth, options);
419
- } else {
420
- str += `${serializeRecordKeyOpt(key, options)}: ${serializeImpl(val, depth, options)}`;
421
- }
422
- }
423
- str += ")";
424
- return str;
425
- }
426
- function serializeImpl(value, depth, options) {
427
- if (value == null) {
428
- return options.serializeNil(options);
429
- }
430
- if (typeof value == "boolean") {
431
- return options.serializeBoolean(value, options);
432
- }
433
- if (typeof value == "number") {
434
- return options.serializeNumber(value, options);
435
- }
436
- if (typeof value == "string") {
437
- return options.serializeString(value, options);
438
- }
439
- if (isVmFunction(value)) {
440
- return options.serializeFunction(value, options);
441
- }
442
- if (isVmModule(value)) {
443
- return options.serializeModule(value, depth + 1, options);
444
- }
445
- if (isVmExtern(value)) {
446
- return options.serializeExtern(value, depth + 1, options);
447
- }
448
- if (isVmArray(value)) {
449
- return options.serializeArray(value, depth + 1, options);
450
- }
451
- if (isVmRecord(value)) {
452
- return options.serializeRecord(value, depth + 1, options);
453
- }
454
- value;
455
- return options.serializeNil(options);
456
- }
457
- function serialize(value, options) {
458
- return serializeImpl(value, 0, getSerializeOptions(options));
459
- }
460
- function displayFunction(value) {
461
- try {
462
- const name = getVmFunctionInfo(value)?.fullName;
463
- return name ? `<function ${name}>` : `<function>`;
464
- } catch {
465
- return `<function>`;
466
- }
467
- }
468
- function displayModule(value) {
469
- try {
470
- return value.toString(true);
471
- } catch {
472
- return `<module>`;
473
- }
474
- }
475
- function displayExtern(value) {
476
- try {
477
- const tag = `<extern ${value.tag}>`;
478
- const rep = value.toString(true);
479
- if (rep === tag || rep.length > 50) {
480
- return tag;
481
- }
482
- return `${tag} ${rep}`;
483
- } catch {
484
- return `<extern>`;
485
- }
486
- }
487
- var DISPLAY_OPTIONS = Object.freeze({
488
- maxDepth: 3,
489
- serializeNil,
490
- serializeBoolean,
491
- serializeNumber,
492
- serializeString: serializeStringImpl,
493
- serializeStringQuote: (value) => value,
494
- serializeStringEscape: (value) => value,
495
- serializeStringContent: (value) => value,
496
- serializeArray,
497
- serializeRecord,
498
- serializePropName: String,
499
- serializeFunction: displayFunction,
500
- serializeModule: displayModule,
501
- serializeExtern: displayExtern
502
- });
503
- function display(value, options) {
504
- const opt = mergeOptions(DISPLAY_OPTIONS, options);
505
- return serializeImpl(value, 0, opt);
506
- }
507
-
508
- // src/helpers/error.ts
509
- var VmError = class _VmError extends Error {
510
- constructor(message, recovered) {
511
- super(message);
512
- this.recovered = recovered;
513
- this.name = "VmError";
514
- }
515
- /** 从其他错误构造 */
516
- static from(prefix, error, recovered) {
517
- if (prefix) {
518
- if (prefix.endsWith(":")) {
519
- prefix += " ";
520
- } else if (!prefix.endsWith(": ")) {
521
- prefix += ": ";
522
- }
523
- }
524
- let vmError;
525
- if (error instanceof Error) {
526
- vmError = new _VmError(`${prefix}${error.message}`, recovered);
527
- vmError.stack = error.stack;
528
- } else {
529
- vmError = new _VmError(`${prefix}${String(error)}`, recovered);
530
- }
531
- vmError.cause = error;
532
- return vmError;
533
- }
534
- };
535
-
536
- // src/compiler/generate-bytecode.ts
537
- import { getModule, loadModule } from "@mirascript/bindings";
538
- function generateBytecodeSync(script, options) {
539
- const module = getModule();
540
- const result = module.compileSync(script, options);
541
- return [result.chunk, result.diagnostics];
542
- }
543
- async function generateBytecode(script, options) {
544
- if (options == null) {
545
- throw new TypeError("options must be provided");
546
- }
547
- const module = await loadModule();
548
- const result = "compile" in module ? await module.compile(script, options) : module.compileSync(script, options);
549
- return [result.chunk, result.diagnostics];
550
- }
551
-
552
- // src/compiler/emit/index.ts
553
- import { OpCode } from "@mirascript/constants";
554
-
555
- // src/helpers/convert/to-string.ts
556
- function numberToString(value) {
557
- if (isNaN(value)) return "nan";
558
- if (value === Infinity) return "inf";
559
- if (value === -Infinity) return "-inf";
560
- return String(value);
561
- }
562
- function innerToString(value, useBraces) {
563
- if (value == null) return "nil";
564
- if (typeof value == "number") return numberToString(value);
565
- if (typeof value == "string" || typeof value == "boolean") return String(value);
566
- if (typeof value == "function") return displayFunction(value);
567
- if (isVmWrapper(value)) return value.toString(useBraces);
568
- if (isVmArray(value)) {
569
- const strings = [];
570
- for (const item of value) {
571
- strings.push(innerToString(item, true));
572
- }
573
- const results = strings.join(", ");
574
- if (!useBraces) return results;
575
- return `[${results}]`;
576
- } else {
577
- const entries2 = keys(value).map((key) => `${key}: ${innerToString(value[key], true)}`).join(", ");
578
- if (!useBraces) return entries2;
579
- return `(${entries2})`;
580
- }
581
- }
582
- function toString(value, fallback) {
583
- if (typeof value === "string") return value;
584
- if (value == null) return "";
585
- try {
586
- return innerToString(value, false);
587
- } catch (ex) {
588
- if (fallback === void 0) {
589
- const e = new VmError(`Failed to convert value to string: ${display(value)}`, "");
590
- e.cause = ex;
591
- throw e;
592
- }
593
- return fallback;
594
- }
595
- }
596
-
597
- // src/compiler/emit/consts.ts
598
- var { stringify } = JSON;
599
- function toJsLiteral(value) {
600
- if (value === null) return "null";
601
- if (value === void 0) return "undefined";
602
- if (typeof value == "string") {
603
- return stringify(value);
604
- }
605
- if (value === 0) {
606
- if (1 / value === -Infinity) return "-0";
607
- return "0";
608
- }
609
- return String(value);
610
- }
611
- var DECODER = new TextDecoder();
612
- var { fromCharCode } = String;
613
- function shortStringInJS(buf, begin, length) {
614
- if (length < 4) {
615
- if (length < 2) {
616
- if (length === 0) return "";
617
- const a3 = buf[begin];
618
- if ((a3 & 128) > 0) {
619
- return;
620
- }
621
- return fromCharCode(a3);
622
- }
623
- const a2 = buf[begin++];
624
- const b2 = buf[begin++];
625
- if ((a2 & 128) > 0 || (b2 & 128) > 0) {
626
- return;
627
- }
628
- if (length < 3) return fromCharCode(a2, b2);
629
- const c2 = buf[begin++];
630
- if ((c2 & 128) > 0) {
631
- return;
632
- }
633
- return fromCharCode(a2, b2, c2);
634
- }
635
- const a = buf[begin++];
636
- const b = buf[begin++];
637
- const c = buf[begin++];
638
- const d = buf[begin++];
639
- if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
640
- return;
641
- }
642
- if (length < 6) {
643
- if (length === 4) return fromCharCode(a, b, c, d);
644
- const e2 = buf[begin++];
645
- if ((e2 & 128) > 0) {
646
- return;
647
- }
648
- return fromCharCode(a, b, c, d, e2);
649
- }
650
- const e = buf[begin++];
651
- const f = buf[begin++];
652
- if ((e & 128) > 0 || (f & 128) > 0) {
653
- return;
654
- }
655
- if (length < 8) {
656
- if (length < 7) return fromCharCode(a, b, c, d, e, f);
657
- const g2 = buf[begin++];
658
- if ((g2 & 128) > 0) {
659
- return;
660
- }
661
- return fromCharCode(a, b, c, d, e, f, g2);
662
- }
663
- const g = buf[begin++];
664
- const h = buf[begin++];
665
- if ((g & 128) > 0 || (h & 128) > 0) {
666
- return;
667
- }
668
- if (length < 10) {
669
- if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
670
- const i2 = buf[begin++];
671
- if ((i2 & 128) > 0) {
672
- return;
673
- }
674
- return fromCharCode(a, b, c, d, e, f, g, h, i2);
675
- }
676
- const i = buf[begin++];
677
- const j = buf[begin++];
678
- if ((i & 128) > 0 || (j & 128) > 0) {
679
- return;
680
- }
681
- if (length < 12) {
682
- if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
683
- const k2 = buf[begin++];
684
- if ((k2 & 128) > 0) {
685
- return;
686
- }
687
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k2);
688
- }
689
- const k = buf[begin++];
690
- const l = buf[begin++];
691
- if ((k & 128) > 0 || (l & 128) > 0) {
692
- return;
693
- }
694
- if (length < 14) {
695
- if (length === 12) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
696
- const m2 = buf[begin++];
697
- if ((m2 & 128) > 0) {
698
- begin -= 13;
699
- return;
700
- }
701
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m2);
702
- }
703
- const m = buf[begin++];
704
- const n = buf[begin++];
705
- if ((m & 128) > 0 || (n & 128) > 0) {
706
- return;
707
- }
708
- if (length < 15) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
709
- const o = buf[begin++];
710
- if ((o & 128) > 0) {
711
- return;
712
- }
713
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
714
- }
715
- function decodeStr(buffer, offset, length) {
716
- if (length < 16) {
717
- const shortStr = shortStringInJS(buffer, offset, length);
718
- if (shortStr !== void 0) {
719
- return shortStr;
720
- }
721
- }
722
- return DECODER.decode(buffer.subarray(offset, offset + length));
723
- }
724
- function readConsts(constChunk) {
725
- const reader = new DataView(constChunk.buffer, constChunk.byteOffset, constChunk.byteLength);
726
- const consts = [];
727
- let offset = 0;
728
- const length = reader.byteLength;
729
- while (offset < length) {
730
- const type = reader.getUint8(offset);
731
- switch (type) {
732
- /* c8 ignore next 2 */
733
- case 0:
734
- consts.push(null);
735
- offset += 1;
736
- break;
737
- case 1:
738
- consts.push(true);
739
- offset += 1;
740
- break;
741
- case 2:
742
- consts.push(false);
743
- offset += 1;
744
- break;
745
- case 3: {
746
- const ordinal = reader.getInt32(offset + 1, true);
747
- consts.push(ordinal);
748
- offset += 5;
749
- break;
750
- }
751
- case 4: {
752
- const num = reader.getFloat64(offset + 1, true);
753
- consts.push(num);
754
- offset += 9;
755
- break;
756
- }
757
- case 5: {
758
- const len = reader.getUint32(offset + 1, true);
759
- const str = decodeStr(constChunk, offset + 5, len);
760
- consts.push(str);
761
- offset += 5 + len;
762
- break;
763
- }
764
- /* c8 ignore next 2 */
765
- default:
766
- throw new Error(`Unknown constant type: ${type}`);
767
- }
768
- }
769
- return consts;
770
- }
771
-
772
- // src/compiler/emit/sourcemap.ts
773
- import { SourceMapGenerator } from "source-map-js";
774
- var ORIGIN = `mira://MiraScript/`;
775
- var { SOURCE_URL, SOURCE_MAPPING_URL } = ((source, mapping, url) => {
776
- const prefix = "//# ";
777
- return {
778
- SOURCE_URL: prefix.concat(source, url),
779
- SOURCE_MAPPING_URL: prefix.concat(source, mapping, url)
780
- };
781
- })(`source`, `Mapping`, `URL`);
782
- var SOURCE_OFFSET = 4;
783
- 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)}`;
784
- var sourceId = 1;
785
- function createSourceMap(source, scriptPrefix, sourcemaps, codeLines, functions, options) {
786
- let fileName = (options.fileName ?? "").trim();
787
- const hasSchema = /^\w+:/.test(fileName);
788
- if (!hasSchema) {
789
- if (fileName.startsWith("/")) {
790
- fileName = fileName.replace(/^\/+\s*/, "");
791
- }
792
- if (!fileName) {
793
- fileName = `${sourceId++}.${options.input_mode === "Template" ? "miratpl" : "mira"}`;
794
- }
795
- }
796
- const map = new SourceMapGenerator({
797
- file: fileName + ".js"
798
- });
799
- if (typeof source === "string") {
800
- map.setSourceContent(fileName, source);
801
- }
802
- let hasStartMap = false;
803
- for (let i = 1; i < sourcemaps.length; i++) {
804
- const range = sourcemaps[i];
805
- if (!range) break;
806
- if (!hasStartMap && range.startLineNumber === 1 && range.startColumn === 1) {
807
- hasStartMap = true;
808
- }
809
- map.addMapping({
810
- generated: {
811
- line: i + SOURCE_OFFSET,
812
- column: 0
813
- },
814
- original: {
815
- line: range.startLineNumber,
816
- column: range.startColumn - 1
817
- },
818
- source: fileName
819
- });
820
- }
821
- if (!hasStartMap) {
822
- map.addMapping({
823
- generated: {
824
- line: SOURCE_OFFSET,
825
- column: scriptPrefix.length - "CpEnter();".length
826
- },
827
- original: {
828
- line: 1,
829
- column: 0
830
- },
831
- source: fileName
832
- });
833
- }
834
- if (typeof source === "string") {
835
- const lines = source.split(/\r?\n/);
836
- for (const codeLineNo of functions) {
837
- const mapping = sourcemaps[codeLineNo];
838
- const sourceLine = mapping ? lines[mapping.startLineNumber - 1] : void 0;
839
- const fnName = mapping && sourceLine ? sourceLine.slice(mapping.startColumn - 1, mapping.endColumn - 1) : "";
840
- if (!fnName) continue;
841
- const codeLine = codeLines[codeLineNo];
842
- codeLines[codeLineNo] = codeLine.replace(`= $Fn(null,`, `= $Fn(${toJsLiteral(fnName)},`);
843
- }
844
- }
845
- const sourceURL = hasSchema ? fileName : `${ORIGIN}${fileName}`;
846
- const dataUrl = toDataUrl(map.toString());
847
- codeLines.push(
848
- // Prevent source map from being recognized as of this file
849
- `${SOURCE_URL}=${sourceURL}.js`,
850
- `${SOURCE_MAPPING_URL}=${dataUrl}`
851
- );
852
- }
853
-
854
- // src/compiler/emit/constants.ts
855
- var SCRIPT_PREFIX = `((global) => { global ??= $GlobalFallback(); try { $CpEnter();`;
856
- var SCRIPT_PREFIX_NO_GLOBAL = `(() => { try { $CpEnter();`;
857
-
858
- // src/compiler/emit/index.ts
859
- function emit(source, chunk, sourcemaps, options) {
860
- const gen = new Emitter(source, chunk, sourcemaps, options);
861
- gen.read();
862
- const code = gen.codeLines.join("\n");
863
- return code;
864
- }
865
- function createArray(length, fn) {
866
- const result = [];
867
- for (let i = 0; i < length; i++) {
868
- result.push(fn(i));
869
- }
870
- return result;
871
- }
872
- var Emitter = class {
873
- constructor(source, chunk, sourcemaps, options) {
874
- this.source = source;
875
- this.chunk = chunk;
876
- this.sourcemaps = sourcemaps;
877
- this.options = options;
878
- this.constLits = [];
879
- this.codeOffset = 0;
880
- this.closureCounter = 0;
881
- this.identCounter = 0;
882
- /** 记录函数声明所在位置 */
883
- this.functions = [];
884
- /** 是否使用全局变量 */
885
- this.useGlobal = false;
886
- this.codeLines = [];
887
- this.pretty = options.pretty ?? false;
888
- const reader = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
889
- this.chunkSize = reader.getUint32(0, true);
890
- this.codeSize = reader.getUint32(4, true);
891
- this.constSize = reader.getUint32(8 + this.codeSize, true);
892
- this.codeReader = new DataView(chunk.buffer, chunk.byteOffset + 8, this.codeSize);
893
- this.constVals = readConsts(
894
- new Uint8Array(chunk.buffer, chunk.byteOffset + 12 + this.codeSize, this.constSize)
895
- );
896
- }
897
- /** 读取常量表 */
898
- readConsts() {
899
- const { constVals, constLits } = this;
900
- for (let i = 0, len = constVals.length; i < len; i++) {
901
- constLits.push(toJsLiteral(constVals[i]));
902
- }
903
- }
904
- /** 制造缩进 */
905
- ident(len = 0) {
906
- if (!this.pretty) return "";
907
- return " ".repeat(this.identCounter + len);
908
- }
909
- /** 读取全局变量 */
910
- rg(constIdx) {
911
- const constName = this.constVals[constIdx];
912
- let lit;
913
- if (typeof constName == "string") {
914
- lit = this.constLits[constIdx];
915
- } else {
916
- lit = toJsLiteral(toString(constName, void 0));
917
- }
918
- this.useGlobal = true;
919
- return `global.get(${lit})`;
920
- }
921
- /** Read variable */
922
- rv(i, level = 0) {
923
- if (!i) return "null";
924
- const c = this.closureCounter - level;
925
- return `var_${c}_${i}`;
926
- }
927
- /** Write variable */
928
- wv(i, level = 0) {
929
- if (!i) return "_";
930
- return this.rv(i, level);
931
- }
932
- /** 读取 code param */
933
- readParam(wide) {
934
- const value = wide ? this.codeReader.getUint32(this.codeOffset, true) : this.codeReader.getUint8(this.codeOffset);
935
- this.codeOffset += wide ? 4 : 1;
936
- return value;
937
- }
938
- /** 读取 code param */
939
- readIndex(wide) {
940
- const value = wide ? this.codeReader.getInt32(this.codeOffset, true) : this.codeReader.getInt8(this.codeOffset);
941
- this.codeOffset += wide ? 4 : 1;
942
- return value;
943
- }
944
- /** 读取闭包 */
945
- readClosure() {
946
- this.closureCounter++;
947
- this.identCounter++;
948
- while (this.codeOffset < this.codeSize) {
949
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
950
- const opcode = opcode_raw & 127;
951
- if (opcode !== OpCode.FuncEnd) {
952
- this.readCode();
953
- continue;
954
- }
955
- this.codeOffset++;
956
- const body = this.ident(-1) + `} finally { $CpExit(); } });`;
957
- this.codeLines.push(body);
958
- this.closureCounter--;
959
- this.identCounter--;
960
- break;
961
- }
962
- }
963
- /** 读取块结束 */
964
- readBlockEnd(end) {
965
- while (this.codeOffset < this.codeSize) {
966
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
967
- const opcode = opcode_raw & 127;
968
- if (opcode !== end) {
969
- this.readCode();
970
- continue;
971
- }
972
- this.codeOffset++;
973
- this.identCounter--;
974
- if (end === OpCode.LoopEnd) {
975
- this.closureCounter--;
976
- }
977
- const body = this.ident() + `};`;
978
- this.codeLines.push(body);
979
- break;
980
- }
981
- }
982
- /** 读取 if else 或 if 结束 */
983
- readIfElse() {
984
- this.identCounter++;
985
- while (this.codeOffset < this.codeSize) {
986
- const opcode_raw = this.codeReader.getUint8(this.codeOffset);
987
- const opcode = opcode_raw & 127;
988
- if (opcode === OpCode.IfEnd) {
989
- return this.readBlockEnd(OpCode.IfEnd);
990
- }
991
- if (opcode === OpCode.Else) {
992
- this.codeOffset++;
993
- const body = this.ident(-1) + `} else {`;
994
- this.codeLines.push(body);
995
- break;
996
- }
997
- if (opcode === OpCode.ElIf) {
998
- this.codeOffset++;
999
- const body = this.ident(-1) + `} else `;
1000
- this.codeLines.push(body);
1001
- return this.readCode();
1002
- }
1003
- this.readCode();
1004
- }
1005
- return this.readBlockEnd(OpCode.IfEnd);
1006
- }
1007
- /** 读取 module */
1008
- readModule(obj) {
1009
- this.identCounter++;
1010
- while (this.codeOffset < this.codeSize) {
1011
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1012
- const opcode = opcode_raw & 127;
1013
- const wide = opcode_raw >= 128;
1014
- const read = () => this.readParam(wide);
1015
- let code = "";
1016
- switch (opcode) {
1017
- case OpCode.Field: {
1018
- const field = read();
1019
- const field_name = this.constLits[field];
1020
- if (!field_name) {
1021
- throw new Error(`Unknown field ${field}`);
1022
- }
1023
- const value = read();
1024
- code = `[${field_name}]: () => $Upvalue(${this.rv(value)}),`;
1025
- break;
1026
- }
1027
- case OpCode.Freeze: {
1028
- this.identCounter--;
1029
- code = `});`;
1030
- break;
1031
- }
1032
- default: {
1033
- code = `// ?${OpCode[opcode] ?? opcode}`;
1034
- break;
1035
- }
1036
- }
1037
- const ident = this.ident();
1038
- this.codeLines.push(ident + code);
1039
- if (opcode === OpCode.Freeze) {
1040
- return;
1041
- }
1042
- }
1043
- }
1044
- /** 读取 record */
1045
- readRecord(obj) {
1046
- this.identCounter++;
1047
- while (this.codeOffset < this.codeSize) {
1048
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1049
- const opcode = opcode_raw & 127;
1050
- const wide = opcode_raw >= 128;
1051
- const read = () => this.readParam(wide);
1052
- let code = "";
1053
- switch (opcode) {
1054
- case OpCode.FieldOpt:
1055
- case OpCode.Field: {
1056
- const field = read();
1057
- const field_name = this.constLits[field];
1058
- if (!field_name) {
1059
- throw new Error(`Unknown field ${field}`);
1060
- }
1061
- const value = read();
1062
- const opt = opcode === OpCode.FieldOpt;
1063
- code = opt ? `...$ElOpt(${field_name}, ${this.rv(value)}),` : `[${field_name}]: $El(${this.rv(value)}),`;
1064
- break;
1065
- }
1066
- case OpCode.FieldOptDyn:
1067
- case OpCode.FieldDyn: {
1068
- const field = read();
1069
- const value = read();
1070
- const opt = opcode === OpCode.FieldOptDyn;
1071
- code = opt ? `...$ElOpt(${this.rv(field)}, ${this.rv(value)}),` : `[${this.rv(field)}]: $El(${this.rv(value)}),`;
1072
- break;
1073
- }
1074
- case OpCode.FieldOptIndex:
1075
- case OpCode.FieldIndex: {
1076
- const field = this.readIndex(wide);
1077
- const value = read();
1078
- const opt = opcode === OpCode.FieldOptIndex;
1079
- code = opt ? `...$ElOpt(${field}, ${this.rv(value)}),` : `[${field}]: $El(${this.rv(value)}),`;
1080
- break;
1081
- }
1082
- case OpCode.Spread: {
1083
- const value = read();
1084
- code = `...$RecordSpread(${this.rv(value)}),`;
1085
- break;
1086
- }
1087
- case OpCode.Freeze: {
1088
- this.identCounter--;
1089
- code = `});`;
1090
- break;
1091
- }
1092
- default: {
1093
- code = `// ?${OpCode[opcode] ?? opcode}`;
1094
- break;
1095
- }
1096
- }
1097
- const ident = this.ident();
1098
- this.codeLines.push(ident + code);
1099
- if (opcode === OpCode.Freeze) {
1100
- return;
1101
- }
1102
- }
1103
- }
1104
- /** 读取 array */
1105
- readArray(arr) {
1106
- this.identCounter++;
1107
- while (this.codeOffset < this.codeSize) {
1108
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1109
- const opcode = opcode_raw & 127;
1110
- const wide = opcode_raw >= 128;
1111
- const read = () => this.readParam(wide);
1112
- let code = "";
1113
- switch (opcode) {
1114
- case OpCode.Item: {
1115
- const value = read();
1116
- code = `$El(${this.rv(value)}),`;
1117
- break;
1118
- }
1119
- case OpCode.ItemRange: {
1120
- const start = this.readIndex(wide);
1121
- const end = this.readIndex(wide);
1122
- code = `...$ArrayRange(${start}, ${end}),`;
1123
- break;
1124
- }
1125
- case OpCode.ItemRangeDyn: {
1126
- const start = read();
1127
- const end = read();
1128
- code = `...$ArrayRange(${this.rv(start)}, ${this.rv(end)}),`;
1129
- break;
1130
- }
1131
- case OpCode.ItemRangeExclusiveDyn: {
1132
- const start = read();
1133
- const end = read();
1134
- code = `...$ArrayRangeExclusive(${this.rv(start)}, ${this.rv(end)}),`;
1135
- break;
1136
- }
1137
- case OpCode.Spread: {
1138
- const value = read();
1139
- code = `...$ArraySpread(${this.rv(value)}),`;
1140
- break;
1141
- }
1142
- case OpCode.Freeze: {
1143
- this.identCounter--;
1144
- code = `]);`;
1145
- break;
1146
- }
1147
- default: {
1148
- code = `// ?${OpCode[opcode] ?? opcode}`;
1149
- break;
1150
- }
1151
- }
1152
- const ident = this.ident();
1153
- this.codeLines.push(ident + code);
1154
- if (opcode === OpCode.Freeze) {
1155
- return;
1156
- }
1157
- }
1158
- }
1159
- /** 读取代码 */
1160
- readCode() {
1161
- const opcode_raw = this.codeReader.getUint8(this.codeOffset++);
1162
- const opcode = opcode_raw & 127;
1163
- const wide = opcode_raw >= 128;
1164
- const read = () => this.readParam(wide);
1165
- const readIndex = () => this.readIndex(wide);
1166
- const ident = this.ident();
1167
- let code = "";
1168
- let reg = 0;
1169
- switch (opcode) {
1170
- case OpCode.FuncVarg:
1171
- case OpCode.Func: {
1172
- const startFunc = this.codeOffset === 1;
1173
- reg = read();
1174
- const varg = opcode === OpCode.FuncVarg;
1175
- const argn = read();
1176
- const regn = read();
1177
- const args = createArray(argn, (i) => {
1178
- const wv = this.wv(i + 1, -1);
1179
- if (varg && i === argn - 1) {
1180
- return `...args`;
1181
- }
1182
- return `${wv} = null`;
1183
- });
1184
- let regs = "_";
1185
- for (let i = 1 + argn; i < regn + 1; i++) {
1186
- regs += "," + this.wv(i, -1);
1187
- }
1188
- const script = startFunc && !varg && argn === 0;
1189
- if (script) {
1190
- code = `var ${regs};`;
1191
- } else {
1192
- if (this.options.sourceMap) {
1193
- this.functions.push(this.codeLines.length);
1194
- }
1195
- code = `${this.wv(reg)} = $Fn(null, (${args.join(", ")}) => { try { $CpEnter(); var ${regs};`;
1196
- }
1197
- if (varg) {
1198
- code += ` var ${this.wv(argn, -1)} = $VArgs(args);`;
1199
- }
1200
- break;
1201
- }
1202
- case OpCode.Constant: {
1203
- reg = read();
1204
- const i = read();
1205
- const c = this.constLits[i];
1206
- code = `${this.wv(reg)} = ${c};`;
1207
- break;
1208
- }
1209
- case OpCode.Uninit: {
1210
- reg = read();
1211
- code = `${this.wv(reg)} = undefined;`;
1212
- break;
1213
- }
1214
- case OpCode.Return: {
1215
- reg = read();
1216
- code = `return ${this.rv(reg)};`;
1217
- break;
1218
- }
1219
- case OpCode.Add:
1220
- case OpCode.Sub:
1221
- case OpCode.Mul:
1222
- case OpCode.Div:
1223
- case OpCode.Mod:
1224
- case OpCode.Pow:
1225
- case OpCode.Gt:
1226
- case OpCode.Gte:
1227
- case OpCode.Lt:
1228
- case OpCode.Lte:
1229
- case OpCode.Eq:
1230
- case OpCode.Neq:
1231
- case OpCode.Aeq:
1232
- case OpCode.Naeq:
1233
- case OpCode.Same:
1234
- case OpCode.Nsame:
1235
- case OpCode.In:
1236
- case OpCode.And:
1237
- case OpCode.Or: {
1238
- reg = read();
1239
- const left = read();
1240
- const right = read();
1241
- code = `${this.wv(reg)} = $${OpCode[opcode]}(${this.rv(left)}, ${this.rv(right)});`;
1242
- break;
1243
- }
1244
- case OpCode.Format: {
1245
- reg = read();
1246
- const left = read();
1247
- const right = readIndex();
1248
- code = `${this.wv(reg)} = $${OpCode[opcode]}(${this.rv(left)}, ${this.constLits[right]});`;
1249
- break;
1250
- }
1251
- case OpCode.InGlobal: {
1252
- reg = read();
1253
- const left = read();
1254
- this.useGlobal = true;
1255
- code = `${this.wv(reg)} = global.has($ToString(${this.rv(left)}));`;
1256
- break;
1257
- }
1258
- case OpCode.Concat: {
1259
- reg = read();
1260
- const n = read();
1261
- const args = createArray(n, () => read());
1262
- code = `${this.wv(reg)} = $${OpCode[opcode]}(${args.map((a) => this.rv(a)).join(", ")});`;
1263
- break;
1264
- }
1265
- case OpCode.Omit:
1266
- case OpCode.Pick: {
1267
- reg = read();
1268
- const value = read();
1269
- const n = read();
1270
- const args = createArray(n, () => this.constLits[read()]);
1271
- code = `${this.wv(reg)} = $${OpCode[opcode]}(${this.rv(value)}, [${args.join(", ")}]);`;
1272
- break;
1273
- }
1274
- case OpCode.Call:
1275
- case OpCode.CallDyn: {
1276
- reg = read();
1277
- const func = read();
1278
- const n = read();
1279
- const args = createArray(n, () => read());
1280
- const ns = read();
1281
- const spreads = createArray(ns, () => read());
1282
- const callTarget = opcode === OpCode.Call ? this.rg(func) : this.rv(func);
1283
- code = `${this.wv(reg)} = $Call(${callTarget}, [${args.map((a, i) => {
1284
- if (spreads.includes(i)) return `...$ArraySpread(${this.rv(a)})`;
1285
- else return this.rv(a);
1286
- }).join(", ")}]);`;
1287
- break;
1288
- }
1289
- case OpCode.Assign: {
1290
- reg = read();
1291
- const value = read();
1292
- code = `${this.wv(reg)} = ${this.rv(value)};`;
1293
- break;
1294
- }
1295
- case OpCode.Pos:
1296
- case OpCode.Neg:
1297
- case OpCode.Not:
1298
- case OpCode.Type:
1299
- case OpCode.ToBoolean:
1300
- case OpCode.ToNumber:
1301
- case OpCode.ToString:
1302
- case OpCode.IsBoolean:
1303
- case OpCode.IsNumber:
1304
- case OpCode.IsString:
1305
- case OpCode.IsRecord:
1306
- case OpCode.IsArray:
1307
- case OpCode.Length: {
1308
- reg = read();
1309
- const value = read();
1310
- code = `${this.wv(reg)} = $${OpCode[opcode]}(${this.rv(value)});`;
1311
- break;
1312
- }
1313
- case OpCode.AssertInit:
1314
- case OpCode.AssertNonNil: {
1315
- reg = read();
1316
- code = `$${OpCode[opcode]}(${this.rv(reg)})`;
1317
- break;
1318
- }
1319
- case OpCode.Get: {
1320
- reg = read();
1321
- const obj = read();
1322
- const prop = this.constLits[read()];
1323
- code = `${this.wv(reg)} = $Get(${this.rv(obj)}, ${prop});`;
1324
- break;
1325
- }
1326
- case OpCode.GetIndex: {
1327
- reg = read();
1328
- const obj = read();
1329
- const index = readIndex();
1330
- code = `${this.wv(reg)} = $Get(${this.rv(obj)}, ${index});`;
1331
- break;
1332
- }
1333
- case OpCode.GetDyn: {
1334
- reg = read();
1335
- const obj = read();
1336
- const index = read();
1337
- code = `${this.wv(reg)} = $Get(${this.rv(obj)}, ${this.rv(index)});`;
1338
- break;
1339
- }
1340
- case OpCode.Has: {
1341
- reg = read();
1342
- const obj = read();
1343
- const prop = this.constLits[read()];
1344
- code = `${this.wv(reg)} = $Has(${this.rv(obj)}, ${prop});`;
1345
- break;
1346
- }
1347
- case OpCode.HasIndex: {
1348
- reg = read();
1349
- const obj = read();
1350
- const index = readIndex();
1351
- code = `${this.wv(reg)} = $Has(${this.rv(obj)}, ${index});`;
1352
- break;
1353
- }
1354
- case OpCode.HasDyn: {
1355
- reg = read();
1356
- const obj = read();
1357
- const index = read();
1358
- code = `${this.wv(reg)} = $Has(${this.rv(obj)}, ${this.rv(index)});`;
1359
- break;
1360
- }
1361
- case OpCode.Set: {
1362
- reg = read();
1363
- const obj = read();
1364
- const prop = this.constLits[read()];
1365
- code = `$Set(${this.rv(obj)}, ${prop}, ${this.rv(reg)});`;
1366
- break;
1367
- }
1368
- case OpCode.SetIndex: {
1369
- reg = read();
1370
- const obj = read();
1371
- const index = readIndex();
1372
- code = `$Set(${this.rv(obj)}, ${index}, ${this.rv(reg)});`;
1373
- break;
1374
- }
1375
- case OpCode.SetDyn: {
1376
- reg = read();
1377
- const obj = read();
1378
- const index = read();
1379
- code = `$Set(${this.rv(obj)}, ${this.rv(index)}, ${this.rv(reg)});`;
1380
- break;
1381
- }
1382
- case OpCode.GetGlobal: {
1383
- reg = read();
1384
- const i = read();
1385
- code = `${this.wv(reg)} = ${this.rg(i)};`;
1386
- break;
1387
- }
1388
- case OpCode.GetGlobalDyn: {
1389
- reg = read();
1390
- const name = read();
1391
- this.useGlobal = true;
1392
- code = `${this.wv(reg)} = global.get($ToString(${this.rv(name)}));`;
1393
- break;
1394
- }
1395
- case OpCode.GetUpvalue: {
1396
- reg = read();
1397
- const level = read();
1398
- const up = read();
1399
- code = `${this.wv(reg)} = $Upvalue(${this.rv(up, level)});`;
1400
- break;
1401
- }
1402
- case OpCode.SetUpvalue: {
1403
- reg = read();
1404
- const level = read();
1405
- const up = read();
1406
- code = `${this.wv(up, level)} = ${this.rv(reg)};`;
1407
- break;
1408
- }
1409
- case OpCode.Slice: {
1410
- reg = read();
1411
- const obj = read();
1412
- const start = readIndex();
1413
- const end = readIndex();
1414
- code = `${this.wv(reg)} = $Slice(${this.rv(obj)}, ${start}, ${end});`;
1415
- break;
1416
- }
1417
- case OpCode.SliceStart: {
1418
- reg = read();
1419
- const obj = read();
1420
- const end = readIndex();
1421
- code = `${this.wv(reg)} = $Slice(${this.rv(obj)}, null, ${end});`;
1422
- break;
1423
- }
1424
- case OpCode.SliceEnd: {
1425
- reg = read();
1426
- const obj = read();
1427
- const start = readIndex();
1428
- code = `${this.wv(reg)} = $Slice(${this.rv(obj)}, ${start}, null);`;
1429
- break;
1430
- }
1431
- case OpCode.SliceDyn: {
1432
- reg = read();
1433
- const obj = read();
1434
- const start = read();
1435
- const end = read();
1436
- code = `${this.wv(reg)} = $Slice(${this.rv(obj)}, ${this.rv(start)}, ${this.rv(end)});`;
1437
- break;
1438
- }
1439
- case OpCode.SliceExclusiveDyn: {
1440
- reg = read();
1441
- const obj = read();
1442
- const start = read();
1443
- const end = read();
1444
- code = `${this.wv(reg)} = $SliceExclusive(${this.rv(obj)}, ${this.rv(start)}, ${this.rv(end)});`;
1445
- break;
1446
- }
1447
- case OpCode.Module: {
1448
- reg = read();
1449
- const nameIdx = read();
1450
- const name = this.constLits[nameIdx];
1451
- code = `${this.wv(reg)} = $Module(${name}, {`;
1452
- break;
1453
- }
1454
- case OpCode.Record: {
1455
- reg = read();
1456
- code = `${this.wv(reg)} = ({`;
1457
- break;
1458
- }
1459
- case OpCode.Array: {
1460
- reg = read();
1461
- code = `${this.wv(reg)} = ([`;
1462
- break;
1463
- }
1464
- case OpCode.If: {
1465
- const cond = read();
1466
- code = `if ($ToBoolean(${this.rv(cond)})) {`;
1467
- break;
1468
- }
1469
- case OpCode.IfNot: {
1470
- const cond = read();
1471
- code = `if (!$ToBoolean(${this.rv(cond)})) {`;
1472
- break;
1473
- }
1474
- case OpCode.IfInit: {
1475
- const cond = read();
1476
- code = `if (${this.rv(cond)} !== undefined) {`;
1477
- break;
1478
- }
1479
- case OpCode.IfNotInit: {
1480
- const cond = read();
1481
- code = `if (${this.rv(cond)} === undefined) {`;
1482
- break;
1483
- }
1484
- case OpCode.IfNil: {
1485
- const cond = read();
1486
- code = `if (${this.rv(cond)} === null) {`;
1487
- break;
1488
- }
1489
- case OpCode.IfNotNil: {
1490
- const cond = read();
1491
- code = `if (${this.rv(cond)} !== null) {`;
1492
- break;
1493
- }
1494
- case OpCode.LoopFor: {
1495
- const nreg = read();
1496
- const iterable = read();
1497
- const regs = createArray(nreg - 1, (i) => this.wv(i + 2, -1));
1498
- regs.unshift("_");
1499
- const ir = this.wv(1, -1);
1500
- code = `for (let ${ir} of $Iterable(${this.rv(iterable)})) { ${ir} ??= null; $Cp(); let ${regs.join(", ")};`;
1501
- break;
1502
- }
1503
- case OpCode.LoopRange:
1504
- case OpCode.LoopRangeExclusive: {
1505
- const nreg = read();
1506
- const start = read();
1507
- const end = read();
1508
- const exclusive = opcode === OpCode.LoopRangeExclusive;
1509
- const regs = createArray(nreg - 1, (i2) => this.wv(i2 + 2, -1));
1510
- regs.unshift("_");
1511
- const i = this.wv(1, -1);
1512
- code = `for (let start = $ToNumber(${this.rv(start)}), end = $ToNumber(${this.rv(end)}), ${i} = start; ${i} ${exclusive ? "<" : "<="} end; ${i} += 1) { $Cp(); let ${regs.join(", ")};`;
1513
- break;
1514
- }
1515
- case OpCode.Loop: {
1516
- const nreg = read();
1517
- const regs = createArray(nreg, (i) => this.wv(i + 1, -1));
1518
- regs.unshift("_");
1519
- code = `while (true) { $Cp(); let ${regs.join(", ")};`;
1520
- break;
1521
- }
1522
- case OpCode.Break: {
1523
- code = `break;`;
1524
- break;
1525
- }
1526
- case OpCode.Continue: {
1527
- code = `continue;`;
1528
- break;
1529
- }
1530
- case OpCode.Noop: {
1531
- return;
1532
- }
1533
- default: {
1534
- code = `; // ${OpCode[opcode] ?? opcode}`;
1535
- break;
1536
- }
1537
- }
1538
- this.codeLines.push(ident + code);
1539
- switch (opcode) {
1540
- case OpCode.FuncVarg:
1541
- case OpCode.Func: {
1542
- this.readClosure();
1543
- break;
1544
- }
1545
- case OpCode.If:
1546
- case OpCode.IfNot:
1547
- case OpCode.IfNil:
1548
- case OpCode.IfNotNil:
1549
- case OpCode.IfInit:
1550
- case OpCode.IfNotInit: {
1551
- this.readIfElse();
1552
- break;
1553
- }
1554
- case OpCode.Loop:
1555
- case OpCode.LoopFor:
1556
- case OpCode.LoopRange:
1557
- case OpCode.LoopRangeExclusive: {
1558
- this.identCounter++;
1559
- this.closureCounter++;
1560
- this.readBlockEnd(OpCode.LoopEnd);
1561
- break;
1562
- }
1563
- case OpCode.Module: {
1564
- this.readModule(reg);
1565
- break;
1566
- }
1567
- case OpCode.Record: {
1568
- this.readRecord(reg);
1569
- break;
1570
- }
1571
- case OpCode.Array: {
1572
- this.readArray(reg);
1573
- break;
1574
- }
1575
- }
1576
- }
1577
- /** 读取 chunk */
1578
- read() {
1579
- this.readConsts();
1580
- this.readCode();
1581
- const prefix = this.useGlobal ? SCRIPT_PREFIX : SCRIPT_PREFIX_NO_GLOBAL;
1582
- this.codeLines[0] = `${prefix} ${this.codeLines[0]}`;
1583
- this.addSourceMap(prefix);
1584
- }
1585
- /** 添加源映射 */
1586
- addSourceMap(prefix) {
1587
- if (this.options.sourceMap) {
1588
- createSourceMap(this.source, prefix, this.sourcemaps, this.codeLines, this.functions, this.options);
1589
- }
1590
- }
1591
- };
1592
-
1593
- // src/compiler/diagnostic.ts
1594
- import { DIAGNOSTIC_MESSAGES, DiagnosticCode } from "@mirascript/constants";
1595
- function getDiagnosticMessage(code) {
1596
- if (code < 0 || code >= 65535 || !isSafeInteger(code)) {
1597
- throw new RangeError(`Invalid DiagnosticCode: ${code}`);
1598
- }
1599
- return DIAGNOSTIC_MESSAGES[code] ?? null;
1600
- }
1601
- function formatDiagnosticMessage(code, $0) {
1602
- let template = getDiagnosticMessage(code);
1603
- if (!template) {
1604
- return `Unknown diagnostic code: ${code}`;
1605
- }
1606
- if (template.includes(`$0`)) {
1607
- const replacement = (typeof $0 == "function" ? $0() : $0) ?? "";
1608
- template = template.replaceAll(`$0`, replacement.replaceAll("$", "$$$$"));
1609
- }
1610
- return template;
1611
- }
1612
- function getDiagnosticSeverity(code) {
1613
- if (code > DiagnosticCode.ErrorStart && code < DiagnosticCode.ErrorEnd) {
1614
- return "error";
1615
- } else if (code > DiagnosticCode.WarningStart && code < DiagnosticCode.WarningEnd) {
1616
- return "warning";
1617
- } else if (code > DiagnosticCode.InfoStart && code < DiagnosticCode.InfoEnd) {
1618
- return "info";
1619
- } else if (code > DiagnosticCode.HintStart && code < DiagnosticCode.HintEnd) {
1620
- return "hint";
1621
- } else if (code > DiagnosticCode.TagStart && code < DiagnosticCode.TagEnd) {
1622
- return "tag";
1623
- } else {
1624
- return void 0;
1625
- }
1626
- }
1627
- var { ReferenceStart, ReferenceEnd, TagRefStart, TagRefEnd, SourceMap } = DiagnosticCode;
1628
- function parseDiagnostics(source, diagnostics, filter) {
1629
- const parsed = [];
1630
- const bufLen = diagnostics.length;
1631
- for (let i = 0; i < bufLen; i += 5) {
1632
- const code = diagnostics[i + 4];
1633
- if (filter && !filter(code)) {
1634
- continue;
1635
- }
1636
- const startLineNumber = diagnostics[i];
1637
- const startColumn = diagnostics[i + 1];
1638
- const endLineNumber = diagnostics[i + 2];
1639
- const endColumn = diagnostics[i + 3];
1640
- parsed.push({
1641
- code,
1642
- range: {
1643
- startLineNumber,
1644
- startColumn,
1645
- endLineNumber,
1646
- endColumn
1647
- }
1648
- });
1649
- }
1650
- const _errors = [];
1651
- const _warnings = [];
1652
- const _infos = [];
1653
- const _hints = [];
1654
- const _tags = [];
1655
- const _references = [];
1656
- const _tagsReferences = [];
1657
- const _sourcemaps = [];
1658
- for (let i = 0; i < parsed.length; i++) {
1659
- const diagnostic = parsed[i];
1660
- const { code } = diagnostic;
1661
- if (code === SourceMap) {
1662
- _sourcemaps.push(diagnostic.range);
1663
- continue;
1664
- }
1665
- switch (getDiagnosticSeverity(code)) {
1666
- case "error":
1667
- _errors.push(diagnostic);
1668
- break;
1669
- case "warning":
1670
- _warnings.push(diagnostic);
1671
- break;
1672
- case "info":
1673
- _infos.push(diagnostic);
1674
- break;
1675
- case "hint":
1676
- _hints.push(diagnostic);
1677
- break;
1678
- case "tag":
1679
- _tags.push(diagnostic);
1680
- break;
1681
- case void 0:
1682
- default:
1683
- continue;
1684
- }
1685
- diagnostic.references = [];
1686
- while (i + 1 < parsed.length) {
1687
- const ref = parsed[i + 1];
1688
- let isRef = false;
1689
- if (ref.code > TagRefStart && ref.code < TagRefEnd) {
1690
- isRef = true;
1691
- _tagsReferences.push(ref);
1692
- }
1693
- if (ref.code > ReferenceStart && ref.code < ReferenceEnd) {
1694
- isRef = true;
1695
- _references.push(ref);
1696
- }
1697
- if (!isRef) {
1698
- break;
1699
- }
1700
- i++;
1701
- ref.diagnostic = diagnostic;
1702
- diagnostic.references.push(ref);
1703
- }
1704
- }
1705
- return {
1706
- errors: _errors,
1707
- warnings: _warnings,
1708
- infos: _infos,
1709
- hints: _hints,
1710
- tags: _tags,
1711
- references: _references,
1712
- tagsReferences: _tagsReferences,
1713
- sourcemaps: _sourcemaps
1714
- };
1715
- }
1716
- function formatRange(range) {
1717
- if (range.startLineNumber === range.endLineNumber) {
1718
- if (range.startColumn === range.endColumn) {
1719
- return `${range.startLineNumber}:${range.startColumn}`;
1720
- }
1721
- return `${range.startLineNumber}:${range.startColumn}-${range.endColumn}`;
1722
- }
1723
- return `${range.startLineNumber}:${range.startColumn}-${range.endLineNumber}:${range.endColumn}`;
1724
- }
1725
- function formatDiagnostics(diagnostics, source, fileName) {
1726
- const rangePrefix = fileName ? `${fileName}:` : "";
1727
- const messages = [];
1728
- for (const diagnostic of diagnostics) {
1729
- const range = formatRange(diagnostic.range);
1730
- const codeName = DiagnosticCode[diagnostic.code] || `Unknown(${diagnostic.code})`;
1731
- let message = getDiagnosticMessage(diagnostic.code);
1732
- for (const ref of diagnostic.references) {
1733
- const refRange = formatRange(ref.range);
1734
- message += `
1735
- (${refRange}): ${getDiagnosticMessage(ref.code)}`;
1736
- }
1737
- messages.push(` ${codeName}(${rangePrefix}${range}): ${message}`);
1738
- }
1739
- return messages;
1740
- }
1741
-
1742
- export {
1743
- __export,
1744
- REG_IDENTIFIER,
1745
- VM_ARRAY_MAX_LENGTH,
1746
- kVmScript,
1747
- kVmFunction,
1748
- kVmFunctionProxy,
1749
- kVmContext,
1750
- kVmExtern,
1751
- kVmModule,
1752
- kVmWrapper,
1753
- VM_SCRIPT_NAME,
1754
- VM_FUNCTION_ANONYMOUS_NAME,
1755
- constants_exports,
1756
- isArray,
1757
- isFinite,
1758
- isNaN,
1759
- isInteger,
1760
- isSafeInteger,
1761
- NotNumber,
1762
- PositiveInfinity,
1763
- NegativeInfinity,
1764
- hasOwn,
1765
- keys,
1766
- values,
1767
- entries,
1768
- create,
1769
- fromEntries,
1770
- defineProperty,
1771
- getOwnPropertyNames,
1772
- apply,
1773
- getPrototypeOf,
1774
- hasOwnEnumerable,
1775
- isVmScript,
1776
- isVmContext,
1777
- isVmFunction,
1778
- getVmFunctionInfo,
1779
- isVmWrapper,
1780
- isVmModule,
1781
- isVmExtern,
1782
- isVmCallable,
1783
- isVmPrimitive,
1784
- isVmArray,
1785
- isVmRecord,
1786
- isVmArrayLikeRecordByEntires,
1787
- isVmArrayLikeRecordByKeys,
1788
- isVmArrayLikeRecord,
1789
- isVmConst,
1790
- isVmImmutable,
1791
- isVmAny,
1792
- isVmValue,
1793
- serializeString,
1794
- serializeRecordKey,
1795
- serializeNil,
1796
- serializeBoolean,
1797
- serializeNumber,
1798
- serializeArray,
1799
- serializeRecord,
1800
- serialize,
1801
- display,
1802
- VmError,
1803
- innerToString,
1804
- toString,
1805
- generateBytecodeSync,
1806
- generateBytecode,
1807
- emit,
1808
- DiagnosticCode,
1809
- getDiagnosticMessage,
1810
- formatDiagnosticMessage,
1811
- getDiagnosticSeverity,
1812
- parseDiagnostics,
1813
- formatDiagnostics
1814
- };
1815
- //# sourceMappingURL=chunk-VRGQ23AK.js.map