1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-25 19:54:48 +00:00

Abstract type getters can indicate key absence.

This change to the c api allows abstract types to indicate
to the runtime if a key was absent, or if it meant to return nil.
This commit is contained in:
Andrew Chambers
2019-12-09 16:50:33 +13:00
parent 4c5734c2ee
commit 57ccfb692c
8 changed files with 54 additions and 43 deletions

View File

@@ -29,7 +29,7 @@
static JANET_THREAD_LOCAL JanetRNG janet_vm_rng = {0, 0, 0, 0, 0};
static Janet janet_rng_get(void *p, Janet key);
static int janet_rng_get(void *p, Janet key, Janet *out);
static void janet_rng_marshal(void *p, JanetMarshalContext *ctx) {
JanetRNG *rng = (JanetRNG *)p;
@@ -196,10 +196,11 @@ static const JanetMethod rng_methods[] = {
{NULL, NULL}
};
static Janet janet_rng_get(void *p, Janet key) {
static int janet_rng_get(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD)) janet_panicf("expected keyword method");
return janet_getmethod(janet_unwrap_keyword(key), rng_methods);
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), rng_methods);
return !janet_checktype(*out, JANET_NIL);
}
/* Get a random number */