fix tests

This commit is contained in:
2026-02-24 16:55:07 -06:00
parent 7bd17c6476
commit c2f57d1dae
10 changed files with 1179 additions and 96 deletions

View File

@@ -246,7 +246,7 @@ typedef enum MachOpcode {
MACH_LENGTH, /* R(A) = length(R(B)) — array/text/blob length */
MACH_IS_PROXY, /* R(A) = is_function(R(B)) && R(B).length == 2 */
MACH_IS_BLOB, /* R(A) = is_blob(R(B)) */
MACH_IS_DATA, /* R(A) = is_data(R(B)) — plain record, not array/func/blob */
MACH_IS_DATA, /* R(A) = is_data(R(B)) — not function or null */
MACH_IS_TRUE, /* R(A) = (R(B) === true) */
MACH_IS_FALSE, /* R(A) = (R(B) === false) */
MACH_IS_FIT, /* R(A) = is_fit(R(B)) — safe integer */
@@ -2398,10 +2398,8 @@ vm_dispatch:
VM_BREAK();
VM_CASE(MACH_IS_DATA): {
JSValue v = frame->slots[b];
int result = 0;
if (mist_is_gc_object(v) && !mist_is_array(v)
&& !mist_is_function(v) && !mist_is_blob(v))
result = 1;
/* data is text, number, logical, array, blob, or record — not function, null */
int result = (v != JS_NULL && !mist_is_function(v));
frame->slots[a] = JS_NewBool(ctx, result);
VM_BREAK();
}

View File

@@ -8375,7 +8375,15 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu
/* Map - GC-safe: root result throughout, use rooted refs for func and array */
int arity = ((JSFunction *)JS_VALUE_GET_FUNCTION (arg1_ref.val))->length;
int reverse = argc > 2 && JS_ToBool (ctx, argv[2]);
int reverse = 0;
if (argc > 2 && !JS_IsNull (argv[2])) {
if (!JS_IsBool (argv[2])) {
JS_PopGCRef (ctx, &arg1_ref);
JS_PopGCRef (ctx, &arg0_ref);
return JS_RaiseDisrupt (ctx, "array: reverse must be a logical");
}
reverse = JS_VALUE_GET_BOOL (argv[2]);
}
JSValue exit_val = argc > 3 ? argv[3] : JS_NULL;
JSGCRef result_ref;
@@ -8505,7 +8513,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu
if (!JS_IsInteger (argv[1])) {
JS_PopGCRef (ctx, &arg1_ref);
JS_PopGCRef (ctx, &arg0_ref);
return JS_NULL;
return JS_RaiseDisrupt (ctx, "array slice: from must be an integer");
}
int from = JS_VALUE_GET_INT (argv[1]);
int to;
@@ -8513,7 +8521,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu
if (!JS_IsNumber (argv[2]) || !JS_IsInteger (argv[2])) {
JS_PopGCRef (ctx, &arg1_ref);
JS_PopGCRef (ctx, &arg0_ref);
return JS_NULL;
return JS_RaiseDisrupt (ctx, "array slice: to must be an integer");
}
to = JS_VALUE_GET_INT (argv[2]);
} else {
@@ -8550,7 +8558,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu
JS_PopGCRef (ctx, &arg1_ref);
JS_PopGCRef (ctx, &arg0_ref);
return JS_NULL;
return JS_RaiseDisrupt (ctx, "array: invalid argument combination");
}
/* array(object) - keys */
@@ -8822,7 +8830,12 @@ static JSValue js_cell_array_reduce (JSContext *ctx, JSValue this_val, int argc,
word_t len = arr->len;
JSValue fn = argv[1];
int reverse = argc > 3 && JS_ToBool (ctx, argv[3]);
int reverse = 0;
if (argc > 3 && !JS_IsNull (argv[3])) {
if (!JS_IsBool (argv[3]))
return JS_RaiseDisrupt (ctx, "reduce: reverse must be a logical");
reverse = JS_VALUE_GET_BOOL (argv[3]);
}
JSGCRef acc_ref;
JSValue acc;
@@ -8898,7 +8911,12 @@ static JSValue js_cell_array_for (JSContext *ctx, JSValue this_val, int argc, JS
word_t len = arr->len;
if (len == 0) return JS_NULL;
int reverse = argc > 2 && JS_ToBool (ctx, argv[2]);
int reverse = 0;
if (argc > 2 && !JS_IsNull (argv[2])) {
if (!JS_IsBool (argv[2]))
return JS_RaiseDisrupt (ctx, "arrfor: reverse must be a logical");
reverse = JS_VALUE_GET_BOOL (argv[2]);
}
JSValue exit_val = argc > 3 ? argv[3] : JS_NULL;
if (reverse) {
@@ -8938,7 +8956,12 @@ static JSValue js_cell_array_find (JSContext *ctx, JSValue this_val, int argc, J
JSArray *arr = JS_VALUE_GET_ARRAY (argv[0]);
word_t len = arr->len;
int reverse = argc > 2 && JS_ToBool (ctx, argv[2]);
int reverse = 0;
if (argc > 2 && !JS_IsNull (argv[2])) {
if (!JS_IsBool (argv[2]))
return JS_RaiseDisrupt (ctx, "find: reverse must be a logical");
reverse = JS_VALUE_GET_BOOL (argv[2]);
}
int32_t from;
if (argc > 3 && !JS_IsNull (argv[3])) {
if (JS_ToInt32 (ctx, &from, argv[3])) return JS_NULL;
@@ -9416,7 +9439,7 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal
/* Use text directly as key */
JSValue prop_key = js_key_from_string (ctx, key);
JSValue val;
if (argc < 2 || JS_IsNull (func_ref.val)) {
if (argc < 2) {
val = JS_TRUE;
} else if (is_func) {
JSValue arg_key = key;
@@ -11336,11 +11359,9 @@ static JSValue js_cell_is_blob (JSContext *ctx, JSValue this_val, int argc, JSVa
static JSValue js_cell_is_data (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
if (argc < 1) return JS_FALSE;
JSValue val = argv[0];
if (!mist_is_gc_object (val)) return JS_FALSE;
if (JS_IsArray (val)) return JS_FALSE;
/* data is text, number, logical, array, blob, or record — not function, null */
if (JS_IsNull (val)) return JS_FALSE;
if (JS_IsFunction (val)) return JS_FALSE;
if (mist_is_blob (val)) return JS_FALSE;
/* Check if it's a plain object (prototype is Object.prototype or null) */
return JS_TRUE;
}

View File

@@ -1323,6 +1323,10 @@ TEST(cell_not) {
CELL CORE FUNCTION TESTS
============================================================================ */
static JSValue cfunc_returns_99(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
return JS_NewInt32(ctx, 99);
}
TEST(cell_length_array) {
JSGCRef arr_ref;
JS_PushGCRef(ctx, &arr_ref);
@@ -1343,6 +1347,81 @@ TEST(cell_length_string) {
return 1;
}
TEST(cell_length_blob) {
uint8_t data[] = {0xAA, 0xBB, 0xCC};
JSValue blob = js_new_blob_stoned_copy(ctx, data, 3);
JSValue result = JS_CellLength(ctx, blob);
/* blob length is in bits: 3 bytes = 24 bits */
ASSERT_INT(result, 24);
return 1;
}
TEST(cell_length_null) {
JSValue result = JS_CellLength(ctx, JS_NULL);
ASSERT(JS_IsNull(result));
return 1;
}
TEST(cell_length_logical) {
JSValue result_t = JS_CellLength(ctx, JS_TRUE);
JSValue result_f = JS_CellLength(ctx, JS_FALSE);
ASSERT(JS_IsNull(result_t));
ASSERT(JS_IsNull(result_f));
return 1;
}
TEST(cell_length_number) {
JSValue result = JS_CellLength(ctx, JS_NewInt32(ctx, 42));
ASSERT(JS_IsNull(result));
return 1;
}
TEST(cell_length_function_arity) {
JSGCRef func_ref;
JS_PushGCRef(ctx, &func_ref);
func_ref.val = JS_NewCFunction(ctx, cfunc_add, "add", 2);
JSValue result = JS_CellLength(ctx, func_ref.val);
JS_PopGCRef(ctx, &func_ref);
ASSERT_INT(result, 2);
return 1;
}
TEST(cell_length_object_no_length) {
JSGCRef obj_ref;
JS_PushGCRef(ctx, &obj_ref);
obj_ref.val = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, obj_ref.val, "x", JS_NewInt32(ctx, 10));
JSValue result = JS_CellLength(ctx, obj_ref.val);
JS_PopGCRef(ctx, &obj_ref);
ASSERT(JS_IsNull(result));
return 1;
}
TEST(cell_length_object_number) {
JSGCRef obj_ref;
JS_PushGCRef(ctx, &obj_ref);
obj_ref.val = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, obj_ref.val, "length", JS_NewInt32(ctx, 7));
JSValue result = JS_CellLength(ctx, obj_ref.val);
JS_PopGCRef(ctx, &obj_ref);
ASSERT_INT(result, 7);
return 1;
}
TEST(cell_length_object_function) {
JSGCRef obj_ref, func_ref;
JS_PushGCRef(ctx, &obj_ref);
JS_PushGCRef(ctx, &func_ref);
obj_ref.val = JS_NewObject(ctx);
func_ref.val = JS_NewCFunction(ctx, cfunc_returns_99, "length", 0);
JS_SetPropertyStr(ctx, obj_ref.val, "length", func_ref.val);
JSValue result = JS_CellLength(ctx, obj_ref.val);
JS_PopGCRef(ctx, &func_ref);
JS_PopGCRef(ctx, &obj_ref);
ASSERT_INT(result, 99);
return 1;
}
TEST(cell_reverse_array) {
JSGCRef arr_ref;
JS_PushGCRef(ctx, &arr_ref);
@@ -2245,6 +2324,14 @@ int run_c_test_suite(JSContext *ctx)
printf("\nCell Core Functions:\n");
RUN_TEST(cell_length_array);
RUN_TEST(cell_length_string);
RUN_TEST(cell_length_blob);
RUN_TEST(cell_length_null);
RUN_TEST(cell_length_logical);
RUN_TEST(cell_length_number);
RUN_TEST(cell_length_function_arity);
RUN_TEST(cell_length_object_no_length);
RUN_TEST(cell_length_object_number);
RUN_TEST(cell_length_object_function);
RUN_TEST(cell_reverse_array);
RUN_TEST(cell_reverse_string);
RUN_TEST(cell_meme_shallow);