mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
vm.c: replace type assertions with runtime checks
Replace mrb_assert with mrb_ensure_*_type for VM opcodes that require specific types: - OP_ARYCAT: mrb_ensure_array_type - OP_ARYPUSH: mrb_ensure_array_type - OP_ASET: mrb_ensure_array_type (also fixed: was checking wrong register) - OP_INTERN: mrb_ensure_string_type - OP_HASHCAT: mrb_ensure_hash_type These checks catch codegen bugs with clear error messages in both debug and release builds. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3003,7 +3003,7 @@ RETRY_TRY_BLOCK:
|
||||
regs[a] = splat;
|
||||
}
|
||||
else {
|
||||
mrb_assert(mrb_array_p(regs[a]));
|
||||
mrb_ensure_array_type(mrb, regs[a]);
|
||||
mrb_ary_concat(mrb, regs[a], splat);
|
||||
}
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
@@ -3011,7 +3011,7 @@ RETRY_TRY_BLOCK:
|
||||
}
|
||||
|
||||
CASE(OP_ARYPUSH, BB) {
|
||||
mrb_assert(mrb_array_p(regs[a]));
|
||||
mrb_ensure_array_type(mrb, regs[a]);
|
||||
for (mrb_int i=0; i<b; i++) {
|
||||
mrb_ary_push(mrb, regs[a], regs[a+i+1]);
|
||||
}
|
||||
@@ -3045,7 +3045,7 @@ RETRY_TRY_BLOCK:
|
||||
}
|
||||
|
||||
CASE(OP_ASET, BBB) {
|
||||
mrb_assert(mrb_array_p(regs[a]));
|
||||
mrb_ensure_array_type(mrb, regs[b]);
|
||||
mrb_ary_set(mrb, regs[b], c, regs[a]);
|
||||
NEXT;
|
||||
}
|
||||
@@ -3085,7 +3085,7 @@ RETRY_TRY_BLOCK:
|
||||
}
|
||||
|
||||
CASE(OP_INTERN, B) {
|
||||
mrb_assert(mrb_string_p(regs[a]));
|
||||
mrb_ensure_string_type(mrb, regs[a]);
|
||||
mrb_sym sym = mrb_intern_str(mrb, regs[a]);
|
||||
regs[a] = mrb_symbol_value(sym);
|
||||
NEXT;
|
||||
@@ -3158,7 +3158,7 @@ RETRY_TRY_BLOCK:
|
||||
CASE(OP_HASHCAT, B) {
|
||||
mrb_value hash = regs[a];
|
||||
|
||||
mrb_assert(mrb_hash_p(hash));
|
||||
mrb_ensure_hash_type(mrb, hash);
|
||||
mrb_hash_merge(mrb, hash, regs[a+1]);
|
||||
ci = mrb->c->ci;
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
|
||||
Reference in New Issue
Block a user