From 5c35d24e13a7dd5a82dbf74f4ca3e149e7651b9d Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 29 Oct 2021 19:28:02 -0500 Subject: [PATCH] Fix nil check issue. --- src/boot/boot.janet | 11 +---------- src/core/struct.c | 24 ++++++++---------------- src/core/table.c | 34 +++++++++------------------------- 3 files changed, 18 insertions(+), 51 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index b246f650..47584349 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -3689,15 +3689,6 @@ (do - (defn proto-flatten - "Flatten a table and its prototypes into a single table." - [into x] - (when x - (proto-flatten into (table/getproto x)) - (loop [k :keys x] - (put into k (x k)))) - into) - # Deprecate file/popen (when-let [v (get root-env 'file/popen)] (put v :deprecated true)) @@ -3706,7 +3697,7 @@ # flatten nested tables. (loop [[k v] :in (pairs root-env) :when (symbol? k)] - (def flat (proto-flatten @{} v)) + (def flat (table/proto-flatten @{} v)) (when (boot/config :no-docstrings) (put flat :doc nil)) (when (boot/config :no-sourcemaps) diff --git a/src/core/struct.c b/src/core/struct.c index ecc132f3..853d61ea 100644 --- a/src/core/struct.c +++ b/src/core/struct.c @@ -167,30 +167,22 @@ Janet janet_struct_rawget(const JanetKV *st, Janet key) { /* Get an item from a struct */ Janet janet_struct_get(const JanetKV *st, Janet key) { - int i = JANET_MAX_PROTO_DEPTH; - for (; st && i; --i) { + for (int i = JANET_MAX_PROTO_DEPTH; st && i; --i, st = janet_struct_proto(st)) { const JanetKV *kv = janet_struct_find(st, key); - if (NULL != kv) + if (NULL != kv && !janet_checktype(kv->key, JANET_NIL)) { return kv->value; - st = janet_struct_proto(st); + } } return janet_wrap_nil(); } /* Get an item from a struct, and record which prototype the item came from. */ Janet janet_struct_get_ex(const JanetKV *st, Janet key, JanetStruct *which) { - const JanetKV *kv = janet_struct_find(st, key); - if (NULL != kv) - return kv->value; - /* Check prototypes */ - { - int i = JANET_MAX_PROTO_DEPTH; - for (st = janet_struct_proto(st); st && i; st = janet_struct_proto(st), --i) { - kv = janet_struct_find(st, key); - if (NULL != kv) { - *which = kv; - return kv->value; - } + for (int i = JANET_MAX_PROTO_DEPTH; st && i; --i, st = janet_struct_proto(st)) { + const JanetKV *kv = janet_struct_find(st, key); + if (NULL != kv && !janet_checktype(kv->key, JANET_NIL)) { + *which = st; + return kv->value; } } return janet_wrap_nil(); diff --git a/src/core/table.c b/src/core/table.c index 78abc433..a2b895cb 100644 --- a/src/core/table.c +++ b/src/core/table.c @@ -132,37 +132,21 @@ static void janet_table_rehash(JanetTable *t, int32_t size) { /* Get a value out of the table */ Janet janet_table_get(JanetTable *t, Janet key) { - JanetKV *bucket = janet_table_find(t, key); - if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) - return bucket->value; - /* Check prototypes */ - { - int i; - for (i = JANET_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) { - bucket = janet_table_find(t, key); - if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) - return bucket->value; - } + for (int i = JANET_MAX_PROTO_DEPTH; t && i; t = t->proto, --i) { + JanetKV *bucket = janet_table_find(t, key); + if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) + return bucket->value; } return janet_wrap_nil(); } /* Get a value out of the table, and record which prototype it was from. */ Janet janet_table_get_ex(JanetTable *t, Janet key, JanetTable **which) { - JanetKV *bucket = janet_table_find(t, key); - if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) { - *which = t; - return bucket->value; - } - /* Check prototypes */ - { - int i; - for (i = JANET_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) { - bucket = janet_table_find(t, key); - if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) { - *which = t; - return bucket->value; - } + for (int i = JANET_MAX_PROTO_DEPTH; t && i; t = t->proto, --i) { + JanetKV *bucket = janet_table_find(t, key); + if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) { + *which = t; + return bucket->value; } } return janet_wrap_nil();