Esempio n. 1
0
func go_value_to_tcl_obj(value interface{}) *C.Tcl_Obj {
	v := reflect.ValueOf(value)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Uint()))
	case reflect.Float32, reflect.Float64:
		return C.Tcl_NewDoubleObj(C.double(v.Float()))
	case reflect.Bool:
		if v.Bool() {
			return C.Tcl_NewBooleanObj(1)
		}
		return C.Tcl_NewBooleanObj(0)
	case reflect.String:
		s := v.String()
		sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
		return C.Tcl_NewStringObj((*C.char)(unsafe.Pointer(sh.Data)), C.int(len(s)))
	}
	return nil
}
Esempio n. 2
0
func go_value_to_tcl_obj(value interface{}, interp *interpreter) *C.Tcl_Obj {
	v := reflect.ValueOf(value)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Uint()))
	case reflect.Float32, reflect.Float64:
		return C.Tcl_NewDoubleObj(C.double(v.Float()))
	case reflect.Bool:
		if v.Bool() {
			return C.Tcl_NewBooleanObj(1)
		}
		return C.Tcl_NewBooleanObj(0)
	case reflect.String:
		s := v.String()
		sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
		return C.Tcl_NewStringObj((*C.char)(unsafe.Pointer(sh.Data)), C.int(len(s)))
	case reflect.Slice, reflect.Array:
		objv := make([]*C.Tcl_Obj, v.Len())
		for i := 0; i < v.Len(); i++ {
			objv[i] = go_value_to_tcl_obj(v.Index(i).Interface(), interp)
		}

		return C.Tcl_NewListObj(C.int(len(objv)), &objv[0])
	case reflect.Map:
		tclmap := C.Tcl_NewDictObj()

		for _, value := range v.MapKeys() {
			key := go_value_to_tcl_obj(value.Interface(), interp)
			val := go_value_to_tcl_obj(v.MapIndex(value).Interface(), interp)
			if key != nil && val != nil {
				C.Tcl_DictObjPut(interp.C, tclmap, key, val)
			}
		}
		return tclmap
	}
	return nil
}