forked from eliasku/fri3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-bridge.ts
118 lines (102 loc) · 2.6 KB
/
gen-bridge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as fs from "node:fs/promises";
const zigExports: Record<string, any> = {
onFrameRequest: {},
onPointerEvent: {},
onKeyboardEvent: {},
};
const imports: Record<string, any> = {
drawTriangles: {
sig: "(vb: [*]const u8, vb_size: u32, ib: [*]const u16, indices_count: u32, handle: u32) void",
},
playUserAudioBuffer: {
sig: "(samples: [*]const f32, length: u32, vol: f32, pan: f32, note: f32, when: f32) void",
},
setupPass: {
sig: "(id: u32) void",
},
sin: {
sig: "(x: f32) f32",
},
cos: {
sig: "(x: f32) f32",
},
pow: {
sig: "(x: f32, y: f32) f32",
},
atan2: {
sig: "(x: f32, y: f32) f32",
},
text: {
sig: "(handle: i32, x: i32, y: i32, color: u32, size: i32, msg_ptr: [*]const u8, msg_len: usize) void",
},
};
let i = 0;
for (const [k, v] of Object.entries(zigExports)) {
zigExports[k].id = String.fromCharCode("a".charCodeAt(0) + i);
++i;
}
i = 0;
for (const [k, v] of Object.entries(imports)) {
imports[k].id = String.fromCharCode("a".charCodeAt(0) + i);
++i;
}
////////
const tsImportCodeBlock = () =>
Object.entries(zigExports)
.map(([k, v]) => `\n\t_${k}: exports.${v.id} as any,`)
.join("");
const tsExportCodeBlock = () =>
Object.entries(imports)
.map(([k, v]) => `\n\t\t${v.id}: _${k},`)
.join("");
const run = async () => {
await fs.writeFile(
"ts/_bridge.ts",
`
export const importZigFunctions = (exports: WebAssembly.Exports) => ({${tsImportCodeBlock()}
_memory: exports.memory as WebAssembly.Memory,
});
export type ExportMap = {${Object.entries(imports)
.map(([k, v]) => `\n\t_${k}: Function,`)
.join("")}
};
export const createExportMap = ({${Object.entries(imports)
.map(([k, v]) => `\n\t_${k},`)
.join("")}
}: ExportMap) => ({
"0": {${tsExportCodeBlock()}
},
});
`,
"utf8",
);
await fs.writeFile(
"zig/gain/_bridge.zig",
`
const builtin = @import("builtin");
pub const enabled = builtin.cpu.arch == .wasm32 and builtin.os.tag == .freestanding;
pub const identifiers = .{${Object.entries(zigExports)
.map(([k, v]) => `\n\t"${v.id}", // _${k}`)
.join("")}
};
pub fn declareExports(comptime functions: type) void {${Object.entries(
zigExports,
)
.map(([k, v]) => `\n\t@export(functions.${k}, .{ .name = "${v.id}" });`)
.join("")}
}
pub const Imports = if (enabled) struct {${Object.entries(imports)
.map(
([k, v]) => `\n\textern "0" fn ${v.id}${v.sig};
\tpub const ${k} = ${v.id};\n`,
)
.join("")}
pub const enabled = true;
} else struct {
pub const enabled = false;
};
`,
"utf8",
);
};
run();