Example #1
0
// interfaceToDynamic - limited runtime conversion of Go->Haxe types
// TODO consider making public
func interfaceToDynamic(a interface{}) uintptr {
	//fmt.Printf("DEBUG interfaceToDynamic a= %v:%T\n", a, a)
	if a == nil {
		return hx.Null()
	}
	switch a.(type) {
	case []interface{}:
		//println("DEBUG []interface{}")
		ret := hx.New("", "Array<Dynamic>", 0)
		for _, aa := range a.([]interface{}) {
			//fmt.Printf("DEBUG aa= %v:%T\n", aa, aa)
			val := interfaceToDynamic(aa)
			//fmt.Println("DEBUG val=" + hx.CallString("", "Std.string", 1, val))
			hx.Code("", "_a.param(0).val.push(_a.param(1).val);", ret, val)
		}
		return ret
	case bool, string, int, float64:
		return hx.CodeDynamic("", "_a.param(0).val;", a)
	case []byte:
		return hx.CodeDynamic("", "Slice.toBytes(cast(_a.param(0).val,Slice));", a)
	default:
		panic("Unhandled Go interface{} to Haxe Dynamic: " + hx.CallString("", "Std.string", 1, a))
	}
	return hx.Null()
}
Example #2
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
}
Example #3
0
// Float64bits returns the IEEE 754 binary representation of f.
//func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
func Float64bits(f float64) uint64 {
	return uint64(hx.Int64(hx.CodeDynamic("",
		"Go_haxegoruntime_FFloat64bits.callFromRT(this._goroutine,_a.itemAddr(0).load().val);", f)))
}
Example #4
0
func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
	if p.erroring {
		return
	}
	// Is it a Formatter?
	if formatter, ok := p.arg.(Formatter); ok {
		handled = true
		defer p.restoreSpecialFlags(p.clearSpecialFlags())
		defer p.catchPanic(p.arg, verb)
		if hx.IsNull(hx.CodeDynamic("", "_a.itemAddr(0).load().val;", formatter)) {
			panic("<nil>")
		}
		formatter.Format(p, verb)
		return
	}

	// If we're doing Go syntax and the argument knows how to supply it, take care of it now.
	if p.fmt.sharpV {
		if stringer, ok := p.arg.(GoStringer); ok {
			handled = true
			defer p.catchPanic(p.arg, verb)
			if hx.IsNull(hx.CodeDynamic("", "_a.itemAddr(0).load().val;", stringer)) {
				panic("<nil>")
			}
			// Print the result of GoString unadorned.
			p.fmt.fmt_s(stringer.GoString())
			return
		}
	} else {
		// If a string is acceptable according to the format, see if
		// the value satisfies one of the string-valued interfaces.
		// Println etc. set verb to %v, which is "stringable".
		switch verb {
		case 'v', 's', 'x', 'X', 'q':
			// Is it an error or Stringer?
			// The duplication in the bodies is necessary:
			// setting handled and deferring catchPanic
			// must happen before calling the method.
			switch v := p.arg.(type) {
			case error:
				handled = true
				defer p.catchPanic(p.arg, verb)
				if hx.IsNull(hx.CodeDynamic("", "_a.itemAddr(0).load().val;", v)) {
					panic("<nil>")
				}
				p.printArg(v.Error(), verb, depth)
				return

			case Stringer:
				handled = true
				defer p.catchPanic(p.arg, verb)
				if hx.IsNull(hx.CodeDynamic("", "_a.itemAddr(0).load().val;", v)) {
					panic("<nil>")
				}
				p.printArg(v.String(), verb, depth)
				return
			}
		}
	}
	return false
}
Example #5
0
func haxe2go(ret *emptyInterface, i interface{}) {
	if ret == nil {
		i = interface{}(nil)
		return
	}
	/*
		if ret.typ == nil { // HELP! assume dynamic?
			panic("DEBUG reflect.haxe2go() nil Go type")
			//println("DEBUG reflect.haxe2go() nil Go type, using uintptr")
			//uip := hx.CodeIface("", "uintptr", "null;")
			//ret.typ = createHaxeType(hx.CodeInt("", "_a.param(0).typ;", uip))
		}
	*/
	ret.word = hx.Malloc(ret.typ.size)

	switch ret.typ.Kind() & kindMask {
	case Bool:
		*(*bool)(ret.word) = hx.CodeBool("", "_a.param(0).val;", i)
	case Int:
		*(*int)(ret.word) = hx.CodeInt("", "_a.param(0).val;", i)
	case Int8:
		*(*int8)(ret.word) = int8(hx.CodeInt("", "_a.param(0).val;", i))
	case Int16:
		*(*int16)(ret.word) = int16(hx.CodeInt("", "_a.param(0).val;", i))
	case Int32:
		*(*int32)(ret.word) = int32(hx.CodeInt("", "_a.param(0).val;", i))
	case Int64:
		*(*int64)(ret.word) = hx.Int64(hx.CodeDynamic("", "_a.param(0).val;", i))
	case Uint:
		*(*uint)(ret.word) = uint(hx.CodeInt("", "_a.param(0).val;", i))
	case Uint8:
		*(*uint8)(ret.word) = uint8(hx.CodeInt("", "_a.param(0).val;", i))
	case Uint16:
		*(*uint16)(ret.word) = uint16(hx.CodeInt("", "_a.param(0).val;", i))
	case Uint32:
		*(*uint32)(ret.word) = uint32(hx.CodeInt("", "_a.param(0).val;", i))
	case Uint64:
		*(*uint64)(ret.word) = uint64(hx.Int64(hx.CodeDynamic("", "_a.param(0).val;", i)))
	case Uintptr:
		*(*uintptr)(ret.word) = uintptr(hx.CodeDynamic("", "_a.param(0).val;", i))
	case Float32:
		*(*float32)(ret.word) = float32(hx.CodeFloat("", "_a.param(0).val;", i))
	case Float64:
		*(*float64)(ret.word) = float64(hx.CodeFloat("", "_a.param(0).val;", i))
	case Complex64:
		*(*complex64)(ret.word) = complex64(hx.Complex(hx.CodeDynamic("", "_a.param(0).val;", i)))
	case Complex128:
		*(*complex128)(ret.word) = hx.Complex(hx.CodeDynamic("", "_a.param(0).val;", i))
	case UnsafePointer, Ptr:
		//*(*unsafe.Pointer)(ret.word) = unsafe.Pointer(hx.CodeDynamic("", "_a.param(0).val;", i))
		ret.word = unsafe.Pointer(hx.CodeDynamic("", "_a.param(0).val;", i))

	case String:
		*(*string)(ret.word) = hx.CodeString("", "_a.param(0).val;", i)

	case Array, Struct:
		hx.Code("",
			"_a.param(1).val.store_object(_a.param(2).val,_a.param(0).val);",
			i, ret.word, ret.typ.size)

	case Slice, Interface, Map, Func, Chan:
		val := hx.CodeDynamic("", "_a.param(0).val;", i)
		*(*uintptr)(ret.word) = val

		/*
			htyp := "null"
			if !hx.IsNull(val) {
				htyp = hx.CallString("", "Type.getClassName", 1, val)
			}
			println("DEBUG unpack haxe type=", htyp, " Go type=", ret.typ.Kind().String(), "val=", val, "encoded=", ret)
		*/
	}

}