https://github.com/livebud/js
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/livebud/js
- Owner: livebud
- License: mit
- Created: 2022-07-30T21:49:42.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-27T05:11:53.000Z (over 2 years ago)
- Last Synced: 2025-06-19T19:53:53.906Z (12 months ago)
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 10
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
README
# JS
[](https://pkg.go.dev/github.com/livebud/js)
The JS package provides a common interface for running Javascript in Go in a consistent environment.
# Examples
## V8
```go
package main
import (
"context"
"fmt"
"os"
"github.com/livebud/js"
v8 "github.com/livebud/js/v8"
)
func main() {
vm, _ := v8.Load(&js.Console{
Log: os.Stdout,
Error: os.Stderr,
})
defer vm.Close()
ctx := context.Background()
vm.Evaluate(ctx, "math.js", `const multiply = (a, b) => a * b`)
value, _ := vm.Evaluate(ctx, "run.js", "multiply(3, 2)")
fmt.Println(value)
}
```
## Goja
```go
package main
import (
"context"
"fmt"
"os"
"github.com/livebud/js"
"github.com/livebud/js/goja"
)
func main() {
vm := goja.New(&js.Console{
Log: os.Stdout,
Error: os.Stderr,
})
ctx := context.Background()
vm.Evaluate(ctx, "math.js", `const multiply = (a, b) => a * b`)
value, _ := vm.Evaluate(ctx, "run.js", "multiply(3, 2)")
fmt.Println(value)
}
```
> Note: This package is still a work in progress. Please see the [issues](https://github.com/livebud/js/issues) for what needs to be done.
## Currently supported Javascript VMs
- [V8](https://github.com/rogchap/v8go)
- [Goja](https://github.com/dop251/goja)
## Goals
- **Swappable JS VMs**: The available VMs each have pros and cons. You should be able to swap VMs out based on your needs.
- **Consistent Runtime**: For each of these VMs, there should be consistent and well-tested globals (e.g. `console`, `setTimeout`, `URL`) that match the web's behavior as much as possible.
## Non-Goals
- **Secure sandbox for user-submitted Javascript**: To provide better performance, the environment is re-used across evaluations. This means that you can set globals to be read in subsequent evaluations. This type of environment is not suitable for user-submitted code.
- **Support non-standard runtime APIs**: There's no plans to add APIs that are specific to certain runtime environments such as Cloudflare Workers, Deno, etc. There's no science to this, but the following heuristic: It should be a [Web API](https://developer.mozilla.org/en-US/docs/Web/API) and be available in Node.js.
- **Module Import/Export Support**: Use [esbuild](https://github.com/evanw/esbuild) for this purpose.
## License
MIT