예제 #1
0
// dynamicToInterface - limited runtime conversion of Haxe->Go types
// with URL un-escaping of strings
// TODO consider making public
func dynamicToInterface(dyn uintptr) interface{} {
	switch {
	case hx.CodeBool("", "Std.is(_a.param(0).val,Array);", dyn):
		l := hx.CodeInt("", "_a.param(0).val.length;", dyn)
		ret := make([]interface{}, l)
		for i := 0; i < l; i++ {
			ret[i] =
				dynamicToInterface(hx.CodeDynamic("",
					"_a.param(0).val[_a.param(1).val];", dyn, i))
		}
		return ret
	case hx.IsNull(dyn):
		return nil
	case hx.CodeBool("", "Std.is(_a.param(0).val,Bool);", dyn):
		return hx.CodeBool("", "_a.param(0).val;", dyn)
	case hx.CodeBool("", "Std.is(_a.param(0).val,Int);", dyn):
		return hx.CodeInt("", "_a.param(0).val;", dyn)
	case hx.CodeBool("", "Std.is(_a.param(0).val,Float);", dyn):
		return hx.CodeFloat("", "_a.param(0).val;", dyn)
	case hx.CodeBool("", "Std.is(_a.param(0).val,String);", dyn):
		return hx.CodeString("", "_a.param(0).val;", dyn)
	case hx.CodeBool("", "Std.is(_a.param(0).val,haxe.io.Bytes);", dyn):
		return hx.CodeIface("", "[]byte",
			"Slice.fromBytes(_a.param(0).val);", dyn)
	default:
		panic("unhandled haxe Dynamic to Go interface{} :" +
			hx.CallString("", "Std.string", 1, dyn))
	}
	return nil
}
예제 #2
0
func haxeInterfacePackB(ei *emptyInterface) interface{} {

	// TODO deal with created types, if any ?

	switch ei.typ.Kind() {
	case Bool:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*bool)(ei.word))
	case Int:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*int)(ei.word))
	case Int8:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*int8)(ei.word))
	case Int16:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*int16)(ei.word))
	case Int32:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*int32)(ei.word))
	case Int64:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*int64)(ei.word))
	case Uint:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uint)(ei.word))
	case Uint8:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uint8)(ei.word))
	case Uint16:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uint16)(ei.word))
	case Uint32:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uint32)(ei.word))
	case Uint64:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uint64)(ei.word))
	case Uintptr:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*uintptr)(ei.word))
	case Float32:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*float32)(ei.word))
	case Float64:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*float64)(ei.word))
	case Complex64:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*complex64)(ei.word))
	case Complex128:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*complex128)(ei.word))
	case UnsafePointer:
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", ei.word)

	case String:
		//println("DEBUG string pack=", *(*string)(ei.word))
		return hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", *(*string)(ei.word))

	case Ptr:
		p := hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", ei.word)
		//println("DEBUG ptr pack p=", p)
		return p

	case Array, Struct:
		return hx.CodeIface("", ei.typ.String(),
			"_a.param(0).val.load_object(_a.param(1).val);",
			ei.word, ei.typ.size)

	case Slice, Map, Interface, Func, Chan:
		if ei.word == nil {
			return hx.CodeIface("", ei.typ.String(), "null;")
		}
		/*
			htyp := "null"
			if !hx.IsNull(uintptr(ei.word)) {
				htyp = hx.CallString("", "Type.getClassName", 1, ei.word)
			}
			println("DEBUG pack haxe type=", htyp, " Go type=", ei.typ.Kind().String(), "val=", ei.word, "encoded=", ei)
		*/
		val := *(*uintptr)(unsafe.Pointer(ei.word))
		r := hx.CodeIface("", ei.typ.String(), "_a.param(0).val;", val)
		//println("DEBUG pack haxe encoded=", ei, "type=", hx.CallString("", "Type.getClassName", 1, ei.word),
		//	"Go type=", ei.typ.Kind().String(), "PtrVal=", ei.word, "Return=", r)
		return r
	}

	panic("reflect.haxeInterfacePack() not yet implemented for " + ei.typ.String() +
		" Kind= " + ei.typ.Kind().String())

	return interface{}(nil)
}