@mirascript/mirascript 0.1.1 → 0.1.3
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-2PWHYDNX.js → chunk-MVHCSH3E.js} +239 -122
- package/dist/chunk-MVHCSH3E.js.map +6 -0
- package/dist/cli/index.js +3 -3
- package/dist/compiler/diagnostic.d.ts +12 -3
- package/dist/compiler/diagnostic.d.ts.map +1 -1
- package/dist/helpers/utils.d.ts +1 -0
- package/dist/helpers/utils.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/subtle.js +1 -1
- 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/lib/global/sequence/with.d.ts +2 -2
- package/dist/vm/lib/global/sequence/with.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/checker.d.ts +1 -1
- package/dist/vm/types/checker.d.ts.map +1 -1
- package/dist/vm/types/context.d.ts +14 -6
- package/dist/vm/types/context.d.ts.map +1 -1
- package/dist/vm/types/extern.d.ts +7 -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 +9 -10
- package/dist/vm/types/index.d.ts.map +1 -1
- package/dist/vm/types/module.d.ts +2 -0
- package/dist/vm/types/module.d.ts.map +1 -1
- package/dist/vm/types/wrapper.d.ts +2 -0
- package/dist/vm/types/wrapper.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/compiler/diagnostic.ts +14 -12
- package/src/helpers/utils.ts +1 -0
- package/src/vm/error.ts +15 -3
- package/src/vm/lib/_helpers.ts +4 -13
- 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/sequence/with.ts +111 -17
- package/src/vm/operations.ts +23 -24
- package/src/vm/types/boundary.ts +95 -0
- package/src/vm/types/checker.ts +8 -15
- package/src/vm/types/context.ts +31 -19
- package/src/vm/types/extern.ts +30 -57
- package/src/vm/types/function.ts +1 -37
- package/src/vm/types/index.ts +12 -24
- package/src/vm/types/module.ts +7 -0
- package/src/vm/types/wrapper.ts +7 -0
- package/dist/chunk-2PWHYDNX.js.map +0 -6
|
@@ -19,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
|
|
|
19
19
|
var { isArray } = Array;
|
|
20
20
|
var { isFinite, isNaN, isInteger, isSafeInteger } = Number;
|
|
21
21
|
var { hasOwn, keys, values, entries, create, getPrototypeOf, fromEntries, defineProperty } = Object;
|
|
22
|
+
var { apply } = Reflect;
|
|
22
23
|
var hasOwnEnumerable = Function.call.bind(
|
|
23
24
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
24
25
|
Object.prototype.propertyIsEnumerable
|
|
@@ -33,13 +34,45 @@ var VmError = class _VmError extends Error {
|
|
|
33
34
|
}
|
|
34
35
|
/** 从其他错误构造 */
|
|
35
36
|
static from(prefix, error, recovered) {
|
|
36
|
-
if (prefix
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
39
52
|
return vmError;
|
|
40
53
|
}
|
|
41
54
|
};
|
|
42
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
|
+
var kVmWrapper = Symbol.for("mirascript.vm.wrapper");
|
|
71
|
+
Object.defineProperty(VmWrapper.prototype, kVmWrapper, { value: true });
|
|
72
|
+
function isVmWrapper(value) {
|
|
73
|
+
return value != null && typeof value == "object" && kVmWrapper in value;
|
|
74
|
+
}
|
|
75
|
+
|
|
43
76
|
// src/vm/helpers.ts
|
|
44
77
|
var helpers_exports = {};
|
|
45
78
|
__export(helpers_exports, {
|
|
@@ -108,23 +141,6 @@ __export(operations_exports, {
|
|
|
108
141
|
$ToString: () => $ToString,
|
|
109
142
|
$Type: () => $Type
|
|
110
143
|
});
|
|
111
|
-
|
|
112
|
-
// src/vm/types/wrapper.ts
|
|
113
|
-
var VmWrapper = class {
|
|
114
|
-
constructor(value) {
|
|
115
|
-
this.value = value;
|
|
116
|
-
}
|
|
117
|
-
/** Convert the object to JSON */
|
|
118
|
-
toJSON() {
|
|
119
|
-
return void 0;
|
|
120
|
-
}
|
|
121
|
-
/** 转为字符串 */
|
|
122
|
-
toString() {
|
|
123
|
-
return `<${this.type} ${this.describe}>`;
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// src/vm/operations.ts
|
|
128
144
|
var { abs, min, trunc, ceil } = Math;
|
|
129
145
|
var { slice, at } = Array.prototype;
|
|
130
146
|
var { POSITIVE_INFINITY, NEGATIVE_INFINITY } = Number;
|
|
@@ -132,8 +148,8 @@ var isSame = (a, b) => {
|
|
|
132
148
|
if (typeof a == "number" && typeof b == "number") return a === b || isNaN(a) && isNaN(b);
|
|
133
149
|
if (a === b) return true;
|
|
134
150
|
if (a == null || typeof a != "object" || b == null || typeof b != "object") return false;
|
|
135
|
-
if (a
|
|
136
|
-
if (b
|
|
151
|
+
if (isVmWrapper(a)) return a.same(b);
|
|
152
|
+
if (isVmWrapper(b)) return b.same(a);
|
|
137
153
|
if (isVmArray(a) && isVmArray(b)) {
|
|
138
154
|
const len2 = a.length;
|
|
139
155
|
if (len2 !== b.length) {
|
|
@@ -230,11 +246,6 @@ var $Eq = (a, b) => {
|
|
|
230
246
|
var $Neq = (a, b) => {
|
|
231
247
|
return !$Eq(a, b);
|
|
232
248
|
};
|
|
233
|
-
var stringComparer = new Intl.Collator("en", {
|
|
234
|
-
usage: "sort",
|
|
235
|
-
numeric: false,
|
|
236
|
-
sensitivity: "base"
|
|
237
|
-
});
|
|
238
249
|
var $Aeq = (a, b) => {
|
|
239
250
|
if (overloadNumberString(a, b)) {
|
|
240
251
|
const an = $ToNumber(a);
|
|
@@ -250,7 +261,12 @@ var $Aeq = (a, b) => {
|
|
|
250
261
|
const as = $ToString(a);
|
|
251
262
|
const bs = $ToString(b);
|
|
252
263
|
if (as === bs) return true;
|
|
253
|
-
|
|
264
|
+
const ai = as.toLowerCase();
|
|
265
|
+
const bi = bs.toLowerCase();
|
|
266
|
+
if (ai === bi) return true;
|
|
267
|
+
const an = ai.normalize("NFC");
|
|
268
|
+
const bn = bi.normalize("NFC");
|
|
269
|
+
return an === bn;
|
|
254
270
|
}
|
|
255
271
|
};
|
|
256
272
|
var $Naeq = (a, b) => {
|
|
@@ -279,7 +295,7 @@ var $In = (value, iterable) => {
|
|
|
279
295
|
value;
|
|
280
296
|
return iterable.some((item = null) => isSame(item, value));
|
|
281
297
|
}
|
|
282
|
-
if (iterable
|
|
298
|
+
if (isVmWrapper(iterable)) return iterable.has($ToString(value));
|
|
283
299
|
if (typeof iterable == "object") return hasOwnEnumerable(iterable, $ToString(value));
|
|
284
300
|
iterable;
|
|
285
301
|
return false;
|
|
@@ -300,7 +316,7 @@ var $Length = (value) => {
|
|
|
300
316
|
$AssertInit(value);
|
|
301
317
|
if (isVmArray(value)) return value.length;
|
|
302
318
|
if (isVmRecord(value)) return keys(value).length;
|
|
303
|
-
if (value
|
|
319
|
+
if (isVmWrapper(value)) {
|
|
304
320
|
return value.keys().length;
|
|
305
321
|
}
|
|
306
322
|
return Number.NaN;
|
|
@@ -365,7 +381,7 @@ var $Call = (func, args) => {
|
|
|
365
381
|
for (const a of args) {
|
|
366
382
|
$AssertInit(a);
|
|
367
383
|
}
|
|
368
|
-
if (func
|
|
384
|
+
if (isVmExtern(func)) {
|
|
369
385
|
return func.call(args) ?? null;
|
|
370
386
|
}
|
|
371
387
|
if (isVmFunction(func)) {
|
|
@@ -375,8 +391,8 @@ var $Call = (func, args) => {
|
|
|
375
391
|
};
|
|
376
392
|
var $Type = (value) => {
|
|
377
393
|
if (value === void 0 || value === null) return "nil";
|
|
378
|
-
if (value
|
|
379
|
-
if (value
|
|
394
|
+
if (isVmExtern(value)) return "extern";
|
|
395
|
+
if (isVmModule(value)) return "module";
|
|
380
396
|
if (isVmArray(value)) return "array";
|
|
381
397
|
if (typeof value == "object") return "record";
|
|
382
398
|
return typeof value;
|
|
@@ -393,7 +409,7 @@ function numberToString(value) {
|
|
|
393
409
|
}
|
|
394
410
|
function innerToString(value, useBraces) {
|
|
395
411
|
if (value == null) return "nil";
|
|
396
|
-
if (value
|
|
412
|
+
if (isVmWrapper(value)) return value.toString();
|
|
397
413
|
if (typeof value == "function") {
|
|
398
414
|
const name = getVmFunctionInfo(value)?.fullName;
|
|
399
415
|
return name ? `<function ${name}>` : `<function>`;
|
|
@@ -470,7 +486,7 @@ var $Has = (obj, key) => {
|
|
|
470
486
|
$AssertInit(obj);
|
|
471
487
|
const pk = $ToString(key);
|
|
472
488
|
if (obj == null || typeof obj != "object") return false;
|
|
473
|
-
if (obj
|
|
489
|
+
if (isVmWrapper(obj)) return obj.has(pk);
|
|
474
490
|
return hasOwnEnumerable(obj, pk);
|
|
475
491
|
};
|
|
476
492
|
var $Get = (obj, key) => {
|
|
@@ -482,7 +498,7 @@ var $Get = (obj, key) => {
|
|
|
482
498
|
}
|
|
483
499
|
const pk = $ToString(key);
|
|
484
500
|
if (obj == null || typeof obj != "object") return null;
|
|
485
|
-
if (obj
|
|
501
|
+
if (isVmWrapper(obj)) return obj.get(pk) ?? null;
|
|
486
502
|
if (!hasOwnEnumerable(obj, pk)) return null;
|
|
487
503
|
return obj[pk] ?? null;
|
|
488
504
|
};
|
|
@@ -496,7 +512,7 @@ var $Set = (obj, key, value) => {
|
|
|
496
512
|
};
|
|
497
513
|
var $Iterable = (value) => {
|
|
498
514
|
$AssertInit(value);
|
|
499
|
-
if (value
|
|
515
|
+
if (isVmWrapper(value)) return value.keys();
|
|
500
516
|
if (isVmArray(value)) return value;
|
|
501
517
|
if (value != null && typeof value == "object") return keys(value);
|
|
502
518
|
throw new VmError(`Value is not iterable`, isVmFunction(value) ? [] : [value]);
|
|
@@ -672,7 +688,6 @@ function GlobalFallback() {
|
|
|
672
688
|
|
|
673
689
|
// src/vm/types/function.ts
|
|
674
690
|
var kVmFunction = Symbol.for("mirascript.vm.function");
|
|
675
|
-
var kProxy = Symbol.for("mirascript.vm.function.proxy");
|
|
676
691
|
function isVmFunction(value) {
|
|
677
692
|
return getVmFunctionInfo(value) != null;
|
|
678
693
|
}
|
|
@@ -718,10 +733,13 @@ function VmFunction(fn, option = {}) {
|
|
|
718
733
|
});
|
|
719
734
|
return fn;
|
|
720
735
|
}
|
|
736
|
+
|
|
737
|
+
// src/vm/types/boundary.ts
|
|
738
|
+
var kProxy = Symbol.for("mirascript.vm.function.proxy");
|
|
721
739
|
function toVmFunctionProxy(fn) {
|
|
722
740
|
if (!isVmFunction(fn)) return fn;
|
|
723
741
|
const cached = fn[kProxy];
|
|
724
|
-
if (cached) return cached;
|
|
742
|
+
if (cached != null) return cached;
|
|
725
743
|
const proxy = (...args) => {
|
|
726
744
|
const ret = $Call(
|
|
727
745
|
fn,
|
|
@@ -744,20 +762,20 @@ function fromVmFunctionProxy(fn) {
|
|
|
744
762
|
if (original && isVmFunction(original)) return original;
|
|
745
763
|
return void 0;
|
|
746
764
|
}
|
|
747
|
-
|
|
748
|
-
// src/vm/types/extern.ts
|
|
749
|
-
var { apply } = Reflect;
|
|
750
|
-
function wrapToVmValue(value, caller) {
|
|
765
|
+
function wrapToVmValue(value, thisArg = null, assumeVmValue) {
|
|
751
766
|
if (value == null) return null;
|
|
752
767
|
switch (typeof value) {
|
|
753
768
|
case "function": {
|
|
754
769
|
const unwrapped = fromVmFunctionProxy(value);
|
|
755
770
|
if (unwrapped) return unwrapped;
|
|
756
|
-
return new VmExtern(value,
|
|
771
|
+
return new VmExtern(value, thisArg);
|
|
772
|
+
}
|
|
773
|
+
case "object": {
|
|
774
|
+
if (isVmWrapper(value)) return value;
|
|
775
|
+
if (value instanceof Date) return value.valueOf();
|
|
776
|
+
if (assumeVmValue?.(value)) return value;
|
|
777
|
+
return new VmExtern(value);
|
|
757
778
|
}
|
|
758
|
-
case "object":
|
|
759
|
-
if (value instanceof VmWrapper) return value;
|
|
760
|
-
return new VmExtern(value, null);
|
|
761
779
|
case "string":
|
|
762
780
|
case "number":
|
|
763
781
|
case "boolean":
|
|
@@ -775,24 +793,27 @@ function unwrapFromVmValue(value) {
|
|
|
775
793
|
return toVmFunctionProxy(value);
|
|
776
794
|
}
|
|
777
795
|
if (value == null || typeof value != "object") return value;
|
|
778
|
-
if (!(value
|
|
779
|
-
if (value.
|
|
796
|
+
if (!isVmExtern(value)) return value;
|
|
797
|
+
if (value.thisArg == null || typeof value.value != "function") {
|
|
780
798
|
return value.value;
|
|
781
799
|
}
|
|
782
|
-
const
|
|
783
|
-
const
|
|
784
|
-
return new Proxy(
|
|
800
|
+
const f = value;
|
|
801
|
+
const caller = f.thisArg.value;
|
|
802
|
+
return new Proxy(f.value, {
|
|
785
803
|
apply(target, thisArg, args) {
|
|
786
804
|
return apply(target, caller, args);
|
|
787
805
|
}
|
|
788
806
|
});
|
|
789
807
|
}
|
|
808
|
+
|
|
809
|
+
// src/vm/types/extern.ts
|
|
790
810
|
var ObjectPrototype = Object.prototype;
|
|
791
811
|
var ObjectToString = ObjectPrototype.toString;
|
|
792
|
-
var
|
|
793
|
-
|
|
812
|
+
var FunctionToString = Function.prototype.toString;
|
|
813
|
+
var VmExtern = class extends VmWrapper {
|
|
814
|
+
constructor(value, thisArg = null) {
|
|
794
815
|
super(value);
|
|
795
|
-
this.
|
|
816
|
+
this.thisArg = thisArg;
|
|
796
817
|
}
|
|
797
818
|
/** Check if the object has a property */
|
|
798
819
|
access(key, read) {
|
|
@@ -809,6 +830,10 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
809
830
|
if (key in Object.prototype && prop === Object.prototype[key]) return false;
|
|
810
831
|
return true;
|
|
811
832
|
}
|
|
833
|
+
/** 决定是否对属性进行包装 */
|
|
834
|
+
assumeVmValue(value, key) {
|
|
835
|
+
return false;
|
|
836
|
+
}
|
|
812
837
|
/** @inheritdoc */
|
|
813
838
|
has(key) {
|
|
814
839
|
return this.access(key, true);
|
|
@@ -817,7 +842,7 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
817
842
|
get(key) {
|
|
818
843
|
if (!this.has(key)) return void 0;
|
|
819
844
|
const prop = this.value[key];
|
|
820
|
-
return wrapToVmValue(prop, this);
|
|
845
|
+
return wrapToVmValue(prop, this, (v) => this.assumeVmValue(v, key));
|
|
821
846
|
}
|
|
822
847
|
/** Set a property on the object */
|
|
823
848
|
set(key, value) {
|
|
@@ -829,14 +854,18 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
829
854
|
/** Call extern value */
|
|
830
855
|
call(args) {
|
|
831
856
|
const { value } = this;
|
|
832
|
-
|
|
857
|
+
if (typeof value != "function") {
|
|
858
|
+
throw VmError.from(`Not a callable extern`, null, null);
|
|
859
|
+
}
|
|
860
|
+
const caller = this.thisArg?.value ?? null;
|
|
833
861
|
const unwrappedArgs = args.map(unwrapFromVmValue);
|
|
862
|
+
let ret;
|
|
834
863
|
try {
|
|
835
|
-
|
|
836
|
-
return wrapToVmValue(ret, null);
|
|
864
|
+
ret = apply(value, caller, unwrappedArgs);
|
|
837
865
|
} catch (ex) {
|
|
838
|
-
throw VmError.from(`
|
|
866
|
+
throw VmError.from(`Callable extern`, ex, null);
|
|
839
867
|
}
|
|
868
|
+
return wrapToVmValue(ret, null, (obj) => this.assumeVmValue(obj, void 0));
|
|
840
869
|
}
|
|
841
870
|
/** @inheritdoc */
|
|
842
871
|
keys() {
|
|
@@ -848,13 +877,13 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
848
877
|
}
|
|
849
878
|
/** @inheritdoc */
|
|
850
879
|
same(other) {
|
|
851
|
-
if (!(other
|
|
852
|
-
return this.value === other.value && this.
|
|
880
|
+
if (!isVmExtern(other)) return false;
|
|
881
|
+
return this.value === other.value && this.thisArg === other.thisArg;
|
|
853
882
|
}
|
|
854
883
|
/** @inheritdoc */
|
|
855
884
|
toString() {
|
|
856
885
|
const { toString } = this.value;
|
|
857
|
-
if (typeof toString != "function" || toString === ObjectToString) {
|
|
886
|
+
if (typeof toString != "function" || toString === ObjectToString || toString === FunctionToString) {
|
|
858
887
|
return super.toString();
|
|
859
888
|
}
|
|
860
889
|
try {
|
|
@@ -892,6 +921,11 @@ var VmExtern = class _VmExtern extends VmWrapper {
|
|
|
892
921
|
return tag;
|
|
893
922
|
}
|
|
894
923
|
};
|
|
924
|
+
var kVmExtern = Symbol.for("mirascript.vm.extern");
|
|
925
|
+
Object.defineProperty(VmExtern.prototype, kVmExtern, { value: true });
|
|
926
|
+
function isVmExtern(value) {
|
|
927
|
+
return value != null && typeof value == "object" && kVmExtern in value;
|
|
928
|
+
}
|
|
895
929
|
|
|
896
930
|
// src/vm/types/module.ts
|
|
897
931
|
var VmModule = class extends VmWrapper {
|
|
@@ -925,6 +959,11 @@ var VmModule = class extends VmWrapper {
|
|
|
925
959
|
return this.name;
|
|
926
960
|
}
|
|
927
961
|
};
|
|
962
|
+
var kVmModule = Symbol.for("mirascript.vm.module");
|
|
963
|
+
Object.defineProperty(VmModule.prototype, kVmModule, { value: true });
|
|
964
|
+
function isVmModule(value) {
|
|
965
|
+
return value != null && typeof value == "object" && kVmModule in value;
|
|
966
|
+
}
|
|
928
967
|
|
|
929
968
|
// src/vm/types/script.ts
|
|
930
969
|
var kVmScript = Symbol.for("mirascript.vm.script");
|
|
@@ -967,7 +1006,7 @@ function isVmConstInner(value, depth) {
|
|
|
967
1006
|
return true;
|
|
968
1007
|
case "object":
|
|
969
1008
|
if (value == null) return true;
|
|
970
|
-
if (value
|
|
1009
|
+
if (isVmWrapper(value)) return false;
|
|
971
1010
|
if (isArray(value)) {
|
|
972
1011
|
return isVmArrayDeep(value, depth);
|
|
973
1012
|
} else {
|
|
@@ -988,7 +1027,7 @@ function isVmConst(value, checkDeep = false) {
|
|
|
988
1027
|
return true;
|
|
989
1028
|
case "object":
|
|
990
1029
|
if (value == null) return true;
|
|
991
|
-
if (value
|
|
1030
|
+
if (isVmWrapper(value)) return false;
|
|
992
1031
|
if (!checkDeep) {
|
|
993
1032
|
if (isArray(value)) {
|
|
994
1033
|
return isVmArrayDeep(value, 0);
|
|
@@ -1007,7 +1046,7 @@ function isVmConst(value, checkDeep = false) {
|
|
|
1007
1046
|
}
|
|
1008
1047
|
}
|
|
1009
1048
|
function isVmImmutable(value, checkDeep = false) {
|
|
1010
|
-
return value
|
|
1049
|
+
return isVmModule(value) || isVmFunction(value) || isVmConst(value, checkDeep);
|
|
1011
1050
|
}
|
|
1012
1051
|
function isVmValue(value, checkDeep = false) {
|
|
1013
1052
|
if (value === void 0) return false;
|
|
@@ -1022,7 +1061,7 @@ function isVmAny(value, checkDeep) {
|
|
|
1022
1061
|
return true;
|
|
1023
1062
|
case "object":
|
|
1024
1063
|
if (value == null) return true;
|
|
1025
|
-
if (value
|
|
1064
|
+
if (isVmWrapper(value)) return true;
|
|
1026
1065
|
return isVmConst(value, checkDeep);
|
|
1027
1066
|
case "function":
|
|
1028
1067
|
return isVmFunction(value);
|
|
@@ -1041,7 +1080,7 @@ function isVmArray(value) {
|
|
|
1041
1080
|
}
|
|
1042
1081
|
function isVmRecord(value) {
|
|
1043
1082
|
if (value == null || typeof value !== "object") return false;
|
|
1044
|
-
if (value
|
|
1083
|
+
if (isVmWrapper(value)) return false;
|
|
1045
1084
|
if (isVmArray(value)) return false;
|
|
1046
1085
|
value;
|
|
1047
1086
|
return true;
|
|
@@ -1054,17 +1093,14 @@ function isVmPrimitive(value) {
|
|
|
1054
1093
|
}
|
|
1055
1094
|
return false;
|
|
1056
1095
|
}
|
|
1057
|
-
function
|
|
1058
|
-
return value
|
|
1059
|
-
}
|
|
1060
|
-
function isVmModule(value) {
|
|
1061
|
-
return value instanceof VmModule;
|
|
1096
|
+
function isVmCallable(value) {
|
|
1097
|
+
return isVmFunction(value) || isVmExtern(value) && typeof value.value == "function";
|
|
1062
1098
|
}
|
|
1063
1099
|
var VM_ARRAY_MAX_LENGTH = 2 ** 31 - 1;
|
|
1064
1100
|
|
|
1065
1101
|
// src/vm/types/context.ts
|
|
1066
1102
|
var kVmContext = Symbol.for("mira:vm-context");
|
|
1067
|
-
var VmSharedContext =
|
|
1103
|
+
var VmSharedContext = create(null);
|
|
1068
1104
|
function defineVmContextValue(name, value, override = false) {
|
|
1069
1105
|
if (!override && name in VmSharedContext) throw new Error(`Global variable '${name}' is already defined.`);
|
|
1070
1106
|
let v;
|
|
@@ -1096,8 +1132,9 @@ var DefaultVmContext = Object.freeze({
|
|
|
1096
1132
|
var _a;
|
|
1097
1133
|
_a = kVmContext;
|
|
1098
1134
|
var ValueVmContext = class {
|
|
1099
|
-
constructor(env) {
|
|
1135
|
+
constructor(env, describe) {
|
|
1100
1136
|
this.env = env;
|
|
1137
|
+
this.describe = describe;
|
|
1101
1138
|
this[_a] = true;
|
|
1102
1139
|
this.cachedKeys = null;
|
|
1103
1140
|
}
|
|
@@ -1118,9 +1155,10 @@ var ValueVmContext = class {
|
|
|
1118
1155
|
var _a2;
|
|
1119
1156
|
_a2 = kVmContext;
|
|
1120
1157
|
var FactoryVmContext = class {
|
|
1121
|
-
constructor(getter, enumerator) {
|
|
1158
|
+
constructor(getter, enumerator, describe) {
|
|
1122
1159
|
this.getter = getter;
|
|
1123
1160
|
this.enumerator = enumerator;
|
|
1161
|
+
this.describe = describe;
|
|
1124
1162
|
this[_a2] = true;
|
|
1125
1163
|
}
|
|
1126
1164
|
/** @inheritdoc */
|
|
@@ -1144,10 +1182,11 @@ function createVmContext(...args) {
|
|
|
1144
1182
|
return { ...DefaultVmContext };
|
|
1145
1183
|
}
|
|
1146
1184
|
if (typeof args[0] == "function") {
|
|
1147
|
-
|
|
1185
|
+
const [getter, enumerator, describer2] = args;
|
|
1186
|
+
return new FactoryVmContext(getter, enumerator ?? void 0, describer2 ?? void 0);
|
|
1148
1187
|
}
|
|
1149
|
-
const [vmValues, externValues] = args;
|
|
1150
|
-
const env =
|
|
1188
|
+
const [vmValues, externValues, describer] = args;
|
|
1189
|
+
const env = create(VmSharedContext);
|
|
1151
1190
|
if (vmValues) {
|
|
1152
1191
|
for (const [key, value] of entries(vmValues)) {
|
|
1153
1192
|
if (!isVmAny(value, false)) continue;
|
|
@@ -1159,7 +1198,7 @@ function createVmContext(...args) {
|
|
|
1159
1198
|
env[key] = value == null ? null : wrapToVmValue(value, null);
|
|
1160
1199
|
}
|
|
1161
1200
|
}
|
|
1162
|
-
return new ValueVmContext(env);
|
|
1201
|
+
return new ValueVmContext(env, describer ?? void 0);
|
|
1163
1202
|
}
|
|
1164
1203
|
function isVmContext(context) {
|
|
1165
1204
|
if (context == null || typeof context != "object") return false;
|
|
@@ -1302,8 +1341,7 @@ function expectConst(name, value, recovered) {
|
|
|
1302
1341
|
}
|
|
1303
1342
|
function expectCallable(name, value, recovered) {
|
|
1304
1343
|
required(name, value, recovered);
|
|
1305
|
-
|
|
1306
|
-
if (!callable) {
|
|
1344
|
+
if (!isVmCallable(value)) {
|
|
1307
1345
|
throwUnexpectedTypeError(name, "callable", value, recovered);
|
|
1308
1346
|
}
|
|
1309
1347
|
}
|
|
@@ -1334,11 +1372,7 @@ function map(data, mapper) {
|
|
|
1334
1372
|
Cp();
|
|
1335
1373
|
const ret = mapper(data[i] ?? null, i, data);
|
|
1336
1374
|
if (ret === void 0) continue;
|
|
1337
|
-
|
|
1338
|
-
result.push(ret);
|
|
1339
|
-
} else {
|
|
1340
|
-
result.push(null);
|
|
1341
|
-
}
|
|
1375
|
+
result.push(ret);
|
|
1342
1376
|
}
|
|
1343
1377
|
return result;
|
|
1344
1378
|
} else {
|
|
@@ -1347,11 +1381,7 @@ function map(data, mapper) {
|
|
|
1347
1381
|
Cp();
|
|
1348
1382
|
const ret = mapper(value ?? null, key, data);
|
|
1349
1383
|
if (ret === void 0) continue;
|
|
1350
|
-
|
|
1351
|
-
e.push([key, ret]);
|
|
1352
|
-
} else {
|
|
1353
|
-
e.push([key, null]);
|
|
1354
|
-
}
|
|
1384
|
+
e.push([key, ret]);
|
|
1355
1385
|
}
|
|
1356
1386
|
return fromEntries(e);
|
|
1357
1387
|
}
|
|
@@ -1665,7 +1695,7 @@ var pow2 = VmLib((x, y) => _pow($ToNumber(x), $ToNumber(y)), {
|
|
|
1665
1695
|
returnsType: "number"
|
|
1666
1696
|
});
|
|
1667
1697
|
var random = VmLib(Math.random, {
|
|
1668
|
-
summary: "返回 0
|
|
1698
|
+
summary: "返回 [0, 1) 之间的伪随机数",
|
|
1669
1699
|
params: {},
|
|
1670
1700
|
paramsType: {},
|
|
1671
1701
|
returnsType: "number"
|
|
@@ -1758,40 +1788,123 @@ var shr = VmLib(
|
|
|
1758
1788
|
);
|
|
1759
1789
|
|
|
1760
1790
|
// src/vm/lib/global/sequence/with.ts
|
|
1791
|
+
var arrIndex = (index) => {
|
|
1792
|
+
const idx = Math.trunc($ToNumber(index));
|
|
1793
|
+
if (!isSafeInteger(idx) || idx < 0 || idx >= VM_ARRAY_MAX_LENGTH) {
|
|
1794
|
+
return -1;
|
|
1795
|
+
}
|
|
1796
|
+
return idx;
|
|
1797
|
+
};
|
|
1798
|
+
var withInner = (obj, key, keyIndex, value) => {
|
|
1799
|
+
if (keyIndex >= key.length) {
|
|
1800
|
+
return value;
|
|
1801
|
+
}
|
|
1802
|
+
const k = key[keyIndex];
|
|
1803
|
+
let result;
|
|
1804
|
+
if (isVmArray(obj)) {
|
|
1805
|
+
result = [...obj];
|
|
1806
|
+
} else if (isVmRecord(obj)) {
|
|
1807
|
+
result = { ...obj };
|
|
1808
|
+
} else if (arrIndex(k) === k) {
|
|
1809
|
+
result = [];
|
|
1810
|
+
} else {
|
|
1811
|
+
result = {};
|
|
1812
|
+
}
|
|
1813
|
+
if (isArray(result)) {
|
|
1814
|
+
const index = arrIndex(k);
|
|
1815
|
+
while (index > result.length) {
|
|
1816
|
+
result.push(null);
|
|
1817
|
+
}
|
|
1818
|
+
result[index] = withInner(result[index], key, keyIndex + 1, value);
|
|
1819
|
+
} else {
|
|
1820
|
+
const prop = $ToString(k);
|
|
1821
|
+
result[prop] = withInner(result[prop], key, keyIndex + 1, value);
|
|
1822
|
+
}
|
|
1823
|
+
return result;
|
|
1824
|
+
};
|
|
1825
|
+
var normalizeEntries = (data, entries3) => {
|
|
1826
|
+
if (entries3.length % 2 !== 0) {
|
|
1827
|
+
throwError("Expected even number of entries", data);
|
|
1828
|
+
}
|
|
1829
|
+
const entryData = /* @__PURE__ */ new Map();
|
|
1830
|
+
for (let i = 0; i < entries3.length; i += 2) {
|
|
1831
|
+
let key = entries3[i];
|
|
1832
|
+
expectConst("key", key, data);
|
|
1833
|
+
if (key == null) {
|
|
1834
|
+
continue;
|
|
1835
|
+
}
|
|
1836
|
+
if (isVmArray(key)) {
|
|
1837
|
+
if (key.length === 0 || key.includes(null) || key.includes(void 0)) {
|
|
1838
|
+
continue;
|
|
1839
|
+
} else if (key.length === 1) {
|
|
1840
|
+
key = key[0];
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
const value = entries3[i + 1];
|
|
1844
|
+
entryData.set(key, Element(value));
|
|
1845
|
+
}
|
|
1846
|
+
return entryData;
|
|
1847
|
+
};
|
|
1761
1848
|
var _with = VmLib(
|
|
1762
1849
|
(data, ...entries3) => {
|
|
1763
1850
|
expectArrayOrRecord("data", data, data);
|
|
1764
|
-
if (entries3.length
|
|
1765
|
-
|
|
1851
|
+
if (entries3.length === 0) {
|
|
1852
|
+
return data;
|
|
1766
1853
|
}
|
|
1854
|
+
const entryData = normalizeEntries(data, entries3);
|
|
1767
1855
|
if (isVmArray(data)) {
|
|
1768
1856
|
const result = [...data];
|
|
1769
|
-
for (
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1857
|
+
for (const [key, value] of entryData) {
|
|
1858
|
+
let index;
|
|
1859
|
+
let val;
|
|
1860
|
+
if (isVmArray(key)) {
|
|
1861
|
+
index = arrIndex(key[0]);
|
|
1862
|
+
if (index < 0) continue;
|
|
1863
|
+
val = withInner(result[index], key, 1, value);
|
|
1864
|
+
} else {
|
|
1865
|
+
index = arrIndex(key);
|
|
1866
|
+
if (index < 0) continue;
|
|
1867
|
+
val = value;
|
|
1868
|
+
}
|
|
1773
1869
|
while (index > result.length) {
|
|
1774
1870
|
result.push(null);
|
|
1775
1871
|
}
|
|
1776
|
-
result[index] =
|
|
1872
|
+
result[index] = val;
|
|
1777
1873
|
}
|
|
1778
1874
|
return result;
|
|
1779
1875
|
} else {
|
|
1780
1876
|
const result = { ...data };
|
|
1781
|
-
for (
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1877
|
+
for (const [key, value] of entryData) {
|
|
1878
|
+
let prop;
|
|
1879
|
+
let val;
|
|
1880
|
+
if (isVmArray(key)) {
|
|
1881
|
+
const firstKey = key[0];
|
|
1882
|
+
prop = $ToString(firstKey);
|
|
1883
|
+
val = withInner(result[prop], key, 1, value);
|
|
1884
|
+
} else {
|
|
1885
|
+
prop = $ToString(key);
|
|
1886
|
+
val = value;
|
|
1887
|
+
}
|
|
1888
|
+
result[prop] = val;
|
|
1785
1889
|
}
|
|
1786
1890
|
return result;
|
|
1787
1891
|
}
|
|
1788
1892
|
},
|
|
1789
1893
|
{
|
|
1790
1894
|
summary: "在数组或记录中设置多个键值对",
|
|
1791
|
-
params: {
|
|
1792
|
-
|
|
1895
|
+
params: {
|
|
1896
|
+
data: "要设置的数组或记录",
|
|
1897
|
+
"..entries": "要设置的键值对,成对出现"
|
|
1898
|
+
},
|
|
1899
|
+
paramsType: {
|
|
1900
|
+
data: "array | record",
|
|
1901
|
+
"..entries": `[..[string | number | (string | number)[], any][]]`
|
|
1902
|
+
},
|
|
1793
1903
|
returnsType: "type(data)",
|
|
1794
|
-
examples: [
|
|
1904
|
+
examples: [
|
|
1905
|
+
`with([10, 20], 2, 99, 3 ,100) // [10, 20, 99, 100]`,
|
|
1906
|
+
`(a: 1)::with(["b", 1], 2) // (a: 1, b: [nil, 2])`
|
|
1907
|
+
]
|
|
1795
1908
|
}
|
|
1796
1909
|
);
|
|
1797
1910
|
|
|
@@ -1908,7 +2021,7 @@ var filter = VmLib(
|
|
|
1908
2021
|
summary: "过滤数组或记录中的元素,返回满足条件的元素",
|
|
1909
2022
|
params: {
|
|
1910
2023
|
data: "要过滤的数组或记录",
|
|
1911
|
-
predicate: "
|
|
2024
|
+
predicate: "用于测试每个元素的函数"
|
|
1912
2025
|
},
|
|
1913
2026
|
paramsType: {
|
|
1914
2027
|
data: "array | record",
|
|
@@ -1927,7 +2040,7 @@ var filter_map = VmLib(
|
|
|
1927
2040
|
summary: "对数组或记录中的每个元素应用函数,并返回非 nil 的结果",
|
|
1928
2041
|
params: {
|
|
1929
2042
|
data: "要映射的数组或记录",
|
|
1930
|
-
f: "
|
|
2043
|
+
f: "应用于每个元素的函数"
|
|
1931
2044
|
},
|
|
1932
2045
|
paramsType: {
|
|
1933
2046
|
data: "array | record",
|
|
@@ -1942,11 +2055,11 @@ var filter_map = VmLib(
|
|
|
1942
2055
|
var find = VmLib(
|
|
1943
2056
|
(data, predicate) => {
|
|
1944
2057
|
expectArrayOrRecord("data", data, null);
|
|
1945
|
-
|
|
1946
|
-
const p = (value, key, data2) => {
|
|
2058
|
+
required("predicate", predicate, null);
|
|
2059
|
+
const p = isVmCallable(predicate) ? (value, key, data2) => {
|
|
1947
2060
|
const ret = $Call(predicate, [value, key, data2]);
|
|
1948
2061
|
return $ToBoolean(ret);
|
|
1949
|
-
};
|
|
2062
|
+
} : (value) => $Same(predicate, value);
|
|
1950
2063
|
if (isVmArray(data)) {
|
|
1951
2064
|
const { length } = data;
|
|
1952
2065
|
for (let i = 0; i < length; i++) {
|
|
@@ -1972,14 +2085,14 @@ var find = VmLib(
|
|
|
1972
2085
|
summary: "查找数组或记录中的键值对,返回第一个满足条件的键值对",
|
|
1973
2086
|
params: {
|
|
1974
2087
|
data: "查的数组或记录",
|
|
1975
|
-
predicate: "
|
|
2088
|
+
predicate: "用于测试每个键值对的函数,或要查找的值"
|
|
1976
2089
|
},
|
|
1977
2090
|
paramsType: {
|
|
1978
2091
|
data: "array | record",
|
|
1979
|
-
predicate: "fn(value: any, key: number | string | nil, input: type(data)) -> boolean"
|
|
2092
|
+
predicate: "(fn(value: any, key: number | string | nil, input: type(data)) -> boolean) | any"
|
|
1980
2093
|
},
|
|
1981
2094
|
returnsType: "(string | number, any) | nil",
|
|
1982
|
-
examples: ["find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)"]
|
|
2095
|
+
examples: ["find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)", `find((x: 1, y: 2, z: 3), 2) // ('y', 2)`]
|
|
1983
2096
|
}
|
|
1984
2097
|
);
|
|
1985
2098
|
|
|
@@ -3064,9 +3177,9 @@ var matrix = createModule("matrix", matrix_exports);
|
|
|
3064
3177
|
|
|
3065
3178
|
// src/vm/lib/_loader.ts
|
|
3066
3179
|
for (const [name, value] of entries(global_exports)) {
|
|
3067
|
-
VmSharedContext[name] = wrapEntry(name, value);
|
|
3180
|
+
VmSharedContext[name] = wrapEntry(name, value, "global");
|
|
3068
3181
|
}
|
|
3069
|
-
function wrapEntry(name, value) {
|
|
3182
|
+
function wrapEntry(name, value, module) {
|
|
3070
3183
|
if (typeof value == "function") {
|
|
3071
3184
|
if (value.name !== name) {
|
|
3072
3185
|
defineProperty(value, "name", {
|
|
@@ -3077,7 +3190,7 @@ function wrapEntry(name, value) {
|
|
|
3077
3190
|
return VmFunction(value, {
|
|
3078
3191
|
isLib: true,
|
|
3079
3192
|
injectCp: true,
|
|
3080
|
-
fullName:
|
|
3193
|
+
fullName: `${module}.${name}`,
|
|
3081
3194
|
...value
|
|
3082
3195
|
});
|
|
3083
3196
|
} else {
|
|
@@ -3087,7 +3200,7 @@ function wrapEntry(name, value) {
|
|
|
3087
3200
|
function createModule(name, lib2) {
|
|
3088
3201
|
const mod = create(null);
|
|
3089
3202
|
for (const [key, value] of entries(lib2)) {
|
|
3090
|
-
mod[key] = wrapEntry(key, value);
|
|
3203
|
+
mod[key] = wrapEntry(key, value, name);
|
|
3091
3204
|
}
|
|
3092
3205
|
return new VmModule(name, mod);
|
|
3093
3206
|
}
|
|
@@ -3571,6 +3684,7 @@ function compileSync(source, options = {}) {
|
|
|
3571
3684
|
|
|
3572
3685
|
export {
|
|
3573
3686
|
VmError,
|
|
3687
|
+
isVmWrapper,
|
|
3574
3688
|
operations_exports,
|
|
3575
3689
|
VmSharedContext,
|
|
3576
3690
|
defineVmContextValue,
|
|
@@ -3581,10 +3695,14 @@ export {
|
|
|
3581
3695
|
isVmFunction,
|
|
3582
3696
|
getVmFunctionInfo,
|
|
3583
3697
|
VmFunction,
|
|
3698
|
+
toVmFunctionProxy,
|
|
3699
|
+
fromVmFunctionProxy,
|
|
3584
3700
|
wrapToVmValue,
|
|
3585
3701
|
unwrapFromVmValue,
|
|
3586
3702
|
VmExtern,
|
|
3703
|
+
isVmExtern,
|
|
3587
3704
|
VmModule,
|
|
3705
|
+
isVmModule,
|
|
3588
3706
|
isVmScript,
|
|
3589
3707
|
isVmConst,
|
|
3590
3708
|
isVmImmutable,
|
|
@@ -3593,8 +3711,7 @@ export {
|
|
|
3593
3711
|
isVmArray,
|
|
3594
3712
|
isVmRecord,
|
|
3595
3713
|
isVmPrimitive,
|
|
3596
|
-
|
|
3597
|
-
isVmModule,
|
|
3714
|
+
isVmCallable,
|
|
3598
3715
|
VM_ARRAY_MAX_LENGTH,
|
|
3599
3716
|
constants_exports,
|
|
3600
3717
|
DiagnosticCode,
|
|
@@ -3610,4 +3727,4 @@ export {
|
|
|
3610
3727
|
serializePropName,
|
|
3611
3728
|
serialize
|
|
3612
3729
|
};
|
|
3613
|
-
//# sourceMappingURL=chunk-
|
|
3730
|
+
//# sourceMappingURL=chunk-MVHCSH3E.js.map
|