quickjsBind

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package quickjs Go bindings to QuickJS: a fast, small, and embeddable ES2020 JavaScript interpreter

Example
// Create a new runtime
rt := quickjsBind.NewRuntime()
defer rt.Close()

// Create a new context
ctx := rt.NewModuleContext()
defer ctx.Close()

// Create a new object
test := ctx.Object()
defer test.Free()
// bind properties to the object
test.Set("A", test.Context().String("String A"))
test.Set("B", ctx.Int32(0))
test.Set("C", ctx.Bool(false))
// bind go function to js object
test.Set("hello", ctx.Function(func(ctx *quickjsBind.Context, this quickjsBind.Value, args []quickjsBind.Value) quickjsBind.Value {
	return ctx.String("Hello " + args[0].String())
}))

// bind "test" object to global object
ctx.Globals().Set("test", test)

// call js function by js
js_ret, _ := ctx.Eval(`test.hello("Javascript!")`)
fmt.Println(js_ret.String())

// call js function by go
go_ret := ctx.Globals().Get("test").Call("hello", ctx.String("Golang!"))
fmt.Println(go_ret.String())

//bind go function to Javascript async function
ctx.Globals().Set("testAsync", ctx.AsyncFunction(func(ctx *quickjsBind.Context, this quickjsBind.Value, promise quickjsBind.Value, args []quickjsBind.Value) quickjsBind.Value {
	return promise.Call("resolve", ctx.String("Hello Async Function!"))
}))

ret, _ := ctx.Eval(`
			var ret;
			testAsync().then(v => ret = v)
		`)
defer ret.Free()

// wait for promise resolve
rt.ExecuteAllPendingJobs()

asyncRet, _ := ctx.Eval("ret")
defer asyncRet.Free()

fmt.Println(asyncRet.String())
Output:

Hello Javascript!
Hello Golang!
Hello Async Function!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGoObject

func GetGoObject[T any](v Value) (*T, error)

func GoInitModule

func GoInitModule(ctx *C.JSContext, m *C.JSModuleDef) C.int

Types

type Array

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

func NewQjsArray

func NewQjsArray(value Value, ctx *Context) *Array

func (Array) Delete

func (a Array) Delete(index int64) (bool, error)

func (Array) Free

func (a Array) Free()

func (Array) Get

func (a Array) Get(index int64) (Value, error)

Get

@Description: get the specific value by subscript
@receiver a :
@param index :
@return Value

func (Array) HasIdx

func (a Array) HasIdx(i int64) bool

HasIdx

@Description: Determine whether there is data at the current subscript position
@receiver a :
@param i :
@return bool

func (Array) Len

func (a Array) Len() int64

Len

@Description: get the length of the array
@receiver a :
@return int64

func (Array) Push

func (a Array) Push(elements ...Value) int64

Push

@Description: add one or more elements after the array,returns the new array length
@receiver a :
@param elements :
@return int64

func (Array) Set

func (a Array) Set(index int64, value Value) error

Set

@Description:
@receiver a :
@param index :
@param value :
@return error

func (Array) ToValue

func (a Array) ToValue() Value

ToValue

@Description: get the value object of qjs
@receiver a :
@return Value

type Atom

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

Object property names and some strings are stored as Atoms (unique strings) to save memory and allow fast comparison. Atoms are represented as a 32 bit integer. Half of the atom range is reserved for immediate integer literals from 0 to 2^{31}-1.

func (Atom) Free

func (a Atom) Free()

Free the value.

func (Atom) String

func (a Atom) String() string

String returns the string representation of the value.

func (Atom) Value

func (a Atom) Value() Value

Value returns the value of the Atom object.

type Context

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

Context represents a Javascript context (or Realm). Each JSContext has its own global objects and system objects. There can be several JSContexts per JSRuntime and they can share objects, similar to frames of the same origin sharing Javascript objects in a web browser.

func (*Context) Array

func (ctx *Context) Array() *Array

Array returns a new array value.

func (*Context) ArrayBuffer

func (ctx *Context) ArrayBuffer(binaryData []byte) Value

ArrayBuffer returns a string value with given binary data.

func (*Context) AsyncFunction

func (ctx *Context) AsyncFunction(asyncFn func(ctx *Context, this Value, promise Value, args []Value) Value) Value

AsyncFunction returns a js async function value with given function template.

func (*Context) Atom

func (ctx *Context) Atom(v string) Atom

Atom returns a new Atom value with given string.

func (*Context) AtomIdx

func (ctx *Context) AtomIdx(idx int64) Atom

Atom returns a new Atom value with given idx.

func (*Context) BigInt64

func (ctx *Context) BigInt64(v int64) Value

BigInt64 returns a int64 value with given uint64.

func (*Context) BigUint64

func (ctx *Context) BigUint64(v uint64) Value

BigUint64 returns a uint64 value with given uint64.

func (*Context) Bool

func (ctx *Context) Bool(b bool) Value

Bool returns a bool value with given bool.

func (*Context) Close

func (ctx *Context) Close()

Free will free context and all associated objects.

func (*Context) Compile

func (ctx *Context) Compile(code string) ([]byte, error)

Compile returns a compiled bytecode with given code.

func (*Context) CompileFile

func (ctx *Context) CompileFile(code, filename string) ([]byte, error)

Compile returns a compiled bytecode with given filename.

func (*Context) Error

func (ctx *Context) Error(err error) Value

Error returns a new exception value with given message.

func (*Context) Eval

func (ctx *Context) Eval(code string) (Value, error)

Eval returns a js value with given code. Need call Free() `quickjs.Value`'s returned by `Eval()` and `EvalFile()` and `EvalBytecode()`.

func (*Context) EvalBytecode

func (ctx *Context) EvalBytecode(buf []byte) (Value, error)

EvalBytecode returns a js value with given bytecode. Need call Free() `quickjs.Value`'s returned by `Eval()` and `EvalFile()` and `EvalBytecode()`.

func (*Context) EvalFile

func (ctx *Context) EvalFile(filePath string) (Value, error)

EvalFile returns a js value with given code and filename. Need call Free() `quickjs.Value`'s returned by `Eval()` and `EvalFile()` and `EvalBytecode()`.

func (*Context) EvalMode added in v0.0.4

func (ctx *Context) EvalMode(code string, mode EvalMode) (Value, error)

EvalMode returns a js value with given code.

func (*Context) Exception

func (ctx *Context) Exception() error

Exception returns a context's exception value.

func (*Context) Float64

func (ctx *Context) Float64(v float64) Value

Float64 returns a float64 value with given float64.

func (*Context) Function

func (ctx *Context) Function(fn func(ctx *Context, this Value, args []Value) Value) Value

Function returns a js function value with given function template.

func (*Context) Globals

func (ctx *Context) Globals() Value

Global returns a context's global object.

func (*Context) Int32

func (ctx *Context) Int32(v int32) Value

Int32 returns a int32 value with given int32.

func (*Context) Int64

func (ctx *Context) Int64(v int64) Value

Int64 returns a int64 value with given int64.

func (*Context) Invoke

func (ctx *Context) Invoke(fn Value, this Value, args ...Value) Value

Invoke invokes a function with given this value and arguments.

func (*Context) Map

func (ctx *Context) Map() *Map

func (*Context) Null

func (ctx *Context) Null() Value

Null return a null value.

func (*Context) Object

func (ctx *Context) Object() Value

Object returns a new object value.

func (*Context) ParseJSON

func (ctx *Context) ParseJSON(v string) Value

ParseJson parses given json string and returns a object value.

func (*Context) ScheduleJob

func (ctx *Context) ScheduleJob(fn func())

ScheduleJob Schedule a context's job.

func (*Context) Set

func (ctx *Context) Set() *Set

func (*Context) SetInterruptHandler

func (ctx *Context) SetInterruptHandler(handler InterruptHandler)

SetInterruptHandler sets a interrupt handler.

func (*Context) String

func (ctx *Context) String(v string) Value

String returns a string value with given string.

func (*Context) Throw

func (ctx *Context) Throw(v Value) Value

Throw returns a context's exception value.

func (*Context) ThrowError

func (ctx *Context) ThrowError(err error) Value

ThrowError returns a context's exception value with given error message.

func (*Context) ThrowInternalError

func (ctx *Context) ThrowInternalError(format string, args ...interface{}) Value

ThrowInternalError returns a context's exception value with given error message.

func (*Context) ThrowRangeError

func (ctx *Context) ThrowRangeError(format string, args ...interface{}) Value

ThrowRangeError returns a context's exception value with given error message.

func (*Context) ThrowReferenceError

func (ctx *Context) ThrowReferenceError(format string, args ...interface{}) Value

ThrowReferenceError returns a context's exception value with given error message.

func (*Context) ThrowSyntaxError

func (ctx *Context) ThrowSyntaxError(format string, args ...interface{}) Value

ThrowSyntaxError returns a context's exception value with given error message.

func (*Context) ThrowTypeError

func (ctx *Context) ThrowTypeError(format string, args ...interface{}) Value

ThrowTypeError returns a context's exception value with given error message.

func (*Context) Uint32

func (ctx *Context) Uint32(v uint32) Value

Uint32 returns a uint32 value with given uint32.

func (*Context) Undefined

func (ctx *Context) Undefined() Value

Undefined return a undefined value.

func (*Context) Uninitialized

func (ctx *Context) Uninitialized() Value

Uninitialized returns a uninitialized value.

type Error

type Error struct {
	Cause string
	Stack string
}

func (Error) Error

func (err Error) Error() string

type EvalMode added in v0.0.4

type EvalMode int
const (
	JS_EVAL_TYPE_GLOBAL            EvalMode = 0
	JS_EVAL_TYPE_MODULE            EvalMode = 1
	JS_EVAL_TYPE_DIRECT            EvalMode = 2
	JS_EVAL_TYPE_INDIRECT          EvalMode = 3
	JS_EVAL_TYPE_MASK              EvalMode = 3
	JS_EVAL_FLAG_STRICT            EvalMode = 1 << 3
	JS_EVAL_FLAG_STRIP             EvalMode = 1 << 4
	JS_EVAL_FLAG_COMPILE_ONLY      EvalMode = 1 << 5
	JS_EVAL_FLAG_BACKTRACE_BARRIER EvalMode = 1 << 6
)

type InterruptHandler

type InterruptHandler func() int

InterruptHandler is a function type for interrupt handler.

return != 0 if the JS code needs to be interrupted

type JSClass

type JSClass struct {
	ClassName string
	// contains filtered or unexported fields
}

func (*JSClass) AddClassFn

func (j *JSClass) AddClassFn(fnName string, fn func(ctx *Context, this Value, args []Value) Value)

func (*JSClass) AddClassGetFn

func (j *JSClass) AddClassGetFn(fieldName string, fn func(ctx *Context, this Value, args []Value) Value)

func (*JSClass) AddClassSetFn

func (j *JSClass) AddClassSetFn(fieldName string, fn func(ctx *Context, this Value, args []Value) Value)

func (*JSClass) CreateGoJsClassObject

func (j *JSClass) CreateGoJsClassObject(args ...Value) Value

func (*JSClass) SetConstructor

func (j *JSClass) SetConstructor(fn func(ctx *Context, this Value, args []Value) interface{})

func (*JSClass) SetFinalizer

func (j *JSClass) SetFinalizer(fn func(obj interface{}))

type JSModule

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

func (*JSModule) AddExportFn

func (m *JSModule) AddExportFn(fnName string, fn func(ctx *Context, this Value, args []Value) Value)

func (*JSModule) AddExportObject added in v0.0.4

func (m *JSModule) AddExportObject(name string, object Value)

func (*JSModule) CreateExportClass

func (m *JSModule) CreateExportClass(className string) *JSClass

func (*JSModule) GetExportObject added in v0.0.4

func (m *JSModule) GetExportObject(name string) *Value

func (*JSModule) SetExportObject added in v0.0.4

func (m *JSModule) SetExportObject(name string, object Value)

type Job

type Job func()

type Loop

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

func NewLoop

func NewLoop() *Loop

type Map

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

func NewQjsMap

func NewQjsMap(value Value, ctx *Context) *Map

func (Map) Call

func (m Map) Call(funcName string, values []Value) Value

Call

@Description: call some internal methods of js
@receiver a :
@param funcName :
@param values :
@return Value

func (Map) Delete

func (m Map) Delete(key Value)

Delete

@Description:delete the value of an element by key
@receiver m :
@param key :

func (Map) ForEach

func (m Map) ForEach(forFn func(key Value, value Value))

ForEach

@Description: iterate map
@receiver m :

func (Map) Free

func (m Map) Free()

func (Map) Get

func (m Map) Get(key Value) Value

Get

@Description: get the value by key
@receiver m :
@param key :
@return Value

func (Map) Has

func (m Map) Has(key Value) bool

Has

@Description:determine whether an element exists
@receiver m :
@param key :

func (Map) Put

func (m Map) Put(key Value, value Value)

Put

@Description:
@receiver m :
@param key :
@param value :

func (Map) ToValue

func (m Map) ToValue() Value

type Runtime

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

Runtime represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.

func NewRuntime

func NewRuntime() Runtime

NewRuntime creates a new quickjs runtime.

func (Runtime) Close

func (r Runtime) Close()

Close will free the runtime pointer.

func (*Runtime) CreateGlobalClass

func (r *Runtime) CreateGlobalClass(className string) *JSClass

CreateGlobalClass add context global class

func (*Runtime) CreateModule added in v0.0.4

func (r *Runtime) CreateModule(moduleName string) *JSModule

func (Runtime) ExecuteAllPendingJobs

func (r Runtime) ExecuteAllPendingJobs() error

func (Runtime) ExecutePendingJob

func (r Runtime) ExecutePendingJob() (Context, error)

ExecutePendingJob will execute all pending jobs.

func (Runtime) IsJobPending

func (r Runtime) IsJobPending() bool

IsJobPending returns true if there is a pending job.

func (Runtime) IsLoopJobPending

func (r Runtime) IsLoopJobPending() bool

IsLoopJobPending returns true if there is a pending loop job.

func (Runtime) NewModuleContext added in v0.0.4

func (r Runtime) NewModuleContext() *Context

NewModuleContext creates a new JavaScript context. enable BigFloat/BigDecimal support and enable . enable operator overloading.

func (Runtime) NewSimpleContext added in v0.0.4

func (r Runtime) NewSimpleContext() *Context

NewSimpleContext create a simple context with no modules and custom class

func (Runtime) RunGC

func (r Runtime) RunGC()

RunGC will call quickjs's garbage collector.

func (Runtime) SetGCThreshold

func (r Runtime) SetGCThreshold(threshold int64)

SetGCThreshold the runtime's GC threshold; use -1 to disable automatic GC.

func (Runtime) SetMaxStackSize

func (r Runtime) SetMaxStackSize(stack_size uint32)

SetMaxStackSize will set max runtime's stack size; default is 255

func (Runtime) SetMemoryLimit

func (r Runtime) SetMemoryLimit(limit uint32)

SetMemoryLimit the runtime memory limit; if not set, it will be unlimit.

type Set

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

func NewQjsSet

func NewQjsSet(value Value, ctx *Context) *Set

func (Set) Add

func (s Set) Add(value Value)

Add

@Description: add element
@receiver s :
@param value :

func (Set) Delete

func (s Set) Delete(value Value)

Delete

@Description: add element
@receiver s :
@param value :

func (Set) ForEach

func (s Set) ForEach(forFn func(value Value))

ForEach

@Description: iterate set
@receiver m :

func (Set) Free

func (s Set) Free()

func (Set) Has

func (s Set) Has(value Value) bool

Has

@Description: determine whether an element exists in the set
@receiver s :
@param value :
@return bool

func (Set) ToValue

func (s Set) ToValue() Value

type Value

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

JSValue represents a Javascript value which can be a primitive type or an object. Reference counting is used, so it is important to explicitly duplicate (JS_DupValue(), increment the reference count) or free (JS_FreeValue(), decrement the reference count) JSValues.

func (Value) BigFloat

func (v Value) BigFloat() *big.Float

BigFloat returns the big.Float value of the value.

func (Value) BigInt

func (v Value) BigInt() *big.Int

BigInt returns the big.Int value of the value.

func (Value) Bool

func (v Value) Bool() bool

Bool returns the boolean value of the value.

func (Value) ByteLen

func (v Value) ByteLen() int64

ByteLen returns the length of the ArrayBuffer.

func (Value) Call

func (v Value) Call(fname string, args ...Value) Value

Call calls the function with the given arguments.

func (Value) Context

func (v Value) Context() *Context

Context represents a Javascript context.

func (Value) Delete

func (v Value) Delete(name string) bool

Delete deletes the property with the given Name.

func (Value) DeleteIdx

func (v Value) DeleteIdx(idx int64) bool

DeleteIdx deletes the property with the given index.

func (Value) Error

func (v Value) Error() error

Error returns the error value of the value.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the float64 value of the value.

func (Value) Free

func (v Value) Free()

Free the value.

func (Value) Get

func (v Value) Get(name string) Value

Get returns the value of the property with the given Name.

func (Value) GetBindGoObject

func (v Value) GetBindGoObject() (interface{}, error)

func (Value) GetIdx

func (v Value) GetIdx(idx int64) Value

GetIdx returns the value of the property with the given index.

func (Value) Has

func (v Value) Has(name string) bool

Has returns true if the value has the property with the given Name.

func (Value) HasIdx

func (v Value) HasIdx(idx int64) bool

HasIdx returns true if the value has the property with the given index.

func (Value) Int32

func (v Value) Int32() int32

Int32 returns the int32 value of the value.

func (Value) Int64

func (v Value) Int64() int64

Int64 returns the int64 value of the value.

func (Value) IsArray

func (v Value) IsArray() bool

func (Value) IsBigDecimal

func (v Value) IsBigDecimal() bool

func (Value) IsBigFloat

func (v Value) IsBigFloat() bool

func (Value) IsBigInt

func (v Value) IsBigInt() bool

func (Value) IsBool

func (v Value) IsBool() bool

func (Value) IsByteArray

func (v Value) IsByteArray() bool

IsByteArray return true if the value is array buffer

func (Value) IsError

func (v Value) IsError() bool

func (Value) IsException

func (v Value) IsException() bool

func (Value) IsFunction

func (v Value) IsFunction() bool

func (Value) IsMap

func (v Value) IsMap() bool

IsMap return true if the value is a map

func (Value) IsNull

func (v Value) IsNull() bool

func (Value) IsNumber

func (v Value) IsNumber() bool

func (Value) IsObject

func (v Value) IsObject() bool

func (Value) IsSet

func (v Value) IsSet() bool

IsSet return true if the value is a set

func (Value) IsString

func (v Value) IsString() bool

func (Value) IsSymbol

func (v Value) IsSymbol() bool

func (Value) IsUndefined

func (v Value) IsUndefined() bool

func (Value) IsUninitialized

func (v Value) IsUninitialized() bool

func (Value) JSONStringify

func (v Value) JSONStringify() string

JSONString returns the JSON string representation of the value.

func (Value) Len

func (v Value) Len() int64

Len returns the length of the array.

func (Value) PropertyNames

func (v Value) PropertyNames() ([]string, error)

PropertyNames returns the names of the properties of the value.

func (Value) Set

func (v Value) Set(name string, val Value)

Set sets the value of the property with the given Name.

func (Value) SetBindGoObject

func (v Value) SetBindGoObject(val interface{}) int32

func (Value) SetIdx

func (v Value) SetIdx(idx int64, val Value)

SetIdx sets the value of the property with the given index.

func (Value) String

func (v Value) String() string

String returns the string representation of the value.

func (Value) ToArray

func (v Value) ToArray() *Array

ToArray

@Description: return array object
@receiver v :
@return *Array

func (Value) ToByteArray

func (v Value) ToByteArray(size uint) ([]byte, error)

func (Value) ToMap

func (v Value) ToMap() *Map

ToMap

@Description: return map object
@receiver v :
@return *Map

func (Value) ToSet

func (v Value) ToSet() *Set

ToSet

@Description: return set object
@receiver v :
@return *Set

func (Value) Uint32

func (v Value) Uint32() uint32

Uint32 returns the uint32 value of the value.

Jump to

Keyboard shortcuts

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