Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit ca026fe

Browse files
author
Fabian
committed
Added *MrbValue.GetInstanceVariable, *MrbValue.SetInstanceVariable, *Mrb.GetGlobalVariable, *Mrb.SetGlobalVariable and their underlying C methods to allow Go to pass variables to/from Ruby.
1 parent 431f823 commit ca026fe

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

gomruby.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,20 @@ static inline struct RObject* _go_mrb_getobj(mrb_value v) {
252252
return mrb_obj_ptr(v);
253253
}
254254

255+
static inline void _go_mrb_iv_set(mrb_state *m, mrb_value self, mrb_sym sym, mrb_value v) {
256+
mrb_iv_set(m, self, sym, v);
257+
}
258+
259+
static inline mrb_value _go_mrb_iv_get(mrb_state *m, mrb_value self, mrb_sym sym) {
260+
return mrb_iv_get(m, self, sym);
261+
}
262+
263+
static inline void _go_mrb_gv_set(mrb_state *m, mrb_sym sym, mrb_value v) {
264+
mrb_gv_set(m, sym, v);
265+
}
266+
267+
static inline mrb_value _go_mrb_gv_get(mrb_state *m, mrb_sym sym) {
268+
return mrb_gv_get(m, sym);
269+
}
270+
255271
#endif

mruby.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ type Mrb struct {
1313
state *C.mrb_state
1414
}
1515

16+
// GetGlobalVariable returns the value of the global variable by the given name.
17+
func (m *Mrb) GetGlobalVariable(name string) *MrbValue {
18+
cs := C.CString(name)
19+
defer C.free(unsafe.Pointer(cs))
20+
return newValue(m.state, C._go_mrb_gv_get(m.state, C.mrb_intern_cstr(m.state, cs)))
21+
}
22+
23+
// SetGlobalVariable sets the value of the global variable by the given name.
24+
func (m *Mrb) SetGlobalVariable(name string, value Value) {
25+
cs := C.CString(name)
26+
defer C.free(unsafe.Pointer(cs))
27+
28+
v := value.MrbValue(m)
29+
C._go_mrb_gv_set(m.state, C.mrb_intern_cstr(m.state, cs), v.value)
30+
}
31+
1632
// ArenaIndex represents the index into the arena portion of the GC.
1733
//
1834
// See ArenaSave for more information.

value.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ func init() {
4141
Nil = [0]byte{}
4242
}
4343

44+
// SetInstanceVariable sets an instance variable on this value.
45+
func (v *MrbValue) SetInstanceVariable(variable string, value *MrbValue) {
46+
cs := C.CString(variable)
47+
defer C.free(unsafe.Pointer(cs))
48+
C._go_mrb_iv_set(v.state, v.value, C.mrb_intern_cstr(v.state, cs), value.value)
49+
}
50+
51+
// GetInstanceVariable gets an instance variable on this value.
52+
func (v *MrbValue) GetInstanceVariable(variable string) *MrbValue {
53+
cs := C.CString(variable)
54+
defer C.free(unsafe.Pointer(cs))
55+
return newValue(v.state, C._go_mrb_iv_get(v.state, v.value, C.mrb_intern_cstr(v.state, cs)))
56+
}
57+
4458
// Call calls a method with the given name and arguments on this
4559
// value.
4660
func (v *MrbValue) Call(method string, args ...Value) (*MrbValue, error) {

0 commit comments

Comments
 (0)