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
// contains filtered or unexported fields
}
Engine implements the jsexecutor.JsEngine interface using the V8 engine. It encapsulates a V8 Isolate and Context.
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 performs a "hard" reload of the engine by creating a new Isolate and Context. It re-applies the original options and then initializes with the provided scripts.
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 ¶
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 ¶
WithRpcScript provides an option to override the default RPC handling script. This allows for custom RPC logic or extending the engine's capabilities.