Documentation
¶
Overview ¶
Package quickjs is a pure Go embeddable Javascript engine. It supports the ECMA script 14 (ES2023) specification including modules, asynchronous generators, proxies and BigInt.
See also the original C QuickJS library.
Internationalization ¶
The engine has no Intl. ECMA-402 is not implemented by the C library this package wraps, so a page or bundle reaching for Intl.DateTimeFormat gets a ReferenceError, and the toLocale* methods it ships ignore both their locale and their timeZone argument. Package github.com/liustack/quickjs/intl provides Intl.DateTimeFormat and Intl.NumberFormat, en-US over Go's time zone database, for a VM that opts in by calling its Register.
Performance ¶
Geomeans of time/op over a set of benchmarks, relative to CCGO, lower number is better. Detailed results available in the testdata/benchmarks directory.
CCGO: github.com/liustack/quickjs/lib GOJA: github.com/dop251/goja@v0.0.0-20260723142020-b4aef50fa347 QJS: github.com/fastschema/qjs@v0.0.6 CCGO GOJA QJS ----------------------------------------------- darwin/amd64 1.000 1.575 1.300 darwin/arm64 1.000 1.069 1.080 freebsd/amd64 1.000 1.943 1.472 (qemu) freebsd/arm64 1.000 1.803 1.030 (qemu) linux/386 1.000 3.082 95.312 (qemu) linux/amd64 1.000 1.419 1.213 linux/arm 1.000 3.859 139.339 linux/arm64 1.000 1.611 1.267 linux/loong64 1.000 1.555 85.539 linux/ppc64le 1.000 2.286 75.183 linux/riscv64 1.000 1.656 76.609 linux/s390x 1.000 2.016 68.871 (qemu) windows/amd64 1.000 2.254 1.438 windows/arm64 1.000 2.002 1.610 ----------------------------------------------- CCGO GOJA QJS
Notes ¶
Parts of the documentation were copied from the quickjs documentation, see LICENSE-QUICKJS for details.
Supported platforms and architectures ¶
These combinations of GOOS and GOARCH are currently supported
OS Arch ------------- darwin amd64 darwin arm64 freebsd amd64 freebsd arm64 linux 386 linux amd64 linux arm linux arm64 linux loong64 linux ppc64le linux riscv64 linux s390x windows amd64 windows arm64
Builders ¶
Builder results are available here.
Change Log ¶
2026-07-27: Upgrade to QuickJS release 2026-06-04. The bytecode format changed, code produced by VM.Compile using an older release will no longer load.
2025-10-10: Upgrade to QuickJS release 2025-09-13.
Example (Ping) ¶
Multiple concurrent Javascript virtual machines communicating via Go channels.
package main // import "github.com/liustack/quickjs"
import (
"fmt"
)
// Multiple concurrent Javascript virtual machines communicating via Go channels.
func main() {
tx := make(chan string, 1)
rx := make(chan string, 1)
client, _ := NewVM()
defer client.Close()
registerFuncs(client, tx, rx)
go func() { // Start the server.
server, _ := NewVM()
defer server.Close()
registerFuncs(server, rx, tx)
server.Eval("send(receive()+' reply');", EvalGlobal)
}()
fmt.Println(client.Eval("send('ping'); receive();", EvalGlobal)) // Ping the server.
}
func registerFuncs(m *VM, tx, rx chan string) {
m.RegisterFunc("send", func(s string) { tx <- s }, false)
m.RegisterFunc("receive", func() string { return <-rx }, false)
}
Output: ping reply <nil>
Index ¶
- Constants
- Variables
- func Version() string
- type Atom
- type Error
- type HostFunc
- type ModuleLoaderFunc
- type ModuleNormalizeFunc
- type Object
- type PromiseCapability
- type Undefined
- type Unsupported
- type VM
- func (m *VM) Call(function string, args ...any) (r any, err error)
- func (m *VM) CallValue(function string, args ...any) (r Value, err error)
- func (m *VM) Close() error
- func (m *VM) Compile(javascript string, flags int) ([]byte, error)
- func (m *VM) Eval(javascript string, flags int) (r any, err error)
- func (m *VM) EvalBytecode(bytecode []byte) (r any, err error)
- func (m *VM) EvalBytecodeValue(bytecode []byte) (r Value, err error)
- func (m *VM) EvalThis(obj Value, javascript string, flags int) (r any, err error)
- func (m *VM) EvalThisValue(obj Value, javascript string, flags int) (r Value, err error)
- func (m *VM) EvalValue(javascript string, flags int) (r Value, err error)
- func (m *VM) ExecutePendingJobs() (n int, err error)
- func (m *VM) GetProperty(this Value, prop Atom) (r any, err error)
- func (m *VM) GetPropertyValue(this Value, prop Atom) (r Value, err error)
- func (m *VM) GlobalObject() (r Value)
- func (m *VM) Interrupt()
- func (m *VM) NewAtom(s string) (r Atom, err error)
- func (m *VM) NewFloat64(n float64) Value
- func (m *VM) NewInt(n int) Value
- func (m *VM) NewObjectValue() (r Value, err error)
- func (m *VM) NewPromiseCapability() (*PromiseCapability, error)
- func (m *VM) NewString(s string) (r Value, err error)
- func (m *VM) RegisterFunc(name string, f any, wantThis bool) error
- func (m *VM) RegisterHostFunc(name string, fn HostFunc) error
- func (m *VM) SetCanBlock(value bool)
- func (m *VM) SetDefaultModuleLoader()
- func (m *VM) SetEvalTimeout(d time.Duration) error
- func (m *VM) SetGCThreshold(threshold uintptr)
- func (m *VM) SetMaxStackSize(threshold uintptr)
- func (m *VM) SetMemoryLimit(limit uintptr)
- func (m *VM) SetModuleLoader(loader ModuleLoaderFunc, normalize ModuleNormalizeFunc)
- func (m *VM) SetProperty(this Value, prop Atom, val any) (err error)
- func (m *VM) SetPropertyValue(this Value, prop Atom, val Value) (err error)
- func (m *VM) StdAddHelpers() error
- type Value
- func (v Value) Any() (r any, err error)
- func (v Value) Call(this Value, args ...any) (r any, err error)
- func (v Value) CallValue(this Value, args ...any) (r Value, err error)
- func (v Value) Dup() Value
- func (v *Value) Free()
- func (v Value) GetProperty(this Value, prop Atom) (r any, err error)deprecated
- func (v Value) GetPropertyValue(prop Atom) (r Value, err error)
- func (v Value) IsUndefined() bool
- func (v Value) MarshalJSON() (r []byte, err error)
- func (v Value) Property(prop Atom) (r any, err error)
- func (v Value) SetProperty(prop Atom, val any) (err error)
- func (v Value) SetPropertyValue(prop Atom, val Value) (err error)
- func (v Value) Then(onFulfilled, onRejected func(Value)) (_ Value, err error)
- func (v *Value) VM() *VM
Examples ¶
- Package (Ping)
- Object.Into
- Object.MarshalJSON
- Object.String
- VM.Call (Function)
- VM.Call (Method)
- VM.Eval (Exception)
- VM.Eval (Expression)
- VM.Eval (Object)
- VM.Interrupt
- VM.RegisterFunc (Error)
- VM.RegisterFunc (MultipleReturn)
- VM.RegisterFunc (SingleReturn)
- VM.RegisterFunc (ThisNonNull)
- VM.RegisterFunc (ThisNonNull2)
- VM.RegisterFunc (ThisNull)
- VM.RegisterFunc (ThisNull2)
- VM.RegisterFunc (Void)
- VM.SetDefaultModuleLoader
- VM.SetEvalTimeout
Constants ¶
const ( EvalGlobal = lib.MJS_EVAL_TYPE_GLOBAL // global code EvalModule = lib.MJS_EVAL_TYPE_MODULE // module code )
Eval flags.
Variables ¶
var ( // UndefinedValue is a Value representing Javascript value 'undefined'. It is // not associated with any particular VM. UndefinedValue = Value{/* contains filtered or unexported fields */} )
Functions ¶
Types ¶
type Atom ¶
Atom is an unique identifier of, for example, a string value. Atom values are VM-specific.
type Error ¶
type Error struct {
Name string // e.g. "ReferenceError", "TypeError"
Message string // the error message
Stack string // stack trace, one " at ...\n" frame per line
LineNumber int // line where the error occurred (parse errors only)
ColumnNumber int // column where the error occurred (parse errors only)
FileName string // file name where the error occurred (parse errors only)
// Uncatchable reports that the call ended with an exception no catch
// or finally block of the script ran for: the VM ran out of memory
// (OutOfMemory), Interrupt or the eval timeout stopped it
// (Interrupted), or a Go function that javascript called panicked
// (neither of the two). Whatever a script throws itself is never
// uncatchable, whatever its name and message.
Uncatchable bool
OutOfMemory bool // the VM ran out of memory (Uncatchable is true)
Interrupted bool // Interrupt or the eval timeout stopped the script (Uncatchable is true)
// contains filtered or unexported fields
}
Error holds structured information from a JavaScript exception. The error returned by VM.Eval, VM.Call and the other eval-family methods is an *Error; recover the fields with errors.As:
var e *quickjs.Error
if errors.As(err, &e) {
fmt.Println(e.Stack)
}
Its Error method reports the same message string the engine has always returned, so existing code comparing the error text keeps working.
Only parse/syntax errors carry the FileName, LineNumber and ColumnNumber properties. For runtime errors those fields are zero and the source location is available only from Stack.
Uncatchable, OutOfMemory and Interrupted tell an error the engine ended the call with from one the script threw, without comparing names or messages, which a script can copy:
if errors.As(err, &e) && e.OutOfMemory {
// the script needs a higher SetMemoryLimit
}
func ErrorFromValue ¶
ErrorFromValue extracts structured error information from JavaScript error value 'v', typically an Error instance caught from a JS exception.
'v' is borrowed: ErrorFromValue does not free it and the caller remains responsible for its lifetime. Like all VM operations it must be called from the VM's own goroutine. See Error for which fields are populated.
A field that cannot be read is left empty: the string form of 'v' (the result of Error) when its toString throws, a property whose getter throws, and any string field when there is no memory for its UTF-8 copy. The exception that reading throws is dropped, so ErrorFromValue leaves no exception pending.
type HostFunc ¶
HostFunc is a Go function exposed to JavaScript by RegisterHostFunc. It receives the call arguments already converted to Go values and returns a single value to convert back to JS, or a non-nil error which is thrown as a JavaScript exception.
type ModuleLoaderFunc ¶
ModuleLoaderFunc defines the signature for a custom module loader. It must return the module's source code (Javascript).
type ModuleNormalizeFunc ¶
ModuleNormalizeFunc defines the signature for a custom module normalizer. It receives the base name (the module making the import) and the requested name, and should return the normalized, absolute module name.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object represents the value of a Javascript object, but not the javascript object instance itself. Do not compare instances of Object.
func (*Object) Into ¶
Into helps you quickly deserialize an Object into a native Go type. This uses encoding/json.Unmarshal internally, so the target type must be compatible with that package.
Example ¶
JSON unmarshalling into native Go struct.
m, _ := NewVM()
defer m.Close()
obj, _ := m.Eval("obj = {a: 42+314, b: 'foo'}; obj;", EvalGlobal)
var dst struct {
A int `json:"a"`
B string `json:"b"`
}
obj.(*Object).Into(&dst)
fmt.Printf("%#v\n", dst)
Output: struct { A int "json:\"a\""; B string "json:\"b\"" }{A:356, B:"foo"}
func (*Object) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler.
Example ¶
JSON marshalling.
m, _ := NewVM()
defer m.Close()
obj, _ := m.Eval("obj = {a: 42+314, b: 'foo'}; obj;", EvalGlobal)
s, _ := (obj.(*Object).MarshalJSON())
fmt.Printf("%s\n", s)
Output: {"a":356,"b":"foo"}
type PromiseCapability ¶
PromiseCapability holds a Promise along with its resolve and reject functions, obtained without evaluating JS source.
func (*PromiseCapability) Dup ¶
func (pc *PromiseCapability) Dup() *PromiseCapability
Dup duplicates the capability — its promise and both resolving functions — returning a new PromiseCapability that must itself be released via Free.
func (*PromiseCapability) Free ¶
func (pc *PromiseCapability) Free()
Free releases all values held by the capability.
type Unsupported ¶
type Unsupported struct{}
Unsupported represents an unsupported Javascript value.
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM represents a Javascript context (or Realm). Each VM has its own global objects and system objects.
Note: VM is not safe for concurrent use by multiple goroutines.
func (*VM) Call ¶
Call evaluates 'function(args...)' and returns the resulting (value, error).
Argument types must be one of:
Go argument type Javascript argument type ---------------------------------------------------------------- nil null Undefined undefined string string int*/uint* (value in int32 range) int int*/uint* (value out of int32 range) float bool bool float64 float64 *math/big.Int BigInt *Object object Value native Javascript Value any other type object from JSON produced by encoding.json/Marshall(arg)
Example (Function) ¶
Call a Javascript function.
m, _ := NewVM()
defer m.Close()
fmt.Println(m.Call("parseInt", "1234"))
Output: 1234 <nil>
Example (Method) ¶
Call a Javascript method.
m, _ := NewVM()
defer m.Close()
fmt.Println(m.Call("Math.abs", -1234))
Output: 1234 <nil>
func (*VM) CallValue ¶
CallValue is like Call but returns (Value, error) like EvalValue
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) Close ¶
Close releases the resources held by 'm'. That includes the module loader and normalizer set by VM.SetModuleLoader: Close removes them, so a closed VM keeps nothing they close over reachable.
func (*VM) Compile ¶
Compile compiles the bytecode representation of the passed script.
The returned bytecode can be later executed using EvalBytecode or EvalBytecodeValue.
Note: The bytecode format is linked to a given QuickJS version.
func (*VM) Eval ¶
Eval evaluates a script or module source in 'javascript'.
Javascript result type Go result type Go result error ------------------------------------------------------------------------------- exception nil non-nil null nil nil undefined Undefined nil string string nil int int nil bool bool nil float64 float64 nil BigInt *math/big.Int nil object *Object nil any other type Unsupported nil
Example (Exception) ¶
Getting exception.
m, _ := NewVM()
defer m.Close()
fmt.Println(m.Eval("throw new Error('failed');", EvalGlobal))
Output: <nil> Error: failed
Example (Expression) ¶
Evaluate a simple Javascript expression.
m, _ := NewVM()
defer m.Close()
fmt.Println(m.Eval("1+2", EvalGlobal))
Output: 3 <nil>
Example (Object) ¶
Object example.
m, _ := NewVM()
defer m.Close()
fmt.Println(m.Eval("obj = {a: 42+314, b: 'foo'}; obj;", EvalGlobal))
Output: {"a":356,"b":"foo"} <nil>
func (*VM) EvalBytecode ¶
EvalBytecode is like Eval but uses QuickJS bytecode.
Note: The bytecode format is linked to a given QuickJS version. Moreover, no security check is done before its execution. Hence the bytecode should not be loaded from untrusted sources.
Note: Javascript 'this' is always set to the global context.
func (*VM) EvalBytecodeValue ¶
EvalBytecodeValue is like EvalValue but operates on QuickJS bytecode.
Exceptions thrown during evaluation of the script are returned as Go errors.
If no error is returned, the caller must properly handle the returned Value using Dup/Free.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) EvalThisValue ¶
EvalThisValue is like EvalValue but sets javascript 'this' to obj.
func (*VM) EvalValue ¶
EvalValue evaluates a script or module source in 'javascript' and returns the resulting Value, or an error, if any.
Exceptions thrown during evaluation of the script are returned as Go errors.
If no error is returned, the caller must properly handle the returned Value using Dup/Free.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) ExecutePendingJobs ¶
ExecutePendingJobs runs queued Javascript jobs — Promise reactions (the .then/.catch/.finally callbacks) and async/await continuations — until the queue is empty, returning the number of jobs executed. The runtime does not run these itself: an embedding event loop calls ExecutePendingJobs at the end of each turn to drain the microtask queue (so a resolved Promise's .then runs, a pending await resumes, etc.). An exception thrown by a job is returned as an error — the first one — while the remaining jobs still run; a self-feeding job queue is bounded so it cannot hang the host.
func (*VM) GetProperty ¶
GetProperty returns this.prop.
func (*VM) GetPropertyValue ¶
GetPropertyValue returns this.prop.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) GlobalObject ¶
GlobalObject returns m's global object.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) Interrupt ¶
func (m *VM) Interrupt()
Interrupt requests 'm' to interrupt Javascript evaluation. Interrupt can be called asynchronously at any time m is evaluating a script. The request stays pending until the outermost call into javascript returns, so javascript a host function evaluates on m meanwhile does not clear it.
Example ¶
const timeout = time.Second
m, _ := NewVM()
defer m.Close()
go func() {
time.Sleep(timeout)
m.Interrupt()
}()
t0 := time.Now()
r, err := m.Eval(`
function f() {
var sink;
for (var i = 0; i < 10000; i++) {
sink += 42;
sink -= 42;
}
}
(function() {
for (var i = 0; i < 10000; i++) {
f();
}
return 42;
})();
`, EvalGlobal)
d := time.Since(t0)
step := timeout / 5
d = d / step * step
fmt.Println(r, err, d)
Output: <nil> InternalError: interrupted 1s
func (*VM) NewFloat64 ¶
NewFloat64 returns a new Value from 'n'.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) NewInt ¶
NewInt returns a new Value from 'n'.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) NewObjectValue ¶
NewObjectValue returns a new Value representing '{}'.
func (*VM) NewPromiseCapability ¶
func (m *VM) NewPromiseCapability() (*PromiseCapability, error)
NewPromiseCapability creates a new JavaScript Promise along with its resolve and reject functions.
The returned PromiseCapability must be freed via Free when no longer needed.
To expose a promise to JS from a Go function registered with RegisterFunc, return pc.Promise from the Go function. The resolve/reject values captured by the closure need no Dup because the C API already holds a reference for each:
m.RegisterFunc("delayed", func(delay int) Value {
pc, err := m.NewPromiseCapability()
if err != nil { panic(err) }
youreventloop.Schedule(func() {
time.Sleep(time.Duration(delay) * time.Millisecond)
pc.Resolve.Call(UndefinedValue, "done")
pc.Resolve.Free()
pc.Reject.Free()
// pc.Promise.Free() // <- only if returned pc.Promise.Dup()!
})
return pc.Promise
}, false)
func (*VM) NewString ¶
NewString returns a new Value from 's'.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*VM) RegisterFunc ¶
RegisterFunc registers a Go function 'f' and makes it callable from Javascript.
The 'f' argument can be a regular Go function, a closure, a method expression or a method value. All of them are called 'Go function' below.
The Go function can have zero or more parameters. If 'wantThis' is true then the first parameter of the Go function will get the Javascript value of 'this'. Depending on context, 'this' can be Javascript null or undefined.
Go functions with multiple results return them as an Javascript array.
Go nil errors are converted to Javascript null.
Go non-nil errors are converted to Javascript strings using the Error() method.
Any Go <-> Javascript failing type conversion between arguments/return values throws a Javascript type error exception.
There is a limit on the total number of currently registered Go functions.
Note: The 'name' argument should be a valid Javascript identifier. It is not currently enforced but this may change later.
For binding host objects (a DOM, console, fetch, …) prefer RegisterHostFunc, which gives the more convenient single-return / error-throws-exception shape.
Example (Error) ¶
Call error returning Go function from Javascript.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(a int) error {
if a < 0 {
return fmt.Errorf("negative")
}
return nil
}, false)
fmt.Println(m.Eval("gofunc(-1)", EvalGlobal))
fmt.Println(m.Eval("gofunc(1)", EvalGlobal))
Output: negative <nil> <nil> <nil>
Example (MultipleReturn) ¶
Call multiple return Go function from Javascript.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(a, b int) (int, int) { return 10 * a, 100 * b }, false)
fmt.Println(m.Eval("gofunc(2, 3)", EvalGlobal))
Output: [20,300] <nil>
Example (SingleReturn) ¶
Call single return Go function from Javascript.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(a, b, c int) int { return a + b*c }, false)
fmt.Println(m.Eval("gofunc(2, 3, 5)", EvalGlobal))
Output: 17 <nil>
Example (ThisNonNull) ¶
Passing Javascript 'this' to a Go function.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(this any) any { return this }, true)
fmt.Println(m.Eval("var obj = { foo: 314, method: gofunc }; obj.method()", EvalGlobal))
Output: {"foo":314} <nil>
Example (ThisNonNull2) ¶
Passing Javascript 'this' to a Go function.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(this any, n int) (any, int) { return this, 10 * n }, true)
fmt.Println(m.Eval("var obj = { foo: 314, method: gofunc }; obj.method(42)", EvalGlobal))
Output: [{"foo":314},420] <nil>
Example (ThisNull) ¶
Passing undefined Javascript 'this' to a Go function.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(this any) any { return this }, true)
fmt.Println(m.Eval("gofunc()", EvalGlobal))
Output: undefined <nil>
Example (ThisNull2) ¶
Passing undefined Javascript 'this' to a Go function.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(this any, n int) (any, int) { return this, 10 * n }, true)
fmt.Println(m.Eval("gofunc(42)", EvalGlobal))
Output: [null,420] <nil>
Example (Void) ¶
Call void Go function from Javascript.
m, _ := NewVM()
defer m.Close()
m.RegisterFunc("gofunc", func(a, b, c int) { fmt.Println(a + b*c) }, false)
fmt.Println(m.Eval("gofunc(2, 3, 5)", EvalGlobal))
Output: 17 undefined <nil>
func (*VM) RegisterHostFunc ¶
RegisterHostFunc exposes fn to JavaScript as a global function named name, using the calling convention an embedding host wants: every JS call argument arrives in the single []any slice (already converted to Go values), fn returns exactly one value to convert back to JS, and a non-nil error is thrown as a JavaScript exception.
This differs from RegisterFunc, whose reflection mapping turns a Go multiple-return into a JS array and converts a returned error like any other value (so a (value, error) pair becomes the array [value, error] rather than a value-or-throw). RegisterHostFunc is the better fit for binding host objects; RegisterFunc remains for direct, typed Go calls.
func (*VM) SetCanBlock ¶
SetCanBlock configures m's blocking mode.
func (*VM) SetDefaultModuleLoader ¶
func (m *VM) SetDefaultModuleLoader()
SetDefaultModuleLoader will enable loading module using the default module loader.
Example ¶
Enabling the module loader.
m, _ := NewVM()
defer m.Close()
m.SetDefaultModuleLoader()
// testdata/power.js:
// export const name = "Power";
//
// export function square(x) {
// return x*x;
// }
//
// export function cube(x) {
// return x*x*x;
// }
m.Eval("import * as Power from './testdata/power.js'; globalThis.Power = Power;", EvalModule)
fmt.Println(m.Eval("[Power.square(2), Power.cube(2)];", EvalGlobal))
Output: [4,8] <nil>
func (*VM) SetEvalTimeout ¶
SetEvalTimeout sets the timeout for the various eval functions. Passing zero 'd' disables timeouts. The timeout starts with the outermost call into javascript: a host function that evaluates more javascript on m runs inside the time left to the call that got it there. The timeout runs on the monotonic clock, so setting the system clock does not move it. A timeout too long for that clock (about 292 years) never fires.
Example ¶
m, _ := NewVM()
defer m.Close()
for _, timeout := range []time.Duration{100 * time.Millisecond, time.Second, 3 * time.Second} {
m.SetEvalTimeout(timeout)
t0 := time.Now()
r, err := m.Eval(`
function f() {
var sink;
for (var i = 0; i < 10000; i++) {
sink += 42;
sink -= 42;
}
}
(function() {
for (var i = 0; i < 10000; i++) {
f();
}
return 42;
})();
`, EvalGlobal)
d := time.Since(t0)
min := timeout / 2
max := timeout * 3 / 2
fmt.Println(r, err, timeout, d >= min && d <= max)
}
Output: <nil> InternalError: interrupted 100ms true <nil> InternalError: interrupted 1s true <nil> InternalError: interrupted 3s true
func (*VM) SetGCThreshold ¶
SetGCThreshold sets the memory threshold at which a GC will be performed, in bytes.
func (*VM) SetMaxStackSize ¶
SetMaxStackSize limits the JS call depth, in libc.TLS stack slots (~one per JS call frame in the CGo-free port). Deep recursion then throws a catchable RangeError instead of overflowing the Go goroutine stack. 0 restores the MaxStackSlots default.
func (*VM) SetMemoryLimit ¶
SetMemoryLimit limits m's maxmimum memory usage, in bytes. Small values of 'limit' are not honored because the VM needs to allocate memory also for the exception object itself. The particular value of "small" is unspecified and subject to change without notice.
func (*VM) SetModuleLoader ¶
func (m *VM) SetModuleLoader(loader ModuleLoaderFunc, normalize ModuleNormalizeFunc)
SetModuleLoader configures custom Go functions to load and normalize Javascript modules. Passing nil for 'loader' removes the custom loader, and so does Close; until then the engine keeps 'loader' and 'normalize', and whatever they close over, reachable. If 'normalize' is nil, the engine falls back to the default QuickJS module normalizer.
func (*VM) SetProperty ¶
SetProperty sets this.prop = val.
func (*VM) SetPropertyValue ¶
SetPropertyValue sets this.prop = val.
func (*VM) StdAddHelpers ¶
StdAddHelpers adds the 'print' and 'console' global objects to 'm'.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents a native Javascript value. Values are reference counted and their lifetime is managed by an independent Javascript garbage collector. To avoid memory corruption/leaks caused by tripping the Javascript GC, a Value must not
- be copied. Use the Dup method instead.
- become unreachable without calling its Free method.
- be used after its Free() method was called.
- outlive its VM.
It is recommended to use native Go values instead of Value where possible.
When passing a Value down the call stack use Dup. For example in main
v, _ := EvalValue(someScript) defer v.Free() foo(v.Dup()) // Instead of foo(v)
In 'foo' Free must be used. For example
func foo(v Value) {
defer v.Free()
...
}
This ensures the/only topmost Free marks 'v' eligible for garbage collection.
Beware that the correct setup/handling becomes more complicated when using closures, Values are sent through a channel etc. In particular, if a goroutine 1 passes a Dup of 'v' to goroutine 2 and goroutine 1 completes and thus frees 'v' before goroutine 2 completes, the reference counting mechanism will fail. In other words, every Free must be strictly paired with the Dup that preceded obtaining the Value and the Dup/Free calls must respect the original nesting. This is correct.
Dup // in main
Dup // in foo
Free // in foo
Free // in main
This will fail, for example in the above discussed goroutines scenario.
Dup // in g1
Dup // in g2
Free // in g1
Free // in g2
The fix might be in this case to arrange goroutine 1 to wait for goroutine 2 to complete before executing Free in goroutine 1.
See TestMemgrind for an example of how to detect memory leaks caused not only by improper Value use.
func (Value) Any ¶
Any attemtps to convert 'v' to any using the same rules as there are for the return value of VM.Eval.
func (Value) Call ¶
Call evaluates 'v(args...)' with 'this' as the receiver and returns the resulting (value, error). 'v' must be a Javascript function. Pass UndefinedValue as 'this' when the function does not use it.
resolve.Call(quickjs.UndefinedValue, "done") reject.Call(quickjs.UndefinedValue, err.Error())
Argument types are converted using the same rules as in VM.Call and the result is unpacked using the same rules as there are for the return value of VM.Eval. Note that a Go error is not one of the recognized types, so it is marshaled to JSON like any other value — and because the standard error types have no exported fields that yields '{}'. Pass err.Error() instead.
Note: A Value passed to a Go function registered by VM.RegisterFunc is valid only for the duration of that call. To call it later, for example to settle a promise after the Go function has returned, retain it using Value.Dup and release it using Value.Free when done.
Note: See the Value documentation for details about manual memory management of Value objects.
func (Value) CallValue ¶
CallValue is like Value.Call but returns (Value, error) like VM.EvalValue.
Note: See the Value documentation for details about manual memory management of Value objects.
func (*Value) Free ¶
func (v *Value) Free()
Free marks 'v' as no longer used and updates its reference count. 'v' must not be used afterwards.
func (Value) GetProperty
deprecated
func (Value) GetPropertyValue ¶
GetPropertyValue returns v.prop.
func (Value) IsUndefined ¶
IsUndefined reports whether 'v' represents the Javascript value 'undefined'.
func (Value) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler.
func (Value) Property ¶
Property returns v.prop. It is Value.GetProperty without the vestigial 'this' argument.
func (Value) SetProperty ¶
SetProperty sets v.prop = val.
func (Value) SetPropertyValue ¶
SetPropertyValue sets v.prop = val.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package fmt exposes some functionality of the Go stdlib fmt package to a quickjs.VM.
|
Package fmt exposes some functionality of the Go stdlib fmt package to a quickjs.VM. |
|
internal
|
|
|
fixlib
command
Command fixlib reapplies this fork's changes to the engine under lib/ after its files were taken from upstream libquickjs, as happens when a git subtree pull hits a conflict in a generated ccgo_*.go file and the upstream version is checked out.
|
Command fixlib reapplies this fork's changes to the engine under lib/ after its files were taken from upstream libquickjs, as happens when a git subtree pull hits a conflict in a generated ccgo_*.go file and the upstream version is checked out. |
|
Package intl implements ECMA-402, the ECMAScript Internationalization API, for a quickjs.VM.
|
Package intl implements ECMA-402, the ECMAScript Internationalization API, for a quickjs.VM. |
|
internal/gen
command
Command gen builds intl/cldr.bin, the locale data package intl formats with, from the JSON distribution of CLDR (github.com/unicode-org/cldr-json).
|
Command gen builds intl/cldr.bin, the locale data package intl formats with, from the JSON distribution of CLDR (github.com/unicode-org/cldr-json). |
|
Package libquickjs is a pure Go embeddable Javascript engine.
|
Package libquickjs is a pure Go embeddable Javascript engine. |
