fix wasm build

This commit is contained in:
zongor 2024-07-12 00:30:26 -04:00
parent 4a0ec4c8fe
commit 9d4cc1ff09
5 changed files with 53 additions and 46 deletions

View File

@ -9,7 +9,6 @@ GOOS=freebsd GOARCH=386 go build -o bin/varaq-freebsd-386 main.go
GOOS=freebsd GOARCH=amd64 go build -o bin/varaq-freebsd-amd64 main.go GOOS=freebsd GOARCH=amd64 go build -o bin/varaq-freebsd-amd64 main.go
GOOS=freebsd GOARCH=arm go build -o bin/varaq-freebsd-arm main.go GOOS=freebsd GOARCH=arm go build -o bin/varaq-freebsd-arm main.go
GOOS=illumos GOARCH=amd64 go build -o bin/varaq-illumos-amd64 main.go GOOS=illumos GOARCH=amd64 go build -o bin/varaq-illumos-amd64 main.go
GOOS=js GOARCH=wasm go build -o bin/varaq.wasm main.go
GOOS=linux GOARCH=386 go build -o bin/varaq-linux-386 main.go GOOS=linux GOARCH=386 go build -o bin/varaq-linux-386 main.go
GOOS=linux GOARCH=amd64 go build -o bin/varaq-linux-amd64 main.go GOOS=linux GOARCH=amd64 go build -o bin/varaq-linux-amd64 main.go
GOOS=linux GOARCH=arm go build -o bin/varaq-linux-arm main.go GOOS=linux GOARCH=arm go build -o bin/varaq-linux-arm main.go
@ -35,3 +34,4 @@ GOOS=windows GOARCH=386 go build -o bin/varaq-windows-386 main.go
GOOS=windows GOARCH=amd64 go build -o bin/varaq-windows-amd64 main.go GOOS=windows GOARCH=amd64 go build -o bin/varaq-windows-amd64 main.go
GOOS=windows GOARCH=arm go build -o bin/varaq-windows-arm main.go GOOS=windows GOARCH=arm go build -o bin/varaq-windows-arm main.go
GOOS=windows GOARCH=arm64 go build -o bin/varaq-windows-arm64 main.go GOOS=windows GOARCH=arm64 go build -o bin/varaq-windows-arm64 main.go
GOOS=js GOARCH=wasm go build -o bin/varaq.wasm webmain.go

View File

@ -0,0 +1,3 @@
{
"index.html" readall disp
} ":3333" servehttp

Binary file not shown.

40
main.go
View File

@ -1,26 +1,14 @@
//go:build js && wasm
// +build js,wasm
package main package main
import ( import (
"bufio" "bufio"
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"runtime"
"syscall/js"
"varaq/varaq" "varaq/varaq"
) )
func main() { func main() {
if runtime.GOARCH == "wasm" {
done := make(chan struct{}, 0)
js.Global().Set("varaq", js.FuncOf(jsInterpret))
fmt.Println("varaq WASM init, Qa'pla!")
<-done
} else {
argsCount := len(os.Args[1:]) argsCount := len(os.Args[1:])
if argsCount >= 1 { if argsCount >= 1 {
runFile(os.Args[1]) runFile(os.Args[1])
@ -28,7 +16,6 @@ func main() {
repl() repl()
} }
os.Exit(0) os.Exit(0)
}
} }
func repl() { func repl() {
@ -74,30 +61,3 @@ func interpret(src string) error {
return nil return nil
} }
func jsInterpret(this js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return js.ValueOf("no input")
}
input := args[0].String()
tokens, err := varaq.Tokenize(input)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
code := varaq.Parse(tokens)
buf := bytes.NewBufferString("")
strings := make([]string, len(args[1:]))
for i, jsVal := range args[1:] {
strings[i] = jsVal.String()
}
err = varaq.Interpret(code, strings, buf)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
return js.ValueOf(buf.String())
}

44
webmain.go Normal file
View File

@ -0,0 +1,44 @@
//go:build (js && wasm)
// +build js,wasm
package main
import (
"bytes"
"fmt"
"syscall/js"
"varaq/varaq"
)
func main() {
done := make(chan struct{}, 0)
js.Global().Set("varaq", js.FuncOf(jsInterpret))
fmt.Println("varaq WASM init, Qa'pla!")
<-done
}
func jsInterpret(this js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return js.ValueOf("no input")
}
input := args[0].String()
tokens, err := varaq.Tokenize(input)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
code := varaq.Parse(tokens)
buf := bytes.NewBufferString("")
strings := make([]string, len(args[1:]))
for i, jsVal := range args[1:] {
strings[i] = jsVal.String()
}
err = varaq.Interpret(code, strings, buf)
if err != nil {
return js.ValueOf(fmt.Errorf("%v", err))
}
return js.ValueOf(buf.String())
}