Skip to content

Commit

Permalink
[Go] Properly set _cgo_* functions on Windows
Browse files Browse the repository at this point in the history
Based on the work of Wei Guangjing[1].
On Windows, dynamic binding of methods is not possible. So we use DllMain to do
that.
[1] https://groups.google.com/forum/#!topic/golang-nuts/9L0U4Q7AtyE
[2] swig#100

Closes swig#100

Signed-off-by: Steeve Morin <steeve.morin@gmail.com>
  • Loading branch information
steeve committed Nov 5, 2014
1 parent 45b9070 commit 21cf52e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Lib/go/goruntime.swg
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,39 @@ swiggo_size_assert(double, 8)
#ifdef __cplusplus
extern "C" {
#endif
#ifdef defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
void (*crosscall2)(void (*fn)(void *, int), void *, int);
char* (*_cgo_topofstack)(void) __attribute__ ((weak));
void (*_cgo_allocate)(void *, int);
void (*_cgo_panic)(void *, int);
#else // defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
extern void crosscall2(void (*fn)(void *, int), void *, int);
extern char* _cgo_topofstack(void) __attribute__ ((weak));
extern void _cgo_allocate(void *, int);
extern void _cgo_panic(void *, int);
#endif // defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#ifdef __cplusplus
}
#endif

#ifdef defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
extern "C" BOOL WINAPI DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
HMODULE hModule;
hModule = GetModuleHandle(NULL);
crosscall2 = (void (*)(void (*fn)(void *, int), void *, int))GetProcAddress(hModule, "crosscall2");
_cgo_topofstack = (char* (*)(void))GetProcAddress(hModule, "_cgo_topofstack");
_cgo_allocate = (void (*)(void *, int))GetProcAddress(hModule, "_cgo_allocate");
_cgo_panic = (void (*)(void *, int))GetProcAddress(hModule, "_cgo_panic");
break;
}
return TRUE;
}
#endif

static char *_swig_topofstack() {
if (_cgo_topofstack) {
return _cgo_topofstack();
Expand Down

0 comments on commit 21cf52e

Please sign in to comment.