Skip to content

Commit

Permalink
Add check inheritable when new class
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed May 2, 2024
1 parent 7912c69 commit 31b9231
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mruby

import (
"fmt"
"reflect"
)

var (
Expand Down Expand Up @@ -69,12 +70,13 @@ func (mrb *State) ClassName(class RClass) string {
}

func (mrb *State) ClassNew(super RClass) (*Class, error) {
class := mrb.bootDefineClass(super)

// mrb_check_inheritable(mrb, super)
superClass, ok := super.(*Class)
isNilSuper := reflect.ValueOf(super).IsNil()
if !isNilSuper {
mrb.checkInheritable(super)
}

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

Expand Down Expand Up @@ -230,6 +232,20 @@ func (mrb *State) initClassNew(class RClass) {
mrb.DefineMethodId(class, _new(mrb), allocObject)
}

func (mrb *State) checkInheritable(super RClass) {
if _, ok := super.(*Class); !ok {
mrb.Raisef(nil, "superclass must be a Class (%T given)", super)
}

if _, ok := super.(*SingletonClass); ok {
mrb.Raisef(nil, "can't make subclass of singleton class")
}

if super == mrb.ClassClass {
mrb.Raisef(nil, "can't make subclass of Class")
}
}

func allocObject(mrb *State, self Value) Value {
args := mrb.GetArgv()
argc := mrb.GetArgc()
Expand Down

0 comments on commit 31b9231

Please sign in to comment.