Пример #1
0
// Converts mruby value to Go value.
func (m *MRuby) goValue(v C.mrb_value) interface{} {
	switch v.tt {
	case C.MRB_TT_UNDEF: // for example, result of syntax error
		return nil
	case C.MRB_TT_TRUE:
		return true
	case C.MRB_TT_FALSE:
		if C._gomruby_is_nil(v) == 0 {
			return false
		}
		return nil
	case C.MRB_TT_FIXNUM:
		return int(C._gomruby_fixnum(v))
	case C.MRB_TT_FLOAT:
		return float64(C._gomruby_float(v))
	case C.MRB_TT_SYMBOL:
		cs := C.mrb_string_value_ptr(m.state, v)
		return Symbol(C.GoString(cs))
	case C.MRB_TT_STRING:
		cs := C.mrb_string_value_ptr(m.state, v)
		return C.GoString(cs)
	case C.MRB_TT_ARRAY:
		l := int(C.mrb_ary_len(m.state, v))
		res := make([]interface{}, l)
		for i := 0; i < l; i++ {
			res[i] = m.goValue(C.mrb_ary_ref(m.state, v, C.mrb_int(i)))
		}
		return res
	case C.MRB_TT_HASH:
		keys := C.mrb_hash_keys(m.state, v)
		l := int(C.mrb_ary_len(m.state, keys))
		res := make(map[interface{}]interface{}, l)
		for i := 0; i < l; i++ {
			key := C.mrb_ary_ref(m.state, keys, C.mrb_int(i))
			val := C.mrb_hash_get(m.state, v, key)
			res[m.goValue(key)] = m.goValue(val)
		}
		return res
	}

	panic(fmt.Errorf("gomruby bug: failed to convert mruby value %#v to Go value", v))
}
Пример #2
0
// Len returns the length of the array.
func (v *Array) Len() int {
	return int(C.mrb_ary_len(v.state, v.value))
}