@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 CHANGED
@@ -2,8 +2,8 @@
2
2
  export declare class ModuleLoader<T> {
3
3
  private readonly loader;
4
4
  constructor(loader: () => Promise<T>);
5
- private module;
6
5
  private loading;
6
+ private result;
7
7
  /** 加载模块 */
8
8
  readonly load: () => Promise<T>;
9
9
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,YAAY;AACZ,qBAAa,YAAY,CAAC,CAAC;IACX,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IACrD,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,OAAO,CAAyB;IAExC,WAAW;IACX,QAAQ,CAAC,IAAI,QAAa,OAAO,CAAC,CAAC,CAAC,CA0BlC;IACF;;;OAGG;IACH,QAAQ,CAAC,GAAG,QAAO,CAAC,CAMlB;CACL"}
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 { module, loading, loader } = this;
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 = (async () => {
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 { module } = this;
37
- if (module == null) {
50
+ const { result } = this;
51
+ if (result == null) {
38
52
  throw new Error('MiraScript compiler module is not loaded.');
39
53
  }
40
- return module;
54
+ return result.get();
41
55
  };
42
56
  }
43
57
  }
@@ -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;QAIr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CAAC;gBACpB,CAAC;gBACD,OAAO,CAAC,CAAC;YACb,CAAC,CAAC,CAAC;YACH,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;YACjB,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC;QACF;;;WAGG;QACM,QAAG,GAAG,GAAM,EAAE;YACnB,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;YACxB,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;YACjE,CAAC;YACD,OAAO,MAAM,CAAC,GAAG,EAAE,CAAC;QACxB,CAAC,CAAC;IA9BsD,CAAC;CA+B5D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirascript/bindings",
3
- "version": "0.1.76",
3
+ "version": "0.1.79",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "description": "MiraScript compiler bindings",
@@ -32,11 +32,11 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "@mirascript/napi": "~0.1.76",
36
- "@mirascript/wasm": "~0.1.76"
35
+ "@mirascript/wasm": "~0.1.79",
36
+ "@mirascript/napi": "~0.1.79"
37
37
  },
38
38
  "devDependencies": {
39
- "@types/node": "^26.1.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 module: T | undefined;
5
- private loading: Promise<T> | undefined;
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 { module, loading, loader } = this;
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 = (async () => {
14
- const mod = await loader();
15
- this.module = mod;
16
- return mod;
17
- })();
18
- // Use .then() rather than .finally() to make Node.js happy
19
- // Avoid Unhandled rejection
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 { module } = this;
41
- if (module == null) {
50
+ const { result } = this;
51
+ if (result == null) {
42
52
  throw new Error('MiraScript compiler module is not loaded.');
43
53
  }
44
- return module;
54
+ return result.get();
45
55
  };
46
56
  }