A single region for garbage collection. Shared by one or more contexts.
A handle used in the Duktape API, associated with a Duktape thread (coroutine) and its call and value stacks.
Book-keeping of the active function call chain of a context. Includes both ECMAScript and Duktape/C function calls.
Storage for tagged values belonging to current activations in a context's call stack.
The values kept in a value stack are traditional tagged types. Stack entries are indexed either from the bottom (>= 0) or from the top (< 0) of the most recent function call.
Non-negative (>= 0) indices refer to stack entries in the current stack frame, relative to the frame bottom:
[ 0 1 2 3 4 5! ]
Negative (< 0) indices refer to stack entries relative to the top:
[ -6 -5 -4 -3 -2 -1! ]
The special constant DUK_INVALID_INDEX
is a negative integer
which denotes an invalid stack index. It can be returned from API calls
and can also be given to some API calls to indicate a "no value".
The value stack top (or just "top") is the non-negative index of an imaginary element just above the highest used index. For instance, above the highest used index is 5, so the stack top is 6. The top indicates the current stack size, and is also the index of the next element pushed to the stack.
[ 0 1 2 3 4 5! 6? ]
API stack operations are always confined to the current stack frame. There is no way to refer to stack entries below the current frame. This is intentional, as it protects functions in the call stack from affecting each other's values.
Type | Type constant | Type mask constant | Description | Heap alloc |
---|---|---|---|---|
(none) | DUK_TYPE_NONE | DUK_TYPE_MASK_NONE | no type (missing value, invalid index, etc) | no |
undefined | DUK_TYPE_UNDEFINED | DUK_TYPE_MASK_UNDEFINED | undefined | no |
null | DUK_TYPE_NULL | DUK_TYPE_MASK_NULL | null | no |
boolean | DUK_TYPE_BOOLEAN | DUK_TYPE_MASK_BOOLEAN | true and false | no |
number | DUK_TYPE_NUMBER | DUK_TYPE_MASK_NUMBER | IEEE double | no |
string | DUK_TYPE_STRING | DUK_TYPE_MASK_STRING | immutable string | yes |
object | DUK_TYPE_OBJECT | DUK_TYPE_MASK_OBJECT | object with properties | yes |
buffer | DUK_TYPE_BUFFER | DUK_TYPE_MASK_BUFFER | mutable byte buffer, fixed/dynamic | yes |
pointer | DUK_TYPE_POINTER | DUK_TYPE_MASK_POINTER | opaque pointer (void *) | no |
lightfunc | DUK_TYPE_LIGHTFUNC | DUK_TYPE_MASK_LIGHTFUNC | plain Duktape/C pointer (non-object) | no |
The "Heap alloc" column indicates whether the tagged value points to a heap allocated object with possibly additional allocations (like object property table).
Each stack type has a bit index which allows a set of stack types to be represented as a bit mask. Calling code can use such bit sets to e.g. check whether or not a certain stack value belongs to a set of types. Example:
if (duk_get_type_mask(ctx, -3) & (DUK_TYPE_MASK_NUMBER | DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_OBJECT)) { printf("type is number, string, or object\n"); }
There is a specific API call for matching a set of types even more conveniently:
if (duk_check_type_mask(ctx, -3, DUK_TYPE_MASK_NUMBER | DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_OBJECT)) { printf("type is number, string, or object\n"); }
ECMAScript object and array keys can only be strings; this includes
array indices (0, 1, 2, ...) which are expressed as canonical string
representations ("0", "1", "2", ...) of the respective numbers for all
integers in the range [0, 2**32-2]. Conceptually any key used in a
property lookup is first coerced to a string, so that obj[123]
is really just a shorthand for obj["123"]
.
Duktape tries to avoid explicit number-to-string conversions for array accesses whenever possible, so it is preferable to use numeric array indices in both ECMAScript code and C code using the Duktape API. There are typically API call variants accepting strings and numeric indices.
A C function with a Duktape/C API signature can be associated with an ECMAScript function object or a lightfunc value, and gets called when the associated value is called from ECMAScript code. Example:
duk_ret_t my_func(duk_context *ctx) { duk_push_int(ctx, 123); return 1; }
Argument count given in value stack is specified when the corresponding
ECMAScript function object is created: either a fixed number of arguments
of DUK_VARARGS
for getting arguments "as is":
undefined
as necessary.DUK_VARARGS
, use
duk_get_top()
to determine the
number of actual arguments.Function return values:
undefined
.DUK_RET_xxx
constants which are negative variants of DUK_ERR_xxx
.No error message can be given when using the error shorthand:
duk_ret_t my_func(duk_context *ctx) { if (duk_get_top(ctx) == 0) { /* throw TypeError if no arguments given */ return DUK_RET_TYPE_ERROR; } /* ... */ }
A stash is an object reachable from C code using the Duktape/C API, but which is unreachable from ECMAScript code. Stashes allow C code to store internal state which can be safely isolated from ECMAScript code. There are three stashes: