-
Notifications
You must be signed in to change notification settings - Fork 0
/
zig_fmt.js
202 lines (164 loc) · 4.43 KB
/
zig_fmt.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
196
197
198
199
200
201
202
let wasm;
export default async function init(input) {
if (wasm !== undefined) return wasm;
if (typeof input === "undefined") {
input = new URL("zig_fmt.wasm", import.meta.url);
}
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}
const imports = get_imports();
const { instance, module } = await load(await input, imports);
return finalize_init(instance, module);
}
async function load(module, imports) {
if (typeof Response === "function" && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === "function") {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
if (module.headers.get("Content-Type") != "application/wasm") {
console.warn(
"`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
e,
);
} else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
}
function finalize_init(instance, module) {
wasm = instance.exports;
return wasm;
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const WASI_ESUCCESS = 0;
const WASI_EBADF = 8;
function get_imports() {
return {
wasi_snapshot_preview1: {
fd_write(fd, iovs_ptr, iovs_len, nwritten_ptr) {
switch (fd) {
case 1:
case 2: {
const buffer = new DataView(wasm.memory.buffer);
const buffer8 = new Uint8Array(wasm.memory.buffer);
const iovecs = read_bytes_array(buffer, iovs_ptr, iovs_len);
const nwritten = fds[fd].fd_write(buffer8, iovecs);
buffer.setUint32(nwritten_ptr, nwritten, true);
return WASI_ESUCCESS;
}
default:
return WASI_EBADF;
}
},
fd_read(fd, iovs_ptr, iovs_len, nread_ptr) {
if (fd !== 0) {
return WASI_EBADF;
}
const buffer = new DataView(wasm.memory.buffer);
const buffer8 = new Uint8Array(wasm.memory.buffer);
const iovecs = read_bytes_array(buffer, iovs_ptr, iovs_len);
const nread = fds[fd].fd_read(buffer8, iovecs);
buffer.setUint32(nread_ptr, nread, true);
return WASI_ESUCCESS;
},
proc_exit(rval) {
return_value = rval;
return WASI_ESUCCESS;
},
},
};
}
function read_bytes(view, ptr) {
const buf = view.getUint32(ptr, true);
const len = view.getUint32(ptr + 4, true);
return [buf, len];
}
function read_bytes_array(view, iovs_ptr, iovs_len) {
const iovecs = [];
for (let i = 0; i < iovs_len; i++) {
iovecs.push(read_bytes(view, iovs_ptr + 8 * i));
}
return iovecs;
}
class StdIO {
data = new Uint8Array();
position = 0;
set string(input) {
this.data = encoder.encode(input);
this.position = 0;
}
get string() {
return decoder.decode(this.data);
}
fd_read(view8, iovs) {
let nread = 0;
for (const [ptr, len] of iovs) {
const buf = new Uint8Array(view8.buffer, ptr, len);
const data = this.data.subarray(this.position, this.position + len);
buf.set(data);
this.position += data.length;
nread += data.length;
}
return nread;
}
fd_write(view8, iovs) {
const total_len = iovs.reduce((acc, [_, len]) => acc + len, 0);
const data = new Uint8Array(total_len);
let nwritten = 0;
for (const [ptr, len] of iovs) {
const buf = new Uint8Array(view8.buffer, ptr, len);
data.set(buf, nwritten);
nwritten += buf.length;
}
if (this.data) {
const new_data = new Uint8Array(this.data.length + data.length);
new_data.set(this.data);
new_data.set(data, this.data.length);
this.data = new_data;
} else {
this.data = data;
}
return nwritten;
}
dispose() {
this.data = undefined;
this.position = 0;
}
}
const fds = [
new StdIO(), // stdin
new StdIO(), // stdout
new StdIO(), // stderr
];
let return_value = 0;
export function format(input) {
fds[0].string = input;
let stdout, stderr, error;
try {
wasm._start();
} catch (err) {
error = err;
} finally {
stdout = fds[1].string;
stderr = fds[2].string;
fds.forEach((fd) => fd.dispose());
}
if (return_value !== 0) {
throw Error(stderr);
}
return stdout;
}