]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/nasal-props.cxx
don't truncate strings output by print() to 1024 bytes. This was apparently
[flightgear.git] / src / Scripting / nasal-props.cxx
index d040d3189b3a91cb9314ff5d66dbda981ddb52de..ea6548ade3f38d42c89458c57b3cfb110d87027e 100644 (file)
@@ -74,6 +74,50 @@ static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
     return NASTR(t);
 }
 
+static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
+{
+    NODEARG();
+    naRef val = naVec_get(argv, 0);
+    char *a = naStr_data(val);
+    SGPropertyNode::Attribute attr;
+    if(!a) a = "";
+    if(!strcmp(a, "read"))             attr = SGPropertyNode::READ;
+    else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
+    else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
+    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, "tied")) {
+        return naNum((*node)->isTied());
+    } else {
+        naRuntimeError(c, "props.getAttribute() with invalid attribute");
+        return naNil();
+    }
+    return naNum((*node)->getAttribute(attr));
+}
+
+static naRef f_setAttribute(naContext c, naRef me, int argc, naRef* args)
+{
+    NODEARG();
+    naRef val = naVec_get(argv, 0);
+    char *a = naStr_data(val);
+    SGPropertyNode::Attribute attr;
+    if(!a) a = "";
+    if(!strcmp(a, "read"))             attr = SGPropertyNode::READ;
+    else if(!strcmp(a, "write"))       attr = SGPropertyNode::WRITE;
+    else if(!strcmp(a, "archive"))     attr = SGPropertyNode::ARCHIVE;
+    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 {
+        naRuntimeError(c, "props.setAttribute() with invalid attribute");
+        return naNil();
+    }
+    naRef ret = naNum((*node)->getAttribute(attr));
+    (*node)->setAttribute(attr, naTrue(naVec_get(argv, 1)) ? true : false);
+    return ret;
+}
+
 static naRef f_getName(naContext c, naRef me, int argc, naRef* args)
 {
     NODEARG();
@@ -107,7 +151,12 @@ static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
     NODEARG();
     naRef val = naVec_get(argv, 0);
     if(naIsString(val)) (*node)->setStringValue(naStr_data(val));
-    else                (*node)->setDoubleValue(naNumValue(val).num);
+    else {
+        naRef n = naNumValue(val);
+        if(naIsNil(n))
+            naRuntimeError(c, "props.setValue() with non-number");
+        (*node)->setDoubleValue(naNumValue(val).num);
+    }
     return naNil();
 }
 
@@ -120,6 +169,8 @@ static naRef f_setIntValue(naContext c, naRef me, int argc, naRef* args)
     // Junk to pacify the gcc-2.95.3 optimizer:
     naRef tmp0 = naVec_get(argv, 0);
     naRef tmp1 = naNumValue(tmp0);
+    if(naIsNil(tmp1))
+        naRuntimeError(c, "props.setIntValue() with non-number");
     double tmp2 = tmp1.num;
     int iv = (int)tmp2;
 
@@ -138,7 +189,10 @@ static naRef f_setBoolValue(naContext c, naRef me, int argc, naRef* args)
 static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
 {
     NODEARG();
-    (*node)->setDoubleValue(naNumValue(naVec_get(argv, 0)).num);
+    naRef r = naNumValue(naVec_get(argv, 0));
+    if(naIsNil(r))
+        naRuntimeError(c, "props.setDoubleValue() with non-number");
+    (*node)->setDoubleValue(r.num);
     return naNil();
 }
 
@@ -267,6 +321,8 @@ static struct {
     char* name;
 } propfuncs[] = {
     { f_getType, "_getType" },
+    { f_getAttribute, "_getAttribute" },
+    { f_setAttribute, "_setAttribute" },
     { f_getName, "_getName" },
     { f_getIndex, "_getIndex" },
     { f_getValue, "_getValue" },