Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / refcount.lucb

#==============================================================================================
#
#   refcount - Duplicating and releasing values, and quickjs.h's other inline helpers
#
#   DESCRIPTION:
#       Port of quickjs.h's inline functions over the reference count: every value with a
#       negative tag points at a js_malloc'ed block whose header holds the count
#       (`js_rc`). Releasing the last reference hands the value to `js_free_value_raw`.
#
#==============================================================================================

# These four run on every stack operation of the interpreter, and each is written so that
# its lowered body stays within the size the native inliner expands at every call (24
# instructions); the plain spelling, `js_value_has_ref_count(v)` and `js_rc(v.u.ptr)`,
# lowers to about forty. So:
# - `(i64)v.tag < 0` is JS_VALUE_HAS_REF_COUNT: every tag from tag_first (-9) up is
#   below zero or one of the non-negative tags, which carry no count;
# - the count is the i32 4 bytes below the payload pointer (the last field of js_rc's
#   header), addressed by integer arithmetic, which lowers to one subtraction where
#   pointer arithmetic lowers to a multiplication and a subtraction;
# - it wraps as C's int does, and is read again rather than kept in a local.
# The helpers they stand for are the value tests and js_rc; the slow path is a call.

## The reference count is the last 4 bytes of a block's header (MallocBlockHeader).
assert(memory.size_of(MallocBlockHeader) - memory.offset_of(MallocBlockHeader, ref_count) == 4)

## A new reference to `v` (JS_DupValue).
pub func js_dup_value(ctx: Context*, v: Value) -> Value:
    if (i64)v.tag < 0:
        *(i32*)((usize)v.u.ptr -% 4) +%= 1
    return v

## A new reference to `v` without a context (JS_DupValueRT).
pub func js_dup_value_rt(rt: Runtime*, v: Value) -> Value:
    if (i64)v.tag < 0:
        *(i32*)((usize)v.u.ptr -% 4) +%= 1
    return v

## Release a reference to `v`; the last one frees it (JS_FreeValue).
pub func js_free_value(ctx: Context*, v: Value):
    if (i64)v.tag < 0:
        *(i32*)((usize)v.u.ptr -% 4) -%= 1
        if *(i32*)((usize)v.u.ptr -% 4) <= 0:
            js_free_value_raw(ctx, v)

## Release a reference to `v` without a context (JS_FreeValueRT).
pub func js_free_value_rt(rt: Runtime*, v: Value):
    if (i64)v.tag < 0:
        *(i32*)((usize)v.u.ptr -% 4) -%= 1
        if *(i32*)((usize)v.u.ptr -% 4) <= 0:
            js_free_value_rt_raw(rt, v)

## The property `prop` of `this_obj` (JS_GetProperty).
pub func js_get_property(ctx: Context*, this_obj: Value, prop: Atom) -> Value!:
    return try js_get_property_internal(ctx, this_obj, prop, this_obj, false)

## Set the property `prop` of `this_obj`, taking ownership of `val` (JS_SetProperty).
pub func js_set_property(ctx: Context*, this_obj: Value, prop: Atom, val: Value) -> bool!:
    return try js_set_property_internal(ctx, this_obj, prop, val, this_obj, prop_throw)

## A C function object (JS_NewCFunction).
pub func js_new_c_function(ctx: Context*, func_: CFunction, name: const u8*, length: i32) -> Value!:
    return try js_new_c_function2(ctx, func_, name, length, CFunctionEnum.generic, 0)

## A string value from NUL-terminated UTF-8 text (JS_NewString).
pub func js_new_string(ctx: Context*, text: const u8*) -> Value!:
    return try js_new_string_len(ctx, text, cutils.strlen(text))