1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-27 00:40:26 +00:00

Allow loading current process on windows as well.

This commit is contained in:
Calvin Rose 2022-06-18 10:31:00 -05:00
parent d803561582
commit e37be627e0
3 changed files with 12 additions and 1 deletions

View File

@ -202,6 +202,7 @@ static const JanetAbstractType janet_struct_type = {
typedef struct { typedef struct {
Clib clib; Clib clib;
int closed; int closed;
int is_self;
} JanetAbstractNative; } JanetAbstractNative;
static const JanetAbstractType janet_native_type = { static const JanetAbstractType janet_native_type = {
@ -1115,6 +1116,7 @@ JANET_CORE_FN(janet_core_raw_native,
JanetAbstractNative *anative = janet_abstract(&janet_native_type, sizeof(JanetAbstractNative)); JanetAbstractNative *anative = janet_abstract(&janet_native_type, sizeof(JanetAbstractNative));
anative->clib = lib; anative->clib = lib;
anative->closed = 0; anative->closed = 0;
anative->is_self = path == NULL;
return janet_wrap_abstract(anative); return janet_wrap_abstract(anative);
} }
@ -1138,6 +1140,7 @@ JANET_CORE_FN(janet_core_native_close,
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
JanetAbstractNative *anative = janet_getabstract(argv, 0, &janet_native_type); JanetAbstractNative *anative = janet_getabstract(argv, 0, &janet_native_type);
if (anative->closed) janet_panic("native object already closed"); if (anative->closed) janet_panic("native object already closed");
if (anative->is_self) janet_panic("cannot close self");
anative->closed = 1; anative->closed = 1;
free_clib(anative->clib); free_clib(anative->clib);
return janet_wrap_nil(); return janet_wrap_nil();

View File

@ -912,6 +912,14 @@ char *error_clib(void) {
error_clib_buf[strlen(error_clib_buf) - 1] = '\0'; error_clib_buf[strlen(error_clib_buf) - 1] = '\0';
return error_clib_buf; return error_clib_buf;
} }
Clib load_clib(const char *name) {
if (name == NULL) {
return GetModuleHandle(NULL);
} else {
return LoadLibrary(name);
}
}
#endif #endif
/* Alloc function macro fills */ /* Alloc function macro fills */

View File

@ -144,9 +144,9 @@ typedef int Clib;
#elif defined(JANET_WINDOWS) #elif defined(JANET_WINDOWS)
#include <windows.h> #include <windows.h>
typedef HINSTANCE Clib; typedef HINSTANCE Clib;
#define load_clib(name) LoadLibrary((name))
#define free_clib(c) FreeLibrary((c)) #define free_clib(c) FreeLibrary((c))
#define symbol_clib(lib, sym) GetProcAddress((lib), (sym)) #define symbol_clib(lib, sym) GetProcAddress((lib), (sym))
Clib load_clib(const char *name);
char *error_clib(void); char *error_clib(void);
#else #else
#include <dlfcn.h> #include <dlfcn.h>