| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- //go:build js && wasm
- // Command agiwasm is the playground engine compiled to WebAssembly. It runs
- // in a Web Worker (static/playground/worker.js) and exposes
- // globalThis.agiPlayground:
- //
- // run(requestJSON, files, onEvent) -> Promise<{json, files}>
- // wsPush(text), wsClose() // frames typed in the page during a run
- // defaults() -> JSON // the mock answers the tool windows start from
- package main
- import (
- "encoding/json"
- "syscall/js"
- "imuslab.com/arozos/agidocgen/playground"
- )
- var live chan playground.WSIn
- func main() {
- api := js.Global().Get("Object").New()
- api.Set("run", js.FuncOf(run))
- api.Set("wsPush", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
- send(playground.WSIn{Text: args[0].String()})
- return nil
- }))
- api.Set("wsClose", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
- send(playground.WSIn{Close: true})
- return nil
- }))
- api.Set("defaults", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
- b, _ := json.Marshal(map[string]interface{}{"cnn": playground.CNNDefaults, "nativeLibs": playground.NativeLibs})
- return string(b)
- }))
- js.Global().Set("agiPlayground", api)
- select {}
- }
- func send(in playground.WSIn) {
- if live == nil {
- return
- }
- select {
- case live <- in:
- default:
- }
- }
- // run decodes the request, runs it on its own goroutine (so delay() and
- // websocket reads can wait without blocking the worker) and resolves with
- // the result JSON plus the written files as Uint8Arrays.
- func run(this js.Value, args []js.Value) interface{} {
- reqJSON, files, onEvent := args[0].String(), args[1], args[2]
- var handler js.Func
- handler = js.FuncOf(func(this js.Value, p []js.Value) interface{} {
- resolve := p[0]
- go func() {
- defer handler.Release()
- var req playground.Request
- if err := json.Unmarshal([]byte(reqJSON), &req); err != nil {
- b, _ := json.Marshal(map[string]interface{}{"error": map[string]string{"kind": "error", "message": "bad request: " + err.Error()}})
- resolve.Invoke(map[string]interface{}{"json": string(b), "files": js.Global().Get("Array").New()})
- return
- }
- for i := 0; i < files.Length(); i++ {
- f := files.Index(i)
- data := f.Get("data")
- var b []byte
- if !data.IsUndefined() && !data.IsNull() {
- b = make([]byte, data.Get("length").Int())
- js.CopyBytesToGo(b, data)
- }
- mt := 0.0
- if v := f.Get("mtime"); v.Type() == js.TypeNumber {
- mt = v.Float()
- }
- req.Files = append(req.Files, playground.File{Path: f.Get("path").String(), Dir: f.Get("dir").Truthy(), Data: b, Mtime: int64(mt)})
- }
- live = make(chan playground.WSIn, 64)
- req.Live = live
- emit := func(kind string, v interface{}) {
- if onEvent.Type() != js.TypeFunction {
- return
- }
- b, _ := json.Marshal(v)
- onEvent.Invoke(kind, string(b))
- }
- res := playground.Run(req, playground.Hooks{
- Log: func(l playground.LogLine) { emit("log", l) },
- WS: func(f playground.WSFrame) { emit("ws", f) },
- })
- live = nil
- out := js.Global().Get("Array").New()
- for _, f := range res.Written {
- o := js.Global().Get("Object").New()
- o.Set("path", f.Path)
- o.Set("dir", f.Dir)
- o.Set("mtime", f.Mtime)
- if !f.Dir {
- u := js.Global().Get("Uint8Array").New(len(f.Data))
- js.CopyBytesToJS(u, f.Data)
- o.Set("data", u)
- }
- out.Call("push", o)
- }
- for i := range res.Written {
- res.Written[i].Data = nil
- }
- b, _ := json.Marshal(res)
- resolve.Invoke(map[string]interface{}{"json": string(b), "files": out})
- }()
- return nil
- })
- return js.Global().Get("Promise").New(handler)
- }
|