Documentation
¶
Overview ¶
Package quickjs Go bindings to QuickJS: a fast, small, and embeddable ES2020 JavaScript interpreter
Example ¶
// Create a new runtime
rt := quickjs.NewRuntime()
defer rt.Close()
// Create a new context
ctx := rt.NewContext()
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 *quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.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 *quickjs.Context, this quickjs.Value, promise quickjs.Value, args []quickjs.Value) quickjs.Value {
return promise.Call("resolve", ctx.String("Hello Async Function!"))
}))
ret, _ := ctx.Eval(`
var ret;
testAsync().then(v => ret = v)
`)
defer ret.Free()
for {
_, err := rt.ExecutePendingJob()
if err == io.EOF {
err = nil
break
}
}
asyncRet, _ := ctx.Eval("ret")
defer asyncRet.Free()
fmt.Println(asyncRet.String())
Output: Hello Javascript! Hello Golang! Hello Async Function!
Index ¶
- type Atom
- type Context
- func (ctx *Context) Array() Value
- func (ctx *Context) AsyncFunction(asyncFn func(ctx *Context, this Value, promise Value, args []Value) Value) Value
- func (ctx *Context) Atom(v string) Atom
- func (ctx *Context) AtomIdx(idx int64) Atom
- func (ctx *Context) BigInt64(v int64) Value
- func (ctx *Context) BigUint64(v uint64) Value
- func (ctx *Context) Bool(b bool) Value
- func (ctx *Context) Close()
- func (ctx *Context) Compile(code string) ([]byte, error)
- func (ctx *Context) CompileFile(code, filename string) ([]byte, error)
- func (ctx *Context) Error(err error) Value
- func (ctx *Context) Eval(code string) (Value, error)
- func (ctx *Context) EvalBytecode(buf []byte) (Value, error)
- func (ctx *Context) EvalFile(code, filename string) (Value, error)
- func (ctx *Context) Exception() error
- func (ctx *Context) Float64(v float64) Value
- func (ctx *Context) Function(fn func(ctx *Context, this Value, args []Value) Value) Value
- func (ctx *Context) Globals() Value
- func (ctx *Context) Int32(v int32) Value
- func (ctx *Context) Int64(v int64) Value
- func (ctx *Context) Invoke(fn Value, this Value, args ...Value) (Value, error)
- func (ctx *Context) Null() Value
- func (ctx *Context) Object() Value
- func (ctx *Context) String(v string) Value
- func (ctx *Context) Throw(v Value) Value
- func (ctx *Context) ThrowError(err error) Value
- func (ctx *Context) ThrowInternalError(format string, args ...interface{}) Value
- func (ctx *Context) ThrowRangeError(format string, args ...interface{}) Value
- func (ctx *Context) ThrowReferenceError(format string, args ...interface{}) Value
- func (ctx *Context) ThrowSyntaxError(format string, args ...interface{}) Value
- func (ctx *Context) ThrowTypeError(format string, args ...interface{}) Value
- func (ctx *Context) Uint32(v uint32) Value
- func (ctx *Context) Undefined() Value
- func (ctx *Context) Uninitialized() Value
- type Error
- type Runtime
- type Value
- func (v Value) BigFloat() *big.Float
- func (v Value) BigInt() *big.Int
- func (v Value) Bool() bool
- func (v Value) Call(fname string, args ...Value) Value
- func (v Value) Context() *Context
- func (v Value) Delete(name string) bool
- func (v Value) DeleteIdx(idx int64) bool
- func (v Value) Error() error
- func (v Value) Float64() float64
- func (v Value) Free()
- func (v Value) Get(name string) Value
- func (v Value) GetIdx(idx int64) Value
- func (v Value) Has(name string) bool
- func (v Value) HasIdx(idx int64) bool
- func (v Value) Int32() int32
- func (v Value) Int64() int64
- func (v Value) IsArray() bool
- func (v Value) IsBigDecimal() bool
- func (v Value) IsBigFloat() bool
- func (v Value) IsBigInt() bool
- func (v Value) IsBool() bool
- func (v Value) IsError() bool
- func (v Value) IsException() bool
- func (v Value) IsFunction() bool
- func (v Value) IsNull() bool
- func (v Value) IsNumber() bool
- func (v Value) IsObject() bool
- func (v Value) IsString() bool
- func (v Value) IsSymbol() bool
- func (v Value) IsUndefined() bool
- func (v Value) IsUninitialized() bool
- func (v Value) Len() int64
- func (v Value) PropertyNames() ([]string, error)
- func (v Value) Set(name string, val Value)
- func (v Value) SetIdx(idx int64, val Value)
- func (v Value) String() string
- func (v Value) Uint32() uint32
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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.
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) AsyncFunction ¶ added in v0.1.2
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) Close ¶
func (ctx *Context) Close()
Free will free context and all associated objects.
func (*Context) CompileFile ¶
Compile returns a compiled bytecode with given filename.
func (*Context) Eval ¶
Eval returns a js value with given code. Need call Free() `quickjs.Value`'s returned by `Eval()` and `EvalFile()` and `EvalBytecode()`.
func (*Context) EvalBytecode ¶
EvalBytecode returns a js value with given bytecode. Need call Free() `quickjs.Value`'s returned by `Eval()` and `EvalFile()` and `EvalBytecode()`.
func (*Context) EvalFile ¶
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) Invoke ¶ added in v0.1.3
Invoke invokes a function with given this value and arguments.
func (*Context) ThrowError ¶
ThrowError returns a context's exception value with given error message.
func (*Context) ThrowInternalError ¶
ThrowInternalError returns a context's exception value with given error message.
func (*Context) ThrowRangeError ¶
ThrowRangeError returns a context's exception value with given error message.
func (*Context) ThrowReferenceError ¶
ThrowReferenceError returns a context's exception value with given error message.
func (*Context) ThrowSyntaxError ¶
ThrowSyntaxError returns a context's exception value with given error message.
func (*Context) ThrowTypeError ¶
ThrowTypeError returns a context's exception value with given error message.
func (*Context) Uninitialized ¶
Uninitialized returns a uninitialized 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 (Runtime) ExecutePendingJob ¶ added in v0.1.1
ExecutePendingJob will execute all pending jobs.
func (Runtime) NewContext ¶
NewContext creates a new JavaScript context. enable BigFloat/BigDecimal support and enable . enable operator overloading.
func (Runtime) SetGCThreshold ¶
SetGCThreshold the runtime's GC threshold; use -1 to disable automatic GC.
func (Runtime) SetMaxStackSize ¶
SetMaxStackSize will set max runtime's stack size; default is 255
func (Runtime) SetMemoryLimit ¶
SetMemoryLimit the runtime memory limit; if not set, it will be unlimit.
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) IsBigDecimal ¶
func (Value) IsBigFloat ¶
func (Value) IsException ¶
func (Value) IsFunction ¶
func (Value) IsUndefined ¶
func (Value) IsUninitialized ¶
func (Value) PropertyNames ¶
PropertyNames returns the names of the properties of the value.