@mirascript/wasm 0.1.0

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.
Binary file
@@ -0,0 +1,48 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const __wbg_compileresult_free: (a: number, b: number) => void;
5
+ export const compileresult_chunk: (a: number, b: number) => void;
6
+ export const compileresult_diagnostics: (a: number, b: number) => void;
7
+ export const compile_buffer: (a: number, b: number, c: number) => number;
8
+ export const compile: (a: number, b: number, c: number) => number;
9
+ export const get_diagnostic_message: (a: number, b: number) => void;
10
+ export const keywords: (a: number) => void;
11
+ export const control_keywords: (a: number) => void;
12
+ export const numeric_keywords: (a: number) => void;
13
+ export const constant_keywords: (a: number) => void;
14
+ export const reserved_keywords: (a: number) => void;
15
+ export const __wbg_monacocompiler_free: (a: number, b: number) => void;
16
+ export const monacocompiler_new: (a: number, b: number, c: number) => number;
17
+ export const monacocompiler_parse: (a: number) => number;
18
+ export const monacocompiler_emit: (a: number, b: number) => void;
19
+ export const monacocompiler_format: (a: number, b: number) => void;
20
+ export const monacocompiler_diagnostics: (a: number, b: number) => void;
21
+ export const main: () => void;
22
+ export const __wbg_config_free: (a: number, b: number) => void;
23
+ export const __wbg_get_config_track_references: (a: number) => number;
24
+ export const __wbg_set_config_track_references: (a: number, b: number) => void;
25
+ export const __wbg_get_config_trivia: (a: number) => number;
26
+ export const __wbg_set_config_trivia: (a: number, b: number) => void;
27
+ export const __wbg_get_config_input_mode: (a: number) => number;
28
+ export const __wbg_set_config_input_mode: (a: number, b: number) => void;
29
+ export const __wbg_get_config_diagnostic_position_encoding: (a: number) => number;
30
+ export const __wbg_set_config_diagnostic_position_encoding: (a: number, b: number) => void;
31
+ export const __wbg_get_config_diagnostic_error: (a: number) => number;
32
+ export const __wbg_set_config_diagnostic_error: (a: number, b: number) => void;
33
+ export const __wbg_get_config_diagnostic_warning: (a: number) => number;
34
+ export const __wbg_set_config_diagnostic_warning: (a: number, b: number) => void;
35
+ export const __wbg_get_config_diagnostic_info: (a: number) => number;
36
+ export const __wbg_set_config_diagnostic_info: (a: number, b: number) => void;
37
+ export const __wbg_get_config_diagnostic_hint: (a: number) => number;
38
+ export const __wbg_set_config_diagnostic_hint: (a: number, b: number) => void;
39
+ export const __wbg_get_config_diagnostic_reference: (a: number) => number;
40
+ export const __wbg_set_config_diagnostic_reference: (a: number, b: number) => void;
41
+ export const __wbg_get_config_diagnostic_other: (a: number) => number;
42
+ export const __wbg_set_config_diagnostic_other: (a: number, b: number) => void;
43
+ export const config_new: () => number;
44
+ export const __wbindgen_export_0: (a: number, b: number, c: number) => void;
45
+ export const __wbindgen_export_1: (a: number, b: number) => number;
46
+ export const __wbindgen_export_2: (a: number, b: number, c: number, d: number) => number;
47
+ export const __wbindgen_add_to_stack_pointer: (a: number) => number;
48
+ export const __wbindgen_start: () => void;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@mirascript/wasm",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "MiraScript compiler for WebAssembly",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": "./dist/index.js",
10
+ "./types": "./dist/types.js"
11
+ },
12
+ "imports": {
13
+ "#loader": {
14
+ "node": "./dist/loader/node.js",
15
+ "default": "./dist/loader/web.js"
16
+ }
17
+ },
18
+ "devDependencies": {
19
+ "nodemon": "^3.1.10"
20
+ },
21
+ "scripts": {
22
+ "wasm": "wasm-pack build --target web --out-dir ../../packages/wasm/lib --no-pack ../../crates/wasm",
23
+ "watch": "nodemon -e rs,js,ts --watch ../../crates/wasm --watch ../../crates/core --watch ./src --exec 'pnpm build:dev'",
24
+ "build:dev": "pnpm wasm --dev && tsc",
25
+ "build": "pnpm wasm --release && tsc",
26
+ "clean": "rimraf dist lib"
27
+ }
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,71 @@
1
+ import * as wasm from '../lib/wasm.js';
2
+ import type { Config, InputMode, DiagnosticPositionEncoding, ScriptInput } from './types.js';
3
+
4
+ export * from './types.js';
5
+ export { wasm };
6
+
7
+ export const ready = import('#loader').then(async ({ module }) =>
8
+ wasm.default({
9
+ module_or_path: await module,
10
+ }),
11
+ );
12
+
13
+ /** 创建可重用的配置 */
14
+ export function createConfig(config?: Config | wasm.Config): wasm.Config {
15
+ if (!config) return new wasm.Config();
16
+ if (config instanceof wasm.Config) return config;
17
+ const cfg = new wasm.Config();
18
+ for (const key in config) {
19
+ if (key === 'free') continue; // 忽略 free 方法
20
+ if (!Object.hasOwn(config, key)) continue;
21
+ let value = config[key as keyof Config] as never;
22
+ if (key === 'input_mode') {
23
+ value = wasm.InputMode[value as InputMode] satisfies wasm.InputMode as never;
24
+ }
25
+ if (key === 'diagnostic_position_encoding') {
26
+ value = wasm.DiagnosticPositionEncoding[
27
+ value as DiagnosticPositionEncoding
28
+ ] satisfies wasm.DiagnosticPositionEncoding as never;
29
+ }
30
+ if (value === undefined) continue;
31
+ if (!(key in cfg)) continue;
32
+ cfg[key as keyof Config] = value;
33
+ }
34
+ return cfg;
35
+ }
36
+
37
+ /** 编译结果 */
38
+ export interface CompileResult {
39
+ /** 编译诊断 */
40
+ readonly diagnostics: Uint32Array;
41
+ /** 编译生成的字节码 */
42
+ readonly chunk?: Uint8Array;
43
+ }
44
+
45
+ /** 编译 */
46
+ function compileImpl<T>(
47
+ compiler: (script: T, config: wasm.Config) => wasm.CompileResult,
48
+ script: T,
49
+ config: Config | wasm.Config,
50
+ ): CompileResult {
51
+ const cfg = createConfig(config);
52
+ const result = compiler(script, cfg);
53
+ try {
54
+ const diagnostics = result.diagnostics();
55
+ const chunk = result.chunk();
56
+ return { diagnostics, chunk };
57
+ } finally {
58
+ result.free();
59
+ // 只在 cfg 是新创建的情况下释放
60
+ if (cfg !== config) {
61
+ cfg.free();
62
+ }
63
+ }
64
+ }
65
+
66
+ /** 编译 MiraScript 代码 */
67
+ export function compileSync(script: ScriptInput, config: Config | wasm.Config): CompileResult {
68
+ return typeof script == 'string'
69
+ ? compileImpl(wasm.compile, script, config)
70
+ : compileImpl(wasm.compile_buffer, script, config);
71
+ }
@@ -0,0 +1,5 @@
1
+ import fs from 'node:fs/promises';
2
+ import type { InitInput } from '../../lib/wasm.js';
3
+
4
+ const file = /* @__PURE__ */ new URL('../../lib/wasm_bg.wasm', import.meta.url);
5
+ export const module: Promise<InitInput> = /* @__PURE__ */ fs.readFile(file);
@@ -0,0 +1,17 @@
1
+ import type { InitInput } from '../../lib/wasm.js';
2
+
3
+ /** 回退加载 */
4
+ async function loadFallback() {
5
+ const fallbackUrl =
6
+ (document?.currentScript instanceof HTMLScriptElement
7
+ ? document.currentScript.src
8
+ : (document.currentScript?.href?.baseVal ?? '')) || document.location.href;
9
+ return await fetch(new URL('../../lib/wasm_bg.wasm', fallbackUrl));
10
+ }
11
+
12
+ export const module: Promise<InitInput> = /* @__PURE__ */ (async () => {
13
+ if (!import.meta.url) {
14
+ return await loadFallback();
15
+ }
16
+ return await fetch(new URL('../../lib/wasm_bg.wasm?url', import.meta.url));
17
+ })();
package/src/types.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type * as wasm from '../lib/wasm.js';
2
+ export { DiagnosticCode, OpCode } from '../lib/wasm.js';
3
+
4
+ /**
5
+ * 配置选项
6
+ */
7
+ export type Config = Partial<Omit<wasm.Config, 'free' | 'input_mode' | 'diagnostic_position_encoding'>> & {
8
+ input_mode?: InputMode;
9
+ diagnostic_position_encoding?: DiagnosticPositionEncoding;
10
+ };
11
+ /** Encoding for counting positions in diagnostics. */
12
+ export type DiagnosticPositionEncoding = keyof typeof wasm.DiagnosticPositionEncoding;
13
+ /** 输入模式 */
14
+ export type InputMode = keyof typeof wasm.InputMode;
15
+ /** 编译输入,支持字符串和 UTF-8 字节数组 */
16
+ export type ScriptInput = string | Uint8Array;