Skip to content

Commit

Permalink
Fix nil *mruby.Class pass as RClass in vmDefineClass
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Aug 8, 2024
1 parent 25330f1 commit 1181178
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 11 additions & 9 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ func (mrb *State) ClassName(class RClass) string {
}

func (mrb *State) ClassNew(super RClass) (*Class, error) {
isNilSuper := reflect.ValueOf(super).IsNil()
if !isNilSuper {
if super != nil {
mrb.checkInheritable(super)
}

class := mrb.bootDefineClass(super)
if !isNilSuper {
if super != nil {
class.flags |= super.Flags() & FlagUndefinedAllocate
}

Expand Down Expand Up @@ -104,16 +103,19 @@ func (mrb *State) VmFindMethod(recv Value, class RClass, mid Symbol) Method {
}

func (mrb *State) vmDefineClass(outer Value, super Value, id Symbol) (RClass, error) {
superClass, ok := super.(*Class)
if super != nil && !ok {
panic("super is not a class")
var superClass RClass
if super != nil {
if ClassP(super) {
superClass = super.(RClass)
} else {
panic("super is not a class")
}
}

// NOTE: check_if_class_or_module
outerModule, ok := outer.(*Class)
if !ok {
if !ClassPointerP(outer) {
panic("outer is not a class or module")
}
outerModule := outer.(RClass)

return mrb.defineClass(id, superClass, outerModule)
}
Expand Down
22 changes: 22 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,25 @@ func Bool(v Value) bool {
func Test(v Value) bool {
return Bool(v)
}

func ClassP(v Value) bool {
switch v.(type) {
case *Class:
return true
default:
return false
}
}

func ClassPointerP(v Value) bool {
switch v.(type) {
case *Class:
return true
case *SingletonClass:
return true
case *Module:
return true
default:
return false
}
}

0 comments on commit 1181178

Please sign in to comment.