@wasm-fmt/gofmt 0.3.0 → 0.3.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/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: function(fd, iovs_ptr, iovs_len, nwritten_ptr) {
129
- let nwritten = 0;
130
- if (fd == 1) {
131
- for (let iovs_i=0; iovs_i<iovs_len;iovs_i++) {
132
- let iov_ptr = iovs_ptr+iovs_i*8; // assuming wasm32
133
- let ptr = mem().getUint32(iov_ptr + 0, true);
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
- "undefined" != typeof globalThis ? globalThis : global || self,
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 mod;
4
+ let inst;
5
5
 
6
6
  export default async function init(wasm_url) {
7
- if (!mod) {
8
- if (!wasm_url) {
9
- wasm_url = new URL("lib.wasm", import.meta.url);
10
- }
7
+ if (inst) {
8
+ return await inst;
9
+ }
11
10
 
12
- if (typeof wasm_url === "string") {
13
- wasm_url = new URL(wasm_url);
14
- }
11
+ if (!wasm_url) {
12
+ wasm_url = new URL("lib.wasm", import.meta.url);
13
+ }
15
14
 
16
- if (
17
- typeof __webpack_require__ !== "function" &&
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);
30
17
  }
31
- }
32
18
 
33
- export function format(input) {
34
- const inst = new WebAssembly.Instance(mod, go.importObject);
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
- const input_len = go.storeString(input);
38
- const output_len = inst.exports.format(input_len);
39
- if (output_len < 0) {
40
- throw new Error(go.loadString(-output_len));
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 go.loadString(output_len);
46
+ return result;
44
47
  }
package/lib.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wasm-fmt/gofmt",
3
3
  "author": "magic-akari <akari.ccino@gamil.com>",
4
- "version": "0.3.0",
4
+ "version": "0.3.1",
5
5
  "description": "wasm based gofmt",
6
6
  "main": "lib.js",
7
7
  "types": "lib.d.ts",
package/src/lib.go CHANGED
@@ -1,31 +1,23 @@
1
1
  package main
2
2
 
3
- import "go/format"
3
+ import (
4
+ "go/format"
5
+ "syscall/js"
6
+ )
4
7
 
5
- const buf_len = 8192
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 -copy(buf[:], []byte(err.Error()))
13
+ return []any{true, err.Error()}
20
14
  }
21
- result := len(output)
22
15
 
23
- if result > buf_len {
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
+ }