Esempio n. 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
}
Esempio n. 2
0
func haxeInterfacePack(ei *emptyInterface) interface{} {
	i := haxeInterfacePackB(ei)

	ityp := hx.CodeInt("", "_a.param(0).typ;", i)
	if unsafe.Pointer(ei.typ) != unsafe.Pointer(haxegoruntime.TypeTable[ityp]) {
		typ := typeIdFromPtr(ei.typ)
		//println("DEBUG typ(new)!=ityp(old) ", typ, ityp)
		hx.Code("",
			"var x=cast(_a.param(0),Interface); x.typ=_a.param(1).val; {var p=_a.itemAddr(2); p.store(x); p=null;}; ",
			i, typ, &i)
		//println("DEBUG amended to ", hx.CodeInt("", "_a.param(0).typ;", i))
	}

	return i
}
Esempio n. 3
0
func haxeInterfaceUnpack(i interface{}) *emptyInterface {
	ret := new(emptyInterface)
	if i == nil {
		//panic("reflect.haxeInterfaceUnpack() nil value")
		return ret
	}
	haxeTypeID := hx.CodeInt("", "_a.param(0).typ;", i)
	ret.typ = createHaxeType(haxeTypeID)
	//if ret.typ == nil {
	//	panic("reflect.haxeInterfaceUnpack() nil type pointer for HaxeTypeID " +
	//		hx.CallString("", "Std.string", 1, haxeTypeID))
	//}

	haxe2go(ret, i)
	return ret
}
Esempio n. 4
0
// IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
func IndexByte(s string, c byte) int {
	if s == "" {
		return -1
	}
	return hx.CodeInt("", "cast(_a.itemAddr(0).load().val,String).indexOf(_a.itemAddr(1).load().val);",
		s, string(rune(c)))
	/*
		sb := []byte(s)
		for i := 0; i < len(sb); i++ {
			if sb[i] == c {
				return i
			}
		}
		return -1
	*/
} // ../runtime/asm_$GOARCH.s
Esempio n. 5
0
func main() {
	var array [4][5]int
	array[3][2] = 12
	if array[3][2] != 12 {
		fmt.Println("Array handling error:", array[3][2])
	}
	//fmt.Println("Start test running in: " + runtime.GOARCH)
	testManyGoroutines() // here to run alongside the other code execution
	tourfib()
	testCaseSensitivity()
	testInit()
	testConst()
	testUTF()
	testFloat()
	testMultiRet()
	testAppend()
	testStruct()
	testHeader()
	testCopy()
	testInFuncPtr()
	testCallBy()
	testMap()
	testNamed()
	testFuncPtr()
	testIntOverflow()
	testSlices()
	testChan()
	testComplex()
	testUTF8()
	testString()
	testClosure()
	testVariadic(42)
	testVariadic(40, 2)
	testVariadic(42, -5, 3, 2)
	testInterface()
	testInterfaceMethods()
	testStrconv()
	testTour64()
	testUintDiv32()
	testUintDiv64()
	testDefer()
	testPtr()
	testChanSelect()
	testEmbed()
	testUnsafe()
	testObjMap()
	testFloatConv()
	testUnaligned()
	//aGrWG.Wait()
	TEQint32(""+" testManyGoroutines() (NOT sync/atomic) counter:", aGrCtr, 0)
	if runtime.GOOS == "nacl" { // really a haxe emulation of nacl
		TEQ("", hx.CodeInt("", "42;"), int(42))
		TEQ("", hx.CodeString("", "'test';"), "test")
		TEQ(""+"Num Haxe GR post-wait", runtime.NumGoroutine(), 1)
		//panic("show GRs active")
	} else {
		//fmt.Println(runtime.NumGoroutine())
		TEQ(""+"Num Haxe GR post-wait", runtime.NumGoroutine(), 3)
	}
	TEQ("", unicode.IsSpace(' '), true) // makes the test longer but more complete
	//fmt.Println("End test running in: " + runtime.GOARCH)
	//fmt.Println("Σ sigma, " + "再见!Previous two chinese characters should say goodbye! (testing unicode output)")
	//fmt.Println()
	//hx.Call("", "Tgotypes.setup", 0)
	//hx.Call("", "Go_haxegoruntime_typetest.hx", 0)
	//testTypes()
	//fmt.Println(hx.GetFloat("", "Object.MinFloat64"))
}
Esempio n. 6
0
// Float32bits returns the IEEE 754 binary representation of f.
//func Float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }
func Float32bits(f float32) uint32 {
	return uint32(hx.CodeInt("",
		"Go_haxegoruntime_FFloat32bits.callFromRT(this._goroutine,_a.itemAddr(0).load().val);", f))
}
Esempio n. 7
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)
		*/
	}

}