# include "config.h"
#endif
+#include <cstring>
+
#include <simgear/nasal/nasal.h>
#include <simgear/props/props.hxx>
+#include <simgear/props/vectorPropTemplates.hxx>
#include <Main/globals.hxx>
#include "NasalSys.hxx"
+using namespace std;
+
// Implementation of a Nasal wrapper for the SGPropertyNode class,
// using the Nasal "ghost" (er... Garbage collection Handle for
// OutSide Thingy) facility.
return propNodeGhostCreate(_context, handle);
}
+SGPropertyNode* ghostToPropNode(naRef ref)
+{
+ if (!naIsGhost(ref) || (naGhost_type(ref) != &PropNodeGhostType))
+ return NULL;
+
+ SGPropertyNode_ptr* pp = (SGPropertyNode_ptr*) naGhost_ptr(ref);
+ return pp->ptr();
+}
+
#define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
//
// array. This allows the Nasal handlers to do things like:
// Node.getChild = func { _getChild(me.ghost, arg) }
//
-#define NODEARG() \
+#define NODENOARG() \
if(argc < 2 || !naIsGhost(args[0]) || \
naGhost_type(args[0]) != &PropNodeGhostType) \
naRuntimeError(c, "bad argument to props function"); \
- SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]); \
+ SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(args[0]);
+
+#define NODEARG() \
+ NODENOARG(); \
naRef argv = args[1]
static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
{
- using namespace simgear::props;
- NODEARG();
+ using namespace simgear;
+ NODENOARG();
const char* t = "unknown";
switch((*node)->getType()) {
- case NONE: t = "NONE"; break;
- case ALIAS: t = "ALIAS"; break;
- case BOOL: t = "BOOL"; break;
- case INT: t = "INT"; break;
- case LONG: t = "LONG"; break;
- case FLOAT: t = "FLOAT"; break;
- case DOUBLE: t = "DOUBLE"; break;
- case STRING: t = "STRING"; break;
- case UNSPECIFIED: t = "UNSPECIFIED"; break;
- case VEC3D: t = "VEC3D"; break;
- case VEC4D: t = "VEC4D"; break;
+ case props::NONE: t = "NONE"; break;
+ case props::ALIAS: t = "ALIAS"; break;
+ case props::BOOL: t = "BOOL"; break;
+ case props::INT: t = "INT"; break;
+ case props::LONG: t = "LONG"; break;
+ case props::FLOAT: t = "FLOAT"; break;
+ case props::DOUBLE: t = "DOUBLE"; break;
+ case props::STRING: t = "STRING"; break;
+ case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
+ case props::VEC3D: t = "VEC3D"; break;
+ case props::VEC4D: t = "VEC4D"; break;
+ case props::EXTENDED: t = "EXTENDED"; break; // shouldn't happen
}
return NASTR(t);
}
else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
+ else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
else {
naRuntimeError(c, "props.getAttribute() with invalid attribute");
return naNil();
else if(!strcmp(a, "trace-read")) attr = SGPropertyNode::TRACE_READ;
else if(!strcmp(a, "trace-write")) attr = SGPropertyNode::TRACE_WRITE;
else if(!strcmp(a, "userarchive")) attr = SGPropertyNode::USERARCHIVE;
+ else if(!strcmp(a, "preserve")) attr = SGPropertyNode::PRESERVE;
else {
naRuntimeError(c, "props.setAttribute() with invalid attribute");
return naNil();
static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
{
- NODEARG();
+ NODENOARG();
return NASTR((*node)->getName());
}
static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
{
- NODEARG();
+ NODENOARG();
return naNum((*node)->getIndex());
}
template<typename T>
naRef makeVectorFromVec(naContext c, const T& vec)
{
- const unsigned num_components
+ const int num_components
= sizeof(vec.data()) / sizeof(typename T::value_type);
naRef vector = naNewVector(c);
- naVec_setsize(vector, num_components);
+ naVec_setsize(c, vector, num_components);
for (int i = 0; i < num_components; ++i)
naVec_set(vector, i, naNum(vec[i]));
return vector;
static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
{
- using namespace simgear::props;
- NODEARG();
+ using namespace simgear;
+ NODENOARG();
switch((*node)->getType()) {
- case BOOL: case INT:
- case LONG: case FLOAT:
- case DOUBLE:
- return naNum((*node)->getDoubleValue());
- case STRING:
- case UNSPECIFIED:
+ case props::BOOL: case props::INT:
+ case props::LONG: case props::FLOAT:
+ case props::DOUBLE:
+ {
+ double dv = (*node)->getDoubleValue();
+ if (SGMisc<double>::isNaN(dv)) {
+ SG_LOG(SG_NASAL, SG_ALERT, "Nasal getValue: property " << (*node)->getPath() << " is NaN");
+ return naNil();
+ }
+
+ return naNum(dv);
+ }
+
+ case props::STRING:
+ case props::UNSPECIFIED:
return NASTR((*node)->getStringValue());
- case VEC3D:
+ case props::VEC3D:
return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
- case VEC4D:
+ case props::VEC4D:
return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
default:
return naNil();
T makeVecFromVector(naRef vector)
{
T vec;
- const unsigned num_components
+ const int num_components
= sizeof(vec.data()) / sizeof(typename T::value_type);
int size = naVec_size(vector);
naRef n = naNumValue(val);
if(naIsNil(n))
naRuntimeError(c, "props.setValue() with non-number");
- result = (*node)->setDoubleValue(naNumValue(val).num);
+
+ double d = naNumValue(val).num;
+ if (SGMisc<double>::isNaN(d)) {
+ naRuntimeError(c, "props.setValue() passed a NaN");
+ }
+
+ result = (*node)->setDoubleValue(d);
}
return naNum(result);
}
{
NODEARG();
naRef r = naNumValue(naVec_get(argv, 0));
- if(naIsNil(r))
+ if (naIsNil(r))
naRuntimeError(c, "props.setDoubleValue() with non-number");
+
+ if (SGMisc<double>::isNaN(r.num)) {
+ naRuntimeError(c, "props.setDoubleValue() passed a NaN");
+ }
+
return naNum((*node)->setDoubleValue(r.num));
}
static naRef f_getParent(naContext c, naRef me, int argc, naRef* args)
{
- NODEARG();
+ NODENOARG();
SGPropertyNode* n = (*node)->getParent();
if(!n) return naNil();
return propNodeGhostCreate(c, n);
naRef child = naVec_get(argv, 0);
if(!naIsString(child)) return naNil();
naRef idx = naNumValue(naVec_get(argv, 1));
- bool create = naTrue(naVec_get(argv, 2));
+ bool create = naTrue(naVec_get(argv, 2)) != 0;
SGPropertyNode* n;
try {
if(naIsNil(idx) || !naIsNum(idx)) {
return result;
}
+static naRef f_addChild(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ naRef child = naVec_get(argv, 0);
+ if(!naIsString(child)) return naNil();
+ naRef ref_min_index = naNumValue(naVec_get(argv, 1));
+ naRef ref_append = naVec_get(argv, 2);
+ SGPropertyNode* n;
+ try
+ {
+ int min_index = 0;
+ if( !naIsNil(ref_min_index) && naIsNum(ref_min_index) )
+ min_index = ref_min_index.num;
+
+ bool append = true;
+ if( !naIsNil(ref_append) )
+ append = naTrue(ref_append) != 0;
+
+ n = (*node)->addChild(naStr_data(child), min_index, append);
+ }
+ catch (const string& err)
+ {
+ naRuntimeError(c, (char *)err.c_str());
+ return naNil();
+ }
+
+ return propNodeGhostCreate(c, n);
+}
+
+static naRef f_addChildren(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ naRef child = naVec_get(argv, 0);
+ if(!naIsString(child)) return naNil();
+ naRef ref_count = naNumValue(naVec_get(argv, 1));
+ naRef ref_min_index = naNumValue(naVec_get(argv, 2));
+ naRef ref_append = naVec_get(argv, 3);
+ try
+ {
+ size_t count = 0;
+ if( !naIsNum(ref_count) )
+ throw string("props.addChildren() missing number of children");
+ count = ref_count.num;
+
+ int min_index = 0;
+ if( !naIsNil(ref_min_index) && naIsNum(ref_min_index) )
+ min_index = ref_min_index.num;
+
+ bool append = true;
+ if( !naIsNil(ref_append) )
+ append = naTrue(ref_append) != 0;
+
+ const simgear::PropertyList& nodes =
+ (*node)->addChildren(naStr_data(child), count, min_index, append);
+
+ naRef result = naNewVector(c);
+ for( size_t i = 0; i < nodes.size(); ++i )
+ naVec_append(result, propNodeGhostCreate(c, nodes[i]));
+ return result;
+ }
+ catch (const string& err)
+ {
+ naRuntimeError(c, (char *)err.c_str());
+ }
+
+ return naNil();
+}
+
static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
{
NODEARG();
if(naIsNil(argv) || naVec_size(argv) == 0) {
// Remove all children
for(int i = (*node)->nChildren() - 1; i >=0; i--)
- naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i)));
+ naVec_append(result, propNodeGhostCreate(c, (*node)->removeChild(i, false)));
} else {
// Remove all children of a specified name
naRef name = naVec_get(argv, 0);
static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
{
- NODEARG();
+ NODENOARG();
return naNum((*node)->unalias());
}
static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
{
- NODEARG();
+ NODENOARG();
return propNodeGhostCreate(c, (*node)->getAliasTarget());
}
{
NODEARG();
naRef path = naVec_get(argv, 0);
- bool create = naTrue(naVec_get(argv, 1));
+ bool create = naTrue(naVec_get(argv, 1)) != 0;
if(!naIsString(path)) return naNil();
SGPropertyNode* n;
try {
{ f_getParent, "_getParent" },
{ f_getChild, "_getChild" },
{ f_getChildren, "_getChildren" },
+ { f_addChild, "_addChild" },
+ { f_addChildren, "_addChildren" },
{ f_removeChild, "_removeChild" },
{ f_removeChildren, "_removeChildren" },
{ f_alias, "_alias" },