-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use
syscall/js
for interop (#3)
- Loading branch information
1 parent
c251cbe
commit c655602
Showing
3 changed files
with
124 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,47 @@ | ||
import { Go } from "./go_wasm.js"; | ||
const go = new Go(); | ||
|
||
let mod; | ||
let inst; | ||
|
||
export default async function init(wasm_url) { | ||
if (!mod) { | ||
if (!wasm_url) { | ||
wasm_url = new URL("lib.wasm", import.meta.url); | ||
} | ||
if (inst) { | ||
return await inst; | ||
} | ||
|
||
if (typeof wasm_url === "string") { | ||
wasm_url = new URL(wasm_url); | ||
} | ||
if (!wasm_url) { | ||
wasm_url = new URL("lib.wasm", import.meta.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)); | ||
} else { | ||
const response = await fetch(wasm_url); | ||
const bytes = await response.arrayBuffer(); | ||
mod = new WebAssembly.Module(bytes); | ||
} | ||
if (typeof wasm_url === "string") { | ||
wasm_url = new URL(wasm_url); | ||
} | ||
} | ||
|
||
export function format(input) { | ||
const inst = new WebAssembly.Instance(mod, go.importObject); | ||
if ( | ||
typeof __webpack_require__ !== "function" && | ||
wasm_url.protocol === "file:" | ||
) { | ||
inst = import("node:fs/promises") | ||
.then((fs) => fs.readFile(wasm_url)) | ||
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject)); | ||
} else if ("instantiateStreaming" in WebAssembly) { | ||
inst = WebAssembly.instantiateStreaming( | ||
fetch(wasm_url), | ||
go.importObject | ||
); | ||
} else { | ||
inst = fetch(wasm_url) | ||
.then((response) => response.arrayBuffer()) | ||
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject)); | ||
} | ||
inst = (await inst).instance; | ||
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)); | ||
export function format(input) { | ||
const [err, result] = inst.format(input); | ||
if (err) { | ||
throw new Error(result); | ||
} | ||
|
||
return go.loadString(output_len); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
package main | ||
|
||
import "go/format" | ||
import ( | ||
"go/format" | ||
"syscall/js" | ||
) | ||
|
||
const buf_len = 8192 | ||
func Format(this js.Value, args []js.Value) any { | ||
input := ([]byte)(args[0].String()) | ||
|
||
var buf [buf_len]byte | ||
|
||
//go:export getBuffer | ||
func GetBuffer() *byte { | ||
return &buf[0] | ||
} | ||
|
||
//go:export format | ||
func Format(input_len uint) int { | ||
input := buf[:input_len] | ||
output, err := format.Source(input) | ||
if err != nil { | ||
return -copy(buf[:], []byte(err.Error())) | ||
return []interface{}{true, err.Error()} | ||
} | ||
result := len(output) | ||
|
||
if result > buf_len { | ||
return -copy(buf[:], []byte("Buffer out of memory")) | ||
} | ||
|
||
copy(buf[:], output) | ||
return result | ||
return []interface{}{false, string(output)} | ||
} | ||
|
||
func main() {} | ||
func main() { | ||
done := make(chan bool) | ||
js.Global().Set("format", js.FuncOf(Format)) | ||
<-done | ||
} |