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