Ejemplo n.º 1
0
func go2mruby(mrb *C.mrb_state, o interface{}) C.mrb_value {
	v := reflect.ValueOf(o)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return C.mrb_fixnum_value(C.mrb_int(v.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return C.mrb_fixnum_value(C.mrb_int(v.Uint()))
	case reflect.Float32, reflect.Float64:
		return C.mrb_float_value((C.mrb_float)(v.Float()))
	case reflect.Complex64, reflect.Complex128:
		return C.mrb_float_value((C.mrb_float)(v.Float()))
	case reflect.String:
		ptr := C.CString(v.String())
		return C.mrb_str_new(mrb, ptr, C.strlen(ptr))
	case reflect.Bool:
		if v.Bool() {
			return C.mrb_true_value()
		}
		return C.mrb_false_value()
	case reflect.Array, reflect.Slice:
		ary := C.mrb_ary_new(mrb)
		for i := 0; i < v.Len(); i++ {
			C.mrb_ary_push(mrb, ary, go2mruby(mrb, v.Index(i).Interface()))
		}
		return ary
	case reflect.Map:
		hash := C.mrb_hash_new(mrb, 32)
		for _, key := range v.MapKeys() {
			val := v.MapIndex(key)
			C.mrb_hash_set(mrb, hash, go2mruby(mrb, key.String()), go2mruby(mrb, val.Interface()))
		}
		return hash
	case reflect.Interface:
		return go2mruby(mrb, v.Elem().Interface())
	}
	return C.mrb_nil_value()
}
Ejemplo n.º 2
0
func (m *MRuby) NewString(str string) C.mrb_value {
	return C.mrb_str_new(m.state, C.CString(str), C.int(len(str)))
}