src/burrito

Search:
Group by:
Source   Edit  

Burrito - QuickJS Nim Wrapper

A wrapper for the QuickJS JavaScript engine. This module provides comprehensive functionality to create JS contexts, evaluate JavaScript code, and expose Nim functions to JavaScript.

Example: Basic Usage

import burrito

# Create a QuickJS instance
var js = newQuickJS()

# Evaluate JavaScript expressions
echo js.eval("2 + 3")                    # Output: 5
echo js.eval("'Hello ' + 'World!'")      # Output: Hello World!

# Expose Nim functions to JavaScript
proc greet(ctx: ptr JSContext, name: JSValue): JSValue =
  let nameStr = toNimString(ctx, name)
  result = nimStringToJS(ctx, "Hello from Nim, " & nameStr & "!")

js.registerFunction("greet", greet)
echo js.eval("greet('Burrito')")        # Output: Hello from Nim, Burrito!
js.close()

Example: Embedded REPL

import burrito

# Create QuickJS with full standard library support
var js = newQuickJS(configWithBothLibs())

# Add custom functions accessible in the REPL
proc getCurrentTime(ctx: ptr JSContext): JSValue =
  nimStringToJS(ctx, now().format("yyyy-MM-dd HH:mm:ss"))

js.registerFunction("getCurrentTime", getCurrentTime)

# Load and start the REPL
let replCode = readFile("quickjs/repl.js")
discard js.evalModule(replCode, "<repl>")
js.runPendingJobs()
js.processStdLoop()  # Interactive REPL runs here
js.close()

Example: Bytecode Compilation

import burrito

var js = newQuickJS()

# Add a simple function
proc multiply(ctx: ptr JSContext, a: JSValue, b: JSValue): JSValue =
  let numA = toNimFloat(ctx, a)
  let numB = toNimFloat(ctx, b)
  result = nimFloatToJS(ctx, numA * numB)

js.registerFunction("multiply", multiply)

# Compile JavaScript to bytecode
let code = "multiply(5, 6);"
let bytecode = js.compileToBytecode(code)

# Execute bytecode (faster, no compilation needed) - works with any bytecode!
let result = js.evalBytecode(bytecode)

# For REPL: Use pre-compiled bytecode (run: nimble compile_repl_bytecode)
import ../build/src/repl_bytecode
discard js.evalBytecodeModule(qjsc_replBytecode)
js.close()

Types

BurritoContextData = object
  functions*: Table[cint, NimFunctionEntry]
  context*: ptr JSContext
Source   Edit  
JSAtom = uint32
Source   Edit  
JSAutoDetectedValue = object
  case kind*: JSValueKind
  of jvkString:
    strVal*: string
  of jvkInt:
    intVal*: int
  of jvkFloat:
    floatVal*: float64
  of jvkBool:
    boolVal*: bool
  of jvkNull:
    nil
  of jvkUndefined:
    nil
  of jvkObject:
    objRepr*: string
  of jvkArray:
    arrRepr*: string
Source   Edit  
JSAutoValue = object
Source   Edit  
JSCFunction = proc (ctx: ptr JSContext; thisVal: JSValueConst; argc: cint;
                    argv: ptr JSValueConst): JSValue {.cdecl.}
Source   Edit  
JSCFunctionData = proc (ctx: ptr JSContext; thisVal: JSValueConst; argc: cint;
                        argv: ptr JSValueConst; magic: cint; data: ptr JSValue): JSValue {.
    cdecl.}
Source   Edit  
JSCFunctionMagic = proc (ctx: ptr JSContext; thisVal: JSValueConst; argc: cint;
                         argv: ptr JSValueConst; magic: cint): JSValue {.cdecl.}
Source   Edit  
JSClassID = uint32
Source   Edit  
JSContext {.importc: "struct JSContext", header: "quickjs/quickjs.h".} = object
Source   Edit  
JSException = object of CatchableError
  jsValue*: JSValue
Source   Edit  
JSModuleDef {.importc: "struct JSModuleDef", header: "quickjs/quickjs.h".} = object
Source   Edit  
JSRuntime {.importc: "struct JSRuntime", header: "quickjs/quickjs.h".} = object
Source   Edit  
JSValue {.importc, header: "quickjs/quickjs.h".} = object
Source   Edit  
JSValueKind = enum
  jvkString, jvkInt, jvkFloat, jvkBool, jvkNull, jvkUndefined, jvkObject,
  jvkArray
Source   Edit  
NimFunction0 = proc (ctx: ptr JSContext): JSValue {.nimcall.}
Source   Edit  
NimFunction1 = proc (ctx: ptr JSContext; arg: JSValue): JSValue {.nimcall.}
Source   Edit  
NimFunction2 = proc (ctx: ptr JSContext; arg1, arg2: JSValue): JSValue {.nimcall.}
Source   Edit  
NimFunction3 = proc (ctx: ptr JSContext; arg1, arg2, arg3: JSValue): JSValue {.
    nimcall.}
Source   Edit  
NimFunctionEntry = object
  case kind*: NimFunctionKind
  of nimFunc0:
    func0*: NimFunction0
  of nimFunc1:
    func1*: NimFunction1
  of nimFunc2:
    func2*: NimFunction2
  of nimFunc3:
    func3*: NimFunction3
  of nimFuncVar:
    funcVar*: NimFunctionVariadic
Source   Edit  
NimFunctionKind = enum
  nimFunc0 = 0, nimFunc1, nimFunc2, nimFunc3, nimFuncVar
Source   Edit  
NimFunctionVariadic = proc (ctx: ptr JSContext; args: seq[JSValue]): JSValue {.
    nimcall.}
Source   Edit  
QuickJS = object
  runtime*: ptr JSRuntime
  context*: ptr JSContext
  contextData*: ptr BurritoContextData
  nextFunctionId*: cint
  config*: QuickJSConfig

QuickJS wrapper object containing runtime and context

⚠️ THREAD SAFETY WARNING: QuickJS instances are NOT thread-safe. You must either:

  1. Access each QuickJS instance from only one thread (recommended), OR
  2. Use external synchronization (Lock/Mutex) around ALL QuickJS method calls if sharing an instance across threads

Each thread should ideally have its own QuickJS instance for best performance.

Source   Edit  
QuickJSConfig = object
  includeStdLib*: bool       ## Include std module (default: false)
  includeOsLib*: bool        ## Include os module (default: false)
  enableStdHandlers*: bool   ## Enable std event handlers (default: false)
Configuration for QuickJS instance creation Source   Edit  

Procs

proc `$`(val: JSAutoDetectedValue): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc `$`(val: JSAutoValue): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc `[]=`[T](ctx: ptr JSContext; name: string; value: T)
Set a global property value using idiomatic syntax for assignment to JavaScript globals Source   Edit  
proc autoDetect(ctx: ptr JSContext; name: string): JSAutoDetectedValue {.
    ...raises: [JSException], tags: [], forbids: [].}
Automatically detect the JavaScript type and return appropriate Nim value Source   Edit  
proc canUseOsLib(js: QuickJS): bool {....raises: [], tags: [], forbids: [].}
Check if os module is available in this QuickJS instance Source   Edit  
proc canUseStdLib(js: QuickJS): bool {....raises: [], tags: [], forbids: [].}
Check if std module is available in this QuickJS instance Source   Edit  
proc close(js: var QuickJS) {....raises: [], tags: [], forbids: [].}
Clean up QuickJS instance with proper sequence to avoid memory issues Source   Edit  
proc collectArray[T](ctx: ptr JSContext; arr: JSValueConst; target: typedesc[T]): seq[
    T]
Collect array elements into a Nim sequence with automatic memory management Source   Edit  
proc compileToBytecode(js: QuickJS; code: string; filename: string = "<input>";
                       isModule: bool = false): seq[byte] {.
    ...raises: [JSException], tags: [], forbids: [].}

Compile JavaScript code to bytecode format Returns a byte value containing the compiled bytecode

The bytecode can be saved and later executed with evalBytecode Note: The bytecode format is tied to the QuickJS version

Source   Edit  
proc configWithBothLibs(): QuickJSConfig {....raises: [], tags: [], forbids: [].}
Create configuration with both std and os modules enabled Source   Edit  
proc configWithOsLib(): QuickJSConfig {....raises: [], tags: [], forbids: [].}
Create configuration with os module enabled Source   Edit  
proc configWithStdLib(): QuickJSConfig {....raises: [], tags: [], forbids: [].}
Create configuration with std module enabled Source   Edit  
proc defaultConfig(): QuickJSConfig {....raises: [], tags: [], forbids: [].}
Create default configuration (no std/os modules) Source   Edit  
proc detectType(ctx: ptr JSContext; name: string): JSAutoDetectedValue {.
    ...raises: [JSException], tags: [], forbids: [].}
Detect the actual JavaScript type and return the appropriate Nim value Usage: let value = ctx.detectType("someProperty") echo "Type: ", value.kind, ", Value: ", value Source   Edit  
proc eval(js: QuickJS; code: string; filename: string = "<eval>"): string {.
    ...raises: [], tags: [], forbids: [].}
Evaluate JavaScript code and return result as string Source   Edit  
proc evalBytecode(js: QuickJS; bytecode: openArray[byte]; loadOnly: bool = false): string {.
    ...raises: [ValueError, JSException], tags: [], forbids: [].}

Evaluate JavaScript bytecode - works with any bytecode (qjsc or compileToBytecode)

Parameters:

  • bytecode: The compiled bytecode to execute
  • loadOnly: If true, load but don't execute (for module dependencies)

This automatically detects the bytecode type and handles it appropriately. Works with both std/os libraries (configWithBothLibs) and isolated mode (defaultConfig). Returns the result as a string, or empty string for void operations.

Source   Edit  
proc evalBytecodeModule(js: QuickJS; bytecode: openArray[byte]): string {.
    ...raises: [ValueError, JSException], tags: [], forbids: [].}
Evaluate JavaScript module bytecode The bytecode is executed immediately (equivalent to evalBytecode with loadOnly=false) Source   Edit  
proc evalModule(js: QuickJS; code: string; filename: string = "<module>"): string {.
    ...raises: [JSException], tags: [], forbids: [].}

Evaluate JavaScript code as a module (enables import/export syntax) This is useful when using std/os modules with ES6 import syntax

Note: ES6 modules return undefined by specification. IMPORTANT: Due to QuickJS internals, using modules with std library imports may cause issues during cleanup. Consider using regular eval() for simple scripts.

Source   Edit  
proc evalWithGlobals(js: QuickJS; code: string; globals: Table[string, string] = initTable[
    string, string]()): string {....raises: [], tags: [], forbids: [].}
Evaluate JavaScript code with some global variables set as strings Source   Edit  
proc get(ctx: ptr JSContext; name: string): JSAutoValue {....raises: [], tags: [],
    forbids: [].}
Get a property that can auto-convert to the expected type Usage: let magicText: string = ctx.get("magic") # auto-converts to string let magicNumber: int = ctx.get("number") # auto-converts to int Source   Edit  
proc get[T](ctx: ptr JSContext; name: string; t: typedesc[T]): T
Get a global property value: ctx.get("userName", string) Source   Edit  
proc getArrayElement(ctx: ptr JSContext; arr: JSValueConst; index: uint32): JSValue {.
    ...raises: [], tags: [], forbids: [].}
Get an element from a JavaScript array by index Source   Edit  
proc getArrayElementValue[T](ctx: ptr JSContext; arr: JSValueConst;
                             index: uint32; target: typedesc[T]): T
Get an array element value and automatically convert to Nim type with automatic memory management Source   Edit  
proc getArrayLength(ctx: ptr JSContext; arr: JSValueConst): uint32 {.
    ...raises: [JSException], tags: [], forbids: [].}
Get the length of a JavaScript array Source   Edit  
proc getBool(ctx: ptr JSContext; name: string): bool {....raises: [], tags: [],
    forbids: [].}
Get a global bool property: ctx.getBool("isActive") Source   Edit  
proc getFloat(ctx: ptr JSContext; name: string): float64 {.
    ...raises: [JSException], tags: [], forbids: [].}
Get a global float property: ctx.getFloat("userScore") Source   Edit  
proc getGlobalProperty[T](ctx: ptr JSContext; name: string; target: typedesc[T]): T
Get a global property value with automatic memory management Source   Edit  
proc getInt(ctx: ptr JSContext; name: string): int {....raises: [JSException],
    tags: [], forbids: [].}
Get a global int property: ctx.getInt("userAge") Source   Edit  
proc getProperty(ctx: ptr JSContext; obj: JSValueConst; key: string): JSValue {.
    ...raises: [], tags: [], forbids: [].}
Get a property from a JavaScript object by string key Source   Edit  
proc getPropertyValue[T](ctx: ptr JSContext; obj: JSValueConst; key: string;
                         target: typedesc[T]): T
Get a property value and automatically convert to Nim type with automatic memory management Source   Edit  
proc getString(ctx: ptr JSContext; name: string): string {....raises: [], tags: [],
    forbids: [].}
Get a global string property: ctx.getString("userName") Source   Edit  
proc isArray(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc isBool(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc isFunction(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc isNull(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc isNumber(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc isObject(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc isString(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc isUndefined(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc iterateArray(ctx: ptr JSContext; arr: JSValueConst; callback: proc (
    ctx: ptr JSContext; index: uint32; element: JSValueConst)) {.
    ...raises: [JSException, Exception], tags: [RootEffect], forbids: [].}
Iterate over array elements with automatic memory management for each element Source   Edit  
proc JS_AtomToString(ctx: ptr JSContext; atom: JSAtom): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_DefinePropertyValueStr(ctx: ptr JSContext; thisObj: JSValueConst;
                               prop: cstring; val: JSValue; flags: cint): cint {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_DeleteProperty(ctx: ptr JSContext; thisObj: JSValueConst; prop: JSAtom;
                       flags: cint): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_DetectModule(input: cstring; inputLen: csize_t): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_DupValue(ctx: ptr JSContext; v: JSValueConst): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_Eval(ctx: ptr JSContext; input: cstring; inputLen: csize_t;
             filename: cstring; evalFlags: cint): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_EvalFunction(ctx: ptr JSContext; fun_obj: JSValue): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ExecutePendingJob(rt: ptr JSRuntime; pctx: ptr ptr JSContext): cint {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_FreeAtom(ctx: ptr JSContext; atom: JSAtom) {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_FreeContext(ctx: ptr JSContext) {.importc, header: "quickjs/quickjs.h",
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_FreeCString(ctx: ptr JSContext; str: cstring) {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_FreeRuntime(rt: ptr JSRuntime) {.importc, header: "quickjs/quickjs.h",
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_FreeValue(ctx: ptr JSContext; v: JSValue) {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetContextOpaque(ctx: ptr JSContext): pointer {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetException(ctx: ptr JSContext): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetGlobalObject(ctx: ptr JSContext): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetModuleNamespace(ctx: ptr JSContext; m: ptr JSModuleDef): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetProperty(ctx: ptr JSContext; thisObj: JSValueConst; prop: JSAtom): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetPropertyStr(ctx: ptr JSContext; thisObj: JSValueConst; prop: cstring): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_GetPropertyUint32(ctx: ptr JSContext; thisObj: JSValueConst; idx: uint32): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_HasProperty(ctx: ptr JSContext; thisObj: JSValueConst; prop: JSAtom): cint {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_init_module_os(ctx: ptr JSContext; module_name: cstring): ptr JSModuleDef {.
    importc, header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_init_module_std(ctx: ptr JSContext; module_name: cstring): ptr JSModuleDef {.
    importc, header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_IsException(v: JSValueConst): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_module_loader(ctx: ptr JSContext; module_name: cstring; opaque: pointer): ptr JSModuleDef {.
    cdecl, importc, header: "quickjs/quickjs-libc.h", ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc JS_NewArray(ctx: ptr JSContext): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewAtom(ctx: ptr JSContext; str: cstring): JSAtom {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewAtomLen(ctx: ptr JSContext; str: cstring; len: csize_t): JSAtom {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewBool(ctx: ptr JSContext; val: cint): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewCFunction(ctx: ptr JSContext; func: JSCFunction; name: cstring;
                     length: cint): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewCFunction2(ctx: ptr JSContext; func: JSCFunction; name: cstring;
                      length: cint; cproto: cint; magic: cint): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewCFunctionData(ctx: ptr JSContext; func: JSCFunctionData;
                         length: cint; magic: cint; dataLen: cint;
                         data: ptr JSValue): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewCFunctionMagic(ctx: ptr JSContext; func: JSCFunctionMagic;
                          name: cstring; length: cint; cproto: cint; magic: cint): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewContext(rt: ptr JSRuntime): ptr JSContext {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewFloat64(ctx: ptr JSContext; val: float64): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewInt32(ctx: ptr JSContext; val: int32): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewObject(ctx: ptr JSContext): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewRuntime(): ptr JSRuntime {.importc, header: "quickjs/quickjs.h",
                                      ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_NewStringLen(ctx: ptr JSContext; str: cstring; len: csize_t): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_PromiseResult(ctx: ptr JSContext; promise: JSValueConst): JSValue {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_PromiseState(ctx: ptr JSContext; promise: JSValueConst): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ReadObject(ctx: ptr JSContext; buf: ptr uint8; bufLen: csize_t;
                   flags: cint): JSValue {.importc, header: "quickjs/quickjs.h",
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_RunGC(rt: ptr JSRuntime) {.importc, header: "quickjs/quickjs.h",
                                   ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_SetContextOpaque(ctx: ptr JSContext; opaque: pointer) {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_SetModuleLoaderFunc(rt: ptr JSRuntime; module_normalize: pointer;
    module_loader: proc (ctx: ptr JSContext; moduleName: cstring;
                         opaque: pointer): ptr JSModuleDef {.cdecl.};
                            opaque: pointer) {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_SetProperty(ctx: ptr JSContext; thisObj: JSValueConst; prop: JSAtom;
                    val: JSValue): cint {.importc, header: "quickjs/quickjs.h",
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_SetPropertyStr(ctx: ptr JSContext; thisObj: JSValueConst; prop: cstring;
                       val: JSValue): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_SetPropertyUint32(ctx: ptr JSContext; thisObj: JSValueConst;
                          idx: uint32; val: JSValue): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_add_helpers(ctx: ptr JSContext; argc: cint; argv: ptr cstring) {.
    importc, header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_await(ctx: ptr JSContext; val: JSValue): JSValue {.importc,
    header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_eval_binary(ctx: ptr JSContext; buf: ptr uint8; bufLen: csize_t;
                        flags: cint) {.importc,
                                       header: "quickjs/quickjs-libc.h",
                                       ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_free_handlers(rt: ptr JSRuntime) {.importc,
    header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_init_handlers(rt: ptr JSRuntime) {.importc,
    header: "quickjs/quickjs-libc.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc js_std_loop(ctx: ptr JSContext) {.importc,
                                       header: "quickjs/quickjs-libc.h",
                                       ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_Throw(ctx: ptr JSContext; obj: JSValueConst): JSValue {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ThrowInternalError(ctx: ptr JSContext; fmt: cstring): JSValue {.varargs,
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ThrowRangeError(ctx: ptr JSContext; fmt: cstring): JSValue {.varargs,
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ThrowReferenceError(ctx: ptr JSContext; fmt: cstring): JSValue {.
    varargs, importc, header: "quickjs/quickjs.h", ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc JS_ThrowTypeError(ctx: ptr JSContext; fmt: cstring): JSValue {.varargs,
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ToBool(ctx: ptr JSContext; val: JSValueConst): cint {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ToCString(ctx: ptr JSContext; val: JSValueConst): cstring {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ToFloat64(ctx: ptr JSContext; pres: ptr float64; val: JSValueConst): cint {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_ToInt32(ctx: ptr JSContext; pres: ptr int32; val: JSValueConst): cint {.
    importc, header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc JS_WriteObject(ctx: ptr JSContext; psize: ptr csize_t; obj: JSValueConst;
                    flags: cint): ptr uint8 {.importc,
    header: "quickjs/quickjs.h", ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc jsArgsToSeq(ctx: ptr JSContext; argc: cint; argv: ptr JSValueConst): seq[
    JSValue] {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc jsFalse(ctx: ptr JSContext): JSValue {....raises: [], tags: [], forbids: [].}
Return JavaScript false value Source   Edit  
proc jsNull(ctx: ptr JSContext): JSValue {....raises: [], tags: [], forbids: [].}
Return JavaScript null value Source   Edit  
proc jsTrue(ctx: ptr JSContext): JSValue {....raises: [], tags: [], forbids: [].}
Return JavaScript true value Source   Edit  
proc jsUndefined(ctx: ptr JSContext): JSValue {....raises: [], tags: [],
    forbids: [].}
Return JavaScript undefined value Source   Edit  
proc loadBytecodeModule(js: QuickJS; bytecode: openArray[byte]): ptr JSModuleDef {.
    ...raises: [JSException], tags: [], forbids: [].}
Load a module from bytecode without executing it Returns the module definition that can be used with JS_GetModuleNamespace Source   Edit  
proc newArray(ctx: ptr JSContext): JSValue {....raises: [], tags: [], forbids: [].}
Create a new JavaScript array Source   Edit  
proc newQuickJS(config: QuickJSConfig = defaultConfig()): QuickJS {.
    ...raises: [JSException], tags: [], forbids: [].}

Create a new QuickJS instance with runtime and context

⚠️ THREAD SAFETY: The returned QuickJS instance is NOT thread-safe. Use one instance per thread or implement external locking.

Parameters:

  • config: Configuration specifying which modules to include
Source   Edit  
proc nimBoolToJS(ctx: ptr JSContext; val: bool): JSValue {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc nimFloatToJS(ctx: ptr JSContext; val: float64): JSValue {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc nimIntToJS(ctx: ptr JSContext; val: int32): JSValue {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc nimStringToJS(ctx: ptr JSContext; str: string): JSValue {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc nimTupleToJSArray[T](ctx: ptr JSContext; tup: T): JSValue
Convert a Nim tuple to a JavaScript array Source   Edit  
proc processStdLoop(js: QuickJS) {....raises: [], tags: [], forbids: [].}
Process the QuickJS standard event loop once This handles timers, I/O, and other async operations Note: Only available when enableStdHandlers is true Source   Edit  
proc registerFunction(js: var QuickJS; name: string; nimFunc: NimFunction0) {.
    ...raises: [], tags: [], forbids: [].}

Register a Nim function with no arguments to be callable from JavaScript

Note: Since this function takes no arguments, no JSValue memory management is required.

Source   Edit  
proc registerFunction(js: var QuickJS; name: string; nimFunc: NimFunction1) {.
    ...raises: [], tags: [], forbids: [].}

Register a Nim function with one argument to be callable from JavaScript

AUTOMATIC MEMORY MANAGEMENT: The JSValue argument is automatically freed by the trampoline - you don't need to call JS_FreeValue manually!

Source   Edit  
proc registerFunction(js: var QuickJS; name: string; nimFunc: NimFunction2) {.
    ...raises: [], tags: [], forbids: [].}

Register a Nim function with two arguments to be callable from JavaScript

AUTOMATIC MEMORY MANAGEMENT: The JSValue arguments are automatically freed by the trampoline - you don't need to call JS_FreeValue manually!

Source   Edit  
proc registerFunction(js: var QuickJS; name: string; nimFunc: NimFunction3) {.
    ...raises: [], tags: [], forbids: [].}

Register a Nim function with three arguments to be callable from JavaScript

AUTOMATIC MEMORY MANAGEMENT: The JSValue arguments are automatically freed by the trampoline - you don't need to call JS_FreeValue manually!

Source   Edit  
proc registerFunction(js: var QuickJS; name: string;
                      nimFunc: NimFunctionVariadic) {....raises: [], tags: [],
    forbids: [].}

Register a Nim function with variadic arguments to be callable from JavaScript

AUTOMATIC MEMORY MANAGEMENT: The JSValue arguments in the args sequence are automatically freed by the trampoline - you don't need to call JS_FreeValue manually!

Source   Edit  
proc runPendingJobs(js: QuickJS) {....raises: [], tags: [], forbids: [].}
Execute all pending JavaScript jobs (promises, async operations) This is needed after loading modules or running async code Source   Edit  
proc seqToJS[T](ctx: ptr JSContext; s: seq[T]): JSValue
Convert a Nim sequence to a JavaScript array Source   Edit  
proc set[T](ctx: ptr JSContext; name: string; value: T)
Set a global property value: ctx.set("userName", "Alice") Source   Edit  
proc setArrayElement(ctx: ptr JSContext; arr: JSValueConst; index: uint32;
                     value: JSValue): bool {....raises: [], tags: [], forbids: [].}
Set an element in a JavaScript array by index Source   Edit  
proc setGlobalProperty[T](ctx: ptr JSContext; name: string; value: T): bool
Set a global property with automatic memory management Source   Edit  
proc setJSFunction(js: QuickJS; name: string; value: string) {....raises: [],
    tags: [], forbids: [].}
Set a JavaScript function as a string in the global scope Source   Edit  
proc setProperty(ctx: ptr JSContext; obj: JSValueConst; key: string;
                 value: JSValue): bool {....raises: [], tags: [], forbids: [].}
Set a property on a JavaScript object by string key Source   Edit  
proc tableToJS[K, V](ctx: ptr JSContext; t: Table[K, V]): JSValue
Convert a Nim Table to a JavaScript object Source   Edit  
proc toNimBool(ctx: ptr JSContext; val: JSValueConst): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc toNimFloat(ctx: ptr JSContext; val: JSValueConst): float64 {.
    ...raises: [JSException], tags: [], forbids: [].}
Source   Edit  
proc toNimInt(ctx: ptr JSContext; val: JSValueConst): int32 {.
    ...raises: [JSException], tags: [], forbids: [].}
Source   Edit  
proc toNimString(ctx: ptr JSContext; val: JSValueConst): string {....raises: [],
    tags: [], forbids: [].}
Source   Edit  

Converters

converter toBoolFromAuto(val: JSAutoValue): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
converter toFloatFromAuto(val: JSAutoValue): float64 {....raises: [JSException],
    tags: [], forbids: [].}
Source   Edit  
converter toInt32FromAuto(val: JSAutoValue): int32 {....raises: [JSException],
    tags: [], forbids: [].}
Source   Edit  
converter toIntFromAuto(val: JSAutoValue): int {....raises: [JSException],
    tags: [], forbids: [].}
Source   Edit  
converter toStringFromAuto(val: JSAutoValue): string {....raises: [], tags: [],
    forbids: [].}
Source   Edit  

Templates

template withArrayElement(ctx: ptr JSContext; arr: JSValueConst; index: uint32;
                          elemVar: untyped; body: untyped): untyped
Automatically manage an array element lifetime in a scoped block Source   Edit  
template withGlobalObject(ctx: ptr JSContext; globalVar: untyped; body: untyped): untyped
Automatically manage the global object lifetime in a scoped block Source   Edit  
template withIdiomatic(ctx: ptr JSContext; body: untyped): untyped
Enable idiomatic syntax for JS objects and arrays within a scope Source   Edit  
template withProperty(ctx: ptr JSContext; obj: JSValueConst; key: string;
                      propVar: untyped; body: untyped): untyped
Automatically manage a property value lifetime in a scoped block Source   Edit