-
Notifications
You must be signed in to change notification settings - Fork 15
Add Go example #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add Go example #26
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Go wasm plugin example | ||
|
||
This is a bare-bone typst plugin, written in Go. | ||
|
||
## Compile | ||
|
||
To compile this example, you need the [TinyGo compiler]. Then, run the command | ||
|
||
```sh | ||
GOOS="wasip1" GOARCH="wasm" tinygo build -o hello.wasm | ||
``` | ||
|
||
[TinyGo compiler]: https://tinygo.org/getting-started/install/ | ||
|
||
|
||
This invocation of Tinygo builds with WASI[^1], so we need to stub WASI functions: | ||
|
||
[^1]: I personally could not get a working binary with `GOOS="js"` (i.e. wasm). | ||
|
||
```sh | ||
pushd ../../wasi-stub | ||
cargo run -- ../examples/hello_go/hello.wasm -o ../examples/hello_go/hello.wasm | ||
popd | ||
``` | ||
|
||
## Build with typst | ||
|
||
Simply run `typst compile hello.typ`, and observe that it works ! |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/astrale-sharp/wasm-minimal-protocol/tree/master/examples/hello_go | ||
|
||
go 1.22.2 |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
// === | ||
// functions for the protocol | ||
|
||
//go:wasmimport typst_env wasm_minimal_protocol_write_args_to_buffer | ||
func write_args_to_buffer(ptr int32) | ||
|
||
func WriteArgsToBuffer(argBuf []byte) { | ||
ptr := int32(uintptr(unsafe.Pointer(unsafe.SliceData(argBuf)))) | ||
write_args_to_buffer(ptr) | ||
} | ||
|
||
//go:wasmimport typst_env wasm_minimal_protocol_send_result_to_host | ||
func send_result_to_host(ptr, size int32) | ||
|
||
func SendResultToHost(resBuf []byte) { | ||
size := int32(len(resBuf)) | ||
ptr := int32(uintptr(unsafe.Pointer(unsafe.SliceData(resBuf)))) | ||
send_result_to_host(ptr, size) | ||
} | ||
|
||
// === | ||
|
||
// main is required for the `wasip1` target, even if it isn't used. | ||
func main() {} | ||
|
||
//go:export hello | ||
func hello() int32 { | ||
const message = "Hello from wasm!!!" | ||
SendResultToHost([]byte(message)) | ||
return 0 | ||
} | ||
|
||
//go:export double_it | ||
func doubleIt(arg1Len int32) int32 { | ||
buf := make([]byte, arg1Len*2) | ||
WriteArgsToBuffer(buf) | ||
|
||
copy(buf[arg1Len:], buf[:arg1Len]) | ||
SendResultToHost(buf) | ||
return 0 | ||
} | ||
|
||
//go:export concatenate | ||
func concatenate(arg1Len, arg2Len int32) int32 { | ||
buf := make([]byte, arg1Len+arg2Len+1) | ||
WriteArgsToBuffer(buf) | ||
|
||
copy(buf[arg1Len+1:], buf[arg1Len:]) | ||
buf[arg1Len] = '*' | ||
SendResultToHost(buf) | ||
return 0 | ||
} | ||
|
||
//go:export shuffle | ||
func shuffle(arg1Len, arg2Len, arg3Len int) int32 { | ||
argBuf := make([]byte, arg1Len+arg2Len+arg3Len) | ||
arg1 := argBuf[:arg1Len] | ||
arg2 := argBuf[arg1Len : arg1Len+arg2Len] | ||
arg3 := argBuf[arg1Len+arg2Len:] | ||
WriteArgsToBuffer(argBuf) | ||
|
||
resBuf := make([]byte, 0, arg1Len+arg2Len+arg3Len+2) | ||
resBuf = append(resBuf, arg3...) | ||
resBuf = append(resBuf, '-') | ||
resBuf = append(resBuf, arg1...) | ||
resBuf = append(resBuf, '-') | ||
resBuf = append(resBuf, arg2...) | ||
SendResultToHost(resBuf) | ||
return 0 | ||
} | ||
|
||
//go:export returns_ok | ||
func returnsOk() int32 { | ||
const message = "This is an `Ok`" | ||
SendResultToHost([]byte(message)) | ||
return 0 | ||
} | ||
|
||
//go:export returns_err | ||
func returnsErr() int32 { | ||
const message = "This is an `Err`" | ||
SendResultToHost([]byte(message)) | ||
return 1 | ||
} | ||
|
||
//go:export will_panic | ||
func willPanic() int32 { | ||
panic("Panicking, this message will not be seen...") | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#{ | ||
let p = plugin("./hello.wasm") | ||
|
||
assert.eq(str(p.hello()), "Hello from wasm!!!") | ||
assert.eq(str(p.double_it(bytes("abc"))), "abcabc") | ||
assert.eq(str(p.concatenate(bytes("hello"), bytes("world"))), "hello*world") | ||
assert.eq(str(p.shuffle(bytes("s1"), bytes("s2"), bytes("s3"))), "s3-s1-s2") | ||
assert.eq(str(p.returns_ok()), "This is an `Ok`") | ||
// p.will_panic() // Fails compilation | ||
// p.returns_err() // Fails compilation with an error message | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I mean by submodule, you added a git submodule here
removing this file, adding and commiting should do the trick