I want to use goja to add a scripting layer to my software with some success. Yet I struggle to implement a bytes.Buffer
accessor. This is the code. I like to use something like d.Field = "foo"
or something similar.
package main
import (
"bytes"
"fmt"
"os"
"github.com/dop251/goja"
)
type document struct {
Field bytes.Buffer
}
func newDocument() *document {
d := &document{}
// I'd like to have this on the javascript side:
// d.Field.Write([]byte("foo"))
return d
}
func (d *document) Check() {
// Goal: should print "foo"
fmt.Println(`~~> d.Field`, d.Field.String())
}
func main() {
runtime := goja.New()
gjNewDoc := func(call goja.FunctionCall) goja.Value {
return runtime.ToValue(newDocument())
}
runtime.Set("newdoc", gjNewDoc)
_, err := runtime.RunString(`
var d = newdoc();
d.Field = "foo";
d.Check();
`)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
Any idea how to add data to the filed d.Field
? If this is not directly possible: what would be a good workaround?
I have also asked this question on the discussion page of goja, but had no luck with an answer.
2