Nasal: add some type-check helpers.
These avoid building a string for the result, as typeof() does
This commit is contained in:
@@ -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 }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user