v8engine

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFactory

func NewFactory(opts ...Option) jsexecutor.JsEngineFactory

NewFactory creates a new jsexecutor.JsEngineFactory for the V8 engine.

Types

type Engine

type Engine struct {
	// Iso is the V8 Isolate, representing a single-threaded VM instance.
	// It is exposed publicly to allow for advanced custom options.
	Iso *v8go.Isolate

	// Ctx is the V8 Context, representing the execution environment.
	// It is exposed publicly to allow for advanced custom options.
	Ctx *v8go.Context

	// Option holds the engine-specific configurations.
	Option *EngineOption

	// RpcScript contains the JavaScript code for handling RPC calls.
	RpcScript string
}

Engine implements the jsexecutor.JsEngine interface using the V8 engine. It encapsulates a V8 Isolate and Context.

func (*Engine) Close

func (e *Engine) Close() error

Close releases all resources associated with the V8 engine.

func (*Engine) Execute

func (e *Engine) Execute(req *jsexecutor.JsRequest) (*jsexecutor.JsResponse, error)

Execute runs a JavaScript request using the RPC script.

func (*Engine) Init

func (e *Engine) Init(scripts []*jsexecutor.InitScript) error

Init executes initialization scripts in the V8 context.

func (*Engine) Reload

func (e *Engine) Reload(scripts []*jsexecutor.InitScript) error

Reload creates a new V8 context and re-initializes it, preserving the Isolate.

type EngineOption

type EngineOption struct {
	// RpcScript allows overriding the default embedded RPC script.
	// If empty, the default script will be used.
	RpcScript string
}

EngineOption holds specific configurations for the V8 engine.

type Option

type Option func(*Engine) error

Option is a function that configures a V8 Engine. It's used in the NewFactory function.

Since Engine.Iso and Engine.Ctx are public, users can easily create their own custom options for advanced configuration. For example, to inject a Go function into the JS context:

func WithGoFunction(name string, fn v8go.FunctionCallback) v8engine.Option {
  return func(e *v8engine.Engine) error {
    global := e.Ctx.Global()
    tmpl := v8go.NewFunctionTemplate(e.Iso, fn)
    fun, err := tmpl.GetFunction(e.Ctx)
    if err != nil {
      return err
    }
    return global.Set(name, fun)
  }
}

func WithRpcScript

func WithRpcScript(script string) Option

WithRpcScript provides an option to override the default RPC handling script. This allows for custom RPC logic or extending the engine's capabilities.

Jump to

Keyboard shortcuts

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