Fix nil check issue.

This commit is contained in:
Calvin Rose 2021-10-29 19:28:02 -05:00
parent 03f99752a7
commit 5c35d24e13
3 changed files with 18 additions and 51 deletions

View File

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

View File

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

View File

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