@mirascript/mirascript 0.1.1 → 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-2PWHYDNX.js → chunk-AOINGBRS.js} +87 -59
- package/dist/chunk-AOINGBRS.js.map +6 -0
- package/dist/cli/index.js +3 -3
- package/dist/compiler/diagnostic.d.ts +1 -1
- 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 +7 -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/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 +4 -4
- package/src/compiler/diagnostic.ts +1 -1
- package/src/helpers/utils.ts +1 -0
- package/src/vm/error.ts +15 -3
- package/src/vm/lib/_helpers.ts +2 -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/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 +1 -37
- package/src/vm/types/index.ts +9 -2
- package/dist/chunk-2PWHYDNX.js.map +0 -6
package/dist/subtle.js
CHANGED
package/dist/vm/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/vm/error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE9C;;GAEG;AACH,qBAAa,OAAQ,SAAQ,KAAK;IAG1B,QAAQ,CAAC,SAAS,EAAE,KAAK;gBADzB,OAAO,EAAE,MAAM,EACN,SAAS,EAAE,KAAK;IAM7B,cAAc;IACd,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,GAAG,OAAO;
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/vm/error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE9C;;GAEG;AACH,qBAAa,OAAQ,SAAQ,KAAK;IAG1B,QAAQ,CAAC,SAAS,EAAE,KAAK;gBADzB,OAAO,EAAE,MAAM,EACN,SAAS,EAAE,KAAK;IAM7B,cAAc;IACd,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,GAAG,OAAO;CAkBzE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_helpers.d.ts","sourceRoot":"","sources":["../../../src/vm/lib/_helpers.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"_helpers.d.ts","sourceRoot":"","sources":["../../../src/vm/lib/_helpers.ts"],"names":[],"mappings":"AAGA,OAAO,EAGH,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,OAAO,EAEZ,KAAK,QAAQ,EACb,KAAK,QAAQ,EAEb,KAAK,OAAO,EAIf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,C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CAC/E,CAAC;AACF,UAAU;AACV,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,cAAc,IAAI,CAAC,GAAG,WAAW,CAAC;AAE/E,YAAY;AACZ,wBAAgB,KAAK,CAAC,CAAC,SAAS,cAAc,EAAE,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAapF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find.d.ts","sourceRoot":"","sources":["../../../../../src/vm/lib/global/sequence/find.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,OAAO,
|
|
1
|
+
{"version":3,"file":"find.d.ts","sourceRoot":"","sources":["../../../../../src/vm/lib/global/sequence/find.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,OAAO,EAA2B,MAAM,yBAAyB,CAAC;AAChF,OAAO,EAAE,KAAK,EAAiC,MAAM,mBAAmB,CAAC;AAEzE,eAAO,MAAM,IAAI;;;;;;SA4ChB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type VmFunctionLike, type VmFunction } from './function.js';
|
|
2
|
+
import { VmExtern } from './extern.js';
|
|
3
|
+
import type { VmAny, VmConst, VmPrimitive, VmValue } from './index.js';
|
|
4
|
+
/** 创建 Mirascript 函数在宿主语言运行的代理 */
|
|
5
|
+
export declare function toVmFunctionProxy<T extends VmFunctionLike>(fn: VmFunction<T>): T;
|
|
6
|
+
/** 解开 Mirascript 函数在宿主语言运行的代理 */
|
|
7
|
+
export declare function fromVmFunctionProxy<T extends VmFunctionLike>(fn: T): VmFunction<T> | undefined;
|
|
8
|
+
/** 将宿主语言的值包装为 Mirascript 类型 */
|
|
9
|
+
export declare function wrapToVmValue(value: unknown, thisArg?: VmExtern | null, assumeVmValue?: (obj: object) => obj is Exclude<VmConst, VmPrimitive>): VmValue;
|
|
10
|
+
/** 取消宿主语言的值的 Mirascript 包装 */
|
|
11
|
+
export declare function unwrapFromVmValue(value: VmAny): unknown;
|
|
12
|
+
//# sourceMappingURL=boundary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boundary.d.ts","sourceRoot":"","sources":["../../../src/vm/types/boundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAY,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAMjF,iCAAiC;AACjC,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,cAAc,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAqBhF;AAED,iCAAiC;AACjC,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,cAAc,EAAE,EAAE,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAO9F;AAED,+BAA+B;AAC/B,wBAAgB,aAAa,CACzB,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,QAAQ,GAAG,IAAW,EAC/B,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,GAAG,IAAI,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC,GACtE,OAAO,CA0BT;AAED,+BAA+B;AAC/B,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAiBvD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/vm/types/context.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,UAAU,EACV,KAAK,KAAK,EACV,KAAK,WAAW,EAChB,KAAK,OAAO,EAGZ,KAAK,cAAc,EACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,KAAK,MAAM,MAAM,wBAAwB,CAAC;AAEtD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;AA8CH,qBAAqB;AACrB,wBAAgB,eAAe,CAC3B,GAAG,IAAI,EACD,SAAS;IACL,QAAQ,CAAC,EAAE,eAAe,GAAG,IAAI,GAAG,SAAS;IAC7C,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,GAAG,SAAS;CAC5D,GACD,SAAS;IACL,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,GAAG,SAAS;IAC5C,UAAU,CAAC,EAAE,CAAC,MAAM,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS;CAC3D,GACR,SAAS,CAuBX;AAED,iBAAiB;AACjB,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,SAAS,CAGlE"}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { VmWrapper } from './wrapper.js';
|
|
2
|
-
import type { TypeName, VmAny, VmValue } from './index.js';
|
|
3
|
-
/** 包装为 Mirascript 类型 */
|
|
4
|
-
export declare function wrapToVmValue(value: unknown, caller: VmExtern | null): VmValue;
|
|
5
|
-
/** 取消 Mirascript 类型包装 */
|
|
6
|
-
export declare function unwrapFromVmValue(value: VmAny): unknown;
|
|
2
|
+
import type { TypeName, VmAny, VmConst, VmPrimitive, VmValue } from './index.js';
|
|
7
3
|
/** 包装 Mirascript `extern` 类型的对象 */
|
|
8
4
|
export declare class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
9
|
-
readonly
|
|
10
|
-
constructor(value: T,
|
|
5
|
+
readonly thisArg: T extends (...args: readonly never[]) => unknown ? VmExtern | null : null;
|
|
6
|
+
constructor(value: T, thisArg?: T extends (...args: readonly never[]) => unknown ? VmExtern | null : null);
|
|
11
7
|
/** Check if the object has a property */
|
|
12
8
|
protected access(key: string, read: boolean): boolean;
|
|
9
|
+
/** 决定是否对属性进行包装 */
|
|
10
|
+
protected assumeVmValue(value: object, key: keyof T | undefined): value is Exclude<VmConst, VmPrimitive>;
|
|
13
11
|
/** @inheritdoc */
|
|
14
12
|
has(key: string): boolean;
|
|
15
13
|
/** @inheritdoc */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extern.d.ts","sourceRoot":"","sources":["../../../src/vm/types/extern.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"extern.d.ts","sourceRoot":"","sources":["../../../src/vm/types/extern.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAOjF,mCAAmC;AACnC,qBAAa,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,SAAS,CAAC,CAAC,CAAC;IAGnE,QAAQ,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,SAAS,KAAK,EAAE,KAAK,OAAO,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI;gBAD3F,KAAK,EAAE,CAAC,EACC,OAAO,GAAE,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,SAAS,KAAK,EAAE,KAAK,OAAO,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAW;IAKtG,yCAAyC;IACzC,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO;IAiBrD,kBAAkB;IAClB,SAAS,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,KAAK,IAAI,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC;IAIxG,kBAAkB;IACT,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAGlC,kBAAkB;IACT,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAKhC,mCAAmC;IACnC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO;IAMzC,wBAAwB;IACxB,IAAI,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,GAAG,KAAK;IAerC,kBAAkB;IACT,IAAI,IAAI,MAAM,EAAE;IAOzB,kBAAkB;IACT,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO;IAIpC,kBAAkB;IACT,QAAQ,IAAI,MAAM;IAY3B,kBAAkB;IAClB,IAAa,IAAI,IAAI,QAAQ,CAE5B;IACD,kBAAkB;IAClB,IAAa,QAAQ,IAAI,MAAM,CAuB9B;CACJ"}
|
|
@@ -41,9 +41,5 @@ export declare function isVmFunction<T extends VmFunctionLike>(value: unknown):
|
|
|
41
41
|
export declare function getVmFunctionInfo(value: unknown): VmFunctionInfo | undefined;
|
|
42
42
|
/** 创建 Mirascript 函数 */
|
|
43
43
|
export declare function VmFunction<T extends VmFunctionLike>(fn: T, option?: VmFunctionOption): VmFunction<T>;
|
|
44
|
-
/** 创建 Mirascript 函数在宿主语言运行的代理 */
|
|
45
|
-
export declare function toVmFunctionProxy<T extends VmFunctionLike>(fn: VmFunction<T>): T;
|
|
46
|
-
/** 解开 Mirascript 函数在宿主语言运行的代理 */
|
|
47
|
-
export declare function fromVmFunctionProxy<T extends VmFunctionLike>(fn: T): VmFunction<T> | undefined;
|
|
48
44
|
export {};
|
|
49
45
|
//# sourceMappingURL=function.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"function.d.ts","sourceRoot":"","sources":["../../../src/vm/types/function.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"function.d.ts","sourceRoot":"","sources":["../../../src/vm/types/function.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGjD,QAAA,MAAM,WAAW,eAAuC,CAAC;AAEzD;;;;GAIG;AAEH,MAAM,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AAED,iCAAiC;AACjC,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,GAAG,SAAS,CAG5E;AAED,uBAAuB;AACvB,wBAAgB,UAAU,CAAC,CAAC,SAAS,cAAc,EAAE,EAAE,EAAE,CAAC,EAAE,MAAM,GAAE,gBAAqB,GAAG,UAAU,CAAC,CAAC,CAAC,CAwCxG"}
|
package/dist/vm/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { VmExtern } from './extern.js';
|
|
2
|
-
import type
|
|
2
|
+
import { type VmFunction } from './function.js';
|
|
3
3
|
import { VmModule } from './module.js';
|
|
4
4
|
/** Mirascript 原始值 */
|
|
5
5
|
export type VmPrimitive = null | string | number | boolean;
|
|
@@ -61,9 +61,12 @@ export declare function isVmRecord(value: VmAny): value is VmRecord;
|
|
|
61
61
|
* 检查值是否为 Mirascript 原始值
|
|
62
62
|
*/
|
|
63
63
|
export declare function isVmPrimitive(value: unknown): value is VmPrimitive;
|
|
64
|
-
export { VmExtern
|
|
64
|
+
export { VmExtern } from './extern.js';
|
|
65
|
+
export { wrapToVmValue, unwrapFromVmValue, toVmFunctionProxy, fromVmFunctionProxy } from './boundary.js';
|
|
65
66
|
/** 检查值是否为 Mirascript 外部值 */
|
|
66
67
|
export declare function isVmExtern(value: unknown): value is VmExtern;
|
|
68
|
+
/** 检查值是否为 Mirascript 可调用值 */
|
|
69
|
+
export declare function isVmCallable(value: unknown): value is VmFunction | VmExtern<Function>;
|
|
67
70
|
export { VmFunction, isVmFunction, getVmFunctionInfo, type VmFunctionInfo, type VmFunctionLike, type VmFunctionOption, } from './function.js';
|
|
68
71
|
export { VmModule } from './module.js';
|
|
69
72
|
/** 检查值是否为 Mirascript 模块 */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/vm/types/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa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E,MAAM,aAAa,CAAC;AAExD,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE5E,eAAO,MAAM,mBAAmB,QAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirascript/mirascript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "An expression based scripting language.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"ansi-styles": "^6.2.3",
|
|
36
36
|
"commander": "^14.0.2",
|
|
37
37
|
"js-base64": "^3.7.8",
|
|
38
|
-
"@mirascript/napi": "~0.1.
|
|
39
|
-
"@mirascript/wasm": "~0.1.
|
|
38
|
+
"@mirascript/napi": "~0.1.2",
|
|
39
|
+
"@mirascript/wasm": "~0.1.2"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@types/node": "^24.9.
|
|
42
|
+
"@types/node": "^24.9.2",
|
|
43
43
|
"@types/sinon": "^17.0.4",
|
|
44
44
|
"ava": "^6.4.1",
|
|
45
45
|
"c8": "^10.1.3",
|
|
@@ -60,7 +60,7 @@ export interface SourceReference<T extends DiagnosticCode = DiagnosticCode> exte
|
|
|
60
60
|
readonly diagnostic: SourceDiagnostic<T>;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
/**
|
|
63
|
+
/** 分析诊断信息,{@link diagnostic_position_encoding} 不能设为 `None` */
|
|
64
64
|
export function parseDiagnostics(
|
|
65
65
|
source: ScriptInput,
|
|
66
66
|
diagnostics: Uint32Array,
|
package/src/helpers/utils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export const { isArray } = Array;
|
|
2
2
|
export const { isFinite, isNaN, isInteger, isSafeInteger } = Number;
|
|
3
3
|
export const { hasOwn, keys, values, entries, create, getPrototypeOf, fromEntries, defineProperty } = Object;
|
|
4
|
+
export const { apply } = Reflect;
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Determines whether an object has an enumerable property with the specified name.
|
package/src/vm/error.ts
CHANGED
|
@@ -14,9 +14,21 @@ export class VmError extends Error {
|
|
|
14
14
|
|
|
15
15
|
/** 从其他错误构造 */
|
|
16
16
|
static from(prefix: string, error: unknown, recovered: VmAny): VmError {
|
|
17
|
-
if (prefix
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (prefix) {
|
|
18
|
+
if (prefix.endsWith(':')) {
|
|
19
|
+
prefix += ' ';
|
|
20
|
+
} else if (!prefix.endsWith(': ')) {
|
|
21
|
+
prefix += ': ';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let vmError: VmError;
|
|
25
|
+
if (error instanceof Error) {
|
|
26
|
+
vmError = new VmError(`${prefix}${error.message}`, recovered);
|
|
27
|
+
vmError.stack = error.stack;
|
|
28
|
+
} else {
|
|
29
|
+
vmError = new VmError(`${prefix}${String(error)}`, recovered);
|
|
30
|
+
}
|
|
31
|
+
vmError.cause = error;
|
|
20
32
|
return vmError;
|
|
21
33
|
}
|
|
22
34
|
}
|
package/src/vm/lib/_helpers.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { VmError } from '../error.js';
|
|
|
3
3
|
import { $ToNumber, $Type } from '../operations.js';
|
|
4
4
|
import {
|
|
5
5
|
isVmArray,
|
|
6
|
-
isVmExtern,
|
|
7
6
|
isVmFunction,
|
|
8
7
|
type VmExtern,
|
|
9
8
|
type VmFunction,
|
|
@@ -17,6 +16,7 @@ import {
|
|
|
17
16
|
type VmConst,
|
|
18
17
|
isVmConst,
|
|
19
18
|
VM_ARRAY_MAX_LENGTH,
|
|
19
|
+
isVmCallable,
|
|
20
20
|
} from '../types/index.js';
|
|
21
21
|
import type { VmFunctionLike, VmFunctionOption } from '../types/function.js';
|
|
22
22
|
import { Cp } from '../helpers.js';
|
|
@@ -127,8 +127,7 @@ export function expectCallable(
|
|
|
127
127
|
recovered: VmAny | (() => VmAny),
|
|
128
128
|
): asserts value is VmFunction | VmExtern {
|
|
129
129
|
required(name, value, recovered);
|
|
130
|
-
|
|
131
|
-
if (!callable) {
|
|
130
|
+
if (!isVmCallable(value)) {
|
|
132
131
|
throwUnexpectedTypeError(name, 'callable', value, recovered);
|
|
133
132
|
}
|
|
134
133
|
}
|
package/src/vm/lib/_loader.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { VmLib, VmLibOption } from './_helpers.js';
|
|
|
6
6
|
import * as global from './global/index.js';
|
|
7
7
|
|
|
8
8
|
for (const [name, value] of entries(global)) {
|
|
9
|
-
VmSharedContext[name] = wrapEntry(name, value as RawValue);
|
|
9
|
+
VmSharedContext[name] = wrapEntry(name, value as RawValue, 'global');
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/** 原始值 */
|
|
@@ -14,7 +14,7 @@ type RawValue = VmLib | VmConst | VmModule;
|
|
|
14
14
|
/** 包装值 */
|
|
15
15
|
type ToWrappedValue<V extends RawValue> = V extends VmFunctionLike ? VmFunction<V> : V;
|
|
16
16
|
/** 包装值 */
|
|
17
|
-
function wrapEntry<const T extends RawValue>(name: string, value: T): ToWrappedValue<T> {
|
|
17
|
+
function wrapEntry<const T extends RawValue>(name: string, value: T, module: string): ToWrappedValue<T> {
|
|
18
18
|
if (typeof value == 'function') {
|
|
19
19
|
if (value.name !== name) {
|
|
20
20
|
// 如果函数名和导出名不一致,则重命名
|
|
@@ -26,7 +26,7 @@ function wrapEntry<const T extends RawValue>(name: string, value: T): ToWrappedV
|
|
|
26
26
|
return VmFunction(value, {
|
|
27
27
|
isLib: true,
|
|
28
28
|
injectCp: true,
|
|
29
|
-
fullName:
|
|
29
|
+
fullName: `${module}.${name}`,
|
|
30
30
|
...(value as VmLibOption),
|
|
31
31
|
}) as ToWrappedValue<T>;
|
|
32
32
|
} else {
|
|
@@ -43,7 +43,7 @@ export type ToWrappedModule<T extends Record<string, RawValue>> = VmModule<{
|
|
|
43
43
|
export function createModule<const T extends Record<string, RawValue>>(name: string, lib: T): ToWrappedModule<T> {
|
|
44
44
|
const mod = create(null) as Record<string, VmImmutable>;
|
|
45
45
|
for (const [key, value] of entries(lib)) {
|
|
46
|
-
mod[key] = wrapEntry(key, value);
|
|
46
|
+
mod[key] = wrapEntry(key, value, name);
|
|
47
47
|
}
|
|
48
48
|
return new VmModule(name, mod) as ToWrappedModule<T>;
|
|
49
49
|
}
|
|
@@ -15,7 +15,7 @@ export const pow = VmLib((x, y) => _pow($ToNumber(x), $ToNumber(y)), {
|
|
|
15
15
|
returnsType: 'number',
|
|
16
16
|
});
|
|
17
17
|
export const random = VmLib(Math.random, {
|
|
18
|
-
summary: '返回 0
|
|
18
|
+
summary: '返回 [0, 1) 之间的伪随机数',
|
|
19
19
|
params: {},
|
|
20
20
|
paramsType: {},
|
|
21
21
|
returnsType: 'number',
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { entries } from '../../../../helpers/utils.js';
|
|
2
2
|
import { Cp } from '../../../helpers.js';
|
|
3
|
-
import { $Call, $ToBoolean } from '../../../operations.js';
|
|
4
|
-
import { type VmValue, isVmArray } from '../../../types/index.js';
|
|
5
|
-
import { VmLib, expectArrayOrRecord,
|
|
3
|
+
import { $Call, $Same, $ToBoolean } from '../../../operations.js';
|
|
4
|
+
import { type VmValue, isVmArray, isVmCallable } from '../../../types/index.js';
|
|
5
|
+
import { VmLib, expectArrayOrRecord, required } from '../../_helpers.js';
|
|
6
6
|
|
|
7
7
|
export const find = VmLib(
|
|
8
8
|
(data, predicate) => {
|
|
9
9
|
expectArrayOrRecord('data', data, null);
|
|
10
|
-
|
|
11
|
-
const p = (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
required('predicate', predicate, null);
|
|
11
|
+
const p = isVmCallable(predicate)
|
|
12
|
+
? (value: VmValue, key: string | number, data: VmValue) => {
|
|
13
|
+
const ret = $Call(predicate, [value, key, data]);
|
|
14
|
+
return $ToBoolean(ret);
|
|
15
|
+
}
|
|
16
|
+
: (value: VmValue) => $Same(predicate, value);
|
|
15
17
|
if (isVmArray(data)) {
|
|
16
18
|
const { length } = data;
|
|
17
19
|
for (let i = 0; i < length; i++) {
|
|
@@ -37,13 +39,13 @@ export const find = VmLib(
|
|
|
37
39
|
summary: '查找数组或记录中的键值对,返回第一个满足条件的键值对',
|
|
38
40
|
params: {
|
|
39
41
|
data: '查的数组或记录',
|
|
40
|
-
predicate: '
|
|
42
|
+
predicate: '用于测试每个键值对的函数,或要查找的值',
|
|
41
43
|
},
|
|
42
44
|
paramsType: {
|
|
43
45
|
data: 'array | record',
|
|
44
|
-
predicate: 'fn(value: any, key: number | string | nil, input: type(data)) -> boolean',
|
|
46
|
+
predicate: '(fn(value: any, key: number | string | nil, input: type(data)) -> boolean) | any',
|
|
45
47
|
},
|
|
46
48
|
returnsType: '(string | number, any) | nil',
|
|
47
|
-
examples: ['find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)'],
|
|
49
|
+
examples: ['find([3, 5, 8], fn (v) { v % 2 == 0 }) // (2, 8)', `find((x: 1, y: 2, z: 3), 2) // ('y', 2)`],
|
|
48
50
|
},
|
|
49
51
|
);
|
|
@@ -49,7 +49,7 @@ export const filter = VmLib(
|
|
|
49
49
|
summary: '过滤数组或记录中的元素,返回满足条件的元素',
|
|
50
50
|
params: {
|
|
51
51
|
data: '要过滤的数组或记录',
|
|
52
|
-
predicate: '
|
|
52
|
+
predicate: '用于测试每个元素的函数',
|
|
53
53
|
},
|
|
54
54
|
paramsType: {
|
|
55
55
|
data: 'array | record',
|
|
@@ -70,7 +70,7 @@ export const filter_map = VmLib(
|
|
|
70
70
|
summary: '对数组或记录中的每个元素应用函数,并返回非 nil 的结果',
|
|
71
71
|
params: {
|
|
72
72
|
data: '要映射的数组或记录',
|
|
73
|
-
f: '
|
|
73
|
+
f: '应用于每个元素的函数',
|
|
74
74
|
},
|
|
75
75
|
paramsType: {
|
|
76
76
|
data: 'array | record',
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { isVmFunction, type VmFunctionLike, type VmFunction } from './function.js';
|
|
2
|
+
import { VmExtern } from './extern.js';
|
|
3
|
+
import { VmWrapper } from './wrapper.js';
|
|
4
|
+
import type { VmAny, VmConst, VmModule, VmPrimitive, VmValue } from './index.js';
|
|
5
|
+
import { $Call } from '../operations.js';
|
|
6
|
+
import { defineProperty, apply } from '../../helpers/utils.js';
|
|
7
|
+
|
|
8
|
+
const kProxy = Symbol.for('mirascript.vm.function.proxy');
|
|
9
|
+
|
|
10
|
+
/** 创建 Mirascript 函数在宿主语言运行的代理 */
|
|
11
|
+
export function toVmFunctionProxy<T extends VmFunctionLike>(fn: VmFunction<T>): T {
|
|
12
|
+
if (!isVmFunction(fn)) return fn;
|
|
13
|
+
|
|
14
|
+
const cached = (fn as unknown as { [kProxy]?: T })[kProxy];
|
|
15
|
+
if (cached != null) return cached;
|
|
16
|
+
|
|
17
|
+
const proxy = (...args: unknown[]) => {
|
|
18
|
+
const ret = $Call(
|
|
19
|
+
fn,
|
|
20
|
+
// 作为函数参数传入的值一定丢失了它的 this
|
|
21
|
+
args.map((v) => wrapToVmValue(v, null)),
|
|
22
|
+
);
|
|
23
|
+
return unwrapFromVmValue(ret);
|
|
24
|
+
};
|
|
25
|
+
defineProperty(fn, kProxy, { value: proxy });
|
|
26
|
+
defineProperty(proxy, kProxy, { value: fn });
|
|
27
|
+
defineProperty(proxy, 'name', {
|
|
28
|
+
value: fn.name,
|
|
29
|
+
configurable: true,
|
|
30
|
+
});
|
|
31
|
+
return proxy as T;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 解开 Mirascript 函数在宿主语言运行的代理 */
|
|
35
|
+
export function fromVmFunctionProxy<T extends VmFunctionLike>(fn: T): VmFunction<T> | undefined {
|
|
36
|
+
if (isVmFunction(fn)) return fn;
|
|
37
|
+
|
|
38
|
+
const original = (fn as unknown as { [kProxy]?: VmFunction<T> })[kProxy];
|
|
39
|
+
if (original && isVmFunction(original)) return original;
|
|
40
|
+
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 将宿主语言的值包装为 Mirascript 类型 */
|
|
45
|
+
export function wrapToVmValue(
|
|
46
|
+
value: unknown,
|
|
47
|
+
thisArg: VmExtern | null = null,
|
|
48
|
+
assumeVmValue?: (obj: object) => obj is Exclude<VmConst, VmPrimitive>,
|
|
49
|
+
): VmValue {
|
|
50
|
+
if (value == null) return null;
|
|
51
|
+
switch (typeof value) {
|
|
52
|
+
case 'function': {
|
|
53
|
+
const unwrapped = fromVmFunctionProxy(value as VmFunctionLike);
|
|
54
|
+
if (unwrapped) return unwrapped;
|
|
55
|
+
return new VmExtern(value as () => never, thisArg);
|
|
56
|
+
}
|
|
57
|
+
case 'object': {
|
|
58
|
+
if (value instanceof VmWrapper) return value as VmModule | VmExtern;
|
|
59
|
+
if (value instanceof Date) return value.valueOf();
|
|
60
|
+
if (assumeVmValue?.(value)) return value;
|
|
61
|
+
// Only functions preserve thisArg
|
|
62
|
+
return new VmExtern(value);
|
|
63
|
+
}
|
|
64
|
+
case 'string':
|
|
65
|
+
case 'number':
|
|
66
|
+
case 'boolean':
|
|
67
|
+
return value;
|
|
68
|
+
case 'bigint':
|
|
69
|
+
return Number(value);
|
|
70
|
+
case 'symbol':
|
|
71
|
+
case 'undefined':
|
|
72
|
+
default:
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 取消宿主语言的值的 Mirascript 包装 */
|
|
78
|
+
export function unwrapFromVmValue(value: VmAny): unknown {
|
|
79
|
+
if (typeof value == 'function') {
|
|
80
|
+
return toVmFunctionProxy(value);
|
|
81
|
+
}
|
|
82
|
+
if (value == null || typeof value != 'object') return value;
|
|
83
|
+
if (!(value instanceof VmExtern)) return value;
|
|
84
|
+
|
|
85
|
+
if (value.thisArg == null || typeof value.value != 'function') {
|
|
86
|
+
return value.value;
|
|
87
|
+
}
|
|
88
|
+
const f = value as VmExtern<(...args: unknown[]) => unknown>;
|
|
89
|
+
const caller = f.thisArg!.value;
|
|
90
|
+
return new Proxy(f.value, {
|
|
91
|
+
apply(target, thisArg, args): unknown {
|
|
92
|
+
return apply(target, caller, args);
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
package/src/vm/types/context.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
isVmAny,
|
|
8
8
|
type VmFunctionLike,
|
|
9
9
|
} from './index.js';
|
|
10
|
-
import { entries, keys } from '../../helpers/utils.js';
|
|
10
|
+
import { create, entries, keys } from '../../helpers/utils.js';
|
|
11
11
|
import type * as global from '../lib/global/index.js';
|
|
12
12
|
|
|
13
13
|
/** 全局导入的标准库 */
|
|
@@ -36,7 +36,7 @@ export interface VmContext {
|
|
|
36
36
|
}
|
|
37
37
|
/** MiraScript 执行上下文 */
|
|
38
38
|
export type VmContextRecord = Record<string, VmValue | undefined>;
|
|
39
|
-
export const VmSharedContext =
|
|
39
|
+
export const VmSharedContext = create(null) as VmSharedContext;
|
|
40
40
|
|
|
41
41
|
/** 定义在所有 MiraScript 执行上下文中共享的全局变量 */
|
|
42
42
|
export function defineVmContextValue(
|
|
@@ -91,7 +91,7 @@ class ValueVmContext implements VmContext {
|
|
|
91
91
|
has(key: string): boolean {
|
|
92
92
|
return key in this.env;
|
|
93
93
|
}
|
|
94
|
-
constructor(private readonly env: VmContextRecord
|
|
94
|
+
constructor(private readonly env: VmContextRecord) {}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/** 以工厂函数为后备的实现 */
|
|
@@ -139,7 +139,7 @@ export function createVmContext(
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
const [vmValues, externValues] = args;
|
|
142
|
-
const env =
|
|
142
|
+
const env = create(VmSharedContext) as VmContextRecord;
|
|
143
143
|
if (vmValues) {
|
|
144
144
|
for (const [key, value] of entries(vmValues)) {
|
|
145
145
|
if (!isVmAny(value, false)) continue;
|
package/src/vm/types/extern.ts
CHANGED
|
@@ -1,53 +1,8 @@
|
|
|
1
1
|
import { VmError } from '../error.js';
|
|
2
|
-
import { fromVmFunctionProxy, toVmFunctionProxy, type VmFunctionLike } from './function.js';
|
|
3
2
|
import { VmWrapper } from './wrapper.js';
|
|
4
|
-
import type { TypeName, VmAny,
|
|
5
|
-
import { getPrototypeOf, hasOwn } from '../../helpers/utils.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** 包装为 Mirascript 类型 */
|
|
9
|
-
export function wrapToVmValue(value: unknown, caller: VmExtern | null): VmValue {
|
|
10
|
-
if (value == null) return null;
|
|
11
|
-
switch (typeof value) {
|
|
12
|
-
case 'function': {
|
|
13
|
-
const unwrapped = fromVmFunctionProxy(value as VmFunctionLike);
|
|
14
|
-
if (unwrapped) return unwrapped;
|
|
15
|
-
return new VmExtern(value, caller);
|
|
16
|
-
}
|
|
17
|
-
case 'object':
|
|
18
|
-
if (value instanceof VmWrapper) return value as VmModule | VmExtern;
|
|
19
|
-
return new VmExtern(value, null);
|
|
20
|
-
case 'string':
|
|
21
|
-
case 'number':
|
|
22
|
-
case 'boolean':
|
|
23
|
-
return value;
|
|
24
|
-
case 'bigint':
|
|
25
|
-
return Number(value);
|
|
26
|
-
case 'symbol':
|
|
27
|
-
case 'undefined':
|
|
28
|
-
default:
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** 取消 Mirascript 类型包装 */
|
|
34
|
-
export function unwrapFromVmValue(value: VmAny): unknown {
|
|
35
|
-
if (typeof value == 'function') {
|
|
36
|
-
return toVmFunctionProxy(value);
|
|
37
|
-
}
|
|
38
|
-
if (value == null || typeof value != 'object') return value;
|
|
39
|
-
if (!(value instanceof VmExtern)) return value;
|
|
40
|
-
if (value.caller == null || typeof value.value != 'function') {
|
|
41
|
-
return value.value;
|
|
42
|
-
}
|
|
43
|
-
const caller = value.caller.value;
|
|
44
|
-
const func = value.value as (...args: unknown[]) => unknown;
|
|
45
|
-
return new Proxy(func, {
|
|
46
|
-
apply(target, thisArg, args): unknown {
|
|
47
|
-
return apply(target, caller, args);
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
}
|
|
3
|
+
import type { TypeName, VmAny, VmConst, VmPrimitive, VmValue } from './index.js';
|
|
4
|
+
import { getPrototypeOf, hasOwn, apply } from '../../helpers/utils.js';
|
|
5
|
+
import { unwrapFromVmValue, wrapToVmValue } from './boundary.js';
|
|
51
6
|
|
|
52
7
|
const ObjectPrototype = Object.prototype;
|
|
53
8
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
@@ -56,7 +11,7 @@ const ObjectToString = ObjectPrototype.toString;
|
|
|
56
11
|
export class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
57
12
|
constructor(
|
|
58
13
|
value: T,
|
|
59
|
-
readonly
|
|
14
|
+
readonly thisArg: T extends (...args: readonly never[]) => unknown ? VmExtern | null : null = null,
|
|
60
15
|
) {
|
|
61
16
|
super(value);
|
|
62
17
|
}
|
|
@@ -79,6 +34,11 @@ export class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
|
79
34
|
return true;
|
|
80
35
|
}
|
|
81
36
|
|
|
37
|
+
/** 决定是否对属性进行包装 */
|
|
38
|
+
protected assumeVmValue(value: object, key: keyof T | undefined): value is Exclude<VmConst, VmPrimitive> {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
82
42
|
/** @inheritdoc */
|
|
83
43
|
override has(key: string): boolean {
|
|
84
44
|
return this.access(key, true);
|
|
@@ -87,7 +47,7 @@ export class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
|
87
47
|
override get(key: string): VmAny {
|
|
88
48
|
if (!this.has(key)) return undefined;
|
|
89
49
|
const prop = (this.value as Record<string, unknown>)[key];
|
|
90
|
-
return wrapToVmValue(prop, this);
|
|
50
|
+
return wrapToVmValue(prop, this, (v) => this.assumeVmValue(v, key as keyof T));
|
|
91
51
|
}
|
|
92
52
|
/** Set a property on the object */
|
|
93
53
|
set(key: string, value: VmValue): boolean {
|
|
@@ -99,14 +59,18 @@ export class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
|
99
59
|
/** Call extern value */
|
|
100
60
|
call(args: readonly VmValue[]): VmAny {
|
|
101
61
|
const { value } = this;
|
|
102
|
-
|
|
62
|
+
if (typeof value != 'function') {
|
|
63
|
+
throw VmError.from(`Not a callable extern`, null, null);
|
|
64
|
+
}
|
|
65
|
+
const caller = this.thisArg?.value ?? null;
|
|
103
66
|
const unwrappedArgs = args.map(unwrapFromVmValue);
|
|
67
|
+
let ret: unknown;
|
|
104
68
|
try {
|
|
105
|
-
|
|
106
|
-
return wrapToVmValue(ret, null);
|
|
69
|
+
ret = apply(value, caller, unwrappedArgs);
|
|
107
70
|
} catch (ex) {
|
|
108
|
-
throw VmError.from(`
|
|
71
|
+
throw VmError.from(`Callable extern`, ex, null);
|
|
109
72
|
}
|
|
73
|
+
return wrapToVmValue(ret, null, (obj) => this.assumeVmValue(obj, undefined));
|
|
110
74
|
}
|
|
111
75
|
/** @inheritdoc */
|
|
112
76
|
override keys(): string[] {
|
|
@@ -119,7 +83,7 @@ export class VmExtern<const T extends object = object> extends VmWrapper<T> {
|
|
|
119
83
|
/** @inheritdoc */
|
|
120
84
|
override same(other: VmAny): boolean {
|
|
121
85
|
if (!(other instanceof VmExtern)) return false;
|
|
122
|
-
return this.value === other.value && this.
|
|
86
|
+
return this.value === other.value && this.thisArg === other.thisArg;
|
|
123
87
|
}
|
|
124
88
|
/** @inheritdoc */
|
|
125
89
|
override toString(): string {
|