Skip to content

Commit

Permalink
add wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-mcgann committed Aug 3, 2024
1 parent efdb60d commit 70613b8
Show file tree
Hide file tree
Showing 8 changed files with 1,080 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"os"

"net/http"
)

var addr = ":9090"

func main() {
fmt.Printf("listening on %v\n", addr)
err := http.ListenAndServe(addr, http.FileServer(http.Dir("web")))
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to start server: %v\n", err)
os.Exit(1)
}
}
150 changes: 150 additions & 0 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//go:build js

package main

import (
"syscall/js"

"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/app"
"github.com/blackchip-org/zc/v6/app/repl"
)

var (
c *app.Calc
r *repl.Repl
)

func zcCommonPrefix() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 1 {
panic("zcCommonPrefix: invalid number of arguments")
}
jsValues := args[0]
var outValues []string
for i := 0; i < jsValues.Length(); i++ {
outValues = append(outValues, jsValues.Index(i).String())
}
common := repl.CommonPrefix(outValues)
return common
})
}

func zcEval() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
in := args[0].String()
r.Eval(in)
var err string
var stack []any

if r.Error() != nil {
err = r.Error().Error()
}

items := c.Items()
for _, item := range items {
stack = append(stack, map[string]any{
"value": valueOf(item),
"label": item.Label,
"unit": item.Unit,
})
}
return map[string]any{
"stack": stack,
"notice": r.Notice(),
"error": err,
}
})
}

func zcStackLen() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
return c.Len()
})
}

func zcStack() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
var stack []any
for _, item := range c.Items() {
stack = append(stack, map[string]any{
"value": valueOf(item),
"label": item.Label,
"unit": item.Unit,
})
}
return stack
})
}

func zcOpNames() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
var names []any
for _, n := range c.Catalog.OpNames() {
names = append(names, n)
}
return names
})
}

func zcSetStack() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 1 {
panic("zcSetStack: invalid number of arguments")
}
jsStack := args[0]
c.SetItems([]zc.Item{})
for i := 0; i < jsStack.Length(); i++ {
item := jsStack.Index(i)
val := item.Get("value")
zc.String.Push(c, val.String())
}
return nil
})
}

func zcQuoteEnd() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
return r.EndQuote
})
}

func zcWordCompleter() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 2 {
panic("zcWordCompleter: invalid number of arguments")
}
line := args[0].String()
pos := args[1].Int()
prefix, candidates, suffix := r.WordCompleter(line, pos)
var jsCandidates []any
for _, c := range candidates {
jsCandidates = append(jsCandidates, c)
}
return map[string]any{
"prefix": prefix,
"candidates": jsCandidates,
"suffix": suffix,
}
})
}

func valueOf(item zc.Item) string {
return item.Val()
}

func main() {
c = app.NewCalc()
r = repl.New(c)

js.Global().Set("zcCommonPrefix", zcCommonPrefix())
js.Global().Set("zcEval", zcEval())
js.Global().Set("zcStack", zcStack())
js.Global().Set("zcStackLen", zcStackLen())
js.Global().Set("zcOpNames", zcOpNames())
js.Global().Set("zcSetStack", zcSetStack())
js.Global().Set("zcQuoteEnd", zcQuoteEnd())
js.Global().Set("zcWordCompleter", zcWordCompleter())

<-make(chan struct{})
}
5 changes: 5 additions & 0 deletions cmd/wasm/main_empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !js

package main

func main() {}
Binary file added web/Inconsolata-Regular.ttf
Binary file not shown.
34 changes: 34 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="zc.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("zc.wasm"), go.importObject).then((result) => {
go.run(result.instance);
init();
});
</script>
<meta name="google" content="notranslate">
</head>
<body>
<div id="main">
<div id="output-panel">
<div id="output"></div>
</div>
<div id="popup"></div>
<div id="footer-panel">
<div id="footer">
<label id="prompt" for="input">zc&nbsp;&gt;</label>
<textarea id="input" type="search" rows="1" autocorrect="off" autocapitalize="off" autocomplete="new-password"></textarea>
<button id="auto">?</button>
</div>
</div>
</div>
</div>
</body>
<script src="zc.js"></script>
</html>
Loading

0 comments on commit 70613b8

Please sign in to comment.