Beispiel #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
}
Beispiel #2
0
// Store sets the value of the Value to x.
// All calls to Store for a given Value must use values of the same concrete type.
// Store of an inconsistent type panics, as does Store(nil).
func (v *Value) Store(x interface{}) {
	if x == nil {
		panic("sync/atomic: store of nil value into Value")
	}
	if v.v != nil && hx.CodeBool("", "_a.itemAddr(0).load().typ!=_a.itemAddr(1).load().typ;", v.v, x) {
		panic("sync/atomic: store of inconsistently typed value into Value")
	}
	v.v = x

	/*
		vp := (*ifaceWords)(unsafe.Pointer(v))
		xp := (*ifaceWords)(unsafe.Pointer(&x))
		for {
			typ := LoadPointer(&vp.typ)
			if typ == nil {
				// Attempt to start first store.
				// Disable preemption so that other goroutines can use
				// active spin wait to wait for completion; and so that
				// GC does not see the fake type accidentally.
				runtime_procPin()
				if !CompareAndSwapPointer(&vp.typ, nil, unsafe.Pointer(^uintptr(0))) {
					runtime_procUnpin()
					continue
				}
				// Complete first store.
				StorePointer(&vp.data, xp.data)
				StorePointer(&vp.typ, xp.typ)
				runtime_procUnpin()
				return
			}
			if uintptr(typ) == ^uintptr(0) {
				// First store in progress. Wait.
				// Since we disable preemption around the first store,
				// we can wait with active spinning.
				continue
			}
			// First store completed. Check type and overwrite data.
			if typ != xp.typ {
				panic("sync/atomic: store of inconsistently typed value into Value")
			}
			StorePointer(&vp.data, xp.data)
			return
		}
	*/
}
Beispiel #3
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)
		*/
	}

}