js

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: AGPL-3.0 Imports: 28 Imported by: 8

Documentation

Overview

Package js the JavaScript implementation

Index

Constants

View Source
const (
	// DefaultMaxTimeToWaitGetVM default retries time
	DefaultMaxTimeToWaitGetVM = 500 * time.Millisecond
	// DefaultMaxRetriesGetVM default retries times
	DefaultMaxRetriesGetVM = 3
)
View Source
const VMContextKey = "__ctx__"

VMContextKey the VM current context

Variables

View Source
var (
	// ErrInvalidModule module is invalid
	ErrInvalidModule = errors.New("invalid module")
	// ErrIllegalModuleName module name is illegal
	ErrIllegalModuleName = errors.New("illegal module name")

	// ErrModuleFileDoesNotExist module not exist
	ErrModuleFileDoesNotExist = errors.New("module file does not exist")
)
View Source
var (

	// ErrSchedulerClosed the scheduler is closed error
	ErrSchedulerClosed = errors.New("scheduler is closed")
)

Functions

func EnableConsole

func EnableConsole(vm *goja.Runtime)

EnableConsole enables the console

func EnableRequire

func EnableRequire(vm *goja.Runtime, path ...string)

EnableRequire set runtime require module

func Format

func Format(call goja.FunctionCall, vm *goja.Runtime) goja.Value

Format implements js format

func InitGlobalModule

func InitGlobalModule(vm *goja.Runtime)

InitGlobalModule init all global modules

func Run

func Run(ctx context.Context, p Program) (goja.Value, error)

Run the js program

func RunString

func RunString(ctx context.Context, script string) (goja.Value, error)

RunString the js string

func SetOptions added in v0.3.0

func SetOptions(opt Options)

SetOptions set the default js Options.

func Throw

func Throw(vm *goja.Runtime, err error)

Throw js exception

func ToBytes

func ToBytes(data any) ([]byte, error)

ToBytes tries to return a byte slice from compatible types.

func ToStrings

func ToStrings(data any) (s any, err error)

ToStrings tries to return a string slice or string from compatible types.

func Unwrap

func Unwrap(value goja.Value) (any, error)

Unwrap the goja.Value to the raw value

func VMContext

func VMContext(vm *goja.Runtime) context.Context

VMContext returns the current context of the goja.Runtime

Types

type Cat

type Cat struct {
	BaseURL string
	URL     string `js:"url"`
	// contains filtered or unexported fields
}

Cat an analyzer context

func NewCat

func NewCat(ctx *plugin.Context) *Cat

NewCat returns a new Cat instance

func (*Cat) Cancel

func (c *Cat) Cancel()

Cancel this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.

func (*Cat) ClearVar

func (c *Cat) ClearVar()

ClearVar clean all values

func (*Cat) GetElement

func (c *Cat) GetElement(key string, rule string, content any) (ret string, err error)

GetElement gets the string of the content with the given arguments

func (*Cat) GetElements

func (c *Cat) GetElements(key string, rule string, content any) (ret []string, err error)

GetElements gets the string of the content with the given arguments

func (*Cat) GetString

func (c *Cat) GetString(key string, rule string, content any) (ret string, err error)

GetString gets the string of the content with the given arguments

func (*Cat) GetStrings

func (c *Cat) GetStrings(key string, rule string, content any) (ret []string, err error)

GetStrings gets the string of the content with the given arguments

func (*Cat) GetVar

func (c *Cat) GetVar(call goja.FunctionCall, vm *goja.Runtime) goja.Value

GetVar returns the value associated with this context for key, or nil if no value is associated with key.

func (*Cat) Log

func (c *Cat) Log(call goja.FunctionCall, vm *goja.Runtime) goja.Value

Log print the msg to logger

func (*Cat) SetVar

func (c *Cat) SetVar(key string, value goja.Value) error

SetVar value associated with key is val.

type FieldNameMapper

type FieldNameMapper struct{}

FieldNameMapper provides custom mapping between Go and JavaScript property names.

func (FieldNameMapper) FieldName

FieldName returns a JavaScript name for the given struct field in the given type. If this method returns "" the field becomes hidden.

func (FieldNameMapper) MethodName

func (FieldNameMapper) MethodName(_ reflect.Type, m reflect.Method) string

MethodName returns a JavaScript name for the given method in the given type. If this method returns "" the method becomes hidden.

type Options

type Options struct {
	InitialVMs         int           `yaml:"initial-vms"`
	MaxVMs             int           `yaml:"max-vms"`
	MaxRetriesGetVM    int           `yaml:"max-retries-get-vm"`
	MaxTimeToWaitGetVM time.Duration `yaml:"max-time-to-wait-get-vm"`
	UseStrict          bool          `yaml:"use-strict"`
	ModulePath         []string      `yaml:"module-path"`
}

Options Scheduler options

type Program

type Program struct {
	Code string
	Args map[string]any
}

Program The js program

type Scheduler

type Scheduler interface {
	// Get the VM
	Get() (VM, error)
	// Release the VM
	Release(VM)
	// Close the scheduler
	Close() error
}

Scheduler the VM scheduler

func GetScheduler

func GetScheduler() Scheduler

GetScheduler returns the default Scheduler.

func NewScheduler

func NewScheduler(opt Options) Scheduler

NewScheduler returns a new Scheduler

type VM

type VM interface {
	// Run the js program
	Run(context.Context, Program) (goja.Value, error)
	// RunString the js string
	RunString(context.Context, string) (goja.Value, error)
	// Runtime the js runtime
	Runtime() *goja.Runtime
}

VM the js runtime. An instance of VM can only be used by a single goroutine at a time.

Directories

Path Synopsis
Package modulestest the module test vm
Package modulestest the module test vm

Jump to

Keyboard shortcuts

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