@wasm-fmt/gofmt 0.3.0 → 0.3.3
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/go_wasm.js +12 -64
- package/lib.js +33 -30
- package/lib.wasm +0 -0
- package/package.json +46 -36
- package/src/lib.go +13 -21
package/go_wasm.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
|
|
7
7
|
const encoder = new TextEncoder("utf-8");
|
|
8
8
|
const decoder = new TextDecoder("utf-8");
|
|
9
|
-
var logLine = [];
|
|
10
9
|
|
|
11
10
|
export class Go {
|
|
12
11
|
constructor() {
|
|
@@ -125,50 +124,12 @@
|
|
|
125
124
|
this.importObject = {
|
|
126
125
|
wasi_snapshot_preview1: {
|
|
127
126
|
// https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write
|
|
128
|
-
fd_write
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
let len = mem().getUint32(iov_ptr + 4, true);
|
|
135
|
-
nwritten += len;
|
|
136
|
-
for (let i=0; i<len; i++) {
|
|
137
|
-
let c = mem().getUint8(ptr+i);
|
|
138
|
-
if (c == 13) { // CR
|
|
139
|
-
// ignore
|
|
140
|
-
} else if (c == 10) { // LF
|
|
141
|
-
// write line
|
|
142
|
-
let line = decoder.decode(new Uint8Array(logLine));
|
|
143
|
-
logLine = [];
|
|
144
|
-
console.log(line);
|
|
145
|
-
} else {
|
|
146
|
-
logLine.push(c);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
console.error('invalid file descriptor:', fd);
|
|
152
|
-
}
|
|
153
|
-
mem().setUint32(nwritten_ptr, nwritten, true);
|
|
154
|
-
return 0;
|
|
155
|
-
},
|
|
156
|
-
fd_close: () => 0, // dummy
|
|
157
|
-
fd_fdstat_get: () => 0, // dummy
|
|
158
|
-
fd_seek: () => 0, // dummy
|
|
159
|
-
"proc_exit": (code) => {
|
|
160
|
-
if (global.process) {
|
|
161
|
-
// Node.js
|
|
162
|
-
process.exit(code);
|
|
163
|
-
} else {
|
|
164
|
-
// Can't exit in a browser.
|
|
165
|
-
throw 'trying to exit with code ' + code;
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
random_get: (bufPtr, bufLen) => {
|
|
169
|
-
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
|
170
|
-
return 0;
|
|
171
|
-
},
|
|
127
|
+
fd_write() {},
|
|
128
|
+
fd_close() {},
|
|
129
|
+
fd_fdstat_get() {},
|
|
130
|
+
fd_seek() {},
|
|
131
|
+
proc_exit() {},
|
|
132
|
+
random_get() {}
|
|
172
133
|
},
|
|
173
134
|
env: {
|
|
174
135
|
// func ticks() float64
|
|
@@ -337,22 +298,6 @@
|
|
|
337
298
|
};
|
|
338
299
|
}
|
|
339
300
|
|
|
340
|
-
storeString(str) {
|
|
341
|
-
const addr = this._inst.exports.getBuffer();
|
|
342
|
-
const buf = this._inst.exports.memory.buffer;
|
|
343
|
-
|
|
344
|
-
const mem = new Uint8Array(buf);
|
|
345
|
-
const view = mem.subarray(addr);
|
|
346
|
-
return encoder.encodeInto(str, view).written;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
loadString(len) {
|
|
350
|
-
const addr = this._inst.exports.getBuffer();
|
|
351
|
-
const buf = this._inst.exports.memory.buffer;
|
|
352
|
-
|
|
353
|
-
return decoder.decode(new DataView(buf, addr, len));
|
|
354
|
-
}
|
|
355
|
-
|
|
356
301
|
async run(instance) {
|
|
357
302
|
this._inst = instance;
|
|
358
303
|
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
|
@@ -361,7 +306,12 @@
|
|
|
361
306
|
null,
|
|
362
307
|
true,
|
|
363
308
|
false,
|
|
364
|
-
|
|
309
|
+
// fake global
|
|
310
|
+
{
|
|
311
|
+
set format(fn){ instance.format = fn; },
|
|
312
|
+
Array,
|
|
313
|
+
Object,
|
|
314
|
+
},
|
|
365
315
|
this,
|
|
366
316
|
];
|
|
367
317
|
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
|
@@ -369,8 +319,6 @@
|
|
|
369
319
|
this._idPool = []; // unused ids that have been garbage collected
|
|
370
320
|
this.exited = false; // whether the Go program has exited
|
|
371
321
|
|
|
372
|
-
const mem = new DataView(this._inst.exports.memory.buffer)
|
|
373
|
-
|
|
374
322
|
while (true) {
|
|
375
323
|
const callbackPromise = new Promise((resolve) => {
|
|
376
324
|
this._resolveCallbackPromise = () => {
|
package/lib.js
CHANGED
|
@@ -1,44 +1,47 @@
|
|
|
1
1
|
import { Go } from "./go_wasm.js";
|
|
2
2
|
const go = new Go();
|
|
3
3
|
|
|
4
|
-
let
|
|
4
|
+
let inst;
|
|
5
5
|
|
|
6
6
|
export default async function init(wasm_url) {
|
|
7
|
-
if (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
7
|
+
if (inst) {
|
|
8
|
+
return await inst;
|
|
9
|
+
}
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
if (!wasm_url) {
|
|
12
|
+
wasm_url = new URL("lib.wasm", import.meta.url);
|
|
13
|
+
}
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
wasm_url.protocol === "file:"
|
|
19
|
-
) {
|
|
20
|
-
const fs = await import("node:fs");
|
|
21
|
-
const bytes = fs.readFileSync(wasm_url);
|
|
22
|
-
mod = new WebAssembly.Module(bytes);
|
|
23
|
-
} else if ("compileStreaming" in WebAssembly) {
|
|
24
|
-
mod = await WebAssembly.compileStreaming(fetch(wasm_url));
|
|
25
|
-
} else {
|
|
26
|
-
const response = await fetch(wasm_url);
|
|
27
|
-
const bytes = await response.arrayBuffer();
|
|
28
|
-
mod = new WebAssembly.Module(bytes);
|
|
29
|
-
}
|
|
15
|
+
if (typeof wasm_url === "string") {
|
|
16
|
+
wasm_url = new URL(wasm_url, import.meta.url);
|
|
30
17
|
}
|
|
31
|
-
}
|
|
32
18
|
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
if (
|
|
20
|
+
typeof __webpack_require__ !== "function" &&
|
|
21
|
+
wasm_url.protocol === "file:"
|
|
22
|
+
) {
|
|
23
|
+
inst = import("node:fs/promises")
|
|
24
|
+
.then((fs) => fs.readFile(wasm_url))
|
|
25
|
+
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
|
|
26
|
+
} else if ("instantiateStreaming" in WebAssembly) {
|
|
27
|
+
inst = WebAssembly.instantiateStreaming(
|
|
28
|
+
fetch(wasm_url),
|
|
29
|
+
go.importObject
|
|
30
|
+
);
|
|
31
|
+
} else {
|
|
32
|
+
inst = fetch(wasm_url)
|
|
33
|
+
.then((response) => response.arrayBuffer())
|
|
34
|
+
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
|
|
35
|
+
}
|
|
36
|
+
inst = (await inst).instance;
|
|
35
37
|
go.run(inst);
|
|
38
|
+
}
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
throw new Error(
|
|
40
|
+
export function format(input) {
|
|
41
|
+
const [err, result] = inst.format(input);
|
|
42
|
+
if (err) {
|
|
43
|
+
throw new Error(result);
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
return
|
|
46
|
+
return result;
|
|
44
47
|
}
|
package/lib.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,40 +1,50 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
"name": "@wasm-fmt/gofmt",
|
|
3
|
+
"description": "A wasm based golang formatter",
|
|
4
|
+
"author": "magic-akari <akari.ccino@gamil.com>",
|
|
5
|
+
"version": "0.3.3",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"wasm",
|
|
9
|
+
"golang",
|
|
10
|
+
"formatter"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/wasm-fmt/gofmt"
|
|
13
15
|
},
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
"homepage": "https://github.com/wasm-fmt/gofmt",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/wasm-fmt/gofmt/issues"
|
|
17
19
|
},
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "lib.js",
|
|
22
|
+
"module": "lib.js",
|
|
23
|
+
"types": "lib.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./lib.d.ts",
|
|
27
|
+
"default": "./lib.js"
|
|
28
|
+
},
|
|
29
|
+
"./vite": {
|
|
30
|
+
"types": "./lib.d.ts",
|
|
31
|
+
"default": "./vite.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json",
|
|
34
|
+
"./*": "./*"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
|
|
38
|
+
"patch": "git apply ./go_wasm.patch",
|
|
39
|
+
"build": "tinygo build -o=lib.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
|
|
40
|
+
"postbuild": "npm run go_wasm && npm run patch",
|
|
41
|
+
"test": "node --test"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=16.17.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
}
|
|
40
50
|
}
|
package/src/lib.go
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
package main
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import (
|
|
4
|
+
"go/format"
|
|
5
|
+
"syscall/js"
|
|
6
|
+
)
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
func Format(this js.Value, args []js.Value) any {
|
|
9
|
+
input := ([]byte)(args[0].String())
|
|
6
10
|
|
|
7
|
-
var buf [buf_len]byte
|
|
8
|
-
|
|
9
|
-
//go:export getBuffer
|
|
10
|
-
func GetBuffer() *byte {
|
|
11
|
-
return &buf[0]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//go:export format
|
|
15
|
-
func Format(input_len uint) int {
|
|
16
|
-
input := buf[:input_len]
|
|
17
11
|
output, err := format.Source(input)
|
|
18
12
|
if err != nil {
|
|
19
|
-
return
|
|
13
|
+
return []any{true, err.Error()}
|
|
20
14
|
}
|
|
21
|
-
result := len(output)
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
return -copy(buf[:], []byte("Buffer out of memory"))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
copy(buf[:], output)
|
|
28
|
-
return result
|
|
16
|
+
return []any{false, string(output)}
|
|
29
17
|
}
|
|
30
18
|
|
|
31
|
-
func main() {
|
|
19
|
+
func main() {
|
|
20
|
+
done := make(chan bool)
|
|
21
|
+
js.Global().Set("format", js.FuncOf(Format))
|
|
22
|
+
<-done
|
|
23
|
+
}
|