js

package module
v0.0.0-...-a15c0b0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 2 Imported by: 4

README

JS

Go Reference

The JS package provides a common interface for running Javascript in Go in a consistent environment.

Examples

V8

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

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 for what needs to be done.

Currently supported Javascript VMs

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 and be available in Node.js.
  • Module Import/Export Support: Use esbuild for this purpose.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Console

type Console struct {
	Log   io.Writer
	Error io.Writer
}

type VM

type VM interface {
	Evaluate(ctx context.Context, path, code string) (string, error)
}

Directories

Path Synopsis
example
v8
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL