wasm-fmt / gofmt Goto Github PK
View Code? Open in Web Editor NEWA WASM Based Golang Formatter
License: MIT License
A WASM Based Golang Formatter
License: MIT License
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:
Lines 16 to 36 in 5241e36
--- 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));
}
Lines 5 to 12 in 3e10fe0
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.