@mirascript/bindings 0.1.76 → 0.1.79
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/loader.d.ts +1 -1
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +34 -20
- package/dist/loader.js.map +1 -1
- package/package.json +4 -4
- package/src/loader.ts +38 -28
package/dist/loader.d.ts
CHANGED
package/dist/loader.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAsBA,YAAY;AACZ,qBAAa,YAAY,CAAC,CAAC;IACX,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IACrD,OAAO,CAAC,OAAO,CAAuC;IACtD,OAAO,CAAC,MAAM,CAA8B;IAE5C,WAAW;IACX,QAAQ,CAAC,IAAI,QAAa,OAAO,CAAC,CAAC,CAAC,CAclC;IACF;;;OAGG;IACH,QAAQ,CAAC,GAAG,QAAO,CAAC,CAMlB;CACL"}
|
package/dist/loader.js
CHANGED
|
@@ -1,43 +1,57 @@
|
|
|
1
|
+
/** 模块加载结果 */
|
|
2
|
+
class ModuleResult {
|
|
3
|
+
constructor(module, error) {
|
|
4
|
+
this.module = module;
|
|
5
|
+
this.error = error;
|
|
6
|
+
}
|
|
7
|
+
/** 获取模块,如果模块未加载则抛出错误 */
|
|
8
|
+
get() {
|
|
9
|
+
if (this.module != null)
|
|
10
|
+
return this.module;
|
|
11
|
+
throw this.error ?? new Error('Module is not loaded.');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** 运行模块加载器 */
|
|
15
|
+
async function runLoader(loader) {
|
|
16
|
+
try {
|
|
17
|
+
const module = await loader();
|
|
18
|
+
return new ModuleResult(module, undefined);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return new ModuleResult(undefined, error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
1
24
|
/** 模块加载器 */
|
|
2
25
|
export class ModuleLoader {
|
|
3
26
|
constructor(loader) {
|
|
4
27
|
this.loader = loader;
|
|
5
28
|
/** 加载模块 */
|
|
6
29
|
this.load = async () => {
|
|
7
|
-
const {
|
|
8
|
-
if (module != null)
|
|
9
|
-
return module;
|
|
30
|
+
const { result, loading, loader } = this;
|
|
31
|
+
if (result?.module != null)
|
|
32
|
+
return result.module;
|
|
10
33
|
if (loading != null)
|
|
11
|
-
return loading;
|
|
12
|
-
const l = (
|
|
13
|
-
const mod = await loader();
|
|
14
|
-
this.module = mod;
|
|
15
|
-
return mod;
|
|
16
|
-
})();
|
|
17
|
-
// Use .then() rather than .finally() to make Node.js happy
|
|
18
|
-
// Avoid Unhandled rejection
|
|
19
|
-
void l.then(() => {
|
|
20
|
-
if (this.loading === l) {
|
|
21
|
-
this.loading = undefined;
|
|
22
|
-
}
|
|
23
|
-
}, () => {
|
|
34
|
+
return loading.then((r) => r.get());
|
|
35
|
+
const l = runLoader(loader).then((r) => {
|
|
24
36
|
if (this.loading === l) {
|
|
25
37
|
this.loading = undefined;
|
|
38
|
+
this.result = r;
|
|
26
39
|
}
|
|
40
|
+
return r;
|
|
27
41
|
});
|
|
28
42
|
this.loading = l;
|
|
29
|
-
return l;
|
|
43
|
+
return l.then((r) => r.get());
|
|
30
44
|
};
|
|
31
45
|
/**
|
|
32
46
|
* 获取已加载的模块
|
|
33
47
|
* @throws {Error} 模块尚未加载
|
|
34
48
|
*/
|
|
35
49
|
this.get = () => {
|
|
36
|
-
const {
|
|
37
|
-
if (
|
|
50
|
+
const { result } = this;
|
|
51
|
+
if (result == null) {
|
|
38
52
|
throw new Error('MiraScript compiler module is not loaded.');
|
|
39
53
|
}
|
|
40
|
-
return
|
|
54
|
+
return result.get();
|
|
41
55
|
};
|
|
42
56
|
}
|
|
43
57
|
}
|
package/dist/loader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,YAAY;AACZ,MAAM,OAAO,YAAY;IACrB,YAA6B,MAAwB;QAAxB,WAAM,GAAN,MAAM,CAAkB;QAIrD,WAAW;QACF,SAAI,GAAG,KAAK,IAAgB,EAAE;YACnC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;YACzC,IAAI,MAAM,IAAI,IAAI;gBAAE,OAAO,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,MAAM,YAAY;IACd,YACa,MAAqB,EACrB,KAAwB;QADxB,WAAM,GAAN,MAAM,CAAe;QACrB,UAAK,GAAL,KAAK,CAAmB;IAClC,CAAC;IACJ,wBAAwB;IACxB,GAAG;QACC,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC;QAC5C,MAAM,IAAI,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3D,CAAC;CACJ;AAED,cAAc;AACd,KAAK,UAAU,SAAS,CAAI,MAAwB;IAChD,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,MAAM,EAAE,CAAC;QAC9B,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,IAAI,YAAY,CAAI,SAAS,EAAE,KAAc,CAAC,CAAC;IAC1D,CAAC;AACL,CAAC;AACD,YAAY;AACZ,MAAM,OAAO,YAAY;IACrB,YAA6B,MAAwB;QAAxB,WAAM,GAAN,MAAM,CAAkB;QAIrD,WAAW;QACF,SAAI,GAAG,K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
"description": "MiraScript compiler bindings",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@mirascript/
|
|
36
|
-
"@mirascript/
|
|
35
|
+
"@mirascript/wasm": "~0.1.79",
|
|
36
|
+
"@mirascript/napi": "~0.1.79"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^26.1.
|
|
39
|
+
"@types/node": "^26.1.2"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"watch": "pnpm build --watch",
|
package/src/loader.ts
CHANGED
|
@@ -1,46 +1,56 @@
|
|
|
1
|
+
/** 模块加载结果 */
|
|
2
|
+
class ModuleResult<T> {
|
|
3
|
+
constructor(
|
|
4
|
+
readonly module: T | undefined,
|
|
5
|
+
readonly error: Error | undefined,
|
|
6
|
+
) {}
|
|
7
|
+
/** 获取模块,如果模块未加载则抛出错误 */
|
|
8
|
+
get(): T {
|
|
9
|
+
if (this.module != null) return this.module;
|
|
10
|
+
throw this.error ?? new Error('Module is not loaded.');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 运行模块加载器 */
|
|
15
|
+
async function runLoader<T>(loader: () => Promise<T>): Promise<ModuleResult<T>> {
|
|
16
|
+
try {
|
|
17
|
+
const module = await loader();
|
|
18
|
+
return new ModuleResult(module, undefined);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
return new ModuleResult<T>(undefined, error as Error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
1
23
|
/** 模块加载器 */
|
|
2
24
|
export class ModuleLoader<T> {
|
|
3
25
|
constructor(private readonly loader: () => Promise<T>) {}
|
|
4
|
-
private
|
|
5
|
-
private
|
|
26
|
+
private loading: Promise<ModuleResult<T>> | undefined;
|
|
27
|
+
private result: ModuleResult<T> | undefined;
|
|
6
28
|
|
|
7
29
|
/** 加载模块 */
|
|
8
30
|
readonly load = async (): Promise<T> => {
|
|
9
|
-
const {
|
|
10
|
-
if (module != null) return module;
|
|
11
|
-
if (loading != null) return loading;
|
|
31
|
+
const { result, loading, loader } = this;
|
|
32
|
+
if (result?.module != null) return result.module;
|
|
33
|
+
if (loading != null) return loading.then((r) => r.get());
|
|
12
34
|
|
|
13
|
-
const l = (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
void l.then(
|
|
21
|
-
() => {
|
|
22
|
-
if (this.loading === l) {
|
|
23
|
-
this.loading = undefined;
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
() => {
|
|
27
|
-
if (this.loading === l) {
|
|
28
|
-
this.loading = undefined;
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
);
|
|
35
|
+
const l = runLoader(loader).then((r) => {
|
|
36
|
+
if (this.loading === l) {
|
|
37
|
+
this.loading = undefined;
|
|
38
|
+
this.result = r;
|
|
39
|
+
}
|
|
40
|
+
return r;
|
|
41
|
+
});
|
|
32
42
|
this.loading = l;
|
|
33
|
-
return l;
|
|
43
|
+
return l.then((r) => r.get());
|
|
34
44
|
};
|
|
35
45
|
/**
|
|
36
46
|
* 获取已加载的模块
|
|
37
47
|
* @throws {Error} 模块尚未加载
|
|
38
48
|
*/
|
|
39
49
|
readonly get = (): T => {
|
|
40
|
-
const {
|
|
41
|
-
if (
|
|
50
|
+
const { result } = this;
|
|
51
|
+
if (result == null) {
|
|
42
52
|
throw new Error('MiraScript compiler module is not loaded.');
|
|
43
53
|
}
|
|
44
|
-
return
|
|
54
|
+
return result.get();
|
|
45
55
|
};
|
|
46
56
|
}
|