jsre

package
v0.0.0-...-5cf00fe Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2025 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package jsre provides execution environment for JavaScript.

Index

Constants

This section is empty.

Variables

View Source
var (
	FunctionColor = color.New(color.FgMagenta).SprintfFunc()
	SpecialColor  = color.New(color.Bold).SprintfFunc()
	NumberColor   = color.New(color.FgRed).SprintfFunc()
	StringColor   = color.New(color.FgGreen).SprintfFunc()
	ErrorColor    = color.New(color.FgHiRed).SprintfFunc()
)

Functions

func MakeCallback

func MakeCallback(vm *goja.Runtime, fn func(Call) (goja.Value, error)) goja.Value

MakeCallback turns the given function into a function that's callable by JS.

func SafeGet

func SafeGet(obj *goja.Object, key string) (ret goja.Value)

SafeGet attempt to get the value associated to `key`, and catches the panic that goja creates if an error occurs in key.

Types

type Call

type Call struct {
	goja.FunctionCall
	VM *goja.Runtime
}

Call is the argument type of Go functions which are callable from JS.

type JSRE

type JSRE struct {
	// contains filtered or unexported fields
}

JSRE is a JS runtime environment embedding the goja interpreter. It provides helper functions to load code from files, run code snippets and bind native go objects to JS.

The runtime runs all code on a dedicated event loop and does not expose the underlying goja runtime directly. To use the runtime, call JSRE.Do. When binding a Go function, use the Call type to gain access to the runtime.

func New

func New(assetPath string, output io.Writer) *JSRE

New creates and initializes a new JavaScript runtime environment (JSRE). The runtime is configured with the provided assetPath for loading scripts and an output writer for logging or printing results.

The returned JSRE must be stopped by calling Stop() after use to release resources. Attempting to use the JSRE after stopping it will result in undefined behavior.

Parameters:

  • assetPath: The path to the directory containing script assets.
  • output: The writer used for logging or printing runtime output.

Returns:

  • A pointer to the newly created JSRE instance.

func (*JSRE) Compile

func (re *JSRE) Compile(filename string, src string) (err error)

Compile compiles and then runs a piece of JS code.

func (*JSRE) CompleteKeywords

func (jsre *JSRE) CompleteKeywords(line string) []string

CompleteKeywords returns potential continuations for the given line. Since line is evaluated, callers need to make sure that evaluating line does not have side effects.

func (*JSRE) Do

func (re *JSRE) Do(fn func(*goja.Runtime))

Do executes the given function on the JS event loop. When the runtime is stopped, fn will not execute.

func (*JSRE) Evaluate

func (re *JSRE) Evaluate(code string, w io.Writer)

Evaluate executes code and pretty prints the result to the specified output stream.

func (*JSRE) Exec

func (re *JSRE) Exec(file string) error

Exec loads and executes the contents of a JavaScript file. If a relative path is provided, the file is resolved relative to the JSRE's assetPath. The file is read, compiled, and executed in the JSRE's runtime environment.

Parameters:

  • file: The path to the JavaScript file to execute. Can be an absolute path or relative to assetPath.

Returns:

  • error: An error if the file cannot be read, compiled, or executed.

func (*JSRE) Interrupt

func (re *JSRE) Interrupt(v interface{})

Interrupt stops the current JS evaluation.

func (*JSRE) Run

func (re *JSRE) Run(code string) (v goja.Value, err error)

Run runs a piece of JS code.

func (*JSRE) Set

func (re *JSRE) Set(ns string, v interface{}) (err error)

Set assigns value v to a variable in the JS environment.

func (*JSRE) Stop

func (re *JSRE) Stop(waitForCallbacks bool)

Stop terminates the event loop, optionally waiting for all timers to expire.

Directories

Path Synopsis
Package deps contains the console JavaScript dependencies Go embedded.
Package deps contains the console JavaScript dependencies Go embedded.

Jump to

Keyboard shortcuts

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