Skip to content

Commit

Permalink
Refactor to implement find method by class
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Oct 26, 2023
1 parent 849f4f3 commit 24b7965
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
25 changes: 25 additions & 0 deletions class.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mruby

import "fmt"

var methods = map[string]*Method{
"puts": {
Function: func(mrb *State, recv Value) Value {
args, ok := recv.([]Value)
if !ok {
return nil
}

fmt.Println(args...)
return args[0]
},
},
}

func findMethod(mrb *State, recv Value, mid string) *Method {
if m, ok := methods[mid]; ok {
return m
}

return nil
}
11 changes: 7 additions & 4 deletions irep.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ func (ir *iRep) Execute(state *State) (Value, error) {
b := ir.iSeq.ReadB()
c := ir.iSeq.ReadB()

funcName := ir.syms[b]
if funcName == "puts" {
recv := regs[0]
mid := ir.syms[b]
method := findMethod(state, recv, mid)

if method != nil {
argc := c & 0xf
fmt.Println(regs[a+1 : a+argc+1]...)
regs[a] = regs[a+1]
args := regs[a+1 : a+argc+1]
regs[a] = method.Function(state, args)
break
}

Expand Down
6 changes: 6 additions & 0 deletions mruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ type (
Value = any
)

type Function func(*State, Value) Value

type Method struct {
Function
}

type State struct {
}

Expand Down

0 comments on commit 24b7965

Please sign in to comment.