1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-13 17:06:49 +00:00

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.
This commit is contained in:
Calvin Rose 2022-01-27 21:24:01 -06:00
parent 61c8c1e8d2
commit 06f613e40b
2 changed files with 16 additions and 18 deletions

View File

@ -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));

View File

@ -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)"))