Skip to content

Commit

Permalink
Fix Wasm bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Jun 29, 2024
1 parent 1489c1f commit c164314
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions main_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,57 @@

package main

import "github.com/marianogappa/truco/truco"
import (
"encoding/json"
"syscall/js"

func NewTruco() *truco.GameState {
return truco.New()
"github.com/marianogappa/truco/truco"
)

func main() {
js.Global().Set("trucoNew", js.FuncOf(trucoNew))
js.Global().Set("trucoRunAction", js.FuncOf(trucoRunAction))
select {}
}

func multiply(x, y int) int {
return x * y
var state *truco.GameState

func trucoNew(this js.Value, p []js.Value) interface{} {
state = truco.New()

nbs, err := json.Marshal(state.ToClientGameState(0))
if err != nil {
panic(err)
}

buffer := js.Global().Get("Uint8Array").New(len(nbs))
js.CopyBytesToJS(buffer, nbs)
return buffer
}

func main() {
func trucoRunAction(this js.Value, p []js.Value) interface{} {
jsonBytes := make([]byte, p[0].Length())
js.CopyBytesToGo(jsonBytes, p[0])

select {}
newBytes := _runAction(jsonBytes)

buffer := js.Global().Get("Uint8Array").New(len(newBytes))
js.CopyBytesToJS(buffer, newBytes)
return buffer
}

func _runAction(bs []byte) []byte {
action, err := truco.DeserializeAction(bs)
if err != nil {
panic(err)
}
err = state.RunAction(action)
if err != nil {
panic(err)
}
nbs, err := json.Marshal(state.ToClientGameState(0))
if err != nil {
panic(err)
}
return nbs
}

0 comments on commit c164314

Please sign in to comment.