mirror of
https://github.com/janet-lang/janet
synced 2025-03-13 11:58:09 +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:
parent
61c8c1e8d2
commit
06f613e40b
@ -201,18 +201,17 @@ static int lookup_missing(
|
|||||||
JanetCompiler *c,
|
JanetCompiler *c,
|
||||||
const uint8_t *sym,
|
const uint8_t *sym,
|
||||||
JanetFunction *handler,
|
JanetFunction *handler,
|
||||||
Janet *out) {
|
JanetBinding *out) {
|
||||||
Janet args[2] = { janet_wrap_symbol(sym), janet_wrap_table(c->env) };
|
int32_t minar = handler->def->min_arity;
|
||||||
JanetFiber *fiberp = janet_fiber(handler, 64, 2, args);
|
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) {
|
if (NULL == fiberp) {
|
||||||
int32_t minar = handler->def->min_arity;
|
janetc_error(c, janet_cstring("failed to call missing symbol lookup handler"));
|
||||||
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);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
fiberp->env = c->env;
|
fiberp->env = c->env;
|
||||||
@ -223,10 +222,11 @@ static int lookup_missing(
|
|||||||
if (status != JANET_SIGNAL_OK) {
|
if (status != JANET_SIGNAL_OK) {
|
||||||
janetc_error(c, janet_formatc("(lookup) %V", tempOut));
|
janetc_error(c, janet_formatc("(lookup) %V", tempOut));
|
||||||
return 0;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,14 +265,12 @@ JanetSlot janetc_resolve(
|
|||||||
JanetBinding binding = janet_resolve_ext(c->env, sym);
|
JanetBinding binding = janet_resolve_ext(c->env, sym);
|
||||||
if (binding.type == JANET_BINDING_NONE) {
|
if (binding.type == JANET_BINDING_NONE) {
|
||||||
Janet handler = janet_table_get(c->env, janet_ckeywordv("missing-symbol"));
|
Janet handler = janet_table_get(c->env, janet_ckeywordv("missing-symbol"));
|
||||||
Janet entry;
|
|
||||||
switch (janet_type(handler)) {
|
switch (janet_type(handler)) {
|
||||||
case JANET_NIL:
|
case JANET_NIL:
|
||||||
break;
|
break;
|
||||||
case JANET_FUNCTION:
|
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());
|
return janetc_cslot(janet_wrap_nil());
|
||||||
binding = janet_binding_from_entry(entry);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
janetc_error(c, janet_formatc("invalid lookup handler %V", handler));
|
janetc_error(c, janet_formatc("invalid lookup handler %V", handler));
|
||||||
|
@ -28,14 +28,14 @@
|
|||||||
|
|
||||||
# missing symbols
|
# missing symbols
|
||||||
|
|
||||||
(def replacement 10)
|
(defn lookup-symbol [sym] (defglobal sym 10) (dyn sym))
|
||||||
(defn lookup-symbol [sym env] (dyn 'replacement))
|
|
||||||
|
|
||||||
(setdyn :missing-symbol lookup-symbol)
|
(setdyn :missing-symbol lookup-symbol)
|
||||||
|
|
||||||
(assert (= (eval-string "(+ a 5)") 15) "lookup missing symbol")
|
(assert (= (eval-string "(+ a 5)") 15) "lookup missing symbol")
|
||||||
|
|
||||||
(setdyn :missing-symbol nil)
|
(setdyn :missing-symbol nil)
|
||||||
|
(setdyn 'a nil)
|
||||||
|
|
||||||
(assert-error "compile error" (eval-string "(+ a 5)"))
|
(assert-error "compile error" (eval-string "(+ a 5)"))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user