Skip to content

Commit

Permalink
Add InheritClass for includeClassNew
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Sep 5, 2024
1 parent 28204ba commit abf0af4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func (mrb *State) AllocSingletonClass() *SingletonClass {
}
}

func (mrb *State) AllocInheritClass() *InheritClass {
return &InheritClass{
class: class{
Object: Object{
class: mrb.ClassClass,
},
},
}
}

func (mrb *State) AllocModule() *Module {
return &Module{
class: class{
Expand Down
34 changes: 33 additions & 1 deletion class.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var _ RClass = &SingletonClass{}
type RClass interface {
RObject
Super() RClass
setClass(RClass)
setSuper(RClass)
getMethodTable() methodTable
setMethodTable(methodTable)
mtPut(Symbol, Method)
mtGet(Symbol) Method
}
Expand All @@ -41,6 +44,10 @@ type SingletonClass struct {
class
}

type InheritClass struct {
class
}

func (mrb *State) Class(v Value) RClass {
switch v := v.(type) {
case RObject:
Expand Down Expand Up @@ -178,7 +185,20 @@ func (mrb *State) prepareSingletonClass(obj RObject) error {
}

func (mrb *State) includeClassNew(module, super RClass) RClass {
ic := mrb.AllocClass()
ic := mrb.AllocInheritClass()
if _, isIClass := module.(*InheritClass); isIClass {
module = module.Class()
}

module = findOrigin(module)
ic.setMethodTable(module.getMethodTable())
ic.setSuper(super)

if _, isIClass := module.(*InheritClass); isIClass {
ic.setClass(module.Class())
} else {
ic.setClass(module)
}

return ic
}
Expand Down Expand Up @@ -239,6 +259,18 @@ func (c *class) setSuper(super RClass) {
c.super = super
}

func (c *class) setClass(class RClass) {
c.class = class
}

func (c *class) getMethodTable() methodTable {
return c.mt
}

func (c *class) setMethodTable(mt methodTable) {
c.mt = mt
}

func (c *class) ivPut(sym Symbol, val Value) {
if c.iv == nil {
c.iv = make(iv)
Expand Down

0 comments on commit abf0af4

Please sign in to comment.