@mirascript/mirascript 0.1.86 → 0.1.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-LSPCAN32.js → chunk-HHKY7Y7L.js} +125 -756
- package/dist/chunk-HHKY7Y7L.js.map +6 -0
- package/dist/{chunk-A425FGG3.js → chunk-Y7S6AHE7.js} +689 -32
- package/dist/chunk-Y7S6AHE7.js.map +6 -0
- package/dist/compiler/{emit → bytecode}/consts.d.ts +0 -2
- package/dist/compiler/bytecode/consts.d.ts.map +1 -0
- package/dist/compiler/bytecode/index.d.ts +28 -0
- package/dist/compiler/bytecode/index.d.ts.map +1 -0
- package/dist/compiler/emit/index.d.ts +2 -13
- package/dist/compiler/emit/index.d.ts.map +1 -1
- package/dist/compiler/emit/utils.d.ts +6 -0
- package/dist/compiler/emit/utils.d.ts.map +1 -0
- package/dist/compiler/generate-bytecode.d.ts +1 -1
- package/dist/compiler/generate-bytecode.d.ts.map +1 -1
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/options.d.ts +5 -0
- package/dist/compiler/options.d.ts.map +1 -0
- package/dist/compiler/verbose.d.ts +11 -0
- package/dist/compiler/verbose.d.ts.map +1 -0
- package/dist/compiler/worker.d.ts +1 -1
- package/dist/compiler/worker.js +3 -3
- package/dist/compiler/worker.js.map +1 -1
- package/dist/index.js +17 -51
- package/dist/index.js.map +2 -2
- package/dist/subtle.d.ts +3 -1
- package/dist/subtle.d.ts.map +1 -1
- package/dist/subtle.js +18 -14
- package/dist/subtle.js.map +1 -1
- package/dist/vm/lib/index.d.ts +9 -1
- package/dist/vm/lib/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/compiler/{emit → bytecode}/consts.ts +0 -16
- package/src/compiler/bytecode/index.ts +58 -0
- package/src/compiler/emit/index.ts +19 -72
- package/src/compiler/emit/sourcemap.ts +1 -1
- package/src/compiler/emit/utils.ts +28 -0
- package/src/compiler/generate-bytecode.ts +3 -3
- package/src/compiler/index.ts +9 -29
- package/src/compiler/options.ts +16 -0
- package/src/compiler/verbose.ts +32 -0
- package/src/compiler/worker.ts +2 -2
- package/src/subtle.ts +3 -1
- package/src/vm/lib/index.ts +10 -1
- package/dist/chunk-A425FGG3.js.map +0 -6
- package/dist/chunk-LSPCAN32.js.map +0 -6
- package/dist/compiler/emit/consts.d.ts.map +0 -1
|
@@ -1,60 +1,35 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DiagnosticCode,
|
|
3
|
+
NegativeInfinity,
|
|
3
4
|
NotNumber,
|
|
4
5
|
PositiveInfinity,
|
|
5
|
-
VM_ARRAY_MAX_LENGTH,
|
|
6
|
-
VM_FUNCTION_ANONYMOUS_NAME,
|
|
7
|
-
VM_SCRIPT_NAME,
|
|
8
|
-
VmError,
|
|
9
6
|
__export,
|
|
7
|
+
__reExport,
|
|
10
8
|
apply,
|
|
11
|
-
constants_exports,
|
|
12
9
|
create,
|
|
13
10
|
defineProperty,
|
|
14
|
-
display,
|
|
15
11
|
emit,
|
|
16
12
|
entries,
|
|
17
13
|
formatDiagnostics,
|
|
18
14
|
freeze,
|
|
19
15
|
fromEntries,
|
|
16
|
+
generateBytecode,
|
|
20
17
|
getOwnPropertyNames,
|
|
21
18
|
getPrototypeOf,
|
|
22
19
|
getRecord,
|
|
23
|
-
getVmType,
|
|
24
20
|
hasOwn,
|
|
25
21
|
hasOwnEnumerable,
|
|
26
|
-
innerToString,
|
|
27
22
|
isArray,
|
|
28
23
|
isFinite,
|
|
29
24
|
isInteger,
|
|
30
25
|
isNaN,
|
|
31
26
|
isSafeInteger,
|
|
32
|
-
isVmArray,
|
|
33
|
-
isVmCallable,
|
|
34
|
-
isVmConst,
|
|
35
|
-
isVmExtern,
|
|
36
|
-
isVmFunction,
|
|
37
|
-
isVmModule,
|
|
38
|
-
isVmPrimitive,
|
|
39
|
-
isVmRecord,
|
|
40
|
-
isVmWrapper,
|
|
41
|
-
kVmContext,
|
|
42
|
-
kVmExtern,
|
|
43
|
-
kVmFunction,
|
|
44
|
-
kVmFunctionProxy,
|
|
45
|
-
kVmModule,
|
|
46
|
-
kVmScript,
|
|
47
|
-
kVmWrapper,
|
|
48
27
|
keys,
|
|
49
28
|
keysLength,
|
|
50
29
|
parseDiagnostics,
|
|
51
30
|
setRecord,
|
|
52
|
-
toBoolean,
|
|
53
|
-
toFormat,
|
|
54
|
-
toNumber,
|
|
55
|
-
toString,
|
|
56
31
|
values
|
|
57
|
-
} from "./chunk-
|
|
32
|
+
} from "./chunk-HHKY7Y7L.js";
|
|
58
33
|
|
|
59
34
|
// src/compiler/load-module.ts
|
|
60
35
|
import { loadModule } from "@mirascript/bindings";
|
|
@@ -63,6 +38,605 @@ try {
|
|
|
63
38
|
} catch (error) {
|
|
64
39
|
}
|
|
65
40
|
|
|
41
|
+
// src/helpers/constants.ts
|
|
42
|
+
var constants_exports = {};
|
|
43
|
+
__export(constants_exports, {
|
|
44
|
+
VM_ARRAY_MAX_LENGTH: () => VM_ARRAY_MAX_LENGTH,
|
|
45
|
+
VM_FUNCTION_ANONYMOUS_NAME: () => VM_FUNCTION_ANONYMOUS_NAME,
|
|
46
|
+
VM_SCRIPT_NAME: () => VM_SCRIPT_NAME,
|
|
47
|
+
kVmContext: () => kVmContext,
|
|
48
|
+
kVmExtern: () => kVmExtern,
|
|
49
|
+
kVmFunction: () => kVmFunction,
|
|
50
|
+
kVmFunctionProxy: () => kVmFunctionProxy,
|
|
51
|
+
kVmModule: () => kVmModule,
|
|
52
|
+
kVmScript: () => kVmScript,
|
|
53
|
+
kVmWrapper: () => kVmWrapper
|
|
54
|
+
});
|
|
55
|
+
__reExport(constants_exports, constants_star);
|
|
56
|
+
import * as constants_star from "@mirascript/constants";
|
|
57
|
+
var VM_ARRAY_MAX_LENGTH = 16777216;
|
|
58
|
+
var kVmScript = /* @__PURE__ */ Symbol.for("mirascript.vm.script");
|
|
59
|
+
var kVmFunction = /* @__PURE__ */ Symbol.for("mirascript.vm.function");
|
|
60
|
+
var kVmFunctionProxy = /* @__PURE__ */ Symbol.for("mirascript.vm.function.proxy");
|
|
61
|
+
var kVmContext = /* @__PURE__ */ Symbol.for("mirascript.vm.context");
|
|
62
|
+
var kVmExtern = /* @__PURE__ */ Symbol.for("mirascript.vm.extern");
|
|
63
|
+
var kVmModule = /* @__PURE__ */ Symbol.for("mirascript.vm.module");
|
|
64
|
+
var kVmWrapper = /* @__PURE__ */ Symbol.for("mirascript.vm.wrapper");
|
|
65
|
+
var VM_SCRIPT_NAME = `<script_root>`;
|
|
66
|
+
var VM_FUNCTION_ANONYMOUS_NAME = `<anonymous>`;
|
|
67
|
+
|
|
68
|
+
// src/helpers/types/basic.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) || isVmArray(value)) return false;
|
|
110
|
+
value;
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/helpers/types/proto.ts
|
|
115
|
+
function isPlainArray(value) {
|
|
116
|
+
const proto = getPrototypeOf(value);
|
|
117
|
+
return isArray(proto) && hasOwn(proto, "push");
|
|
118
|
+
}
|
|
119
|
+
function isPlainObject(value) {
|
|
120
|
+
const proto1 = getPrototypeOf(value);
|
|
121
|
+
if (proto1 == null) return true;
|
|
122
|
+
const proto2 = getPrototypeOf(proto1);
|
|
123
|
+
if (proto2 != null) return false;
|
|
124
|
+
return hasOwn(proto1, "hasOwnProperty");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/helpers/types/const.ts
|
|
128
|
+
var MAX_DEPTH = 16;
|
|
129
|
+
function isVmArrayDeep(value, depth) {
|
|
130
|
+
if (!isPlainArray(value)) return false;
|
|
131
|
+
if (depth <= 0) return true;
|
|
132
|
+
const inner_depth = depth - 1;
|
|
133
|
+
return value.every((item) => isVmConstInner(item, inner_depth));
|
|
134
|
+
}
|
|
135
|
+
function isVmRecordDeep(value, depth) {
|
|
136
|
+
if (!isPlainObject(value)) return false;
|
|
137
|
+
if (depth <= 0) return true;
|
|
138
|
+
const inner_depth = depth - 1;
|
|
139
|
+
return values(value).every((value2) => isVmConstInner(value2, inner_depth));
|
|
140
|
+
}
|
|
141
|
+
function isVmConstInner(value, depth) {
|
|
142
|
+
if (value == null) return true;
|
|
143
|
+
if (typeof value == "string" || typeof value == "number" || typeof value == "boolean") return true;
|
|
144
|
+
if (typeof value == "object") {
|
|
145
|
+
if (isVmWrapper(value)) return false;
|
|
146
|
+
if (isArray(value)) {
|
|
147
|
+
return isVmArrayDeep(value, depth);
|
|
148
|
+
} else {
|
|
149
|
+
return isVmRecordDeep(value, depth);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
function isVmConst(value, checkDeep = false) {
|
|
155
|
+
if (value === void 0) return false;
|
|
156
|
+
return isVmConstInner(value, checkDeep ? MAX_DEPTH : 0);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/helpers/types/get-type.ts
|
|
160
|
+
function getVmType(value) {
|
|
161
|
+
if (value === void 0) return "uninitialized";
|
|
162
|
+
if (value === null) return "nil";
|
|
163
|
+
if (isVmWrapper(value)) return value.type;
|
|
164
|
+
if (isVmArray(value)) return "array";
|
|
165
|
+
if (typeof value == "object") return "record";
|
|
166
|
+
return typeof value;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/helpers/types/array-like.ts
|
|
170
|
+
function isVmArrayLikeRecordByEntries(entries3) {
|
|
171
|
+
const { length } = entries3;
|
|
172
|
+
if (length === 0) return true;
|
|
173
|
+
if (length > VM_ARRAY_MAX_LENGTH) return false;
|
|
174
|
+
const firstKey = entries3[0][0];
|
|
175
|
+
if (firstKey !== "0") return false;
|
|
176
|
+
const lastKey = entries3[length - 1][0];
|
|
177
|
+
if (lastKey !== String(length - 1)) return false;
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
function isVmArrayLikeRecordByKeys(keys4) {
|
|
181
|
+
const { length } = keys4;
|
|
182
|
+
if (length === 0) return true;
|
|
183
|
+
if (length > VM_ARRAY_MAX_LENGTH) return false;
|
|
184
|
+
const firstKey = keys4[0];
|
|
185
|
+
if (firstKey !== "0") return false;
|
|
186
|
+
const lastKey = keys4[length - 1];
|
|
187
|
+
if (lastKey !== String(length - 1)) return false;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
function isVmArrayLikeRecord(value) {
|
|
191
|
+
return isVmArrayLikeRecordByKeys(keys(value));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/helpers/types/composed.ts
|
|
195
|
+
function isVmImmutable(value, checkDeep = false) {
|
|
196
|
+
return isVmModule(value) || isVmFunction(value) || isVmConst(value, checkDeep);
|
|
197
|
+
}
|
|
198
|
+
function isVmValue(value, checkDeep = false) {
|
|
199
|
+
return isVmWrapper(value) || isVmFunction(value) || isVmConst(value, checkDeep);
|
|
200
|
+
}
|
|
201
|
+
function isVmAny(value, checkDeep) {
|
|
202
|
+
if (value == null) return true;
|
|
203
|
+
return isVmValue(value, checkDeep);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/helpers/error.ts
|
|
207
|
+
var VmError = class _VmError extends Error {
|
|
208
|
+
constructor(message, recovered) {
|
|
209
|
+
super(message);
|
|
210
|
+
this.recovered = recovered;
|
|
211
|
+
this.name = "VmError";
|
|
212
|
+
}
|
|
213
|
+
/** 从其他错误构造 */
|
|
214
|
+
static from(prefix, error, recovered) {
|
|
215
|
+
if (prefix) {
|
|
216
|
+
if (prefix.endsWith(":")) {
|
|
217
|
+
prefix += " ";
|
|
218
|
+
} else if (!prefix.endsWith(": ")) {
|
|
219
|
+
prefix += ": ";
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
let vmError;
|
|
223
|
+
if (error instanceof Error) {
|
|
224
|
+
vmError = new _VmError(`${prefix}${error.message}`, recovered);
|
|
225
|
+
vmError.stack = error.stack;
|
|
226
|
+
} else {
|
|
227
|
+
vmError = new _VmError(`${prefix}${String(error)}`, recovered);
|
|
228
|
+
}
|
|
229
|
+
vmError.cause = error;
|
|
230
|
+
return vmError;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// src/helpers/serialize.ts
|
|
235
|
+
import { REG_IDENTIFIER_FULL, REG_ORDINAL_FULL } from "@mirascript/constants";
|
|
236
|
+
|
|
237
|
+
// src/helpers/convert/string.ts
|
|
238
|
+
function numberToString(value, minusZero) {
|
|
239
|
+
if (isNaN(value)) return "nan";
|
|
240
|
+
if (value === Infinity) return "inf";
|
|
241
|
+
if (value === -Infinity) return "-inf";
|
|
242
|
+
if (minusZero && value === 0 && 1 / value < 0) return "-0";
|
|
243
|
+
return String(value);
|
|
244
|
+
}
|
|
245
|
+
function innerToString(value, useBraces) {
|
|
246
|
+
if (value == null) return "nil";
|
|
247
|
+
if (typeof value == "number") return numberToString(value, false);
|
|
248
|
+
if (typeof value == "string" || typeof value == "boolean") return String(value);
|
|
249
|
+
if (typeof value == "function") return displayFunction(value);
|
|
250
|
+
if (isVmWrapper(value)) return value.toString(useBraces);
|
|
251
|
+
if (isVmArray(value)) {
|
|
252
|
+
const strings = [];
|
|
253
|
+
for (const item of value) {
|
|
254
|
+
strings.push(innerToString(item, true));
|
|
255
|
+
}
|
|
256
|
+
const results = strings.join(", ");
|
|
257
|
+
if (!useBraces) return results;
|
|
258
|
+
return `[${results}]`;
|
|
259
|
+
} else {
|
|
260
|
+
const entries3 = keys(value).map((key) => `${key}: ${innerToString(value[key], true)}`).join(", ");
|
|
261
|
+
if (!useBraces) return entries3;
|
|
262
|
+
return `(${entries3})`;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function toString(value, fallback) {
|
|
266
|
+
if (typeof value == "string") return value;
|
|
267
|
+
if (value == null) return "";
|
|
268
|
+
try {
|
|
269
|
+
return innerToString(value, false);
|
|
270
|
+
} catch (ex) {
|
|
271
|
+
if (fallback === void 0) {
|
|
272
|
+
const e2 = new VmError(`Failed to convert value to string: ${display(value)}`, "");
|
|
273
|
+
e2.cause = ex;
|
|
274
|
+
throw e2;
|
|
275
|
+
}
|
|
276
|
+
return fallback;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/helpers/serialize.ts
|
|
281
|
+
function serializeNil() {
|
|
282
|
+
return "nil";
|
|
283
|
+
}
|
|
284
|
+
function serializeBoolean(value) {
|
|
285
|
+
return value ? "true" : "false";
|
|
286
|
+
}
|
|
287
|
+
function serializeNumber(value) {
|
|
288
|
+
return numberToString(value, true);
|
|
289
|
+
}
|
|
290
|
+
function serializeStringEscaped(escaped, options) {
|
|
291
|
+
return options.serializeStringEscape("\\" + escaped, options);
|
|
292
|
+
}
|
|
293
|
+
function serializeStringContent(value, options) {
|
|
294
|
+
return options.serializeStringContent(value, options);
|
|
295
|
+
}
|
|
296
|
+
var STRING_QUOTE = `'`;
|
|
297
|
+
var STRING_REG = new RegExp(String.raw`[${STRING_QUOTE}\\\$\p{C}\u2028\u2029]`, "gu");
|
|
298
|
+
var STRING_MARK = /^[\p{M}]$/u;
|
|
299
|
+
function serializeSpecialChar(char, options) {
|
|
300
|
+
if (char === STRING_QUOTE) {
|
|
301
|
+
return serializeStringEscaped(STRING_QUOTE, options);
|
|
302
|
+
} else if (char === "\0") {
|
|
303
|
+
return serializeStringEscaped(`0`, options);
|
|
304
|
+
} else if (char === "\n") {
|
|
305
|
+
return serializeStringEscaped(`n`, options);
|
|
306
|
+
} else if (char === "\r") {
|
|
307
|
+
return serializeStringEscaped(`r`, options);
|
|
308
|
+
} else if (char === " ") {
|
|
309
|
+
return serializeStringEscaped(`t`, options);
|
|
310
|
+
} else if (char === "\b") {
|
|
311
|
+
return serializeStringEscaped(`b`, options);
|
|
312
|
+
} else if (char === "\f") {
|
|
313
|
+
return serializeStringEscaped(`f`, options);
|
|
314
|
+
} else if (char === "\v") {
|
|
315
|
+
return serializeStringEscaped(`v`, options);
|
|
316
|
+
} else if (char === "\\") {
|
|
317
|
+
return serializeStringEscaped(`\\`, options);
|
|
318
|
+
} else if (char === "$") {
|
|
319
|
+
return serializeStringEscaped(`$`, options);
|
|
320
|
+
} else {
|
|
321
|
+
const code = char.codePointAt(0);
|
|
322
|
+
if (code <= 127) {
|
|
323
|
+
return serializeStringEscaped(`x${code.toString(16).padStart(2, "0")}`, options);
|
|
324
|
+
} else if (code >= 55296 && code <= 57343) {
|
|
325
|
+
return serializeStringContent("�", options);
|
|
326
|
+
} else {
|
|
327
|
+
return serializeStringEscaped(`u{${code.toString(16)}}`, options);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
function serializeStringImpl(value, options) {
|
|
332
|
+
const oq = options.serializeStringQuote(STRING_QUOTE, true, options);
|
|
333
|
+
const cq = options.serializeStringQuote(STRING_QUOTE, false, options);
|
|
334
|
+
if (value.length === 0) {
|
|
335
|
+
return oq + cq;
|
|
336
|
+
}
|
|
337
|
+
let ret = oq;
|
|
338
|
+
let lastIndex = 0;
|
|
339
|
+
while (value.length > lastIndex) {
|
|
340
|
+
const cp2 = value.codePointAt(lastIndex);
|
|
341
|
+
const ch = String.fromCodePoint(cp2);
|
|
342
|
+
if (!STRING_MARK.test(ch)) break;
|
|
343
|
+
ret += serializeStringEscaped(`u{${cp2.toString(16)}}`, options);
|
|
344
|
+
lastIndex += ch.length;
|
|
345
|
+
}
|
|
346
|
+
STRING_REG.lastIndex = lastIndex;
|
|
347
|
+
let match;
|
|
348
|
+
while ((match = STRING_REG.exec(value)) !== null) {
|
|
349
|
+
ret += serializeStringContent(value.slice(lastIndex, match.index), options);
|
|
350
|
+
lastIndex = STRING_REG.lastIndex;
|
|
351
|
+
ret += serializeSpecialChar(match[0], options);
|
|
352
|
+
}
|
|
353
|
+
ret += serializeStringContent(value.slice(lastIndex), options);
|
|
354
|
+
ret += cq;
|
|
355
|
+
return ret;
|
|
356
|
+
}
|
|
357
|
+
function serializeString(value, options) {
|
|
358
|
+
return serializeStringImpl(value, getSerializeOptions(options));
|
|
359
|
+
}
|
|
360
|
+
function serializeRecordKeyDefault(key) {
|
|
361
|
+
if (REG_ORDINAL_FULL.test(key) || REG_IDENTIFIER_FULL.test(key)) {
|
|
362
|
+
return key;
|
|
363
|
+
}
|
|
364
|
+
return serializeStringImpl(key, DEFAULT_OPTIONS);
|
|
365
|
+
}
|
|
366
|
+
function serializeRecordKeyOpt(value, options) {
|
|
367
|
+
if (isDefaultOptions(options)) {
|
|
368
|
+
return serializeRecordKeyDefault(value);
|
|
369
|
+
}
|
|
370
|
+
if (REG_ORDINAL_FULL.test(value)) {
|
|
371
|
+
return options.serializePropName(Number(value), options);
|
|
372
|
+
}
|
|
373
|
+
if (REG_IDENTIFIER_FULL.test(value)) {
|
|
374
|
+
return options.serializePropName(value, options);
|
|
375
|
+
}
|
|
376
|
+
return options.serializeString(value, options);
|
|
377
|
+
}
|
|
378
|
+
function serializeRecordKey(key, options) {
|
|
379
|
+
if (isDefaultOptions(options)) {
|
|
380
|
+
return serializeRecordKeyDefault(key);
|
|
381
|
+
}
|
|
382
|
+
return serializeRecordKeyOpt(key, getSerializeOptions(options));
|
|
383
|
+
}
|
|
384
|
+
function serializeArray(value, depth, options) {
|
|
385
|
+
if (depth > options.maxDepth) return `[]`;
|
|
386
|
+
if (value.length === 0) return "[]";
|
|
387
|
+
let result = "[";
|
|
388
|
+
for (let i = 0; i < value.length; i++) {
|
|
389
|
+
if (i > 0) result += ", ";
|
|
390
|
+
result += serializeImpl(value[i], depth, options);
|
|
391
|
+
}
|
|
392
|
+
result += "]";
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
var { valueOf } = Object.prototype;
|
|
396
|
+
function customValueOf(value) {
|
|
397
|
+
const thisValueOf = value.valueOf;
|
|
398
|
+
if (typeof thisValueOf !== "function" || thisValueOf === valueOf) {
|
|
399
|
+
return void 0;
|
|
400
|
+
}
|
|
401
|
+
const customValue = thisValueOf.call(value);
|
|
402
|
+
if (customValue === value) return void 0;
|
|
403
|
+
return customValue;
|
|
404
|
+
}
|
|
405
|
+
function serializeRecord(value, depth, options) {
|
|
406
|
+
const customValue = customValueOf(value);
|
|
407
|
+
if (customValue !== void 0) {
|
|
408
|
+
return serializeImpl(customValue, depth - 1, options);
|
|
409
|
+
}
|
|
410
|
+
if (depth > options.maxDepth) return `()`;
|
|
411
|
+
const e2 = entries(value);
|
|
412
|
+
if (e2.length === 0) return "()";
|
|
413
|
+
if (e2.length === 1) {
|
|
414
|
+
const [k, v] = e2[0];
|
|
415
|
+
if (k === "0") {
|
|
416
|
+
return `(${serializeImpl(v, depth, options)},)`;
|
|
417
|
+
}
|
|
418
|
+
return `(${serializeRecordKeyOpt(k, options)}: ${serializeImpl(v, depth, options)})`;
|
|
419
|
+
}
|
|
420
|
+
const omitKey = isVmArrayLikeRecordByEntries(e2);
|
|
421
|
+
let result = "(";
|
|
422
|
+
for (const [key, val] of e2) {
|
|
423
|
+
if (result.length > 1) result += ", ";
|
|
424
|
+
if (omitKey) {
|
|
425
|
+
result += serializeImpl(val, depth, options);
|
|
426
|
+
} else {
|
|
427
|
+
result += `${serializeRecordKeyOpt(key, options)}: ${serializeImpl(val, depth, options)}`;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
result += ")";
|
|
431
|
+
return result;
|
|
432
|
+
}
|
|
433
|
+
function serializeImpl(value, depth, options) {
|
|
434
|
+
if (value == null) {
|
|
435
|
+
return options.serializeNil(options);
|
|
436
|
+
}
|
|
437
|
+
if (typeof value == "boolean") {
|
|
438
|
+
return options.serializeBoolean(value, options);
|
|
439
|
+
}
|
|
440
|
+
if (typeof value == "number") {
|
|
441
|
+
return options.serializeNumber(value, options);
|
|
442
|
+
}
|
|
443
|
+
if (typeof value == "string") {
|
|
444
|
+
return options.serializeString(value, options);
|
|
445
|
+
}
|
|
446
|
+
if (isVmFunction(value)) {
|
|
447
|
+
return options.serializeFunction(value, options);
|
|
448
|
+
}
|
|
449
|
+
if (isVmModule(value)) {
|
|
450
|
+
return options.serializeModule(value, depth + 1, options);
|
|
451
|
+
}
|
|
452
|
+
if (isVmExtern(value)) {
|
|
453
|
+
return options.serializeExtern(value, depth + 1, options);
|
|
454
|
+
}
|
|
455
|
+
if (isVmArray(value)) {
|
|
456
|
+
return options.serializeArray(value, depth + 1, options);
|
|
457
|
+
}
|
|
458
|
+
if (isVmRecord(value)) {
|
|
459
|
+
return options.serializeRecord(value, depth + 1, options);
|
|
460
|
+
}
|
|
461
|
+
value;
|
|
462
|
+
return options.serializeNil(options);
|
|
463
|
+
}
|
|
464
|
+
function serialize(value, options) {
|
|
465
|
+
return serializeImpl(value, 0, getSerializeOptions(options));
|
|
466
|
+
}
|
|
467
|
+
function displayFunction(value) {
|
|
468
|
+
try {
|
|
469
|
+
const name = getVmFunctionInfo(value)?.fullName;
|
|
470
|
+
return name ? `<function ${name}>` : `<function>`;
|
|
471
|
+
} catch {
|
|
472
|
+
return `<function>`;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
function displayModule(value) {
|
|
476
|
+
try {
|
|
477
|
+
return value.toString(true);
|
|
478
|
+
} catch {
|
|
479
|
+
return `<module>`;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function displayExtern(value) {
|
|
483
|
+
try {
|
|
484
|
+
const tag = `<extern ${value.tag}>`;
|
|
485
|
+
const rep = value.toString(true);
|
|
486
|
+
if (rep === tag || rep.length > 50) {
|
|
487
|
+
return tag;
|
|
488
|
+
}
|
|
489
|
+
return `${tag} ${rep}`;
|
|
490
|
+
} catch {
|
|
491
|
+
return `<extern>`;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
function display(value, options) {
|
|
495
|
+
const opt = mergeOptions(DISPLAY_OPTIONS, options);
|
|
496
|
+
return serializeImpl(value, 0, opt);
|
|
497
|
+
}
|
|
498
|
+
function isDefaultOptions(options) {
|
|
499
|
+
return options == null || options === DEFAULT_OPTIONS;
|
|
500
|
+
}
|
|
501
|
+
function mergeOptions(base, options) {
|
|
502
|
+
if (options == null) return base;
|
|
503
|
+
let opt = null;
|
|
504
|
+
for (const key in options) {
|
|
505
|
+
if (!hasOwn(options, key) || !hasOwn(base, key)) continue;
|
|
506
|
+
const el = options[key];
|
|
507
|
+
if (el == null) continue;
|
|
508
|
+
opt ??= { ...base };
|
|
509
|
+
opt[key] = el;
|
|
510
|
+
}
|
|
511
|
+
return opt ? Object.freeze(opt) : base;
|
|
512
|
+
}
|
|
513
|
+
function getSerializeOptions(options) {
|
|
514
|
+
if (isDefaultOptions(options)) return DEFAULT_OPTIONS;
|
|
515
|
+
return mergeOptions(DEFAULT_OPTIONS, options);
|
|
516
|
+
}
|
|
517
|
+
var DEFAULT_OPTIONS = Object.freeze({
|
|
518
|
+
maxDepth: 128,
|
|
519
|
+
serializeNil,
|
|
520
|
+
serializeBoolean,
|
|
521
|
+
serializeNumber,
|
|
522
|
+
serializeString: serializeStringImpl,
|
|
523
|
+
serializeStringQuote: (value) => value,
|
|
524
|
+
serializeStringEscape: (value) => value,
|
|
525
|
+
serializeStringContent: (value) => value,
|
|
526
|
+
serializeArray,
|
|
527
|
+
serializeRecord,
|
|
528
|
+
serializePropName: String,
|
|
529
|
+
serializeFunction: serializeNil,
|
|
530
|
+
serializeModule: serializeNil,
|
|
531
|
+
serializeExtern: serializeNil
|
|
532
|
+
});
|
|
533
|
+
var DISPLAY_OPTIONS = Object.freeze({
|
|
534
|
+
maxDepth: 3,
|
|
535
|
+
serializeNil,
|
|
536
|
+
serializeBoolean,
|
|
537
|
+
serializeNumber,
|
|
538
|
+
serializeString: serializeStringImpl,
|
|
539
|
+
serializeStringQuote: (value) => value,
|
|
540
|
+
serializeStringEscape: (value) => value,
|
|
541
|
+
serializeStringContent: (value) => value,
|
|
542
|
+
serializeArray,
|
|
543
|
+
serializeRecord,
|
|
544
|
+
serializePropName: String,
|
|
545
|
+
serializeFunction: displayFunction,
|
|
546
|
+
serializeModule: displayModule,
|
|
547
|
+
serializeExtern: displayExtern
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
// src/helpers/convert/index.ts
|
|
551
|
+
var convert_exports = {};
|
|
552
|
+
__export(convert_exports, {
|
|
553
|
+
toBoolean: () => toBoolean,
|
|
554
|
+
toFormat: () => toFormat,
|
|
555
|
+
toNumber: () => toNumber,
|
|
556
|
+
toString: () => toString
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
// src/helpers/convert/number.ts
|
|
560
|
+
var parseInt = (value, radix) => Number.parseInt(value.slice(2), radix);
|
|
561
|
+
function parseNumericLiteral(value) {
|
|
562
|
+
const ch0 = value.charAt(0);
|
|
563
|
+
if (ch0 < "0" || ch0 > "9") return null;
|
|
564
|
+
if (/^0[bB][01]+$/.test(value)) return parseInt(value, 2);
|
|
565
|
+
if (/^0[oO][0-7]+$/.test(value)) return parseInt(value, 8);
|
|
566
|
+
if (/^0[xX][0-9a-fA-F]+$/.test(value)) return parseInt(value, 16);
|
|
567
|
+
if (/^[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?$/.test(value)) return Number.parseFloat(value);
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
function stringToNumber(value) {
|
|
571
|
+
value = value.trim();
|
|
572
|
+
if (value === "") return null;
|
|
573
|
+
if (value === "inf" || value === "+inf" || value === "Infinity" || value === "+Infinity") return PositiveInfinity;
|
|
574
|
+
if (value === "-inf" || value === "-Infinity") return NegativeInfinity;
|
|
575
|
+
if (value === "nan" || value === "NaN") return NotNumber;
|
|
576
|
+
if (value.startsWith("-")) {
|
|
577
|
+
const num2 = parseNumericLiteral(value.slice(1));
|
|
578
|
+
if (num2 != null) return -num2;
|
|
579
|
+
return null;
|
|
580
|
+
} else if (value.startsWith("+")) {
|
|
581
|
+
return parseNumericLiteral(value.slice(1));
|
|
582
|
+
} else {
|
|
583
|
+
return parseNumericLiteral(value);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function toNumber(value, fallback) {
|
|
587
|
+
if (typeof value == "number") return value;
|
|
588
|
+
if (typeof value == "boolean") return value ? 1 : 0;
|
|
589
|
+
if (typeof value == "string") {
|
|
590
|
+
const num2 = stringToNumber(value);
|
|
591
|
+
if (num2 !== null) return num2;
|
|
592
|
+
}
|
|
593
|
+
if (fallback === void 0) {
|
|
594
|
+
throw new VmError(`Failed to convert value to number: ${display(value)}`, NotNumber);
|
|
595
|
+
}
|
|
596
|
+
return fallback;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// src/helpers/convert/format.ts
|
|
600
|
+
function formatNumber(finite) {
|
|
601
|
+
if (finite === 0) return "0";
|
|
602
|
+
const s2 = finite.toString();
|
|
603
|
+
let ps;
|
|
604
|
+
const abs3 = Math.abs(finite);
|
|
605
|
+
if (abs3 >= 1e3 || abs3 < 1e-3) {
|
|
606
|
+
const ps1 = finite.toExponential();
|
|
607
|
+
const ps2 = finite.toExponential(5);
|
|
608
|
+
ps = ps1.length < ps2.length ? ps1 : ps2;
|
|
609
|
+
} else {
|
|
610
|
+
ps = finite.toPrecision(6);
|
|
611
|
+
}
|
|
612
|
+
return ps.length < s2.length ? ps : s2;
|
|
613
|
+
}
|
|
614
|
+
function toFormat(value, format2) {
|
|
615
|
+
const f = format2 == null ? "" : format2.trim();
|
|
616
|
+
if (typeof value == "number") {
|
|
617
|
+
if (!isFinite(value)) {
|
|
618
|
+
return toString(value, void 0);
|
|
619
|
+
}
|
|
620
|
+
if (/^\.\d+$/.test(f)) {
|
|
621
|
+
let digits = Math.trunc(Number(f.slice(1)));
|
|
622
|
+
if (!(digits <= 100)) digits = 100;
|
|
623
|
+
return value.toFixed(digits);
|
|
624
|
+
} else {
|
|
625
|
+
return formatNumber(value);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return toString(value, void 0);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/helpers/convert/boolean.ts
|
|
632
|
+
function toBoolean(value, fallback) {
|
|
633
|
+
if (typeof value == "boolean") return value;
|
|
634
|
+
if (fallback === void 0) {
|
|
635
|
+
throw new VmError(`Failed to convert value to boolean: ${display(value)}`, false);
|
|
636
|
+
}
|
|
637
|
+
return fallback;
|
|
638
|
+
}
|
|
639
|
+
|
|
66
640
|
// src/vm/types/wrapper.ts
|
|
67
641
|
var VmWrapper = class {
|
|
68
642
|
constructor(value) {
|
|
@@ -642,7 +1216,7 @@ var { now } = Date;
|
|
|
642
1216
|
var TIME_ORIGIN = now() - 1e3 * 3600 * 12;
|
|
643
1217
|
var timestamp = () => now() - TIME_ORIGIN | 0;
|
|
644
1218
|
var CP_DEFAULT_INTERVAL = 100;
|
|
645
|
-
var
|
|
1219
|
+
var MAX_DEPTH2 = 128;
|
|
646
1220
|
var CP_UNSET = -1;
|
|
647
1221
|
var CP_DEFAULT_TIMEOUT = 100;
|
|
648
1222
|
var cpTimeout = CP_DEFAULT_TIMEOUT | 0;
|
|
@@ -667,7 +1241,7 @@ var CpEnter = () => {
|
|
|
667
1241
|
cp = CP_UNSET | 0;
|
|
668
1242
|
cpCounter = 0;
|
|
669
1243
|
cpDepth = 1;
|
|
670
|
-
} else if ((cpDepth | 0) >
|
|
1244
|
+
} else if ((cpDepth | 0) > MAX_DEPTH2) {
|
|
671
1245
|
throw new RangeError("Maximum call depth exceeded");
|
|
672
1246
|
} else {
|
|
673
1247
|
Cp();
|
|
@@ -1276,6 +1850,56 @@ function emitScript(source, [code, diagnostics], options) {
|
|
|
1276
1850
|
return createScript(source, options.input_mode ?? "Script", target);
|
|
1277
1851
|
}
|
|
1278
1852
|
|
|
1853
|
+
// src/compiler/worker-manager.ts
|
|
1854
|
+
import { WorkerPool } from "@cloudpss/worker/pool";
|
|
1855
|
+
var pool = /* @__PURE__ */ new WorkerPool(
|
|
1856
|
+
() => {
|
|
1857
|
+
return new Worker(new URL("#compiler/worker", import.meta.url), {
|
|
1858
|
+
type: "module",
|
|
1859
|
+
name: "@mirascript/compiler"
|
|
1860
|
+
});
|
|
1861
|
+
},
|
|
1862
|
+
{
|
|
1863
|
+
name: "@mirascript/compiler"
|
|
1864
|
+
}
|
|
1865
|
+
);
|
|
1866
|
+
async function compileWorker(...args) {
|
|
1867
|
+
return await pool.call("compile", args);
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
// src/compiler/options.ts
|
|
1871
|
+
var WORKER_MIN_LEN = typeof Worker != "function" ? Number.MAX_VALUE : 1024;
|
|
1872
|
+
function normalizeTranspileOptions(options) {
|
|
1873
|
+
options ??= {};
|
|
1874
|
+
if (options.sourceMap) {
|
|
1875
|
+
options.diagnostic_sourcemap = true;
|
|
1876
|
+
options.diagnostic_position_encoding ??= "Utf16";
|
|
1877
|
+
}
|
|
1878
|
+
options.input_mode ??= "Script";
|
|
1879
|
+
return options;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
// src/compiler/verbose.ts
|
|
1883
|
+
async function compileVerbose(source, options) {
|
|
1884
|
+
options = normalizeTranspileOptions(options);
|
|
1885
|
+
if (source.length < WORKER_MIN_LEN) {
|
|
1886
|
+
const bc = await generateBytecode(source, options);
|
|
1887
|
+
const script2 = emitScript(source, bc, options);
|
|
1888
|
+
return {
|
|
1889
|
+
script: script2,
|
|
1890
|
+
bytecode: bc[0],
|
|
1891
|
+
// Will throw if bytecode is undefined, but that should be handled by emitScript
|
|
1892
|
+
diagnostics: bc[1]
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
const [bytecode, target, diagnostics] = await compileWorker(source, options);
|
|
1896
|
+
if (target == null) {
|
|
1897
|
+
throw new CompileError(source, diagnostics, options.fileName);
|
|
1898
|
+
}
|
|
1899
|
+
const script = createScript(source, options.input_mode ?? "Script", target);
|
|
1900
|
+
return { script, bytecode, diagnostics };
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1279
1903
|
// src/vm/lib/global/index.ts
|
|
1280
1904
|
var global_exports = {};
|
|
1281
1905
|
__export(global_exports, {
|
|
@@ -3979,6 +4603,37 @@ for (const [name, value] of entries(mod_exports)) {
|
|
|
3979
4603
|
var lib = Object.freeze(Object.assign(create(null), global_exports, mod_exports));
|
|
3980
4604
|
|
|
3981
4605
|
export {
|
|
4606
|
+
constants_exports,
|
|
4607
|
+
isVmScript,
|
|
4608
|
+
isVmContext,
|
|
4609
|
+
isVmFunction,
|
|
4610
|
+
getVmFunctionInfo,
|
|
4611
|
+
isVmWrapper,
|
|
4612
|
+
isVmModule,
|
|
4613
|
+
isVmExtern,
|
|
4614
|
+
isVmCallable,
|
|
4615
|
+
isVmPrimitive,
|
|
4616
|
+
isVmArray,
|
|
4617
|
+
isVmRecord,
|
|
4618
|
+
isVmConst,
|
|
4619
|
+
getVmType,
|
|
4620
|
+
isVmArrayLikeRecordByEntries,
|
|
4621
|
+
isVmArrayLikeRecordByKeys,
|
|
4622
|
+
isVmArrayLikeRecord,
|
|
4623
|
+
isVmImmutable,
|
|
4624
|
+
isVmValue,
|
|
4625
|
+
isVmAny,
|
|
4626
|
+
VmError,
|
|
4627
|
+
serializeNil,
|
|
4628
|
+
serializeBoolean,
|
|
4629
|
+
serializeNumber,
|
|
4630
|
+
serializeString,
|
|
4631
|
+
serializeRecordKey,
|
|
4632
|
+
serializeArray,
|
|
4633
|
+
serializeRecord,
|
|
4634
|
+
serialize,
|
|
4635
|
+
display,
|
|
4636
|
+
convert_exports,
|
|
3982
4637
|
VmExtern,
|
|
3983
4638
|
wrapToVmValue,
|
|
3984
4639
|
unwrapFromVmValue,
|
|
@@ -3994,6 +4649,8 @@ export {
|
|
|
3994
4649
|
createScript,
|
|
3995
4650
|
CompileError,
|
|
3996
4651
|
emitScript,
|
|
4652
|
+
normalizeTranspileOptions,
|
|
4653
|
+
compileVerbose,
|
|
3997
4654
|
lib
|
|
3998
4655
|
};
|
|
3999
|
-
//# sourceMappingURL=chunk-
|
|
4656
|
+
//# sourceMappingURL=chunk-Y7S6AHE7.js.map
|