Skip to content

Commit

Permalink
Clean nested struct from Class/SingletonClass/Module struct
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Aug 1, 2024
1 parent aae2108 commit 25330f1
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 32 deletions.
12 changes: 3 additions & 9 deletions allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ package mruby

func (mrb *State) AllocClass() *Class {
return &Class{
class: class{
class: mrb.ClassClass,
},
class: mrb.ClassClass,
}
}

func (mrb *State) AllocSingletonClass() *SingletonClass {
return &SingletonClass{
class: class{
class: mrb.ClassClass,
},
class: mrb.ClassClass,
}
}

func (mrb *State) AllocModule() *Module {
return &Module{
class: class{
class: mrb.ModuleClass,
},
class: mrb.ModuleClass,
}
}

Expand Down
36 changes: 14 additions & 22 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,23 @@ var (

type methodTable map[Symbol]Method

var _ RClass = &Class{}

type RClass interface {
RObject
Super() RClass
mtPut(Symbol, Method)
mtGet(Symbol) Method
}

type class struct {
var _ RClass = &Class{}

type Class struct {
super RClass
class RClass
flags uint32
mt methodTable
iv ivTable
}

type Class struct {
class
}

type SingletonClass struct {
class
}

func (mrb *State) Class(v Value) RClass {
switch v := v.(type) {
case RObject:
Expand Down Expand Up @@ -189,47 +181,47 @@ func (mrb *State) defineMethodRaw(class RClass, name Symbol, method Method) {
class.mtPut(name, method)
}

func (c *class) Class() RClass {
func (c *Class) Class() RClass {
return c.class
}

func (c *class) Flags() uint32 {
func (c *Class) Flags() uint32 {
return c.flags
}

func (c *class) ivPut(sym Symbol, val Value) {
func (c *Class) ivPut(sym Symbol, val Value) {
if c.iv == nil {
c.iv = make(ivTable)
}

c.iv[sym] = val
}

func (c *class) ivGet(sym Symbol) Value {
func (c *Class) ivGet(sym Symbol) Value {
if c.iv == nil {
return nil
}

return c.iv[sym]
}

func (c *class) mtPut(sym Symbol, method Method) {
func (c *Class) mtPut(sym Symbol, method Method) {
if c.mt == nil {
c.mt = make(methodTable)
}

c.mt[sym] = method
}

func (c *class) mtGet(sym Symbol) Method {
func (c *Class) mtGet(sym Symbol) Method {
if c.mt == nil {
return nil
}

return c.mt[sym]
}

func (c *class) Super() RClass {
func (c *Class) Super() RClass {
return c.super
}

Expand Down Expand Up @@ -303,10 +295,10 @@ func initClass(mrb *State) (err error) {
classClass := mrb.bootDefineClass(moduleClass)
mrb.ClassClass = classClass

basicObject.class.class = classClass
objectClass.class.class = classClass
moduleClass.class.class = classClass
classClass.class.class = classClass
basicObject.class = classClass
objectClass.class = classClass
moduleClass.class = classClass
classClass.class = classClass

for _, class := range []RClass{basicObject, objectClass, moduleClass, classClass} {
err = mrb.prepareSingletonClass(class)
Expand Down
50 changes: 49 additions & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,55 @@ package mruby
var _ RClass = &Module{}

type Module struct {
class
super RClass
class RClass
flags uint32
mt methodTable
iv ivTable
}

func (c *Module) Class() RClass {
return c.class
}

func (c *Module) Flags() uint32 {
return c.flags
}

func (c *Module) ivPut(sym Symbol, val Value) {
if c.iv == nil {
c.iv = make(ivTable)
}

c.iv[sym] = val
}

func (c *Module) ivGet(sym Symbol) Value {
if c.iv == nil {
return nil
}

return c.iv[sym]
}

func (c *Module) mtPut(sym Symbol, method Method) {
if c.mt == nil {
c.mt = make(methodTable)
}

c.mt[sym] = method
}

func (c *Module) mtGet(sym Symbol) Method {
if c.mt == nil {
return nil
}

return c.mt[sym]
}

func (c *Module) Super() RClass {
return c.super
}

func (mrb *State) DefineModuleId(name Symbol) RClass {
Expand Down
55 changes: 55 additions & 0 deletions sclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mruby

var _ RClass = &SingletonClass{}

type SingletonClass struct {
super RClass
class RClass
flags uint32
mt methodTable
iv ivTable
}

func (c *SingletonClass) Class() RClass {
return c.class
}

func (c *SingletonClass) Flags() uint32 {
return c.flags
}

func (c *SingletonClass) ivPut(sym Symbol, val Value) {
if c.iv == nil {
c.iv = make(ivTable)
}

c.iv[sym] = val
}

func (c *SingletonClass) ivGet(sym Symbol) Value {
if c.iv == nil {
return nil
}

return c.iv[sym]
}

func (c *SingletonClass) mtPut(sym Symbol, method Method) {
if c.mt == nil {
c.mt = make(methodTable)
}

c.mt[sym] = method
}

func (c *SingletonClass) mtGet(sym Symbol) Method {
if c.mt == nil {
return nil
}

return c.mt[sym]
}

func (c *SingletonClass) Super() RClass {
return c.super
}

0 comments on commit 25330f1

Please sign in to comment.