@wasm-fmt/gofmt 0.4.9 → 0.5.1
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.
- package/README.md +8 -6
- package/gofmt.d.wasm.ts +7 -0
- package/gofmt.js +39 -345
- package/gofmt.wasm +0 -0
- package/gofmt_bundle.js +16 -0
- package/gofmt_entry.d.ts +14 -0
- package/gofmt_esm.js +25 -0
- package/gofmt_node.js +98 -6
- package/gofmt_vite.js +3 -2
- package/gofmt_web.js +89 -0
- package/package.json +6 -8
- package/gofmt.d.ts +0 -10
package/README.md
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
# Install
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/@wasm-fmt/gofmt)
|
|
5
|
+
[](https://www.npmjs.com/package/@wasm-fmt/gofmt)
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @wasm-fmt/gofmt
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
[](https://jsr.io/@fmt/gofmt)
|
|
11
|
+
[](https://jsr.io/@fmt/gofmt)
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
14
|
npx jsr add @fmt/gofmt
|
|
@@ -41,14 +41,16 @@ import init, { format } from "@wasm-fmt/gofmt/vite";
|
|
|
41
41
|
# Build from source
|
|
42
42
|
|
|
43
43
|
```bash
|
|
44
|
-
# 1.
|
|
45
|
-
git clone https://github.com/wasm-fmt/gofmt.git
|
|
44
|
+
# 1. install Go https://go.dev/doc/install
|
|
46
45
|
|
|
47
46
|
# 2. install TinyGo https://tinygo.org/getting-started/install/
|
|
48
47
|
|
|
49
|
-
# 3.
|
|
48
|
+
# 3. clone this repo
|
|
49
|
+
git clone https://github.com/wasm-fmt/gofmt.git
|
|
50
|
+
|
|
51
|
+
# 4. build
|
|
50
52
|
pnpm build
|
|
51
53
|
|
|
52
|
-
#
|
|
54
|
+
# 5. test
|
|
53
55
|
pnpm run /^test:/
|
|
54
56
|
```
|
package/gofmt.d.wasm.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const memory: WebAssembly.Memory;
|
|
2
|
+
export declare function _initialize(): void;
|
|
3
|
+
export declare function alloc(size: number): number;
|
|
4
|
+
export declare function dispose(): void;
|
|
5
|
+
export declare function format(): 0 | 1 | 2;
|
|
6
|
+
export declare function output_ptr(): number;
|
|
7
|
+
export declare function output_len(): number;
|
package/gofmt.js
CHANGED
|
@@ -1,355 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//
|
|
5
|
-
// This file has been modified for use by the TinyGo compiler.
|
|
6
|
-
|
|
7
|
-
const encoder = new TextEncoder("utf-8");
|
|
8
|
-
const decoder = new TextDecoder("utf-8");
|
|
9
|
-
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
|
10
|
-
var logLine = [];
|
|
11
|
-
|
|
12
|
-
class Go {
|
|
13
|
-
constructor() {
|
|
14
|
-
this._callbackTimeouts = new Map();
|
|
15
|
-
this._nextCallbackTimeoutID = 1;
|
|
16
|
-
|
|
17
|
-
const mem = () => {
|
|
18
|
-
// The buffer may change when requesting more memory.
|
|
19
|
-
return new DataView(this._inst.exports.memory.buffer);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const unboxValue = (v_ref) => {
|
|
23
|
-
reinterpretBuf.setBigInt64(0, v_ref, true);
|
|
24
|
-
const f = reinterpretBuf.getFloat64(0, true);
|
|
25
|
-
if (f === 0) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
if (!isNaN(f)) {
|
|
29
|
-
return f;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const id = v_ref & 0xffffffffn;
|
|
33
|
-
return this._values[id];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const loadValue = (addr) => {
|
|
38
|
-
let v_ref = mem().getBigUint64(addr, true);
|
|
39
|
-
return unboxValue(v_ref);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const boxValue = (v) => {
|
|
43
|
-
const nanHead = 0x7FF80000n;
|
|
44
|
-
|
|
45
|
-
if (typeof v === "number") {
|
|
46
|
-
if (isNaN(v)) {
|
|
47
|
-
return nanHead << 32n;
|
|
48
|
-
}
|
|
49
|
-
if (v === 0) {
|
|
50
|
-
return (nanHead << 32n) | 1n;
|
|
51
|
-
}
|
|
52
|
-
reinterpretBuf.setFloat64(0, v, true);
|
|
53
|
-
return reinterpretBuf.getBigInt64(0, true);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
switch (v) {
|
|
57
|
-
case undefined:
|
|
58
|
-
return 0n;
|
|
59
|
-
case null:
|
|
60
|
-
return (nanHead << 32n) | 2n;
|
|
61
|
-
case true:
|
|
62
|
-
return (nanHead << 32n) | 3n;
|
|
63
|
-
case false:
|
|
64
|
-
return (nanHead << 32n) | 4n;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let id = this._ids.get(v);
|
|
68
|
-
if (id === undefined) {
|
|
69
|
-
id = this._idPool.pop();
|
|
70
|
-
if (id === undefined) {
|
|
71
|
-
id = BigInt(this._values.length);
|
|
72
|
-
}
|
|
73
|
-
this._values[id] = v;
|
|
74
|
-
this._goRefCounts[id] = 0;
|
|
75
|
-
this._ids.set(v, id);
|
|
76
|
-
}
|
|
77
|
-
this._goRefCounts[id]++;
|
|
78
|
-
let typeFlag = 1n;
|
|
79
|
-
switch (typeof v) {
|
|
80
|
-
case "string":
|
|
81
|
-
typeFlag = 2n;
|
|
82
|
-
break;
|
|
83
|
-
case "symbol":
|
|
84
|
-
typeFlag = 3n;
|
|
85
|
-
break;
|
|
86
|
-
case "function":
|
|
87
|
-
typeFlag = 4n;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
return id | ((nanHead | typeFlag) << 32n);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const storeValue = (addr, v) => {
|
|
94
|
-
let v_ref = boxValue(v);
|
|
95
|
-
mem().setBigUint64(addr, v_ref, true);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const loadSlice = (array, len, cap) => {
|
|
99
|
-
return new Uint8Array(this._inst.exports.memory.buffer, array, len);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const loadSliceOfValues = (array, len, cap) => {
|
|
103
|
-
const a = new Array(len);
|
|
104
|
-
for (let i = 0; i < len; i++) {
|
|
105
|
-
a[i] = loadValue(array + i * 8);
|
|
106
|
-
}
|
|
107
|
-
return a;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const loadString = (ptr, len) => {
|
|
111
|
-
return decoder.decode(new DataView(this._inst.exports.memory.buffer, ptr, len));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const timeOrigin = Date.now() - performance.now();
|
|
115
|
-
this.importObject = {
|
|
116
|
-
wasi_snapshot_preview1: {
|
|
117
|
-
// https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write
|
|
118
|
-
fd_write: () => 0, // dummy
|
|
119
|
-
},
|
|
120
|
-
gojs: {
|
|
121
|
-
// func ticks() float64
|
|
122
|
-
"runtime.ticks": () => {
|
|
123
|
-
return timeOrigin + performance.now();
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
// func finalizeRef(v ref)
|
|
127
|
-
"syscall/js.finalizeRef": (v_ref) => {
|
|
128
|
-
reinterpretBuf.setBigInt64(0, v_ref, true);
|
|
129
|
-
const f = reinterpretBuf.getFloat64(0, true);
|
|
130
|
-
if (f === 0 || !isNaN(f)) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const id = v_ref & 0xffffffffn;
|
|
134
|
-
this._goRefCounts[id]--;
|
|
135
|
-
if (this._goRefCounts[id] === 0) {
|
|
136
|
-
const v = this._values[id];
|
|
137
|
-
this._values[id] = null;
|
|
138
|
-
this._ids.delete(v);
|
|
139
|
-
this._idPool.push(id);
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
// func stringVal(value string) ref
|
|
144
|
-
"syscall/js.stringVal": (value_ptr, value_len) => {
|
|
145
|
-
const s = loadString(value_ptr, value_len);
|
|
146
|
-
return boxValue(s);
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
// func valueGet(v ref, p string) ref
|
|
150
|
-
"syscall/js.valueGet": (v_ref, p_ptr, p_len) => {
|
|
151
|
-
let prop = loadString(p_ptr, p_len);
|
|
152
|
-
let v = unboxValue(v_ref);
|
|
153
|
-
let result = Reflect.get(v, prop);
|
|
154
|
-
return boxValue(result);
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
// func valueSet(v ref, p string, x ref)
|
|
158
|
-
"syscall/js.valueSet": (v_ref, p_ptr, p_len, x_ref) => {
|
|
159
|
-
const v = unboxValue(v_ref);
|
|
160
|
-
const p = loadString(p_ptr, p_len);
|
|
161
|
-
const x = unboxValue(x_ref);
|
|
162
|
-
Reflect.set(v, p, x);
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
// func valueIndex(v ref, i int) ref
|
|
166
|
-
"syscall/js.valueIndex": (v_ref, i) => {
|
|
167
|
-
return boxValue(Reflect.get(unboxValue(v_ref), i));
|
|
168
|
-
},
|
|
169
|
-
|
|
170
|
-
// valueSetIndex(v ref, i int, x ref)
|
|
171
|
-
"syscall/js.valueSetIndex": (v_ref, i, x_ref) => {
|
|
172
|
-
Reflect.set(unboxValue(v_ref), i, unboxValue(x_ref));
|
|
173
|
-
},
|
|
174
|
-
|
|
175
|
-
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
176
|
-
"syscall/js.valueCall": (ret_addr, v_ref, m_ptr, m_len, args_ptr, args_len, args_cap) => {
|
|
177
|
-
const v = unboxValue(v_ref);
|
|
178
|
-
const name = loadString(m_ptr, m_len);
|
|
179
|
-
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
180
|
-
try {
|
|
181
|
-
const m = Reflect.get(v, name);
|
|
182
|
-
storeValue(ret_addr, Reflect.apply(m, v, args));
|
|
183
|
-
mem().setUint8(ret_addr + 8, 1);
|
|
184
|
-
} catch (err) {
|
|
185
|
-
storeValue(ret_addr, err);
|
|
186
|
-
mem().setUint8(ret_addr + 8, 0);
|
|
187
|
-
}
|
|
188
|
-
},
|
|
189
|
-
|
|
190
|
-
// func valueNew(v ref, args []ref) (ref, bool)
|
|
191
|
-
"syscall/js.valueNew": (ret_addr, v_ref, args_ptr, args_len, args_cap) => {
|
|
192
|
-
const v = unboxValue(v_ref);
|
|
193
|
-
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
194
|
-
try {
|
|
195
|
-
storeValue(ret_addr, Reflect.construct(v, args));
|
|
196
|
-
mem().setUint8(ret_addr + 8, 1);
|
|
197
|
-
} catch (err) {
|
|
198
|
-
storeValue(ret_addr, err);
|
|
199
|
-
mem().setUint8(ret_addr+ 8, 0);
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
|
-
|
|
203
|
-
// func valueLength(v ref) int
|
|
204
|
-
"syscall/js.valueLength": (v_ref) => {
|
|
205
|
-
return unboxValue(v_ref).length;
|
|
206
|
-
},
|
|
207
|
-
|
|
208
|
-
// valuePrepareString(v ref) (ref, int)
|
|
209
|
-
"syscall/js.valuePrepareString": (ret_addr, v_ref) => {
|
|
210
|
-
const s = String(unboxValue(v_ref));
|
|
211
|
-
const str = encoder.encode(s);
|
|
212
|
-
storeValue(ret_addr, str);
|
|
213
|
-
mem().setInt32(ret_addr + 8, str.length, true);
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
// valueLoadString(v ref, b []byte)
|
|
217
|
-
"syscall/js.valueLoadString": (v_ref, slice_ptr, slice_len, slice_cap) => {
|
|
218
|
-
const str = unboxValue(v_ref);
|
|
219
|
-
loadSlice(slice_ptr, slice_len, slice_cap).set(str);
|
|
220
|
-
},
|
|
221
|
-
}
|
|
222
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @import * as WASM from "./gofmt.wasm"
|
|
3
|
+
*/
|
|
223
4
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @param {WASM} wasm
|
|
7
|
+
* @param {string} source
|
|
8
|
+
* @return {string}
|
|
9
|
+
*/
|
|
10
|
+
export function format(wasm, source) {
|
|
11
|
+
try {
|
|
12
|
+
writeStringToWasmMemory(wasm, source);
|
|
13
|
+
const result = wasm.format();
|
|
14
|
+
if (result === 0) {
|
|
15
|
+
return source;
|
|
227
16
|
}
|
|
228
17
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
NaN,
|
|
233
|
-
0,
|
|
234
|
-
null,
|
|
235
|
-
true,
|
|
236
|
-
false,
|
|
237
|
-
// fake global
|
|
238
|
-
{
|
|
239
|
-
set format(fn){ instance.format = fn; },
|
|
240
|
-
Array,
|
|
241
|
-
Object,
|
|
242
|
-
},
|
|
243
|
-
this,
|
|
244
|
-
];
|
|
245
|
-
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
|
246
|
-
this._ids = new Map(); // mapping from JS values to reference ids
|
|
247
|
-
this._idPool = []; // unused ids that have been garbage collected
|
|
248
|
-
this.exited = false; // whether the Go program has exited
|
|
249
|
-
|
|
250
|
-
while (true) {
|
|
251
|
-
const callbackPromise = new Promise((resolve) => {
|
|
252
|
-
this._resolveCallbackPromise = () => {
|
|
253
|
-
if (this.exited) {
|
|
254
|
-
throw new Error("bad callback: Go program has already exited");
|
|
255
|
-
}
|
|
256
|
-
setTimeout(resolve, 0); // make sure it is asynchronous
|
|
257
|
-
};
|
|
258
|
-
});
|
|
259
|
-
this._inst.exports._start();
|
|
260
|
-
if (this.exited) {
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
await callbackPromise;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
18
|
+
const ptr = wasm.output_ptr();
|
|
19
|
+
const length = wasm.output_len();
|
|
20
|
+
const text = readStringFromWasmMemory(wasm, ptr, length);
|
|
266
21
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
throw new Error("Go program has already exited");
|
|
270
|
-
}
|
|
271
|
-
this._inst.exports.resume();
|
|
272
|
-
if (this.exited) {
|
|
273
|
-
this._resolveExitPromise();
|
|
274
|
-
}
|
|
22
|
+
if (result === 1) {
|
|
23
|
+
return text;
|
|
275
24
|
}
|
|
276
25
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const event = { id: id, this: this, args: arguments };
|
|
281
|
-
go._pendingEvent = event;
|
|
282
|
-
go._resume();
|
|
283
|
-
return event.result;
|
|
284
|
-
};
|
|
285
|
-
}
|
|
26
|
+
throw new Error(text);
|
|
27
|
+
} finally {
|
|
28
|
+
wasm.dispose();
|
|
286
29
|
}
|
|
30
|
+
}
|
|
287
31
|
|
|
288
32
|
/**
|
|
289
|
-
*
|
|
33
|
+
* @param {WASM} wasm
|
|
34
|
+
* @param {string} str
|
|
290
35
|
*/
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
/**/ const instance = await WebAssembly.instantiate(module, imports);
|
|
306
|
-
/**/ if (instance instanceof WebAssembly.Instance) return { instance, module };
|
|
307
|
-
/**/ else return instance;
|
|
308
|
-
/**/ }
|
|
309
|
-
/**/}
|
|
310
|
-
/**/function __finalize_init(instance) {
|
|
311
|
-
/**/ return wasm = instance;
|
|
312
|
-
/**/}
|
|
313
|
-
/**/function __init_memory(imports, maybe_memory) { }
|
|
314
|
-
/**/export function initSync(module) {
|
|
315
|
-
/**/ if (wasm !== undefined) return wasm;
|
|
316
|
-
/**/
|
|
317
|
-
/**/ const go = new Go;
|
|
318
|
-
/**/ const imports = go.importObject;
|
|
319
|
-
/**/
|
|
320
|
-
/**/ __init_memory(imports);
|
|
321
|
-
/**/
|
|
322
|
-
/**/ if (!(module instanceof WebAssembly.Module)) module = new WebAssembly.Module(module);
|
|
323
|
-
/**/
|
|
324
|
-
/**/ const instance = new WebAssembly.Instance(module, imports);
|
|
325
|
-
/**/
|
|
326
|
-
/**/ go.run(instance);
|
|
327
|
-
/**/ return __finalize_init(instance, module);
|
|
328
|
-
/**/}
|
|
329
|
-
/**/export default async function initAsync(input) {
|
|
330
|
-
/**/ if (wasm !== undefined) return wasm;
|
|
331
|
-
/**/
|
|
332
|
-
/**/ if (typeof input === 'undefined') input = new URL('gofmt.wasm', import.meta.url);
|
|
333
|
-
/**/
|
|
334
|
-
/**/ const go = new Go;
|
|
335
|
-
/**/ const imports = go.importObject;
|
|
336
|
-
/**/
|
|
337
|
-
/**/ if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
|
|
338
|
-
/**/ input = fetch(input);
|
|
339
|
-
/**/ }
|
|
340
|
-
/**/
|
|
341
|
-
/**/ __init_memory(imports);
|
|
342
|
-
/**/
|
|
343
|
-
/**/ const { instance, module } = await __load(await input, imports);
|
|
344
|
-
/**/
|
|
345
|
-
/**/ go.run(instance);
|
|
346
|
-
/**/ return __finalize_init(instance, module);
|
|
347
|
-
/**/}
|
|
348
|
-
/**/export function format(input) {
|
|
349
|
-
/**/ const [err, result] = wasm.format(input);
|
|
350
|
-
/**/ if (err) {
|
|
351
|
-
/**/ throw new Error(result);
|
|
352
|
-
/**/ }
|
|
353
|
-
/**/ return result;
|
|
354
|
-
/**/}
|
|
355
|
-
/**/
|
|
36
|
+
function writeStringToWasmMemory(wasm, str) {
|
|
37
|
+
const bytes = encoder.encode(str);
|
|
38
|
+
const ptr = wasm.alloc(bytes.length);
|
|
39
|
+
const memory = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
|
|
40
|
+
memory.set(bytes);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readStringFromWasmMemory(wasm, ptr, length) {
|
|
44
|
+
const memory = new Uint8Array(wasm.memory.buffer, ptr, length);
|
|
45
|
+
return decoder.decode(memory);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const encoder = new TextEncoder();
|
|
49
|
+
const decoder = new TextDecoder();
|
package/gofmt.wasm
CHANGED
|
Binary file
|
package/gofmt_bundle.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import wasm from "./gofmt.wasm";
|
|
2
|
+
import { format as _format } from "./gofmt.js";
|
|
3
|
+
|
|
4
|
+
wasm._initialize();
|
|
5
|
+
|
|
6
|
+
export function initSync() {
|
|
7
|
+
return wasm;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default async function initAsync() {
|
|
11
|
+
return wasm;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function format(source) {
|
|
15
|
+
return _format(wasm, source);
|
|
16
|
+
}
|
package/gofmt_entry.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type InitInput =
|
|
2
|
+
| RequestInfo
|
|
3
|
+
| URL
|
|
4
|
+
| Response
|
|
5
|
+
| BufferSource
|
|
6
|
+
| WebAssembly.Module;
|
|
7
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
8
|
+
import type * as InitOutput from "./gofmt.d.wasm.ts";
|
|
9
|
+
|
|
10
|
+
export default function initAsync(
|
|
11
|
+
init_input?: InitInput | Promise<InitInput>,
|
|
12
|
+
): Promise<InitOutput>;
|
|
13
|
+
export declare function initSync(buffer_or_module: SyncInitInput): InitOutput;
|
|
14
|
+
export declare function format(input: string): string;
|
package/gofmt_esm.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import source wasmModule from "./gofmt.wasm";
|
|
2
|
+
import { format as _format } from "./gofmt.js";
|
|
3
|
+
/**
|
|
4
|
+
* @import * as WASM from "./gofmt.wasm"
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const instance = new WebAssembly.Instance(wasmModule);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @type {WASM}
|
|
11
|
+
*/
|
|
12
|
+
let wasm = instance.exports;
|
|
13
|
+
wasm._initialize();
|
|
14
|
+
|
|
15
|
+
export function initSync() {
|
|
16
|
+
return wasm;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default async function initAsync() {
|
|
20
|
+
return wasm;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function format(source) {
|
|
24
|
+
return _format(wasm, source);
|
|
25
|
+
}
|
package/gofmt_node.js
CHANGED
|
@@ -1,10 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
/* @ts-self-types="./gofmt_entry.d.ts" */
|
|
2
|
+
import { format as _format } from "./gofmt.js";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
let wasm, wasmModule;
|
|
5
5
|
|
|
6
|
-
export
|
|
7
|
-
|
|
6
|
+
export function initSync(buffer_or_module) {
|
|
7
|
+
if (wasm !== void 0) return wasm;
|
|
8
|
+
|
|
9
|
+
if (!(buffer_or_module instanceof WebAssembly.Module)) {
|
|
10
|
+
buffer_or_module = new WebAssembly.Module(buffer_or_module);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return finalize_init(
|
|
14
|
+
new WebAssembly.Instance(buffer_or_module),
|
|
15
|
+
buffer_or_module,
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default async function initAsync(init_input) {
|
|
20
|
+
if (wasm !== void 0) return wasm;
|
|
21
|
+
|
|
22
|
+
if (init_input === void 0) {
|
|
23
|
+
init_input = new URL("gofmt.wasm", import.meta.url);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (typeof init_input === "string") {
|
|
27
|
+
init_input = new URL(init_input);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (init_input instanceof URL && init_input.protocol === "file:") {
|
|
31
|
+
const [{ readFile }, { fileURLToPath }] = await Promise.all([
|
|
32
|
+
import("fs/promises"),
|
|
33
|
+
import("url"),
|
|
34
|
+
]);
|
|
35
|
+
init_input = readFile(fileURLToPath(init_input));
|
|
36
|
+
} else if (
|
|
37
|
+
(typeof Request === "function" && init_input instanceof Request) ||
|
|
38
|
+
init_input instanceof URL
|
|
39
|
+
) {
|
|
40
|
+
init_input = fetch(init_input);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const { instance, module } = await load(await init_input);
|
|
44
|
+
|
|
45
|
+
return finalize_init(instance, module);
|
|
8
46
|
}
|
|
9
47
|
|
|
10
|
-
|
|
48
|
+
async function load(module, imports) {
|
|
49
|
+
if (typeof Response === "function" && module instanceof Response) {
|
|
50
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
51
|
+
try {
|
|
52
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
const validResponse =
|
|
55
|
+
module.ok && expectedResponseType(module.type);
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
validResponse &&
|
|
59
|
+
module.headers.get("Content-Type") !== "application/wasm"
|
|
60
|
+
) {
|
|
61
|
+
console.warn(
|
|
62
|
+
"`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",
|
|
63
|
+
e,
|
|
64
|
+
);
|
|
65
|
+
} else {
|
|
66
|
+
throw e;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const bytes = await module.arrayBuffer();
|
|
72
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
73
|
+
} else {
|
|
74
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
75
|
+
|
|
76
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
77
|
+
return { instance, module };
|
|
78
|
+
} else {
|
|
79
|
+
return instance;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function expectedResponseType(type) {
|
|
84
|
+
switch (type) {
|
|
85
|
+
case "basic":
|
|
86
|
+
case "cors":
|
|
87
|
+
case "default":
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function finalize_init(instance, module) {
|
|
95
|
+
wasm = instance.exports, wasmModule = module;
|
|
96
|
+
wasm._initialize();
|
|
97
|
+
return wasm;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function format(source) {
|
|
101
|
+
return _format(wasm, source);
|
|
102
|
+
}
|
package/gofmt_vite.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
/* @ts-self-types="./gofmt_entry.d.ts" */
|
|
2
|
+
import initAsync from "./gofmt_web.js";
|
|
2
3
|
import wasm_url from "./gofmt.wasm?url";
|
|
3
4
|
|
|
4
5
|
export default function (input = wasm_url) {
|
|
5
6
|
return initAsync(input);
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
export * from "./
|
|
9
|
+
export * from "./gofmt_web.js";
|
package/gofmt_web.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* @ts-self-types="./gofmt_entry.d.ts" */
|
|
2
|
+
import { format as _format } from "./gofmt.js";
|
|
3
|
+
let wasm, wasmModule;
|
|
4
|
+
|
|
5
|
+
async function load(module, imports) {
|
|
6
|
+
if (typeof Response === "function" && module instanceof Response) {
|
|
7
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
8
|
+
try {
|
|
9
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
10
|
+
} catch (e) {
|
|
11
|
+
const validResponse =
|
|
12
|
+
module.ok && expectedResponseType(module.type);
|
|
13
|
+
|
|
14
|
+
if (
|
|
15
|
+
validResponse &&
|
|
16
|
+
module.headers.get("Content-Type") !== "application/wasm"
|
|
17
|
+
) {
|
|
18
|
+
console.warn(
|
|
19
|
+
"`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",
|
|
20
|
+
e,
|
|
21
|
+
);
|
|
22
|
+
} else {
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const bytes = await module.arrayBuffer();
|
|
29
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
30
|
+
} else {
|
|
31
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
32
|
+
|
|
33
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
34
|
+
return { instance, module };
|
|
35
|
+
} else {
|
|
36
|
+
return instance;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function expectedResponseType(type) {
|
|
41
|
+
switch (type) {
|
|
42
|
+
case "basic":
|
|
43
|
+
case "cors":
|
|
44
|
+
case "default":
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function finalize_init(instance, module) {
|
|
52
|
+
wasm = instance.exports, wasmModule = module;
|
|
53
|
+
wasm._initialize();
|
|
54
|
+
return wasm;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function initSync(module) {
|
|
58
|
+
if (wasm !== void 0) return wasm;
|
|
59
|
+
|
|
60
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
61
|
+
module = new WebAssembly.Module(module);
|
|
62
|
+
}
|
|
63
|
+
const instance = new WebAssembly.Instance(module);
|
|
64
|
+
return finalize_init(instance, module);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default async function initAsync(module_or_path) {
|
|
68
|
+
if (wasm !== void 0) return wasm;
|
|
69
|
+
|
|
70
|
+
if (module_or_path === void 0) {
|
|
71
|
+
module_or_path = new URL("gofmt.wasm", import.meta.url);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
typeof module_or_path === "string" ||
|
|
76
|
+
(typeof Request === "function" && module_or_path instanceof Request) ||
|
|
77
|
+
(typeof URL === "function" && module_or_path instanceof URL)
|
|
78
|
+
) {
|
|
79
|
+
module_or_path = fetch(module_or_path);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const { instance, module } = await load(await module_or_path);
|
|
83
|
+
|
|
84
|
+
return finalize_init(instance, module);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function format(source) {
|
|
88
|
+
return _format(wasm, source);
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@wasm-fmt/gofmt",
|
|
3
3
|
"description": "A wasm based golang formatter",
|
|
4
4
|
"author": "magic-akari <akari.ccino@gamil.com>",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.5.1",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"wasm",
|
|
@@ -18,17 +18,14 @@
|
|
|
18
18
|
"url": "https://github.com/wasm-fmt/gofmt/issues"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
|
-
"main": "gofmt.js",
|
|
22
|
-
"module": "gofmt.js",
|
|
23
|
-
"types": "gofmt.d.ts",
|
|
24
21
|
"exports": {
|
|
25
22
|
".": {
|
|
26
|
-
"types": "./
|
|
23
|
+
"types": "./gofmt_entry.d.ts",
|
|
27
24
|
"node": "./gofmt_node.js",
|
|
28
|
-
"default": "./
|
|
25
|
+
"default": "./gofmt_web.js"
|
|
29
26
|
},
|
|
30
27
|
"./vite": {
|
|
31
|
-
"types": "./
|
|
28
|
+
"types": "./gofmt_entry.d.ts",
|
|
32
29
|
"default": "./gofmt_vite.js"
|
|
33
30
|
},
|
|
34
31
|
"./package.json": "./package.json",
|
|
@@ -36,7 +33,8 @@
|
|
|
36
33
|
},
|
|
37
34
|
"scripts": {
|
|
38
35
|
"build": "./scripts/build.sh",
|
|
39
|
-
"test:
|
|
36
|
+
"test:go": "go test ./src -v",
|
|
37
|
+
"test:node": "node --test test_node/node.test.js",
|
|
40
38
|
"test:deno": "deno test test_deno --allow-read",
|
|
41
39
|
"test:bun": "bun test test_bun"
|
|
42
40
|
},
|
package/gofmt.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export type InitInput =
|
|
2
|
-
| RequestInfo
|
|
3
|
-
| URL
|
|
4
|
-
| Response
|
|
5
|
-
| BufferSource
|
|
6
|
-
| WebAssembly.Module;
|
|
7
|
-
|
|
8
|
-
export default function initAsync(wasm_url?: InitInput): Promise<void>;
|
|
9
|
-
export declare function initSync(module: BufferSource | WebAssembly.Module): void;
|
|
10
|
-
export declare function format(input: string): string;
|