fix tests; add comprehensive tests for functions and fix bugs in the mach VM regarding them.
This commit is contained in:
18
mcode.cm
18
mcode.cm
@@ -951,6 +951,19 @@ var mcode = function(ast) {
|
||||
return null
|
||||
}
|
||||
|
||||
// --- Helper: guard that reverse param is logical (or null) ---
|
||||
// Disrupts if rev_slot is not null and not a boolean.
|
||||
var emit_reverse_guard = function(rev_slot, msg) {
|
||||
var ok_label = gen_label("rev_ok")
|
||||
var g = alloc_slot()
|
||||
emit_jump_cond("jump_null", rev_slot, ok_label)
|
||||
emit_2("is_bool", g, rev_slot)
|
||||
emit_jump_cond("jump_true", g, ok_label)
|
||||
emit_log_error(msg)
|
||||
emit_0("disrupt")
|
||||
emit_label(ok_label)
|
||||
}
|
||||
|
||||
// --- Helper: forward loop scaffolding ---
|
||||
// L = {arr, len, i, check, item, one, loop_label, done_label}
|
||||
// body_fn(L) — called between element load and increment
|
||||
@@ -1068,6 +1081,7 @@ var mcode = function(ast) {
|
||||
if (nargs <= 2) {
|
||||
emit_forward_loop(fwd_L, body_fn)
|
||||
} else {
|
||||
emit_reverse_guard(args.rev, "arrfor: reverse must be a logical")
|
||||
emit_jump_cond("wary_true", args.rev, rev_label)
|
||||
emit_forward_loop(fwd_L, body_fn)
|
||||
emit_jump(final_label)
|
||||
@@ -1289,6 +1303,9 @@ var mcode = function(ast) {
|
||||
emit_2("int", zero, 0)
|
||||
emit_2("int", one, 1)
|
||||
emit_1("null", null_s)
|
||||
if (nargs > 2) {
|
||||
emit_reverse_guard(args.rev, "find: reverse must be a logical")
|
||||
}
|
||||
emit_2("is_func", is_fn, target)
|
||||
emit_jump_cond("jump_true", is_fn, fn_mode_label)
|
||||
// === Value mode ===
|
||||
@@ -1523,6 +1540,7 @@ var mcode = function(ast) {
|
||||
d2 = gen_label("reduce_d2")
|
||||
d3 = gen_label("reduce_d3")
|
||||
d4 = gen_label("reduce_d4")
|
||||
emit_reverse_guard(rev_slot, "reduce: reverse must be a logical")
|
||||
emit_2("is_null", check, init_slot)
|
||||
emit_jump_cond("jump_false", check, has_init)
|
||||
// No initial
|
||||
|
||||
Reference in New Issue
Block a user