github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/misc/cgo/test/issue4029.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !windows,!static 6 7 package cgotest 8 9 /* 10 #include <stdint.h> 11 #include <dlfcn.h> 12 #cgo linux LDFLAGS: -ldl 13 14 extern uintptr_t dlopen4029(char*, int); 15 extern uintptr_t dlsym4029(uintptr_t, char*); 16 extern int dlclose4029(uintptr_t); 17 18 extern void call4029(uintptr_t arg); 19 */ 20 import "C" 21 22 import ( 23 "testing" 24 ) 25 26 var callbacks int 27 28 //export IMPIsOpaque 29 func IMPIsOpaque() { 30 callbacks++ 31 } 32 33 //export IMPInitWithFrame 34 func IMPInitWithFrame() { 35 callbacks++ 36 } 37 38 //export IMPDrawRect 39 func IMPDrawRect() { 40 callbacks++ 41 } 42 43 //export IMPWindowResize 44 func IMPWindowResize() { 45 callbacks++ 46 } 47 48 func test4029(t *testing.T) { 49 loadThySelf(t, "IMPWindowResize") 50 loadThySelf(t, "IMPDrawRect") 51 loadThySelf(t, "IMPInitWithFrame") 52 loadThySelf(t, "IMPIsOpaque") 53 if callbacks != 4 { 54 t.Errorf("got %d callbacks, expected 4", callbacks) 55 } 56 } 57 58 func loadThySelf(t *testing.T, symbol string) { 59 this_process := C.dlopen4029(nil, C.RTLD_NOW) 60 if this_process == 0 { 61 t.Error("dlopen:", C.GoString(C.dlerror())) 62 return 63 } 64 defer C.dlclose4029(this_process) 65 66 symbol_address := C.dlsym4029(this_process, C.CString(symbol)) 67 if symbol_address == 0 { 68 t.Error("dlsym:", C.GoString(C.dlerror())) 69 return 70 } 71 t.Log(symbol, symbol_address) 72 C.call4029(symbol_address) 73 }