Nasal: extend boolify() to work on containers/objects

Consider non-empty containers and non-nil objects (eg, ghosts) to be
true-like, in boolify(). This makes for more natural testing of return
values.

Since these types previously caused a runtime error, should be backwards
compatible, unless code was relying on the error. This seems improbable,
if it happens in practice, we can revert and find a different solution.
This commit is contained in:
James Turner 2022-03-09 10:36:58 +00:00
parent 47a103f7ea
commit ddb5e52885

View File

@ -61,6 +61,18 @@ static int boolify(naContext ctx, naRef r)
{
if(IS_NUM(r)) return r.num != 0;
if(IS_NIL(r) || IS_END(r)) return 0;
// empty vectors are false, non-empty vectors are true
if (IS_VEC(r)) {
return naVec_size(r) > 0;
}
// empty hashes are false, non-empty are true
if (IS_HASH(r)) {
return naHash_size(r) > 0;
}
if (IS_OBJ(r)) return 1;
if(IS_STR(r)) {
double d;
if(naStr_len(r) == 0) return 0;