mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 08:20:27 +00:00
Prevent double usage of native objects after closing.
This commit is contained in:
parent
986e36720e
commit
e3e485285b
@ -92,6 +92,7 @@ static char *get_processed_name(const char *name) {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Clib clib;
|
Clib clib;
|
||||||
|
int closed;
|
||||||
} JanetAbstractNative;
|
} JanetAbstractNative;
|
||||||
|
|
||||||
static const JanetAbstractType janet_native_type = {
|
static const JanetAbstractType janet_native_type = {
|
||||||
@ -362,6 +363,7 @@ JANET_CORE_FN(janet_core_raw_native,
|
|||||||
if (!lib) janet_panic(error_clib());
|
if (!lib) janet_panic(error_clib());
|
||||||
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;
|
||||||
return janet_wrap_abstract(anative);
|
return janet_wrap_abstract(anative);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,6 +374,7 @@ JANET_CORE_FN(janet_core_native_lookup,
|
|||||||
janet_fixarity(argc, 2);
|
janet_fixarity(argc, 2);
|
||||||
JanetAbstractNative *anative = janet_getabstract(argv, 0, &janet_native_type);
|
JanetAbstractNative *anative = janet_getabstract(argv, 0, &janet_native_type);
|
||||||
const char *sym = janet_getcstring(argv, 1);
|
const char *sym = janet_getcstring(argv, 1);
|
||||||
|
if (anative->closed) janet_panic("native object already closed");
|
||||||
void *value = symbol_clib(anative->clib, sym);
|
void *value = symbol_clib(anative->clib, sym);
|
||||||
if (NULL == value) return janet_wrap_nil();
|
if (NULL == value) return janet_wrap_nil();
|
||||||
return janet_wrap_pointer(value);
|
return janet_wrap_pointer(value);
|
||||||
@ -383,7 +386,10 @@ JANET_CORE_FN(janet_core_native_close,
|
|||||||
"behavior after freeing.") {
|
"behavior after freeing.") {
|
||||||
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");
|
||||||
|
anative->closed = 1;
|
||||||
free_clib(anative->clib);
|
free_clib(anative->clib);
|
||||||
|
anative->clib = NULL;
|
||||||
return janet_wrap_nil();
|
return janet_wrap_nil();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user