Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / tests_array_builtins.lucb

#==============================================================================================
#
#   tests_array_builtins - Unit tests of the Array, Iterator and Number builtins' runtime-free parts
#
#   DESCRIPTION:
#       Tests of the parts of quickjs.c lines 41669-44927 that work without a runtime:
#       the sort comparison on values it can decide without calling JavaScript (its
#       stable tie on the original index, and its silence after a failure), the
#       builtin tables (lengths, magics, aliases and nested objects, as in C), the
#       layout of the Iterator Concat state, and the builtins whose answer for
#       primitive arguments needs no object. The context is a buffer the size of a
#       Context that these paths never read.
#
#==============================================================================================

# mark: Helpers =================================================================================

## A context-sized buffer: the paths under test never read it.
struct TestR23ContextBuf:
    var words: u64[memory.size_of(Context) // 8 + 1]

# The index of the entry named `name` in `tab`, or -1.
func test_find_entry(tab: const CFunctionListEntry[], name: str) -> i64:
    for i in 0..<tab.length:
        if tab[i].name == name:
            return (i64)i
    return -1

# mark: Sort comparison =========================================================================

test "sort: identical values keep their original order (stable tie on the index)":
    var ctx_buf: TestR23ContextBuf = ---
    let ctx = (Context*)&ctx_buf
    # a comparator is given, so identical values compare equal without calling it
    var asc = ArraySortContext(ctx = ctx, exception = false, has_method = true, method = js_undefined())
    let order: i64[6] = [5, 2, 4, 0, 3, 1]
    var slots: ValueSlot[6] = ---
    for i in 0..<order.length:
        slots[i] = ValueSlot(val = js_new_int32(7), str = none, pos = order[i])
    cutils.rqsort(&slots[0], slots.length, memory.size_of(ValueSlot), js_array_cmp_generic, &asc)
    assert(not asc.exception)
    for i in 0..<slots.length:
        assert(slots[i].pos == (i64)i)
        assert(js_value_get_int(slots[i].val) == 7)

test "sort: after a failed comparison every comparison answers 0":
    var ctx_buf: TestR23ContextBuf = ---
    let ctx = (Context*)&ctx_buf
    var asc = ArraySortContext(ctx = ctx, exception = true, has_method = false, method = js_undefined())
    var a = ValueSlot(val = js_new_int32(1), str = none, pos = 0)
    var b = ValueSlot(val = js_new_int32(2), str = none, pos = 1)
    assert(js_array_cmp_generic(&a, &b, &asc) == 0)
    assert(js_array_cmp_generic(&b, &a, &asc) == 0)
    # nothing was converted
    assert(a.str == none and b.str == none)

# mark: Tables ==================================================================================

test "Array.prototype table: magics, the values alias and the unscopables object":
    assert(js_array_proto_funcs.length == 40)
    let every = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "every")]
    assert(every.cproto == CFunctionEnum.generic_magic and every.magic == 0)
    let filter = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "filter")]
    assert(filter.magic == 4)
    let reduce_right = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "reduceRight")]
    assert(reduce_right.magic == 1)
    let find_last_index = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "findLastIndex")]
    assert(find_last_index.magic == 3)
    let shift = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "shift")]
    assert(shift.magic == 1 and shift.length == 0)
    let splice = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "splice")]
    assert(splice.cproto == CFunctionEnum.generic and splice.length == 2 and splice.prop_flags == 3)
    let entries = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "entries")]
    assert(entries.magic == (i16)IteratorKind.key_and_value)
    # [Symbol.iterator] is the same function as values
    let alias = js_array_proto_funcs[(usize)test_find_entry(js_array_proto_funcs, "[Symbol.iterator]")]
    assert(alias.def_type == def_alias and alias.alias_name == "values" and alias.alias_base == -1)
    let unscopables = js_array_proto_funcs[39]
    assert(unscopables.name == "[Symbol.unscopables]" and unscopables.def_type == def_object)
    assert(unscopables.prop_flags == (u8)prop_configurable)
    let list = unscopables.prop_list else trap("the unscopables list")
    assert(list.length == 16)
    for e in list:
        assert(e.def_type == def_prop_bool and e.i32_value == 1 and e.prop_flags == (u8)prop_c_w_e)
        assert(test_find_entry(js_array_proto_funcs, e.name) >= 0)

test "the typed arrays' magics combine with special_ta":
    assert(special_every | special_ta == 8)
    assert(special_filter | special_ta == 12)
    assert(special_reduce_right | special_ta == 9)
    assert(special_for_each == 2 and special_map == 3)

test "Iterator tables: helper kinds in QuickJS's order, next/return magics":
    assert((i32)IteratorHelperKind.drop == 0 and (i32)IteratorHelperKind.take == 8)
    assert((i32)IteratorHelperKind.flat_map == 4 and (i32)IteratorHelperKind.some == 7)
    assert(js_iterator_proto_funcs.length == 13)
    let take = js_iterator_proto_funcs[(usize)test_find_entry(js_iterator_proto_funcs, "take")]
    assert(take.magic == (i16)IteratorHelperKind.take and take.length == 1)
    let for_each = js_iterator_proto_funcs[(usize)test_find_entry(js_iterator_proto_funcs, "forEach")]
    assert(for_each.magic == (i16)IteratorHelperKind.for_each)
    let tag = js_iterator_proto_funcs[12]
    assert(tag.def_type == def_cgetset and tag.prop_flags == 1)
    assert(js_iterator_helper_proto_funcs[0].magic == (i16)gen_magic_next)
    assert(js_iterator_helper_proto_funcs[1].magic == (i16)gen_magic_return)
    assert(js_iterator_helper_proto_funcs[1].cproto == CFunctionEnum.iterator_next)
    assert(js_iterator_helper_proto_funcs[2].string_value == "Iterator Helper")
    assert(js_iterator_concat_proto_funcs[1].cproto == CFunctionEnum.generic)
    assert(js_array_iterator_proto_funcs[1].string_value == "Array Iterator")
    assert(js_iterator_funcs[1].name == "from" and js_iterator_funcs[1].length == 1)

test "Number table: the constants":
    assert(js_number_funcs.length == 14)
    let parse_int = js_number_funcs[0]
    assert(parse_int.def_type == def_alias and parse_int.alias_base == 0)
    let nan = js_number_funcs[(usize)test_find_entry(js_number_funcs, "NaN")].f64_value
    assert(nan != nan)
    let neg_inf = js_number_funcs[(usize)test_find_entry(js_number_funcs, "NEGATIVE_INFINITY")].f64_value
    assert(neg_inf < -1.7976931348623157e308)
    let pos_inf = js_number_funcs[(usize)test_find_entry(js_number_funcs, "POSITIVE_INFINITY")].f64_value
    assert(pos_inf > 1.7976931348623157e308)
    let max_safe = js_number_funcs[(usize)test_find_entry(js_number_funcs, "MAX_SAFE_INTEGER")].f64_value
    assert(max_safe == f64(max_safe_integer))
    let min_value = js_number_funcs[(usize)test_find_entry(js_number_funcs, "MIN_VALUE")].f64_value
    assert(min_value > 0.0 and min_value / 2.0 == 0.0)

# mark: Layout ==================================================================================

test "the Iterator Concat values follow the state, aligned for values":
    var buf: u64[16] = ---
    let it = (IteratorConcatData*)&buf[0]
    let values = iterator_concat_values(it)
    assert((usize)values - (usize)it == memory.size_of(IteratorConcatData))
    assert(memory.size_of(IteratorConcatData) % memory.align_of(Value) == 0)

# mark: Builtins on primitives ==================================================================

test "Number.isNaN, isFinite and isSafeInteger answer false for a non-number":
    var ctx_buf: TestR23ContextBuf = ---
    let ctx = (Context*)&ctx_buf
    var args: Value[1] = [js_undefined()]
    let r1 = js_number_is_na_n(ctx, js_undefined(), 1, &args[0]) catch: trap("isNaN failed")
    assert(r1.tag == Tag.boolean and not js_value_get_bool(r1))
    let r2 = js_number_is_finite(ctx, js_undefined(), 1, &args[0]) catch: trap("isFinite failed")
    assert(r2.tag == Tag.boolean and not js_value_get_bool(r2))
    args[0] = js_true()
    let r3 = js_number_is_safe_integer(ctx, js_undefined(), 1, &args[0]) catch: trap("isSafeInteger failed")
    assert(r3.tag == Tag.boolean and not js_value_get_bool(r3))

test "the Iterator constructor accessor's getter answers its function data":
    var ctx_buf: TestR23ContextBuf = ---
    let ctx = (Context*)&ctx_buf
    var data: Value[1] = [js_new_int32(42)]
    var no_args = js_undefined()
    let r = js_iterator_constructor_getset(ctx, js_undefined(), 0, &no_args, 0, &data[0]) catch: trap("getter failed")
    assert(r.tag == Tag.int and js_value_get_int(r) == 42)
    let this_val = js_new_int32(5)
    let same = js_iterator_proto_iterator(ctx, this_val, 0, &no_args) catch: trap("[Symbol.iterator] failed")
    assert(js_value_get_int(same) == 5)
    let species = js_get_this(ctx, this_val) catch: trap("[Symbol.species] failed")
    assert(js_value_get_int(species) == 5)