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