Giter Site home page Giter Site logo

gofmt's Introduction

Test

Install

npm

npm install @wasm-fmt/gofmt

jsr.io

npx jsr add @fmt/gofmt

Usage

import init, { format } from "@wasm-fmt/gofmt";

await init();

const source = `
package main
import "fmt"
func main(){fmt.Println("Hello, 世界")
}
`;

const formatted = format(source);
console.log(formatted);

Vite users tip:

import init, { format } from "@wasm-fmt/gofmt/vite";

Build from source

# 1. clone this repo
git clone https://github.com/wasm-fmt/gofmt.git

# 2. install TinyGo https://tinygo.org/getting-started/install/

# 3. build
pnpm build

# 4. test
pnpm run /^test:/

gofmt's People

Contributors

magic-akari avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gofmt's Issues

Error with single wasm instance

In the current implementation, format creates a new wasm instance every time it is called.
If the instance is reused, the following error will be thrown:

panic: runtime error: nil pointer dereference
panic: runtime error: slice out of range

Code changes to reuse the instance:

gofmt/lib.js

Lines 16 to 36 in 5241e36

if (typeof __webpack_require__ !== "function" && wasm_url.protocol === "file:") {
const fs = await import("node:fs");
const bytes = fs.readFileSync(wasm_url);
mod = new WebAssembly.Module(bytes);
} else if ("compileStreaming" in WebAssembly) {
mod = await WebAssembly.compileStreaming(fetch(wasm_url));
} else {
const response = await fetch(wasm_url);
const bytes = await response.arrayBuffer();
mod = new WebAssembly.Module(bytes);
}
}
const inst = new WebAssembly.Instance(mod, go.importObject);
go.run(inst);
const input_len = go.storeString(input);
const output_len = inst.exports.format(input_len);
if (output_len < 0) {
throw new Error(go.loadString(-output_len));
}

--- a/lib.js
+++ b/lib.js
@@ -16,21 +16,23 @@ export async function format(input, wasm_url) {
     if (typeof __webpack_require__ !== "function" && wasm_url.protocol === "file:") {
       const fs = await import("node:fs");
       const bytes = fs.readFileSync(wasm_url);
-      mod = new WebAssembly.Module(bytes);
-    } else if ("compileStreaming" in WebAssembly) {
-      mod = await WebAssembly.compileStreaming(fetch(wasm_url));
+      mod = await WebAssembly.instantiate(bytes, go.importObject);
+      mod = mod.instance;
+    } else if ("instantiateStreaming" in WebAssembly) {
+      mod = await WebAssembly.instantiateStreaming(fetch(wasm_url), go.importObject);
+      mod = mod.instance;
     } else {
       const response = await fetch(wasm_url);
       const bytes = await response.arrayBuffer();
-      mod = new WebAssembly.Module(bytes);
+      mod = await WebAssembly.instantiate(bytes, go.importObject);
+      mod = mod.instance;
     }
   }
 
-  const inst = new WebAssembly.Instance(mod, go.importObject);
-  go.run(inst);
+  go.run(mod);
 
   const input_len = go.storeString(input);
-  const output_len = inst.exports.format(input_len);
+  const output_len = mod.exports.format(input_len);
   if (output_len < 0) {
     throw new Error(go.loadString(-output_len));
   }

The interop of Golang, wasm and JavaScript

gofmt/src/lib.go

Lines 5 to 12 in 3e10fe0

const buf_len = 8192
var buf [buf_len]byte
//go:export getBuffer
func GetBuffer() *byte {
return &buf[0]
}

There is a buf used to exchange string type data between Golang and JavaScript.
There is an obvious issue, how to handle the case of exceeding the limit? Can buf be automatically resized?
What is the typical interop when compiling a Golang program to wasm?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.