elsaland / elsa

❄️ Elsa is a minimal runtime for JavaScript and TypeScript written in Go
MIT License
2.79k stars 61 forks source link

GO interop #121

Open davesheldon opened 2 years ago

davesheldon commented 2 years ago

Is there a way to use this library akin to how something like Goja or Otto usage looks like? I'm working on a CLI where I want to have a JS runtime that will run perhaps many scripts (both files and from variables in go) in the same context. Right now I'm using Otto, and it works, but I'd like something that supports ES6 (especially modules). Here's an example of what I'm doing to setup a vm:

vm := otto.New()

err = vm.Set("someFunc", func(call otto.FunctionCall) otto.Value {
    message := call.Argument(0).String()

    // some go code here

    return otto.Value{}
})

_, err := vm.Run(` // some setup scripts `);

Then later I have some user-defined scripts I want to execute against that vm (via the Run method), using some of those pre-defined setup/functions and interop as necessary. It doesn't look like this is something Elsa currently can handle, but is this a direction you're going in? If not I have some other options, just none as attractive as what you're doing here I think.

Thanks in advance, and best of luck!

theoparis commented 2 years ago

I could also use something like this. I was looking for a decent es6 runtime as well, but Deno cannot run as a statically linked binary so I found this - but I can't seem to find a way to use golang interop either.

I did see that elsa uses a golang fork of quickjs, and that it supports creating functions via go code internally. I dont know if it is exposed through this runtime though.

Also for anyone wondering how I managed to get a binary statically linked with musl:

CC="musl-gcc" CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags '-static -fPIC'" -o ./target/elsa .