Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ jobs:
with:
packages: build-essential cmake python3 emscripten

- uses: actions/setup-go@v6
with:
go-version: '1.25'

- name: Build C++ (cmake)
working-directory: cpp
run: |
emcmake cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release --parallel

- name: Build Go
run: |
GOOS=js GOARCH=wasm go build -o node/test/go-integration-test.wasm ./go/cmd/integration-test/

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/jc-lab/walink

go 1.25.0

require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
27 changes: 27 additions & 0 deletions go/cmd/integration-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/jc-lab/walink/go/wlvalue"
)

//export add
//go:wasmexport add
func add(a, b wlvalue.Value) wlvalue.Value {
return wlvalue.FromInt32(wlvalue.ToInt32(a) + wlvalue.ToInt32(b))
}

//export echo_string
//go:wasmexport echo_string
func echoString(v wlvalue.Value) wlvalue.Value {
s := wlvalue.ToString(v, true)
return wlvalue.MakeString(s, true)
}

//export call_me_back
//go:wasmexport call_me_back
func callMeBack(cb wlvalue.Value, val wlvalue.Value) wlvalue.Value {
f := cb.ToFunction()
return f(val)
}

func main() {}
21 changes: 21 additions & 0 deletions go/wlvalue/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wlvalue

import (
"fmt"
"unsafe"
)

type CallbackFunc func(args ...Value) Value

func (v Value) ToFunction() CallbackFunc {
if !v.IsFunction() {
panic(fmt.Errorf("value(0x%x) is not a function", v))
}
return func(args ...Value) Value {
var argsPtr uint32
if len(args) > 0 {
argsPtr = uint32(uintptr(unsafe.Pointer(&args[0])))
}
return walinkCallback(v, argsPtr, uint32(len(args)))
}
}
7 changes: 7 additions & 0 deletions go/wlvalue/callback_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !wasm

package wlvalue

func walinkCallback(v Value, argsPtr uint32, argsLen uint32) Value {
panic("walinkCallback is only available in wasm")
}
7 changes: 7 additions & 0 deletions go/wlvalue/callback_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build wasm

package wlvalue

//export walink_callback
//go:wasmimport env walink_callback
func walinkCallback(v Value, argsPtr uint32, argsLen uint32) Value
30 changes: 30 additions & 0 deletions go/wlvalue/msgpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package wlvalue

import (
"bytes"

"github.com/vmihailenco/msgpack/v5"
)

type MsgpackOption func(d *msgpack.Decoder) error

func MsgpackTag(tag string) MsgpackOption {
return func(d *msgpack.Decoder) error {
d.SetCustomStructTag(tag)
return nil
}
}

func DecodeMsgpack(v Value, allowFree bool, out interface{}, options ...MsgpackOption) error {
raw := ToMsgpackBytes(v, allowFree)
d := msgpack.NewDecoder(bytes.NewReader(raw))
for _, option := range options {
if err := option(d); err != nil {
return err
}
}
if out == nil {
return nil
}
return d.Decode(out)
}
Loading
Loading