Skip to content

Commit

Permalink
Add define module support
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Jun 27, 2024
1 parent 3c23c7b commit 088375a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions features/module.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: Module
Scenario: I can define a Module
When I execute ruby code:
"""
module Generic
end
Generic
"""
Then there should return object
And there should return module "Generic"
4 changes: 4 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func (mrb *State) DefineModuleId(name Symbol) RClass {
return defineModule(mrb, name, mrb.ObjectClass)
}

func (mrb *State) vmDefineModule(outer RClass, name Symbol) RClass {
return defineModule(mrb, name, outer)
}

func defineModule(mrb *State, name Symbol, outer RClass) *Module {
module := mrb.AllocModule()
module.mt = make(methodTable)
Expand Down
12 changes: 12 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ func (mrb *State) VmExec(proc RProc, code *insn.Sequence) (ret Value, err error)
}

ctx.Set(int(a), NewObjectValue(class))
case op.Module:
a := code.ReadB()
b := code.ReadB()

id := rep.Symbol(b)
base, ok := ctx.Get(int(a)).(RClass)
if !ok {
base = mrb.ObjectClass
}

module := mrb.vmDefineModule(base, id)
ctx.Set(int(a), NewObjectValue(module))
case op.Method:
a := code.ReadB()
b := code.ReadB()
Expand Down

0 comments on commit 088375a

Please sign in to comment.