Skip to content

Commit

Permalink
Add State#ClassOf to get mruby value class
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Nov 16, 2023
1 parent e3baf37 commit e27d42b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
8 changes: 8 additions & 0 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ var methods = map[string]*Method{
},
}

type RClass struct {
}

func (mrb *State) ClassOf(v Value) *RClass {
// NOTE: Null Pointer fallback to FalseClass
return mrb.falseClass
}

func (mrb *State) FindMethod(recv Value, mid string) *Method {
if m, ok := methods[mid]; ok {
return m
Expand Down
6 changes: 5 additions & 1 deletion irep.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ func (ir *iRep) Execute(state *State) (Value, error) {
b := ir.iSeq.ReadB()
c := ir.iSeq.ReadB()

ci := state.PushCallinfo(ir.syms[b], c)
regs[a] = regs[0]
opCode = opSend

Check failure on line 88 in irep.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to opCode (ineffassign)

ci := state.PushCallinfo(ir.syms[b], c, nil)
ci.stack = append(ci.stack, regs[int(a)+1:int(a)+ci.numArgs+1]...)

recv := regs[0]
ci.targetClass = state.ClassOf(recv)
method := state.FindMethod(recv, ci.methodId)

if method == nil {
Expand Down
10 changes: 7 additions & 3 deletions mruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ type Method struct {
}

type callinfo struct {
numArgs int
methodId Symbol
stack []Value
numArgs int
methodId Symbol
stack []Value
targetClass *RClass
}

type context struct {
Expand All @@ -28,6 +29,8 @@ func (ctx *context) GetCallinfo() *callinfo {

type State struct {
context *context

falseClass *RClass
}

func New() *State {
Expand All @@ -36,6 +39,7 @@ func New() *State {
ciCursor: -1,
callinfos: []*callinfo{},
},
falseClass: &RClass{},
}
}

Expand Down
9 changes: 5 additions & 4 deletions vm.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package mruby

func (state *State) PushCallinfo(mid string, argc byte) *callinfo {
func (state *State) PushCallinfo(mid string, argc byte, targetClass *RClass) *callinfo {
ctx := state.context

callinfo := &callinfo{
methodId: mid,
numArgs: int(argc & 0xf),
stack: []Value{nil},
methodId: mid,
numArgs: int(argc & 0xf),
stack: []Value{nil},
targetClass: targetClass,
}

ctx.ciCursor++
Expand Down

0 comments on commit e27d42b

Please sign in to comment.