@mirascript/mirascript 0.1.0 → 0.1.2
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-35JGBXRE.js +1 -0
- package/dist/{chunk-5FQWUJIY.js → chunk-3RUWGMBP.js} +20 -6
- package/dist/chunk-3RUWGMBP.js.map +6 -0
- package/dist/{chunk-DCXIWIW5.js → chunk-AOINGBRS.js} +306 -84
- package/dist/chunk-AOINGBRS.js.map +6 -0
- package/dist/cli/index.js +10 -10
- package/dist/cli/index.js.map +1 -1
- package/dist/compiler/diagnostic.d.ts +1 -1
- package/dist/compiler/diagnostic.d.ts.map +1 -1
- package/dist/compiler/generate-bytecode.d.ts +12 -0
- package/dist/compiler/generate-bytecode.d.ts.map +1 -0
- package/dist/compiler/index.d.ts +6 -0
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/worker.js +4 -5
- package/dist/compiler/worker.js.map +1 -1
- package/dist/helpers/utils.d.ts +1 -0
- package/dist/helpers/utils.d.ts.map +1 -1
- package/dist/index.js +18 -7
- package/dist/subtle.d.ts +1 -0
- package/dist/subtle.d.ts.map +1 -1
- package/dist/subtle.js +9 -2
- package/dist/vm/error.d.ts.map +1 -1
- package/dist/vm/lib/_helpers.d.ts.map +1 -1
- package/dist/vm/lib/global/sequence/find.d.ts.map +1 -1
- package/dist/vm/operations.d.ts.map +1 -1
- package/dist/vm/types/boundary.d.ts +12 -0
- package/dist/vm/types/boundary.d.ts.map +1 -0
- package/dist/vm/types/context.d.ts.map +1 -1
- package/dist/vm/types/extern.d.ts +5 -7
- package/dist/vm/types/extern.d.ts.map +1 -1
- package/dist/vm/types/function.d.ts +0 -4
- package/dist/vm/types/function.d.ts.map +1 -1
- package/dist/vm/types/index.d.ts +5 -2
- package/dist/vm/types/index.d.ts.map +1 -1
- package/package.json +7 -5
- package/src/compiler/diagnostic.ts +2 -1
- package/src/compiler/emit.ts +4 -0
- package/src/compiler/{compile-bytecode.ts → generate-bytecode.ts} +5 -2
- package/src/compiler/index.ts +7 -6
- package/src/compiler/worker.ts +2 -2
- package/src/helpers/utils.ts +1 -0
- package/src/subtle.ts +1 -0
- package/src/vm/error.ts +15 -3
- package/src/vm/lib/_helpers.ts +3 -3
- package/src/vm/lib/_loader.ts +4 -4
- package/src/vm/lib/global/math.ts +1 -1
- package/src/vm/lib/global/sequence/find.ts +13 -11
- package/src/vm/lib/global/sequence/map-filter.ts +2 -2
- package/src/vm/lib/global/time.ts +1 -1
- package/src/vm/operations.ts +21 -9
- package/src/vm/types/boundary.ts +95 -0
- package/src/vm/types/context.ts +4 -4
- package/src/vm/types/extern.ts +19 -55
- package/src/vm/types/function.ts +2 -37
- package/src/vm/types/index.ts +9 -2
- package/dist/chunk-5FQWUJIY.js.map +0 -6
- package/dist/chunk-BTDGMWFK.js +0 -202
- package/dist/chunk-BTDGMWFK.js.map +0 -6
- package/dist/chunk-DCXIWIW5.js.map +0 -6
- package/dist/chunk-RAPJ3XLV.js +0 -10
- package/dist/compiler/compile-bytecode.d.ts +0 -12
- package/dist/compiler/compile-bytecode.d.ts.map +0 -1
- /package/dist/{chunk-RAPJ3XLV.js.map → chunk-35JGBXRE.js.map} +0 -0
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
__export
|
|
3
|
-
|
|
2
|
+
__export,
|
|
3
|
+
emit,
|
|
4
|
+
generateBytecode,
|
|
5
|
+
generateBytecodeSync,
|
|
6
|
+
loadModule
|
|
7
|
+
} from "./chunk-3RUWGMBP.js";
|
|
4
8
|
|
|
5
9
|
// src/helpers/constants.ts
|
|
6
10
|
var constants_exports = {};
|
|
@@ -15,6 +19,7 @@ var REG_ORDINAL = /(?:0|[1-9]\d{0,8}|1\d{9}|20\d{8}|21[0-3]\d{7}|214[0-6]\d{6}|2
|
|
|
15
19
|
var { isArray } = Array;
|
|
16
20
|
var { isFinite, isNaN, isInteger, isSafeInteger } = Number;
|
|
17
21
|
var { hasOwn, keys, values, entries, create, getPrototypeOf, fromEntries, defineProperty } = Object;
|
|
22
|
+
var { apply } = Reflect;
|
|
18
23
|
var hasOwnEnumerable = Function.call.bind(
|
|
19
24
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
20
25
|
Object.prototype.propertyIsEnumerable
|
|
@@ -29,13 +34,40 @@ var VmError = class _VmError extends Error {
|
|
|
29
34
|
}
|
|
30
35
|
/** 从其他错误构造 */
|
|
31
36
|
static from(prefix, error, recovered) {
|
|
32
|
-
if (prefix
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
if (prefix) {
|
|
38
|
+
if (prefix.endsWith(":")) {
|
|
39
|
+
prefix += " ";
|
|
40
|
+
} else if (!prefix.endsWith(": ")) {
|
|
41
|
+
prefix += ": ";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
let vmError;
|
|
45
|
+
if (error instanceof Error) {
|
|
46
|
+
vmError = new _VmError(`${prefix}${error.message}`, recovered);
|
|
47
|
+
vmError.stack = error.stack;
|
|
48
|
+
} else {
|
|
49
|
+
vmError = new _VmError(`${prefix}${String(error)}`, recovered);
|
|
50
|
+
}
|
|
51
|
+
vmError.cause = error;
|
|
35
52
|
return vmError;
|
|
36
53
|
}
|
|
37
54
|
};
|
|
38
55
|
|
|
56
|
+
// src/vm/types/wrapper.ts
|
|
57
|
+
var VmWrapper = class {
|
|
58
|
+
constructor(value) {
|
|
59
|
+
this.value = value;
|
|
60
|
+
}
|
|
61
|
+
/** Convert the object to JSON */
|
|
62
|
+
toJSON() {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
/** 转为字符串 */
|
|
66
|
+
toString() {
|
|
67
|
+
return `<${this.type} ${this.describe}>`;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
39
71
|
// src/vm/helpers.ts
|
|
40
72
|
var helpers_exports = {};
|
|
41
73
|
__export(helpers_exports, {
|
|
@@ -104,23 +136,6 @@ __export(operations_exports, {
|
|
|
104
136
|
$ToString: () => $ToString,
|
|
105
137
|
$Type: () => $Type
|
|
106
138
|
});
|
|
107
|
-
|
|
108
|
-
// src/vm/types/wrapper.ts
|
|
109
|
-
var VmWrapper = class {
|
|
110
|
-
constructor(value) {
|
|
111
|
-
this.value = value;
|
|
112
|
-
}
|
|
113
|
-
/** Convert the object to JSON */
|
|
114
|
-
toJSON() {
|
|
115
|
-
return void 0;
|
|
116
|
-
}
|
|
117
|
-
/** 转为字符串 */
|
|
118
|
-
toString() {
|
|
119
|
-
return `<${this.type} ${this.describe}>`;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
// src/vm/operations.ts
|
|
124
139
|
var { abs, min, trunc, ceil } = Math;
|
|
125
140
|
var { slice, at } = Array.prototype;
|
|
126
141
|
var { POSITIVE_INFINITY, NEGATIVE_INFINITY } = Number;
|
|
@@ -131,10 +146,10 @@ var isSame = (a, b) => {
|
|
|
131
146
|
if (a instanceof VmWrapper) return a.same(b);
|
|
132
147
|
if (b instanceof VmWrapper) return b.same(a);
|
|
133
148
|
if (isVmArray(a) && isVmArray(b)) {
|
|
134
|
-
|
|
149
|
+
const len2 = a.length;
|
|
150
|
+
if (len2 !== b.length) {
|
|
135
151
|
return false;
|
|
136
152
|
}
|
|
137
|
-
const len2 = a.length;
|
|
138
153
|
for (let i = 0; i < len2; i++) {
|
|
139
154
|
if (!isSame(a[i] ?? null, b[i] ?? null)) {
|
|
140
155
|
return false;
|
|
@@ -223,7 +238,9 @@ var $Eq = (a, b) => {
|
|
|
223
238
|
if (typeof a == "number" && typeof b == "number") return a === b;
|
|
224
239
|
return isSame(a, b);
|
|
225
240
|
};
|
|
226
|
-
var $Neq = (a, b) =>
|
|
241
|
+
var $Neq = (a, b) => {
|
|
242
|
+
return !$Eq(a, b);
|
|
243
|
+
};
|
|
227
244
|
var stringComparer = new Intl.Collator("en", {
|
|
228
245
|
usage: "sort",
|
|
229
246
|
numeric: false,
|
|
@@ -247,13 +264,17 @@ var $Aeq = (a, b) => {
|
|
|
247
264
|
return stringComparer.compare(as, bs) === 0;
|
|
248
265
|
}
|
|
249
266
|
};
|
|
250
|
-
var $Naeq = (a, b) =>
|
|
267
|
+
var $Naeq = (a, b) => {
|
|
268
|
+
return !$Aeq(a, b);
|
|
269
|
+
};
|
|
251
270
|
var $Same = (a, b) => {
|
|
252
271
|
$AssertInit(a);
|
|
253
272
|
$AssertInit(b);
|
|
254
273
|
return isSame(a, b);
|
|
255
274
|
};
|
|
256
|
-
var $Nsame = (a, b) =>
|
|
275
|
+
var $Nsame = (a, b) => {
|
|
276
|
+
return !$Same(a, b);
|
|
277
|
+
};
|
|
257
278
|
var $In = (value, iterable) => {
|
|
258
279
|
$AssertInit(value);
|
|
259
280
|
$AssertInit(iterable);
|
|
@@ -277,9 +298,15 @@ var $In = (value, iterable) => {
|
|
|
277
298
|
var $Concat = (...args) => {
|
|
278
299
|
return args.map($Format).join("");
|
|
279
300
|
};
|
|
280
|
-
var $Pos = (a) =>
|
|
281
|
-
|
|
282
|
-
|
|
301
|
+
var $Pos = (a) => {
|
|
302
|
+
return $ToNumber(a);
|
|
303
|
+
};
|
|
304
|
+
var $Neg = (a) => {
|
|
305
|
+
return -$ToNumber(a);
|
|
306
|
+
};
|
|
307
|
+
var $Not = (a) => {
|
|
308
|
+
return !$ToBoolean(a);
|
|
309
|
+
};
|
|
283
310
|
var $Length = (value) => {
|
|
284
311
|
$AssertInit(value);
|
|
285
312
|
if (isVmArray(value)) return value.length;
|
|
@@ -656,7 +683,6 @@ function GlobalFallback() {
|
|
|
656
683
|
|
|
657
684
|
// src/vm/types/function.ts
|
|
658
685
|
var kVmFunction = Symbol.for("mirascript.vm.function");
|
|
659
|
-
var kProxy = Symbol.for("mirascript.vm.function.proxy");
|
|
660
686
|
function isVmFunction(value) {
|
|
661
687
|
return getVmFunctionInfo(value) != null;
|
|
662
688
|
}
|
|
@@ -702,10 +728,13 @@ function VmFunction(fn, option = {}) {
|
|
|
702
728
|
});
|
|
703
729
|
return fn;
|
|
704
730
|
}
|
|
731
|
+
|
|
732
|
+
// src/vm/types/boundary.ts
|
|
733
|
+
var kProxy = Symbol.for("mirascript.vm.function.proxy");
|
|
705
734
|
function toVmFunctionProxy(fn) {
|
|
706
735
|
if (!isVmFunction(fn)) return fn;
|
|
707
736
|
const cached = fn[kProxy];
|
|
708
|
-
if (cached) return cached;
|
|
737
|
+
if (cached != null) return cached;
|
|
709
738
|
const proxy = (...args) => {
|
|
710
739
|
const ret = $Call(
|
|
711
740
|
fn,
|
|
@@ -728,20 +757,20 @@ function fromVmFunctionProxy(fn) {
|
|
|
728
757
|
if (original && isVmFunction(original)) return original;
|
|
729
758
|
return void 0;
|
|
730
759
|
}
|
|
731
|
-
|
|
732
|
-
// src/vm/types/extern.ts
|
|
733
|
-
var { apply } = Reflect;
|
|
734
|
-
function wrapToVmValue(value, caller) {
|
|
760
|
+
function wrapToVmValue(value, thisArg = null, assumeVmValue) {
|
|
735
761
|
if (value == null) return null;
|
|
736
762
|
switch (typeof value) {
|
|
737
763
|
case "function": {
|
|
738
764
|
const unwrapped = fromVmFunctionProxy(value);
|
|
739
765
|
if (unwrapped) return unwrapped;
|
|
740
|
-
return new VmExtern(value,
|
|
766
|
+
return new VmExtern(value, thisArg);
|
|
741
767
|
}
|
|
742
|
-
case "object":
|
|
768
|
+
case "object": {
|
|
743
769
|
if (value instanceof VmWrapper) return value;
|
|
744
|
-
|
|
770
|
+
if (value instanceof Date) return value.valueOf();
|
|
771
|
+
if (assumeVmValue?.(value)) return value;
|
|
772
|
+
return new VmExtern(value);
|
|
773
|
+
}
|
|
745
774
|
case "string":
|
|
746
775
|
case "number":
|
|
747
776
|
case "boolean":
|
|
@@ -760,23 +789,25 @@ function unwrapFromVmValue(value) {
|
|
|
760
789
|
}
|
|
761
790
|
if (value == null || typeof value != "object") return value;
|
|
762
791
|
if (!(value instanceof VmExtern)) return value;
|
|
763
|
-
if (value.
|
|
792
|
+
if (value.thisArg == null || typeof value.value != "function") {
|
|
764
793
|
return value.value;
|
|
765
794
|
}
|
|
766
|
-
const
|
|
767
|
-
const
|
|
768
|
-
return new Proxy(
|
|
795
|
+
const f = value;
|
|
796
|
+
const caller = f.thisArg.value;
|
|
797
|
+
return new Proxy(f.value, {
|
|
769
798
|
apply(target, thisArg, args) {
|
|
770
799
|
return apply(target, caller, args);
|
|
771
800
|
}
|
|
772
801
|
});
|
|
773
802
|
}
|
|
803
|
+
|
|
804
|
+
// src/vm/types/extern.ts
|
|
774
805
|
var ObjectPrototype = Object.prototype;
|
|
775
806
|
var ObjectToString = ObjectPrototype.toString;
|
|
776
807
|
var VmExtern = class _VmExtern extends VmWrapper {
|
|
777
|
-
constructor(value,
|
|
808
|
+
constructor(value, thisArg = null) {
|
|
778
809
|
super(value);
|
|
779
|
-
this.
|
|
810
|
+
this.thisArg = thisArg;
|
|
780
811
|
}
|
|
781
812
|
/** Check if the object has a property */
|
|
782
813
|
access(key, read) {
|
|
@@ -793,6 +824,10 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
793
824
|
if (key in Object.prototype && prop === Object.prototype[key]) return false;
|
|
794
825
|
return true;
|
|
795
826
|
}
|
|
827
|
+
/** 决定是否对属性进行包装 */
|
|
828
|
+
assumeVmValue(value, key) {
|
|
829
|
+
return false;
|
|
830
|
+
}
|
|
796
831
|
/** @inheritdoc */
|
|
797
832
|
has(key) {
|
|
798
833
|
return this.access(key, true);
|
|
@@ -801,7 +836,7 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
801
836
|
get(key) {
|
|
802
837
|
if (!this.has(key)) return void 0;
|
|
803
838
|
const prop = this.value[key];
|
|
804
|
-
return wrapToVmValue(prop, this);
|
|
839
|
+
return wrapToVmValue(prop, this, (v) => this.assumeVmValue(v, key));
|
|
805
840
|
}
|
|
806
841
|
/** Set a property on the object */
|
|
807
842
|
set(key, value) {
|
|
@@ -813,27 +848,31 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
813
848
|
/** Call extern value */
|
|
814
849
|
call(args) {
|
|
815
850
|
const { value } = this;
|
|
816
|
-
|
|
851
|
+
if (typeof value != "function") {
|
|
852
|
+
throw VmError.from(`Not a callable extern`, null, null);
|
|
853
|
+
}
|
|
854
|
+
const caller = this.thisArg?.value ?? null;
|
|
817
855
|
const unwrappedArgs = args.map(unwrapFromVmValue);
|
|
856
|
+
let ret;
|
|
818
857
|
try {
|
|
819
|
-
|
|
820
|
-
return wrapToVmValue(ret, null);
|
|
858
|
+
ret = apply(value, caller, unwrappedArgs);
|
|
821
859
|
} catch (ex) {
|
|
822
|
-
throw VmError.from(`
|
|
860
|
+
throw VmError.from(`Callable extern`, ex, null);
|
|
823
861
|
}
|
|
862
|
+
return wrapToVmValue(ret, null, (obj) => this.assumeVmValue(obj, void 0));
|
|
824
863
|
}
|
|
825
864
|
/** @inheritdoc */
|
|
826
865
|
keys() {
|
|
827
|
-
const
|
|
866
|
+
const keys4 = [];
|
|
828
867
|
for (const key in this.value) {
|
|
829
|
-
if (this.has(key))
|
|
868
|
+
if (this.has(key)) keys4.push(key);
|
|
830
869
|
}
|
|
831
|
-
return
|
|
870
|
+
return keys4;
|
|
832
871
|
}
|
|
833
872
|
/** @inheritdoc */
|
|
834
873
|
same(other) {
|
|
835
874
|
if (!(other instanceof _VmExtern)) return false;
|
|
836
|
-
return this.value === other.value && this.
|
|
875
|
+
return this.value === other.value && this.thisArg === other.thisArg;
|
|
837
876
|
}
|
|
838
877
|
/** @inheritdoc */
|
|
839
878
|
toString() {
|
|
@@ -1041,6 +1080,9 @@ function isVmPrimitive(value) {
|
|
|
1041
1080
|
function isVmExtern(value) {
|
|
1042
1081
|
return value instanceof VmExtern;
|
|
1043
1082
|
}
|
|
1083
|
+
function isVmCallable(value) {
|
|
1084
|
+
return isVmFunction(value) || isVmExtern(value) && typeof value.value == "function";
|
|
1085
|
+
}
|
|
1044
1086
|
function isVmModule(value) {
|
|
1045
1087
|
return value instanceof VmModule;
|
|
1046
1088
|
}
|
|
@@ -1048,7 +1090,7 @@ var VM_ARRAY_MAX_LENGTH = 2 ** 31 - 1;
|
|
|
1048
1090
|
|
|
1049
1091
|
// src/vm/types/context.ts
|
|
1050
1092
|
var kVmContext = Symbol.for("mira:vm-context");
|
|
1051
|
-
var VmSharedContext =
|
|
1093
|
+
var VmSharedContext = create(null);
|
|
1052
1094
|
function defineVmContextValue(name, value, override = false) {
|
|
1053
1095
|
if (!override && name in VmSharedContext) throw new Error(`Global variable '${name}' is already defined.`);
|
|
1054
1096
|
let v;
|
|
@@ -1131,7 +1173,7 @@ function createVmContext(...args) {
|
|
|
1131
1173
|
return new FactoryVmContext(args[0], args[1]);
|
|
1132
1174
|
}
|
|
1133
1175
|
const [vmValues, externValues] = args;
|
|
1134
|
-
const env =
|
|
1176
|
+
const env = create(VmSharedContext);
|
|
1135
1177
|
if (vmValues) {
|
|
1136
1178
|
for (const [key, value] of entries(vmValues)) {
|
|
1137
1179
|
if (!isVmAny(value, false)) continue;
|
|
@@ -1286,8 +1328,7 @@ function expectConst(name, value, recovered) {
|
|
|
1286
1328
|
}
|
|
1287
1329
|
function expectCallable(name, value, recovered) {
|
|
1288
1330
|
required(name, value, recovered);
|
|
1289
|
-
|
|
1290
|
-
if (!callable) {
|
|
1331
|
+
if (!isVmCallable(value)) {
|
|
1291
1332
|
throwUnexpectedTypeError(name, "callable", value, recovered);
|
|
1292
1333
|
}
|
|
1293
1334
|
}
|
|
@@ -1355,8 +1396,8 @@ function VmLib(fn, option) {
|
|
|
1355
1396
|
|
|
1356
1397
|
// src/vm/lib/global/math-arr.ts
|
|
1357
1398
|
function build(f) {
|
|
1358
|
-
return (...
|
|
1359
|
-
const numbers = getNumbers(
|
|
1399
|
+
return (...values4) => {
|
|
1400
|
+
const numbers = getNumbers(values4);
|
|
1360
1401
|
return f(...numbers);
|
|
1361
1402
|
};
|
|
1362
1403
|
}
|
|
@@ -1382,8 +1423,8 @@ var hypot = VmLib(build(Math.hypot), {
|
|
|
1382
1423
|
examples: ["hypot(3, 4) // 5"]
|
|
1383
1424
|
});
|
|
1384
1425
|
var sum = VmLib(
|
|
1385
|
-
(...
|
|
1386
|
-
const numbers = getNumbers(
|
|
1426
|
+
(...values4) => {
|
|
1427
|
+
const numbers = getNumbers(values4);
|
|
1387
1428
|
return numbers.reduce((a, b) => a + b, 0);
|
|
1388
1429
|
},
|
|
1389
1430
|
{
|
|
@@ -1395,8 +1436,8 @@ var sum = VmLib(
|
|
|
1395
1436
|
}
|
|
1396
1437
|
);
|
|
1397
1438
|
var product = VmLib(
|
|
1398
|
-
(...
|
|
1399
|
-
const numbers = getNumbers(
|
|
1439
|
+
(...values4) => {
|
|
1440
|
+
const numbers = getNumbers(values4);
|
|
1400
1441
|
return numbers.reduce((a, b) => a * b, 1);
|
|
1401
1442
|
},
|
|
1402
1443
|
{
|
|
@@ -1649,7 +1690,7 @@ var pow2 = VmLib((x, y) => _pow($ToNumber(x), $ToNumber(y)), {
|
|
|
1649
1690
|
returnsType: "number"
|
|
1650
1691
|
});
|
|
1651
1692
|
var random = VmLib(Math.random, {
|
|
1652
|
-
summary: "返回 0
|
|
1693
|
+
summary: "返回 [0, 1) 之间的伪随机数",
|
|
1653
1694
|
params: {},
|
|
1654
1695
|
paramsType: {},
|
|
1655
1696
|
returnsType: "number"
|
|
@@ -1892,7 +1933,7 @@ var filter = VmLib(
|
|
|
1892
1933
|
summary: "过滤数组或记录中的元素,返回满足条件的元素",
|
|
1893
1934
|
params: {
|
|
1894
1935
|
data: "要过滤的数组或记录",
|
|
1895
|
-
predicate: "
|
|
1936
|
+
predicate: "用于测试每个元素的函数"
|
|
1896
1937
|
},
|
|
1897
1938
|
paramsType: {
|
|
1898
1939
|
data: "array | record",
|
|
@@ -1911,7 +1952,7 @@ var filter_map = VmLib(
|
|
|
1911
1952
|
summary: "对数组或记录中的每个元素应用函数,并返回非 nil 的结果",
|
|
1912
1953
|
params: {
|
|
1913
1954
|
data: "要映射的数组或记录",
|
|
1914
|
-
f: "
|
|
1955
|
+
f: "应用于每个元素的函数"
|
|
1915
1956
|
},
|
|
1916
1957
|
paramsType: {
|
|
1917
1958
|
data: "array | record",
|
|
@@ -1926,11 +1967,11 @@ var filter_map = VmLib(
|
|
|
1926
1967
|
var find = VmLib(
|
|
1927
1968
|
(data, predicate) => {
|
|
1928
1969
|
expectArrayOrRecord("data", data, null);
|
|
1929
|
-
|
|
1930
|
-
const p = (value, key, data2) => {
|
|
1970
|
+
required("predicate", predicate, null);
|
|
1971
|
+
const p = isVmCallable(predicate) ? (value, key, data2) => {
|
|
1931
1972
|
const ret = $Call(predicate, [value, key, data2]);
|
|
1932
1973
|
return $ToBoolean(ret);
|
|
1933
|
-
};
|
|
1974
|
+
} : (value) => $Same(predicate, value);
|
|
1934
1975
|
if (isVmArray(data)) {
|
|
1935
1976
|
const { length } = data;
|
|
1936
1977
|
for (let i = 0; i < length; i++) {
|
|
@@ -1956,14 +1997,14 @@ var find = VmLib(
|
|
|
1956
1997
|
summary: "查找数组或记录中的键值对,返回第一个满足条件的键值对",
|
|
1957
1998
|
params: {
|
|
1958
1999
|
data: "查的数组或记录",
|
|
1959
|
-
predicate: "
|
|
2000
|
+
predicate: "用于测试每个键值对的函数,或要查找的值"
|
|
1960
2001
|
},
|
|
1961
2002
|
paramsType: {
|
|
1962
2003
|
data: "array | record",
|
|
1963
|
-
predicate: "fn(value: any, key: number | string | nil, input: type(data)) -> boolean"
|
|
2004
|
+
predicate: "(fn(value: any, key: number | string | nil, input: type(data)) -> boolean) | any"
|
|
1964
2005
|
},
|
|
1965
2006
|
returnsType: "(string | number, any) | nil",
|
|
1966
|
-
examples: ["find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)"]
|
|
2007
|
+
examples: ["find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)", `find((x: 1, y: 2, z: 3), 2) // ('y', 2)`]
|
|
1967
2008
|
}
|
|
1968
2009
|
);
|
|
1969
2010
|
|
|
@@ -2518,7 +2559,7 @@ var to_datetime = VmLib(
|
|
|
2518
2559
|
};
|
|
2519
2560
|
},
|
|
2520
2561
|
{
|
|
2521
|
-
summary: "将数据转换为 Date
|
|
2562
|
+
summary: "将数据转换为 Date 记录",
|
|
2522
2563
|
params: {
|
|
2523
2564
|
datetime: "要转换的数据,默认为当前时间",
|
|
2524
2565
|
offset: "时区偏移量(单位:小时),默认为 0"
|
|
@@ -3048,9 +3089,9 @@ var matrix = createModule("matrix", matrix_exports);
|
|
|
3048
3089
|
|
|
3049
3090
|
// src/vm/lib/_loader.ts
|
|
3050
3091
|
for (const [name, value] of entries(global_exports)) {
|
|
3051
|
-
VmSharedContext[name] = wrapEntry(name, value);
|
|
3092
|
+
VmSharedContext[name] = wrapEntry(name, value, "global");
|
|
3052
3093
|
}
|
|
3053
|
-
function wrapEntry(name, value) {
|
|
3094
|
+
function wrapEntry(name, value, module) {
|
|
3054
3095
|
if (typeof value == "function") {
|
|
3055
3096
|
if (value.name !== name) {
|
|
3056
3097
|
defineProperty(value, "name", {
|
|
@@ -3061,7 +3102,7 @@ function wrapEntry(name, value) {
|
|
|
3061
3102
|
return VmFunction(value, {
|
|
3062
3103
|
isLib: true,
|
|
3063
3104
|
injectCp: true,
|
|
3064
|
-
fullName:
|
|
3105
|
+
fullName: `${module}.${name}`,
|
|
3065
3106
|
...value
|
|
3066
3107
|
});
|
|
3067
3108
|
} else {
|
|
@@ -3071,7 +3112,7 @@ function wrapEntry(name, value) {
|
|
|
3071
3112
|
function createModule(name, lib2) {
|
|
3072
3113
|
const mod = create(null);
|
|
3073
3114
|
for (const [key, value] of entries(lib2)) {
|
|
3074
|
-
mod[key] = wrapEntry(key, value);
|
|
3115
|
+
mod[key] = wrapEntry(key, value, name);
|
|
3075
3116
|
}
|
|
3076
3117
|
return new VmModule(name, mod);
|
|
3077
3118
|
}
|
|
@@ -3373,10 +3414,187 @@ function formatDiagnostic(diagnostic) {
|
|
|
3373
3414
|
return ` ${codeName}(${range}): ${message}`;
|
|
3374
3415
|
}
|
|
3375
3416
|
|
|
3417
|
+
// src/vm/env.ts
|
|
3418
|
+
var keys3 = [];
|
|
3419
|
+
var values3 = [];
|
|
3420
|
+
for (const [key, value] of entries(operations_exports)) {
|
|
3421
|
+
keys3.push(key);
|
|
3422
|
+
values3.push(value);
|
|
3423
|
+
}
|
|
3424
|
+
for (const [key, value] of entries(helpers_exports)) {
|
|
3425
|
+
keys3.push(key);
|
|
3426
|
+
values3.push(value);
|
|
3427
|
+
}
|
|
3428
|
+
|
|
3429
|
+
// src/compiler/create-script.ts
|
|
3430
|
+
var kVmScript2 = Symbol.for("mirascript.vm.script");
|
|
3431
|
+
function wrapScript(source, script) {
|
|
3432
|
+
if (kVmScript2 in script) {
|
|
3433
|
+
return script;
|
|
3434
|
+
}
|
|
3435
|
+
defineProperty(script, kVmScript2, { value: true });
|
|
3436
|
+
if (typeof source === "string") {
|
|
3437
|
+
defineProperty(script, "source", { value: source, configurable: true });
|
|
3438
|
+
} else if (source instanceof Uint8Array) {
|
|
3439
|
+
defineProperty(script, "source", { value: "<buffer>", configurable: true });
|
|
3440
|
+
}
|
|
3441
|
+
return script;
|
|
3442
|
+
}
|
|
3443
|
+
function createScript(source, code) {
|
|
3444
|
+
let script;
|
|
3445
|
+
try {
|
|
3446
|
+
script = new Function(...keys3, code)(...values3);
|
|
3447
|
+
} catch (error) {
|
|
3448
|
+
throw new Error(`Failed to create script`, { cause: error });
|
|
3449
|
+
}
|
|
3450
|
+
return wrapScript(source, script);
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
// src/compiler/compile-fast.ts
|
|
3454
|
+
var REG_NUMBER_FULL = /^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
3455
|
+
var REG_IDENTIFIER_FAST = /^(?:\$+|@+)[a-zA-Z0-9_]*$/;
|
|
3456
|
+
var FAST_SCRIPT_MAX_LEN = 32;
|
|
3457
|
+
function compileScriptFast(code, options) {
|
|
3458
|
+
if (code.length > FAST_SCRIPT_MAX_LEN) return void 0;
|
|
3459
|
+
const trimmedCode = code.trim();
|
|
3460
|
+
if (!trimmedCode) {
|
|
3461
|
+
return wrapScript(code, () => null);
|
|
3462
|
+
}
|
|
3463
|
+
switch (trimmedCode) {
|
|
3464
|
+
case "nil":
|
|
3465
|
+
return wrapScript(code, () => null);
|
|
3466
|
+
case "true":
|
|
3467
|
+
return wrapScript(code, () => true);
|
|
3468
|
+
case "false":
|
|
3469
|
+
return wrapScript(code, () => false);
|
|
3470
|
+
case "nan":
|
|
3471
|
+
return wrapScript(code, () => 0 / 0);
|
|
3472
|
+
case "inf":
|
|
3473
|
+
case "+inf":
|
|
3474
|
+
return wrapScript(code, () => 1 / 0);
|
|
3475
|
+
case "-inf":
|
|
3476
|
+
return wrapScript(code, () => -1 / 0);
|
|
3477
|
+
}
|
|
3478
|
+
if (REG_IDENTIFIER_FAST.test(trimmedCode)) {
|
|
3479
|
+
const id = trimmedCode;
|
|
3480
|
+
return wrapScript(code, (global = GlobalFallback()) => global.get(id) ?? null);
|
|
3481
|
+
}
|
|
3482
|
+
if (REG_NUMBER_FULL.test(trimmedCode)) {
|
|
3483
|
+
const num2 = Number(trimmedCode);
|
|
3484
|
+
if (!isFinite(num2)) return void 0;
|
|
3485
|
+
return wrapScript(code, () => num2);
|
|
3486
|
+
}
|
|
3487
|
+
return void 0;
|
|
3488
|
+
}
|
|
3489
|
+
var FAST_TEMPLATE_MAX_LEN = 1024;
|
|
3490
|
+
function compileTemplateFast(code, options) {
|
|
3491
|
+
if (code.length > FAST_TEMPLATE_MAX_LEN) return void 0;
|
|
3492
|
+
if (!code.includes("$")) {
|
|
3493
|
+
return wrapScript(code, () => code);
|
|
3494
|
+
}
|
|
3495
|
+
return void 0;
|
|
3496
|
+
}
|
|
3497
|
+
function compileFast(code, options) {
|
|
3498
|
+
if (options.sourceMap) return void 0;
|
|
3499
|
+
return (options.input_mode === "Template" ? compileTemplateFast : compileScriptFast)(code, options);
|
|
3500
|
+
}
|
|
3501
|
+
|
|
3502
|
+
// src/compiler/worker-manager.ts
|
|
3503
|
+
var worker;
|
|
3504
|
+
async function getWorker() {
|
|
3505
|
+
if (worker) return worker;
|
|
3506
|
+
worker = new Promise((resolve, reject) => {
|
|
3507
|
+
const w = new Worker(new URL("#compiler/worker", import.meta.url), {
|
|
3508
|
+
type: "module",
|
|
3509
|
+
name: "@mirascript/compiler"
|
|
3510
|
+
});
|
|
3511
|
+
const onError = (e) => {
|
|
3512
|
+
cleanUp();
|
|
3513
|
+
reject(new Error(`Worker failed to start: ${e.message}`));
|
|
3514
|
+
};
|
|
3515
|
+
const onMessage = (e) => {
|
|
3516
|
+
if (e.data === "ready") {
|
|
3517
|
+
cleanUp();
|
|
3518
|
+
resolve(w);
|
|
3519
|
+
} else if (e.data instanceof Error) {
|
|
3520
|
+
cleanUp();
|
|
3521
|
+
reject(e.data);
|
|
3522
|
+
}
|
|
3523
|
+
};
|
|
3524
|
+
w.addEventListener("error", onError);
|
|
3525
|
+
w.addEventListener("message", onMessage);
|
|
3526
|
+
const cleanUp = () => {
|
|
3527
|
+
w.removeEventListener("error", onError);
|
|
3528
|
+
w.removeEventListener("message", onMessage);
|
|
3529
|
+
};
|
|
3530
|
+
setTimeout(() => {
|
|
3531
|
+
onError(new ErrorEvent("error", { message: "Worker did not respond in time" }));
|
|
3532
|
+
}, 3e4);
|
|
3533
|
+
});
|
|
3534
|
+
return worker;
|
|
3535
|
+
}
|
|
3536
|
+
async function compileWorker(...args) {
|
|
3537
|
+
const worker2 = await getWorker();
|
|
3538
|
+
const seq = Math.random();
|
|
3539
|
+
worker2.postMessage([seq, ...args]);
|
|
3540
|
+
return await new Promise((resolve, reject) => {
|
|
3541
|
+
const callback = (ev) => {
|
|
3542
|
+
const data = ev.data;
|
|
3543
|
+
if (!Array.isArray(data)) return;
|
|
3544
|
+
const [retSeq, ...rest] = data;
|
|
3545
|
+
if (seq !== retSeq) return;
|
|
3546
|
+
worker2.removeEventListener("message", callback);
|
|
3547
|
+
if (rest.length === 2) {
|
|
3548
|
+
resolve(rest);
|
|
3549
|
+
} else {
|
|
3550
|
+
reject(rest[0]);
|
|
3551
|
+
}
|
|
3552
|
+
};
|
|
3553
|
+
worker2.addEventListener("message", callback);
|
|
3554
|
+
});
|
|
3555
|
+
}
|
|
3556
|
+
|
|
3557
|
+
// src/compiler/index.ts
|
|
3558
|
+
await loadModule();
|
|
3559
|
+
var WORKER_MIN_LEN = typeof Worker != "function" ? Number.MAX_VALUE : 1024;
|
|
3560
|
+
function reportDiagnostic(source, diagnostics) {
|
|
3561
|
+
const parsed = parseDiagnostics(source, diagnostics);
|
|
3562
|
+
const messages = parsed.errors.map(formatDiagnostic);
|
|
3563
|
+
throw new Error(`Failed to compile:
|
|
3564
|
+
${messages.join("\n")}`);
|
|
3565
|
+
}
|
|
3566
|
+
function emitScript(source, [code, diagnostics], options) {
|
|
3567
|
+
if (!code) {
|
|
3568
|
+
reportDiagnostic(source, diagnostics);
|
|
3569
|
+
}
|
|
3570
|
+
const target = emit(source, code, options);
|
|
3571
|
+
return createScript(source, target);
|
|
3572
|
+
}
|
|
3573
|
+
async function compile(source, options = {}) {
|
|
3574
|
+
if (typeof source == "string") {
|
|
3575
|
+
const result = compileFast(source, options);
|
|
3576
|
+
if (result) return result;
|
|
3577
|
+
}
|
|
3578
|
+
if (source.length < WORKER_MIN_LEN) {
|
|
3579
|
+
const bc = await generateBytecode(source, options);
|
|
3580
|
+
return emitScript(source, bc, options);
|
|
3581
|
+
}
|
|
3582
|
+
const [target, diagnostics] = await compileWorker(source, options);
|
|
3583
|
+
if (target == null) {
|
|
3584
|
+
reportDiagnostic(source, diagnostics);
|
|
3585
|
+
}
|
|
3586
|
+
return createScript(source, target);
|
|
3587
|
+
}
|
|
3588
|
+
function compileSync(source, options = {}) {
|
|
3589
|
+
if (typeof source == "string") {
|
|
3590
|
+
const result = compileFast(source, options);
|
|
3591
|
+
if (result) return result;
|
|
3592
|
+
}
|
|
3593
|
+
const bc = generateBytecodeSync(source, options);
|
|
3594
|
+
return emitScript(source, bc, options);
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3376
3597
|
export {
|
|
3377
|
-
isFinite,
|
|
3378
|
-
entries,
|
|
3379
|
-
defineProperty,
|
|
3380
3598
|
VmError,
|
|
3381
3599
|
operations_exports,
|
|
3382
3600
|
VmSharedContext,
|
|
@@ -3385,11 +3603,11 @@ export {
|
|
|
3385
3603
|
createVmContext,
|
|
3386
3604
|
isVmContext,
|
|
3387
3605
|
configCheckpoint,
|
|
3388
|
-
GlobalFallback,
|
|
3389
|
-
helpers_exports,
|
|
3390
3606
|
isVmFunction,
|
|
3391
3607
|
getVmFunctionInfo,
|
|
3392
3608
|
VmFunction,
|
|
3609
|
+
toVmFunctionProxy,
|
|
3610
|
+
fromVmFunctionProxy,
|
|
3393
3611
|
wrapToVmValue,
|
|
3394
3612
|
unwrapFromVmValue,
|
|
3395
3613
|
VmExtern,
|
|
@@ -3403,6 +3621,7 @@ export {
|
|
|
3403
3621
|
isVmRecord,
|
|
3404
3622
|
isVmPrimitive,
|
|
3405
3623
|
isVmExtern,
|
|
3624
|
+
isVmCallable,
|
|
3406
3625
|
isVmModule,
|
|
3407
3626
|
VM_ARRAY_MAX_LENGTH,
|
|
3408
3627
|
constants_exports,
|
|
@@ -3410,10 +3629,13 @@ export {
|
|
|
3410
3629
|
getDiagnosticMessage,
|
|
3411
3630
|
parseDiagnostics,
|
|
3412
3631
|
formatDiagnostic,
|
|
3632
|
+
emitScript,
|
|
3633
|
+
compile,
|
|
3634
|
+
compileSync,
|
|
3413
3635
|
debug_print,
|
|
3414
3636
|
lib,
|
|
3415
3637
|
serializeString,
|
|
3416
3638
|
serializePropName,
|
|
3417
3639
|
serialize
|
|
3418
3640
|
};
|
|
3419
|
-
//# sourceMappingURL=chunk-
|
|
3641
|
+
//# sourceMappingURL=chunk-AOINGBRS.js.map
|