From 06f613e40bd59e62e235ff049b71f5f1f154400b Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 27 Jan 2022 21:24:01 -0600 Subject: [PATCH] Update signature of :missing-symbol hook. don't take env table as explicit argument - it is already available as the env table of the fiber. --- src/core/compile.c | 30 ++++++++++++++---------------- test/suite0011.janet | 4 ++-- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/core/compile.c b/src/core/compile.c index b80947cd..e64a3615 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -201,18 +201,17 @@ static int lookup_missing( JanetCompiler *c, const uint8_t *sym, JanetFunction *handler, - Janet *out) { - Janet args[2] = { janet_wrap_symbol(sym), janet_wrap_table(c->env) }; - JanetFiber *fiberp = janet_fiber(handler, 64, 2, args); + JanetBinding *out) { + int32_t minar = handler->def->min_arity; + int32_t maxar = handler->def->max_arity; + if (minar > 1 || maxar < 1) { + janetc_error(c, janet_cstring("missing symbol lookup handler must take 1 argument")); + return 0; + } + Janet args[1] = { janet_wrap_symbol(sym) }; + JanetFiber *fiberp = janet_fiber(handler, 64, 1, args); if (NULL == fiberp) { - int32_t minar = handler->def->min_arity; - int32_t maxar = handler->def->max_arity; - const uint8_t *es = NULL; - if (minar > 2) - es = janet_formatc("lookup handler arity mismatch, minimum at most 2, got %d", minar); - if (maxar < 2) - es = janet_formatc("lookup handler arity mismatch, maximum at least 2, got %d", maxar); - janetc_error(c, es); + janetc_error(c, janet_cstring("failed to call missing symbol lookup handler")); return 0; } fiberp->env = c->env; @@ -223,10 +222,11 @@ static int lookup_missing( if (status != JANET_SIGNAL_OK) { janetc_error(c, janet_formatc("(lookup) %V", tempOut)); return 0; - } else { - *out = tempOut; } + /* Convert return value as entry. */ + /* Alternative could use janet_resolve_ext(c->env, sym) to read result from environment. */ + *out = janet_binding_from_entry(tempOut); return 1; } @@ -265,14 +265,12 @@ JanetSlot janetc_resolve( JanetBinding binding = janet_resolve_ext(c->env, sym); if (binding.type == JANET_BINDING_NONE) { Janet handler = janet_table_get(c->env, janet_ckeywordv("missing-symbol")); - Janet entry; switch (janet_type(handler)) { case JANET_NIL: break; case JANET_FUNCTION: - if (!lookup_missing(c, sym, janet_unwrap_function(handler), &entry)) + if (!lookup_missing(c, sym, janet_unwrap_function(handler), &binding)) return janetc_cslot(janet_wrap_nil()); - binding = janet_binding_from_entry(entry); break; default: janetc_error(c, janet_formatc("invalid lookup handler %V", handler)); diff --git a/test/suite0011.janet b/test/suite0011.janet index a67ac787..31ad99d8 100644 --- a/test/suite0011.janet +++ b/test/suite0011.janet @@ -28,14 +28,14 @@ # missing symbols -(def replacement 10) -(defn lookup-symbol [sym env] (dyn 'replacement)) +(defn lookup-symbol [sym] (defglobal sym 10) (dyn sym)) (setdyn :missing-symbol lookup-symbol) (assert (= (eval-string "(+ a 5)") 15) "lookup missing symbol") (setdyn :missing-symbol nil) +(setdyn 'a nil) (assert-error "compile error" (eval-string "(+ a 5)"))