-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.wasm.mjs
286 lines (263 loc) · 7.02 KB
/
patch.wasm.mjs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { readFileSync, rmSync, writeFileSync } from "fs";
const cargo = readFileSync(`./src/wasm/Cargo.toml`, "utf8")
const packp = cargo.split("\n\n").find(p => p.startsWith("[package]"))
const namel = packp.split("\n").find(l => l.startsWith("name = "))
const name = namel.split(" = ")[1].replaceAll('"', "").trim()
const wasm = readFileSync(`./src/wasm/pkg/${name}_bg.wasm`)
writeFileSync(`./src/wasm/pkg/${name}.wasm.js`, `export const data = "data:application/wasm;base64,${wasm.toString("base64")}";`);
writeFileSync(`./src/wasm/pkg/${name}.wasm.d.ts`, `export const data: string;`);
const beforeMemoryJs = `export class Memory {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Memory.prototype);
obj.__wbg_ptr = ptr;
MemoryFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
MemoryFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr, 0);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
this.__wbg_ptr = ret >>> 0;
MemoryFinalization.register(this, this.__wbg_ptr, this);
return this;
}
/**
* @returns {number}
*/
ptr() {
const ret = wasm.memory_ptr(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
len() {
const ret = wasm.memory_len(this.__wbg_ptr);
return ret >>> 0;
}
}`
const beforeMemoryJs2 = `export class Memory {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
MemoryFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr, 0);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
this.__wbg_ptr = ret >>> 0;
MemoryFinalization.register(this, this.__wbg_ptr, this);
return this;
}
/**
* @returns {number}
*/
ptr() {
const ret = wasm.memory_ptr(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
len() {
const ret = wasm.memory_len(this.__wbg_ptr);
return ret >>> 0;
}
}`
const afterMemoryJs = `export class Memory {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Memory.prototype);
obj.__wbg_ptr = ptr;
MemoryFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
this.__wbg_ptr0 = 0;
this.__wbg_len0 = 0;
MemoryFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr, 0);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
this.__wbg_ptr = ret >>> 0;
this.__wbg_ptr0 = ptr0 >>> 0;
this.__wbg_len0 = len0 >>> 0;
MemoryFinalization.register(this, this.__wbg_ptr, this);
return this;
}
/**
* @returns {number}
*/
ptr() {
const ret = wasm.memory_ptr(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
len() {
const ret = wasm.memory_len(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
get ptr0() {
return this.__wbg_ptr0 ??= this.ptr();
}
/**
* @returns {number}
*/
get len0() {
return this.__wbg_len0 ??= this.len();
}
/**
* @returns {Uint8Array}
*/
get bytes() {
return getUint8ArrayMemory0().subarray(this.ptr0, this.ptr0 + this.len0);
}
}`
const afterMemoryJs2 = `export class Memory {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
this.__wbg_ptr0 = 0;
this.__wbg_len0 = 0;
MemoryFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr, 0);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
this.__wbg_ptr = ret >>> 0;
this.__wbg_ptr0 = ptr0 >>> 0;
this.__wbg_len0 = len0 >>> 0;
MemoryFinalization.register(this, this.__wbg_ptr, this);
return this;
}
/**
* @returns {number}
*/
ptr() {
const ret = wasm.memory_ptr(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
len() {
const ret = wasm.memory_len(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
get ptr0() {
return this.__wbg_ptr0 ??= this.ptr();
}
/**
* @returns {number}
*/
get len0() {
return this.__wbg_len0 ??= this.len();
}
/**
* @returns {Uint8Array}
*/
get bytes() {
return getUint8ArrayMemory0().subarray(this.ptr0, this.ptr0 + this.len0);
}
}`
const beforeMemoryTs = `export class Memory {
free(): void;
/**
* @param {Uint8Array} inner
*/
constructor(inner: Uint8Array);
/**
* @returns {number}
*/
ptr(): number;
/**
* @returns {number}
*/
len(): number;
}`
const afterMemoryTs = `export class Memory {
free(): void;
/**
* @param {Uint8Array} inner
*/
constructor(inner: Uint8Array);
/**
* @returns {number}
*/
ptr(): number;
/**
* @returns {number}
*/
len(): number;
/**
* @returns {Uint8Array}
*/
get bytes(): Uint8Array;
}`
const glueJs = readFileSync(`./src/wasm/pkg/${name}.js`, "utf8")
.replaceAll(beforeMemoryJs, afterMemoryJs)
.replaceAll(beforeMemoryJs2, afterMemoryJs2)
.replaceAll(`free()`, `[Symbol.dispose]()`)
.replaceAll(`(typeof FinalizationRegistry === 'undefined')`, `true`)
.replaceAll(`.register(this, this.__wbg_ptr, this)`, ``)
.replaceAll(`.register(obj, obj.__wbg_ptr, obj)`, ``)
.replaceAll(`.unregister(this)`, ``)
.replaceAll(`module_or_path = new URL('${name}_bg.wasm', import.meta.url);`, `throw new Error();`)
const glueTs = readFileSync(`./src/wasm/pkg/${name}.d.ts`, "utf8")
.replaceAll(beforeMemoryTs, afterMemoryTs)
.replaceAll(`free()`, `[Symbol.dispose]()`)
writeFileSync(`./src/wasm/pkg/${name}.js`, glueJs)
writeFileSync(`./src/wasm/pkg/${name}.d.ts`, glueTs)
rmSync(`./src/wasm/pkg/.gitignore`, { force: true });