From ddb5e52885e353a2a511635fa853631b1b97039c Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 9 Mar 2022 10:36:58 +0000 Subject: [PATCH] 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. --- simgear/nasal/code.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/simgear/nasal/code.c b/simgear/nasal/code.c index 2f920ab0..b54ab70e 100644 --- a/simgear/nasal/code.c +++ b/simgear/nasal/code.c @@ -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;