forked from mitchellh/go-mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
91 lines (73 loc) · 2.2 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mruby
import (
"fmt"
"unsafe"
)
// #include <stdlib.h>
// #include "gomruby.h"
import "C"
// Func is the signature of a function in Go that you use to expose to Ruby
// code.
//
// The first return value is the actual return value for the code.
//
// The second return value is an exception, if any. This will be raised.
type Func func(m *Mrb, self *MrbValue) (Value, Value)
type classMethodMap map[*C.struct_RClass]methodMap
type methodMap map[C.mrb_sym]Func
type stateMethodMap map[*C.mrb_state]classMethodMap
// stateMethodTable is the lookup table for methods that we define in Go and
// expose in Ruby. This is cleaned up by Mrb.Close.
var stateMethodTable stateMethodMap
func init() {
stateMethodTable = make(stateMethodMap)
}
//export go_mrb_func_call
func go_mrb_func_call(s *C.mrb_state, v *C.mrb_value, c_exc *C.mrb_value) C.mrb_value {
// Lookup the classes that we've registered methods for in this state
classTable := stateMethodTable[s]
if classTable == nil {
panic(fmt.Sprintf("func call from unknown state: %p", s))
}
// Get the call info, which we use to lookup the proc
ci := s.c.ci
// Lookup the class itself
methodTable := classTable[ci.proc.target_class]
if methodTable == nil {
panic(fmt.Sprintf("func call on unknown class"))
}
// Lookup the method
f := methodTable[ci.mid]
if f == nil {
panic(fmt.Sprintf("func call on unknown method"))
}
// Call the method to get our *Value
// TODO(mitchellh): reuse the Mrb instead of allocating every time
mrb := &Mrb{s}
result, exc := f(mrb, newValue(s, *v))
if exc != nil {
*c_exc = exc.MrbValue(mrb).value
return mrb.NilValue().value
}
// If the result was a Go nil, convert it to a Ruby nil
if result == nil {
result = mrb.NilValue()
}
return result.MrbValue(mrb).value
}
func insertMethod(s *C.mrb_state, c *C.struct_RClass, n string, f Func) {
classLookup := stateMethodTable[s]
if classLookup == nil {
classLookup = make(classMethodMap)
stateMethodTable[s] = classLookup
}
methodLookup := classLookup[c]
if methodLookup == nil {
methodLookup = make(methodMap)
classLookup[c] = methodLookup
}
cs := C.CString(n)
defer C.free(unsafe.Pointer(cs))
sym := C.mrb_intern_cstr(s, cs)
methodLookup[sym] = f
}