-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.js
195 lines (164 loc) · 4.21 KB
/
patch.js
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
import { readFileSync, rmSync, writeFileSync } from "fs";
const wasm = readFileSync("./wasm/pkg/naberius_bg.wasm")
writeFileSync(`./wasm/pkg/naberius.wasm.js`, `export const data = "data:application/wasm;base64,${wasm.toString("base64")}";`);
writeFileSync(`./wasm/pkg/naberius.wasm.d.ts`, `export const data: string;`);
const disposableJs = ` [Symbol.dispose]() {
this.free()
}
free() {`
const disposableTs = ` [Symbol.dispose](): void
free(): void;`
const beforeMemoryJs = `export class Memory {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Memory.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free()
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
return Memory.__wrap(ret);
}
/**
* @returns {number}
*/
ptr() {
const ret = wasm.memory_ptr(this.__wbg_ptr);
return ret;
}
/**
* @returns {number}
*/
len() {
const ret = wasm.memory_len(this.__wbg_ptr);
return ret >>> 0;
}
}`
const afterMemoryJs = `export class Memory {
static __wrap(ptr, ptr0, len0) {
ptr = ptr >>> 0;
const obj = Object.create(Memory.prototype);
obj.__wbg_ptr = ptr;
obj.__wbg_ptr0 = ptr0;
obj.__wbg_len0 = len0;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free()
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
return Memory.__wrap(ret, ptr0, len0);
}
/**
* @returns {number}
*/
ptr() {
return this.__wbg_ptr0 ??= wasm.memory_ptr(this.__wbg_ptr);
}
/**
* @returns {number}
*/
len() {
return this.__wbg_len0 ??= wasm.memory_len(this.__wbg_ptr);
}
freeNextTick() {
setTimeout(() => this.free(), 0);
return this;
}
get bytes() {
return getUint8Memory0().subarray(this.ptr(), this.ptr() + this.len());
}
copyAndDispose() {
const bytes = this.bytes.slice();
this.free();
return bytes;
}
}`
const beforeMemoryTs = `export class Memory {
[Symbol.dispose](): void
free(): void;
/**
* @param {Uint8Array} inner
*/
constructor(inner: Uint8Array);
/**
* @returns {number}
*/
ptr(): number;
/**
* @returns {number}
*/
len(): number;
}`
const afterMemoryTs = `export class Memory {
[Symbol.dispose](): void
free(): void;
/**
* @param {Uint8Array} inner
*/
constructor(inner: Uint8Array);
/**
* @returns {number}
*/
ptr(): number;
/**
* @returns {number}
*/
len(): number;
/**
* Free on next tick
**/
freeNextTick(): Memory
/**
* Get the bytes in memory
**/
get bytes(): Uint8Array
/**
* Copy the bytes and free them
**/
copyAndDispose(): Uint8Array
}`
const glueJs = readFileSync(`./wasm/pkg/naberius.js`, "utf8")
.replace("async function __wbg_init", "export async function __wbg_init")
.replace("input = new URL('naberius_bg.wasm', import.meta.url);", "throw new Error();")
.replaceAll(" free() {", disposableJs)
.replaceAll(beforeMemoryJs, afterMemoryJs)
const glueTs = readFileSync(`./wasm/pkg/naberius.d.ts`, "utf8")
.replace("export default function __wbg_init", "export function __wbg_init")
.replaceAll(" free(): void;", disposableTs)
.replaceAll(beforeMemoryTs, afterMemoryTs)
writeFileSync(`./wasm/pkg/naberius.js`, glueJs)
writeFileSync(`./wasm/pkg/naberius.d.ts`, glueTs)
rmSync(`./wasm/pkg/.gitignore`, { force: true });