func getUrl(url string, urc chan urlReply) urlReply { httpHandler := hx.New("", "haxe.Http", 1, url) hx.Code("", "_a.param(0).val.onData=_a.param(1).val;", httpHandler, hx.CallbackFunc( func(data string) { urc <- urlReply{err: nil, dat: data} })) hx.Code("", "_a.param(0).val.onError=_a.param(1).val;", httpHandler, hx.CallbackFunc( func(msg string) { urc <- urlReply{err: errors.New(msg), dat: ""} })) hx.Meth("", httpHandler, "haxe.Http", "request", 0) return <-urc }
func Exit(code int) (err error) { //_, _, e1 := Syscall(sys_exit, uintptr(code), 0, 0) //if e1 != 0 { // err = e1 //} hx.Call("(cpp || cs || java || macro || neko || php || python)", "Sys.exit", 1, code) if code == 0 { hx.Code("js", "untyped __js__('process.exit(0)');") // only works on Node } else { // all non-zero values return as 1 hx.Code("js", "untyped __js__('process.exit(1)');") // only works on Node } panic("syscall.Exit(" + hx.CallString("", "Std.string", 1, code) + ")") return }
func badExit() { if runtime.GOARCH == "" { // running the interpreter os.Exit(1) } hx.Call("(cpp || cs || java || macro || neko || php || python)", "Sys.exit", 1, 1) hx.Code("js", "untyped __js__('process.exit(1)');") // only works on Node }
// An internal function but exported because it is cross-package; part of the implementation // of the "go test" command. func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) { //os.Exit(MainStart(matchString, tests, benchmarks, examples).Run()) fmt.Println("testing.Main") if runtime.GOARCH != "" { // not running in the interpreter runtime.UnzipTestFS() } var t T names := []string{} for _, f := range tests { names = append(names, f.Name) } sort.Strings(names) for _, n := range names { for _, f := range tests { if n == f.Name { println("\nExecuting test:", n) f.F(&t) } } } if hadError { badExit() } if runtime.GOARCH == "" { // running the interpreter os.Exit(0) } hx.Call("(cpp || cs || java || macro || neko || php || python)", "Sys.exit", 1, 0) hx.Code("js", "untyped __js__('process.exit(0)');") // only works on Node }
// 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() }
// Main allows code that might be targeted to broser-based JS code to schedule TARDISgo code periodically, separate from call-backs. // For non-JS targets it simply calls the given mainFN. // This should be the last function called in main.main() func BrowserMain(mainFN func(), msInvocationInterval, runLimit int) { if runtime.GOARCH == "js" { // probably only want to do this in a browser JScallbackOK = true ht := hx.New("js", "haxe.Timer", 1, msInvocationInterval) go func() { // put the main program into a goroutine mainFN() hx.Meth("js", ht, "haxe.Timer", "stop", 0) }() hx.SetInt("js", "Scheduler.runLimit", runLimit) hx.Code("js", "_a.param(0).val.run=Scheduler.timerEventHandler;", ht) } else { mainFN() } }
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 }
func init() { hx.Code("cpp", "cpp.vm.Gc.enable(true);") // to allow it to be set to false for testing }
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) */ } }