delete prop;
}
-naGhostType PropNodeGhostType = { propNodeGhostDestroy };
+naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
static naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
{
return NASTR(t);
}
+static naRef f_getAttribute(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ if(naVec_size(argv) == 0) return naNum(unsigned((*node)->getAttributes()));
+ naRef val = naVec_get(argv, 0);
+ char *a = naStr_data(val);
+ SGPropertyNode::Attribute attr;
+ if(!a) a = "";
+ if(!strcmp(a, "last")) return naNum(SGPropertyNode::LAST_USED_ATTRIBUTE);
+ else if(!strcmp(a, "children")) return naNum((*node)->nChildren());
+ else if(!strcmp(a, "listeners")) return naNum((*node)->nListeners());
+ else if(!strcmp(a, "references")) return naNum(node->getNumRefs());
+ else if(!strcmp(a, "tied")) return naNum((*node)->isTied());
+ else if(!strcmp(a, "alias")) return naNum((*node)->isAlias());
+ else if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
+ else if(!strcmp(a, "writable")) 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.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);
+ if(naVec_size(argv) == 1 && naIsNum(val)) {
+ naRef ret = naNum((*node)->getAttributes());
+ (*node)->setAttributes((int)val.num);
+ return ret;
+ }
+ SGPropertyNode::Attribute attr;
+ char *a = naStr_data(val);
+ if(!a) a = "";
+ if(!strcmp(a, "readable")) attr = SGPropertyNode::READ;
+ else if(!strcmp(a, "writable")) 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();
case SGPropertyNode::STRING:
case SGPropertyNode::UNSPECIFIED:
return NASTR((*node)->getStringValue());
+ default:
+ return naNil();
}
- return naNil();
}
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();
}
// 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;
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();
}
naRef child = naVec_get(argv, 0);
naRef index = naVec_get(argv, 1);
if(!naIsString(child) || !naIsNum(index)) return naNil();
+ SGPropertyNode_ptr n = 0;
try {
- (*node)->removeChild(naStr_data(child), (int)index.num, false);
+ n = (*node)->removeChild(naStr_data(child), (int)index.num, false);
} catch (const string& err) {
naRuntimeError(c, (char *)err.c_str());
}
- return naNil();
+ return propNodeGhostCreate(c, n);
}
static naRef f_removeChildren(naContext c, naRef me, int argc, naRef* args)
return result;
}
+static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ SGPropertyNode* al;
+ naRef prop = naVec_get(argv, 0);
+ try {
+ if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
+ else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
+ else throw string("props.alias() with bad argument");
+ } catch (const string& err) {
+ naRuntimeError(c, (char *)err.c_str());
+ return naNil();
+ }
+ return naNum((*node)->alias(al));
+}
+
+static naRef f_unalias(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ return naNum((*node)->unalias());
+}
+
+static naRef f_getAliasTarget(naContext c, naRef me, int argc, naRef* args)
+{
+ NODEARG();
+ return propNodeGhostCreate(c, (*node)->getAliasTarget());
+}
+
static naRef f_getNode(naContext c, naRef me, int argc, naRef* args)
{
NODEARG();
static struct {
naCFunction func;
- char* name;
+ const char* name;
} propfuncs[] = {
{ f_getType, "_getType" },
+ { f_getAttribute, "_getAttribute" },
+ { f_setAttribute, "_setAttribute" },
{ f_getName, "_getName" },
{ f_getIndex, "_getIndex" },
{ f_getValue, "_getValue" },
{ f_getChildren, "_getChildren" },
{ f_removeChild, "_removeChild" },
{ f_removeChildren, "_removeChildren" },
+ { f_alias, "_alias" },
+ { f_unalias, "_unalias" },
+ { f_getAliasTarget, "_getAliasTarget" },
{ f_getNode, "_getNode" },
{ f_new, "_new" },
{ f_globals, "_globals" },