Permalink
Browse files

Fix segfault when defining class inside instance_exec on primitive

  • Loading branch information...
bouk committed Nov 16, 2016
1 parent a630c4f commit 4523aaec015ff0bf900710efc2ec3411e8841fb1
Showing with 38 additions and 4 deletions.
  1. +28 −0 mrbgems/mruby-object-ext/test/object.rb
  2. +10 −4 src/vm.c
@@ -23,3 +23,31 @@ def initialize
], ret
assert_equal(:tap_ok, Class.new {def m; tap{return :tap_ok}; end}.new.m)
end
assert('instance_exec on primitives with class and module definition') do
begin
class A
1.instance_exec do
class B
end
end
end
assert_kind_of Class, A::B
ensure
Object.remove_const :A
end
begin
class A
1.instance_exec do
module B
end
end
end
assert_kind_of Module, A::B
ensure
Object.remove_const :A
end
end
View
@@ -2261,15 +2261,18 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
CASE(OP_CLASS) {
/* A B R(A) := newclass(R(A),Syms(B),R(A+1)) */
struct RClass *c = 0;
struct RClass *c = 0, *baseclass;
int a = GETARG_A(i);
mrb_value base, super;
mrb_sym id = syms[GETARG_B(i)];
base = regs[a];
super = regs[a+1];
if (mrb_nil_p(base)) {
base = mrb_obj_value(mrb->c->ci->target_class);
baseclass = mrb->c->ci->proc->target_class;
if (!baseclass) baseclass = mrb->c->ci->target_class;
base = mrb_obj_value(baseclass);
}
c = mrb_vm_define_class(mrb, base, super, id);
regs[a] = mrb_obj_value(c);
@@ -2279,14 +2282,17 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
CASE(OP_MODULE) {
/* A B R(A) := newmodule(R(A),Syms(B)) */
struct RClass *c = 0;
struct RClass *c = 0, *baseclass;
int a = GETARG_A(i);
mrb_value base;
mrb_sym id = syms[GETARG_B(i)];
base = regs[a];
if (mrb_nil_p(base)) {
base = mrb_obj_value(mrb->c->ci->target_class);
baseclass = mrb->c->ci->proc->target_class;
if (!baseclass) baseclass = mrb->c->ci->target_class;
base = mrb_obj_value(baseclass);
}
c = mrb_vm_define_module(mrb, base, id);
regs[a] = mrb_obj_value(c);

0 comments on commit 4523aae

Please sign in to comment.