From 8626e0482d19fad309f5ed9c4ce5fd3879b9e1a1 Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 14 Apr 2020 13:24:06 +0100 Subject: [PATCH] Nasal: add some type-check helpers. These avoid building a string for the result, as typeof() does --- simgear/nasal/lib.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/simgear/nasal/lib.c b/simgear/nasal/lib.c index 0322492f..5794516d 100644 --- a/simgear/nasal/lib.c +++ b/simgear/nasal/lib.c @@ -99,11 +99,33 @@ static naRef f_int(naContext c, naRef me, int argc, naRef* args) return naNil(); } +static naRef f_isint(naContext c, naRef me, int argc, naRef* args) +{ + if(argc > 0) { + naRef n = naNumValue(args[0]); + if(naIsNil(n)) return naNum(0); + if(n.num < 0) n.num = -n.num; + if(n.num == floor(n.num)) return naNum(1); + else return naNum(0); + } else ARGERR(); + return naNil(); +} + static naRef f_num(naContext c, naRef me, int argc, naRef* args) { return argc > 0 ? naNumValue(args[0]) : naNil(); } +static naRef f_isnum(naContext c, naRef me, int argc, naRef* args) +{ + if(argc > 0) { + naRef n = naNumValue(args[0]); + if(naIsNil(n)) return naNum(0); + else return naNum(1); + } else ARGERR(); + return naNil(); +} + static naRef f_streq(naContext c, naRef me, int argc, naRef* args) { return argc > 1 ? naNum(naStrEqual(args[0], args[1])) : naNil(); @@ -628,6 +650,8 @@ static naCFuncItem funcs[] = { { "bind", f_bind }, { "sort", f_sort }, { "id", f_id }, + { "isint", f_isint }, + { "isnum", f_isnum }, { 0 } };