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

Commit

Permalink
Added *MrbValue.GetInstanceVariable, *MrbValue.SetInstanceVariable, *…
Browse files Browse the repository at this point in the history
…Mrb.GetGlobalVariable, *Mrb.SetGlobalVariable and their underlying C methods to allow Go to pass variables to/from Ruby.
  • Loading branch information
Fabian committed Sep 26, 2018
1 parent 431f823 commit ca026fe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gomruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,20 @@ static inline struct RObject* _go_mrb_getobj(mrb_value v) {
return mrb_obj_ptr(v);
}

static inline void _go_mrb_iv_set(mrb_state *m, mrb_value self, mrb_sym sym, mrb_value v) {
mrb_iv_set(m, self, sym, v);
}

static inline mrb_value _go_mrb_iv_get(mrb_state *m, mrb_value self, mrb_sym sym) {
return mrb_iv_get(m, self, sym);
}

static inline void _go_mrb_gv_set(mrb_state *m, mrb_sym sym, mrb_value v) {
mrb_gv_set(m, sym, v);
}

static inline mrb_value _go_mrb_gv_get(mrb_state *m, mrb_sym sym) {
return mrb_gv_get(m, sym);
}

#endif
16 changes: 16 additions & 0 deletions mruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ type Mrb struct {
state *C.mrb_state
}

// GetGlobalVariable returns the value of the global variable by the given name.
func (m *Mrb) GetGlobalVariable(name string) *MrbValue {
cs := C.CString(name)
defer C.free(unsafe.Pointer(cs))
return newValue(m.state, C._go_mrb_gv_get(m.state, C.mrb_intern_cstr(m.state, cs)))
}

// SetGlobalVariable sets the value of the global variable by the given name.
func (m *Mrb) SetGlobalVariable(name string, value Value) {
cs := C.CString(name)
defer C.free(unsafe.Pointer(cs))

v := value.MrbValue(m)
C._go_mrb_gv_set(m.state, C.mrb_intern_cstr(m.state, cs), v.value)
}

// ArenaIndex represents the index into the arena portion of the GC.
//
// See ArenaSave for more information.
Expand Down
14 changes: 14 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func init() {
Nil = [0]byte{}
}

// SetInstanceVariable sets an instance variable on this value.
func (v *MrbValue) SetInstanceVariable(variable string, value *MrbValue) {
cs := C.CString(variable)
defer C.free(unsafe.Pointer(cs))
C._go_mrb_iv_set(v.state, v.value, C.mrb_intern_cstr(v.state, cs), value.value)
}

// GetInstanceVariable gets an instance variable on this value.
func (v *MrbValue) GetInstanceVariable(variable string) *MrbValue {
cs := C.CString(variable)
defer C.free(unsafe.Pointer(cs))
return newValue(v.state, C._go_mrb_iv_get(v.state, v.value, C.mrb_intern_cstr(v.state, cs)))
}

// Call calls a method with the given name and arguments on this
// value.
func (v *MrbValue) Call(method string, args ...Value) (*MrbValue, error) {
Expand Down

0 comments on commit ca026fe

Please sign in to comment.