Help us shape the future of Go! Take the 2026 Go Developer Survey

v8

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 48 Imported by: 8

Documentation

Index

Constants

View Source
const (
	// RunnerStatusInit is the runner status init
	RunnerStatusInit uint8 = iota

	// RunnerStatusRunning is the runner status running
	RunnerStatusRunning

	// RunnerStatusCleaning is the runner status cleaning
	RunnerStatusCleaning

	// RunnerStatusReady is the runner status ready
	RunnerStatusReady

	// RunnerStatusDestroy is the runner status destroy
	RunnerStatusDestroy

	// RunnerCommandDestroy is the runner command destroy
	RunnerCommandDestroy

	// RunnerCommandReset is the runner command reset
	RunnerCommandReset

	// RunnerCommandExec is the runner command exec
	RunnerCommandExec

	// RunnerCommandStatus is the runner command status
	RunnerCommandStatus
)
View Source
const (

	// IsoReady isolate is ready
	IsoReady uint8 = 0

	// IsoBusy isolate is in used
	IsoBusy uint8 = 1
)

Variables

View Source
var ImportMap = map[string][]Import{}

ImportMap the import maps

View Source
var ModuleSourceMaps = map[string][]byte{}

ModuleSourceMaps the source maps for modules

View Source
var Modules = map[string]Module{}

Modules the scripts for modules

View Source
var RootScripts = map[string]*Script{}

RootScripts the scripts for studio

View Source
var Scripts = map[string]*Script{}

Scripts loaded scripts

View Source
var SourceCodes = map[string][]byte{}

SourceCodes the source codes

View Source
var SourceMaps = map[string][]byte{}

SourceMaps the source maps

Functions

func CLearModules added in v1.0.0

func CLearModules()

CLearModules clear the modules cache

func Call added in v1.0.0

func Call(options CallOptions, source string, args ...interface{}) (interface{}, error)

Call call the function directly

func DisablePrecompile

func DisablePrecompile()

DisablePrecompile disable the precompile

func EnableDebug added in v1.0.0

func EnableDebug()

EnableDebug enable the debug mode

func EnablePrecompile

func EnablePrecompile()

EnablePrecompile enable the precompile feature

func Exists added in v1.0.0

func Exists(id string) bool

Exists check if the script exists

func GetModuleName added in v1.0.0

func GetModuleName(file string) string

GetModuleName get the module name

func MakeTemplate added in v1.0.0

func MakeTemplate(iso *v8go.Isolate) *v8go.ObjectTemplate

MakeTemplate make a new template

func PrintException added in v1.0.0

func PrintException(method string, args []interface{}, jserr *v8go.JSError, rootMapping interface{})

PrintException print the exception

func RegisterFunction added in v1.0.0

func RegisterFunction(name string, function EmbedFunction, attributes ...v8go.PropertyAttribute) error

RegisterFunction register a third party function

func RegisterObject added in v1.0.0

func RegisterObject(name string, object EmbedObject, attributes ...v8go.PropertyAttribute) error

RegisterObject register a third party object

func Require added in v1.0.0

func Require(iso *v8go.Isolate) *v8go.FunctionTemplate

Require function template

func SelectIsoStandard added in v1.0.0

func SelectIsoStandard(timeout time.Duration) (*store.Isolate, error)

SelectIsoStandard creates a new isolate synchronously for each request Standard mode design: create on-demand, use immediately, dispose after use

func SetHeapAvailableSize

func SetHeapAvailableSize(size uint)

SetHeapAvailableSize set runtime Available

func StackTrace added in v1.0.0

func StackTrace(jserr *v8go.JSError, rootMapping interface{}) string

StackTrace get the stack trace

func Start

func Start(option *Option) error

Start v8 runtime

func Stop

func Stop()

Stop v8 runtime

func Transform added in v1.0.0

func Transform(source string, globalName string) string

Transform the javascript

func TransformTS added in v1.0.0

func TransformTS(file string, source []byte) ([]byte, error)

TransformTS transform the typescript

Types

type CallOptions added in v1.0.0

type CallOptions struct {
	Sid     string
	Global  map[string]interface{}
	Timeout time.Duration
}

CallOptions call options

type Context

type Context struct {
	ID          string                 // the script id
	Sid         string                 // set the session id
	Data        map[string]interface{} // set the global data
	Authorized  map[string]interface{} // authorized information (optional)
	Root        bool
	Timeout     time.Duration // terminate the execution after this time
	SourceRoots interface{}   // the script source root mappping

	*Runner
	*store.Isolate
	*v8go.UnboundScript
	*v8go.Context
	// contains filtered or unexported fields
}

Context v8 Context

func (*Context) Call

func (context *Context) Call(method string, args ...interface{}) (interface{}, error)

Call call the script function

func (*Context) CallAnonymous added in v1.0.0

func (context *Context) CallAnonymous(source string, args ...interface{}) (interface{}, error)

CallAnonymous call the script function with anonymous function

func (*Context) CallAnonymousWith added in v1.0.0

func (context *Context) CallAnonymousWith(ctx context.Context, source string, args ...interface{}) (interface{}, error)

CallAnonymousWith call the script function with anonymous function

func (*Context) CallWith added in v1.0.0

func (context *Context) CallWith(ctx context.Context, method string, args ...interface{}) (interface{}, error)

CallWith call the script function

func (*Context) Close

func (context *Context) Close() error

Close Context

func (*Context) WithAuthorized added in v1.0.0

func (context *Context) WithAuthorized(authorized map[string]interface{}) *Context

WithAuthorized set the authorized information for the context

func (*Context) WithFunction added in v1.0.0

func (context *Context) WithFunction(name string, cb v8go.FunctionCallback)

WithFunction add a function to the context

func (*Context) WithGlobal added in v1.0.0

func (context *Context) WithGlobal(name string, value interface{}) error

WithGlobal add a global variable to the context

type Dispatcher added in v1.0.0

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

Dispatcher is a runner dispatcher

func NewDispatcher added in v1.0.0

func NewDispatcher(min, max uint) *Dispatcher

NewDispatcher is a runner dispatcher

func (*Dispatcher) Scaling added in v1.0.0

func (dispatcher *Dispatcher) Scaling()

Scaling scale the v8 runners, check every 10 seconds Release the v8 runners if the free runners are more than max size Create a new v8 runner if the free runners are less than min size

func (*Dispatcher) Select added in v1.0.0

func (dispatcher *Dispatcher) Select(timeout time.Duration) (*Runner, error)

Select select a free v8 runner

func (*Dispatcher) Start added in v1.0.0

func (dispatcher *Dispatcher) Start() error

Start start the v8 mannager

func (*Dispatcher) Stop added in v1.0.0

func (dispatcher *Dispatcher) Stop()

Stop stop the v8 mannager

type EmbedFunction added in v1.0.0

type EmbedFunction func(iso *v8go.Isolate) *v8go.FunctionTemplate

EmbedFunction embed function

type EmbedObject added in v1.0.0

type EmbedObject func(iso *v8go.Isolate) *v8go.ObjectTemplate

EmbedObject embed object

type Health added in v1.0.0

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

Health is the health check

func (*Health) Reset added in v1.0.0

func (health *Health) Reset()

Reset reset the health

func (*Health) String added in v1.0.0

func (health *Health) String() string

type ID added in v1.0.0

type ID string

ID is the runner id

func (ID) String added in v1.0.0

func (id ID) String() string

type Import added in v1.0.0

type Import struct {
	Name    string
	Path    string
	AbsPath string
	Clause  string
}

Import module

type Isolate

type Isolate struct {
	*v8go.Isolate
	// contains filtered or unexported fields
}

Isolate v8 Isolate

type Isolates

type Isolates struct {
	Len  int
	Data *sync.Map
}

Isolates loaded isolate

type Module added in v1.0.0

type Module struct {
	GlobalName string
	File       string
	Source     string
}

Module the module

type Option

type Option struct {
	Mode              string    `json:"mode,omitempty"`              // the mode of the runtime, the default value is "standard" and the other value is "performance". "performance" mode need more memory but will run faster
	MinSize           uint      `json:"minSize,omitempty"`           // the number of V8 VM when runtime start. max value is 100, the default value is 2
	MaxSize           uint      `json:"maxSize,omitempty"`           // the maximum of V8 VM should be smaller than minSize, the default value is 10
	HeapSizeLimit     uint64    `json:"heapSizeLimit,omitempty"`     // the isolate heap size limit should be smaller than 1.5G, and the default value is 1518338048 (1.5G)
	HeapSizeRelease   uint64    `json:"heapSizeRelease,omitempty"`   // the isolate will be re-created when reaching this value, and the default value is 52428800 (50M)
	HeapAvailableSize uint64    `json:"heapAvailableSize,omitempty"` // the isolate will be re-created when the available size is smaller than this value, and the default value is 524288000 (500M)
	Precompile        bool      `json:"precompile,omitempty"`        // if true compile scripts when the VM is created. this will increase the load time, but the script will run faster. the default value is false
	DefaultTimeout    int       `json:"defaultTimeout,omitempty"`    // the default timeout for the script, the default value is 200ms
	ContextTimeout    int       `json:"contextTimeout,omitempty"`    // the default timeout for the context, the default value is 200ms
	ContetxQueueSize  int       `json:"contextQueueSize,omitempty"`  // the default queue size for the context, the default value is 10, performance only
	DataRoot          string    `json:"dataRoot,omitempty"`          // the data root path
	TSConfig          *TSConfig `json:"tsconfig,omitempty"`          // the TypeScript config
	Debug             bool      `json:"debug,omitempty"`             // if true, the debug mode will be enabled, default value is false
	ConsoleMode       string    `json:"consoleMode,omitempty"`       // the console mode, default value is "production", the other value is "development"

	// The following options are experimental features and not stable.
	// They may be removed once the features become stable. Please do not use them in a production environment.
	Import bool `json:"import,omitempty"` // Always true, do not change it.

}

Option runtime option

func (*Option) Validate

func (option *Option) Validate()

Validate the option

type Runner added in v1.0.0

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

Runner is the v8 runner

func NewRunner added in v1.0.0

func NewRunner(keepalive bool) *Runner

NewRunner create a new v8 runner

func (*Runner) Context added in v1.0.0

func (runner *Runner) Context() (*v8go.Context, error)

Context get the context

func (*Runner) Destroy added in v1.0.0

func (runner *Runner) Destroy(cb func())

Destroy send a destroy signal to the v8 runner

func (*Runner) Exec added in v1.0.0

func (runner *Runner) Exec(script *Script) interface{}

Exec send a script to the v8 runner to execute

func (*Runner) ExecWithContext added in v1.0.0

func (runner *Runner) ExecWithContext(ctx context.Context, script *Script) interface{}

ExecWithContext send a script to the v8 runner to execute with context cancellation support

func (*Runner) Reset added in v1.0.0

func (runner *Runner) Reset()

Reset send a reset signal to the v8 runner

func (*Runner) Start added in v1.0.0

func (runner *Runner) Start(ready chan bool) error

Start start the v8 runner

type Script

type Script struct {
	ID          string
	File        string
	Source      string
	Root        bool
	SourceRoots interface{} // the script source root mappping
	Timeout     time.Duration
}

Script v8 scripts

func Load

func Load(file string, id string) (*Script, error)

Load load the script

func LoadRoot

func LoadRoot(file string, id string) (*Script, error)

LoadRoot load the script with root privileges

func MakeScript added in v1.0.0

func MakeScript(source []byte, file string, timeout time.Duration, isroot ...bool) (*Script, error)

MakeScript make a script from source

func MakeScriptInMemory added in v1.0.0

func MakeScriptInMemory(source []byte, virtualFile string, timeout time.Duration, isroot ...bool) (*Script, error)

MakeScriptInMemory creates a script from source code without file system access. This is useful for scripts stored in database or generated dynamically. Supports TypeScript syntax but NOT imports (since there's no file to resolve). For scripts with imports, use MakeScript with a real file path.

func NewScript

func NewScript(file string, id string, timeout ...time.Duration) *Script

NewScript create a new script

func Select

func Select(id string) (*Script, error)

Select a script

func SelectRoot

func SelectRoot(id string) (*Script, error)

SelectRoot a script with root privileges

func (*Script) ContextTimeout added in v1.0.0

func (script *Script) ContextTimeout() time.Duration

ContextTimeout get the context timeout

func (*Script) Exec added in v1.0.0

func (script *Script) Exec(process *process.Process) interface{}

Exec execute the script the default mode is "standard" and the other value is "performance". the "standard" mode save memory but will run slower. can be used in most cases, especially in arm64 device. the "performance" mode need more memory but will run faster. can be used in high concurrency and large script.

func (*Script) NewContext

func (script *Script) NewContext(sid string, global map[string]interface{}) (*Context, error)

NewContext create a new context

func (*Script) Open added in v1.0.0

func (script *Script) Open(source []byte) error

Open open the script

type SourceMap added in v1.0.0

type SourceMap struct {
	Version        int      `json:"version"`
	File           string   `json:"file"`
	SourceRoot     string   `json:"sourceRoot,omitempty"`
	Sources        []string `json:"sources"`
	Names          []string `json:"names"`
	Mappings       string   `json:"mappings"`
	SourcesContent []string `json:"sourcesContent,omitempty"`
	// contains filtered or unexported fields
}

SourceMap source map

func NewSourceMap added in v1.0.0

func NewSourceMap(data []byte) (*SourceMap, error)

NewSourceMap create a new source map

type StackLogEntry added in v1.0.0

type StackLogEntry struct {
	Function string `json:"function,omitempty"`
	File     string `json:"file,omitempty"`
	Line     int    `json:"line,omitempty"`
	Column   int    `json:"column,omitempty"`
	Message  string `json:"message,omitempty"`
}

StackLogEntry stack log entry

func (*StackLogEntry) String added in v1.0.0

func (entry *StackLogEntry) String() string

type StackLogEntryList added in v1.0.0

type StackLogEntryList []*StackLogEntry

StackLogEntryList stack log entry list

func (StackLogEntryList) String added in v1.0.0

func (list StackLogEntryList) String(rootMapping interface{}) (string, error)

String the stack log entry list to string

type TSConfig added in v1.0.0

type TSConfig struct {
	CompilerOptions *TSConfigCompilerOptions `json:"compilerOptions,omitempty"`
	Exclude         []string                 `json:"exclude,omitempty"`
}

TSConfig TypeScript config

func (*TSConfig) GetFileName added in v1.0.0

func (tsconfg *TSConfig) GetFileName(path string) (string, bool, error)

GetFileName get the file name from the tsconfig

func (*TSConfig) Match added in v1.0.0

func (tsconfg *TSConfig) Match(pattern, path string) bool

Match match the pattern

func (*TSConfig) ReplacePattern added in v1.0.0

func (tsconfg *TSConfig) ReplacePattern(path, pattern string) string

ReplacePattern replace the pattern

type TSConfigCompilerOptions added in v1.0.0

type TSConfigCompilerOptions struct {
	Target string              `json:"target,omitempty"`
	Paths  map[string][]string `json:"paths,omitempty"`
	Lib    []string            `json:"lib,omitempty"`
}

TSConfigCompilerOptions TypeScript compiler options

type ThirdPartyFunction added in v1.0.0

type ThirdPartyFunction struct {
	Name       string
	Function   EmbedFunction
	Attributes []v8go.PropertyAttribute
}

ThirdPartyFunction third party function

type ThirdPartyObject added in v1.0.0

type ThirdPartyObject struct {
	Name       string
	Object     EmbedObject
	Attributes []v8go.PropertyAttribute
}

ThirdPartyObject third party object

Directories

Path Synopsis
functions
all
any
use
objects
fs
job
log

Jump to

Keyboard shortcuts

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