From a3aaa6634dcc49081354f867cc87d0ea1f6e89de Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 14 Mar 2020 10:25:39 -0500 Subject: [PATCH] Use separate registry table for abstract types. This avoids overloading the registry table, which is intended for names of c functions. --- src/core/state.h | 4 ++++ src/core/util.c | 29 ++++++----------------------- src/core/vm.c | 4 ++++ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/core/state.h b/src/core/state.h index 8674b07a..0ed8b31d 100644 --- a/src/core/state.h +++ b/src/core/state.h @@ -53,6 +53,10 @@ extern JANET_THREAD_LOCAL Janet *janet_vm_return_reg; * along with otherwise bare c function pointers. */ extern JANET_THREAD_LOCAL JanetTable *janet_vm_registry; +/* Registry for abstract abstract types that can be marshalled. + * We need this to look up the constructors when unmarshalling. */ +extern JANET_THREAD_LOCAL JanetTable *janet_vm_abstract_registry; + /* Immutable value cache */ extern JANET_THREAD_LOCAL const uint8_t **janet_vm_cache; extern JANET_THREAD_LOCAL uint32_t janet_vm_cache_capacity; diff --git a/src/core/util.c b/src/core/util.c index 57681054..f2df7421 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -421,40 +421,23 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) free(longname_buffer); } -/* Abstract type introspection - not meant to be used directly */ - -static const JanetAbstractType janet_abstract_info_type = { - "core/abstract-info", - JANET_ATEND_NAME -}; - -typedef struct { - const JanetAbstractType *at; -} JanetAbstractTypeWrap; +/* Abstract type introspection */ void janet_register_abstract_type(const JanetAbstractType *at) { - JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *) - janet_abstract(&janet_abstract_info_type, sizeof(JanetAbstractTypeWrap)); - abstract->at = at; Janet sym = janet_csymbolv(at->name); - if (!(janet_checktype(janet_table_get(janet_vm_registry, sym), JANET_NIL))) { + if (!(janet_checktype(janet_table_get(janet_vm_abstract_registry, sym), JANET_NIL))) { janet_panicf("cannot register abstract type %s, " "a type with the same name exists", at->name); } - janet_table_put(janet_vm_registry, sym, janet_wrap_abstract(abstract)); + janet_table_put(janet_vm_abstract_registry, sym, janet_wrap_pointer((void *) at)); } const JanetAbstractType *janet_get_abstract_type(Janet key) { - Janet twrap = janet_table_get(janet_vm_registry, key); - if (janet_checktype(twrap, JANET_NIL)) { + Janet wrapped = janet_table_get(janet_vm_abstract_registry, key); + if (janet_checktype(wrapped, JANET_NIL)) { return NULL; } - if (!janet_checktype(twrap, JANET_ABSTRACT) || - (janet_abstract_type(janet_unwrap_abstract(twrap)) != &janet_abstract_info_type)) { - janet_panic("expected abstract type"); - } - JanetAbstractTypeWrap *w = (JanetAbstractTypeWrap *)janet_unwrap_abstract(twrap); - return w->at; + return (JanetAbstractType *)(janet_unwrap_pointer(wrapped)); } #ifndef JANET_BOOTSTRAP diff --git a/src/core/vm.c b/src/core/vm.c index ead97d68..e96507b3 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -35,6 +35,7 @@ /* VM state */ JANET_THREAD_LOCAL JanetTable *janet_vm_core_env; JANET_THREAD_LOCAL JanetTable *janet_vm_registry; +JANET_THREAD_LOCAL JanetTable *janet_vm_abstract_registry; JANET_THREAD_LOCAL int janet_vm_stackn = 0; JANET_THREAD_LOCAL JanetFiber *janet_vm_fiber = NULL; JANET_THREAD_LOCAL Janet *janet_vm_return_reg = NULL; @@ -1380,7 +1381,9 @@ int janet_init(void) { janet_scratch_cap = 0; /* Initialize registry */ janet_vm_registry = janet_table(0); + janet_vm_abstract_registry = janet_table(0); janet_gcroot(janet_wrap_table(janet_vm_registry)); + janet_gcroot(janet_wrap_table(janet_vm_abstract_registry)); /* Core env */ janet_vm_core_env = NULL; /* Seed RNG */ @@ -1401,6 +1404,7 @@ void janet_deinit(void) { janet_vm_root_count = 0; janet_vm_root_capacity = 0; janet_vm_registry = NULL; + janet_vm_abstract_registry = NULL; janet_vm_core_env = NULL; #ifdef JANET_THREADS janet_threads_deinit();