diff --git a/src/core/ffi.c b/src/core/ffi.c index 04243267..8b5481cb 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -202,6 +202,7 @@ static const JanetAbstractType janet_struct_type = { typedef struct { Clib clib; int closed; + int is_self; } JanetAbstractNative; 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)); anative->clib = lib; anative->closed = 0; + anative->is_self = path == NULL; return janet_wrap_abstract(anative); } @@ -1138,6 +1140,7 @@ JANET_CORE_FN(janet_core_native_close, janet_fixarity(argc, 1); JanetAbstractNative *anative = janet_getabstract(argv, 0, &janet_native_type); if (anative->closed) janet_panic("native object already closed"); + if (anative->is_self) janet_panic("cannot close self"); anative->closed = 1; free_clib(anative->clib); return janet_wrap_nil(); diff --git a/src/core/util.c b/src/core/util.c index 7568299f..8670797b 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -912,6 +912,14 @@ char *error_clib(void) { error_clib_buf[strlen(error_clib_buf) - 1] = '\0'; return error_clib_buf; } + +Clib load_clib(const char *name) { + if (name == NULL) { + return GetModuleHandle(NULL); + } else { + return LoadLibrary(name); + } +} #endif /* Alloc function macro fills */ diff --git a/src/core/util.h b/src/core/util.h index f591b0a2..3031cc2d 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -144,9 +144,9 @@ typedef int Clib; #elif defined(JANET_WINDOWS) #include typedef HINSTANCE Clib; -#define load_clib(name) LoadLibrary((name)) #define free_clib(c) FreeLibrary((c)) #define symbol_clib(lib, sym) GetProcAddress((lib), (sym)) +Clib load_clib(const char *name); char *error_clib(void); #else #include