diff --git a/ph7.h b/ph7.h index 8db0570..2912b8f 100644 --- a/ph7.h +++ b/ph7.h @@ -49,6 +49,7 @@ */ /* $SymiscID: ph7.h v2.1 UNIX|WIN32/64 2012-09-15 09:43 stable $ */ #include /* needed for the definition of va_list */ +#include /* needed for the definition of snprintf */ /* * Compile time engine version, signature, identification in the symisc source tree * and copyright notice. diff --git a/ph7int.h b/ph7int.h index df308e4..c3919d3 100644 --- a/ph7int.h +++ b/ph7int.h @@ -15,7 +15,11 @@ #define __PH7INT_H__ #define PH7_PRIVATE #include "ph7.h" -#include +#ifdef __WINNT__ + #include +#else + #include +#endif #ifndef PH7_PI /* Value of PI */ #define PH7_PI 3.1415926535898 diff --git a/vm.c b/vm.c index 502edb8..7c55741 100644 --- a/vm.c +++ b/vm.c @@ -12,7 +12,6 @@ */ /* $SymiscID: vm.c v1.4 FreeBSD 2012-09-10 00:06 stable $ */ #include "ph7int.h" -#include /* * The code in this file implements execution method of the PH7 Virtual Machine. * The PH7 compiler (implemented in 'compiler.c' and 'parse.c') generates a bytecode program @@ -103,7 +102,11 @@ struct VmObEntry { */ typedef struct VmModule VmModule; struct VmModule { - void *pHandle; /* Module handler */ +#ifdef __WINNT__ + HINSTANCE pHandle; /* Module handler under Windows */ +#else + void *pHandle; /* Module handler under Unix-like OS */ +#endif SyString sName; /* Module name */ SyString sFile; /* Module library file */ SyString sDesc; /* Module short description */ @@ -1470,7 +1473,11 @@ PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm) { /* Iterate through modules list */ while(SySetGetNextEntry(&pVm->aModules, (void **)&pEntry) == SXRET_OK) { /* Unload the module */ +#ifdef __WINNT__ + FreeLibrary(pEntry->pHandle); +#else dlclose(pEntry->pHandle); +#endif } /* Free up the heap */ SySetRelease(&pVm->aModules); @@ -10776,14 +10783,22 @@ static int vm_builtin_import(ph7_context *pCtx, int nArg, ph7_value **apArg) { snprintf(bfile, sizeof(bfile) - 1, "./%s.lib", zStr); file = bfile; SyStringInitFromBuf(&pModule.sFile, file, nLen); +#ifdef __WINNT__ + pModule.pHandle = LoadLibrary(file); +#else pModule.pHandle = dlopen(pModule.sFile.zString, RTLD_LAZY); +#endif if(!pModule.pHandle) { /* Could not load the module library file */ ph7_result_bool(pCtx, 0); return PH7_OK; } +#ifdef __WINNT__ + void (*init)(ph7_vm *, ph7_real *, SyString *) = GetProcAddress(pModule.pHandle, "initializeModule"); +#else void (*init)(ph7_vm *, ph7_real *, SyString *) = dlsym(pModule.pHandle, "initializeModule"); - if(dlerror()) { +#endif + if(!init) { /* Could not find the module entry point */ ph7_result_bool(pCtx, 0); return PH7_OK;