1 // NasalCondition -- expose SGCondition to Nasal
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 James Turner
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "NasalCondition.hxx"
28 #include "NasalSys.hxx"
29 #include <Main/globals.hxx>
31 #include <simgear/sg_inlines.h>
32 #include <simgear/props/condition.hxx>
34 static void conditionGhostDestroy(void* g);
36 naGhostType ConditionGhostType = { conditionGhostDestroy, "condition" };
38 static void hashset(naContext c, naRef hash, const char* key, naRef val)
40 naRef s = naNewString(c);
41 naStr_fromdata(s, (char*)key, strlen(key));
42 naHash_set(hash, s, val);
45 SGCondition* conditionGhost(naRef r)
47 if ((naGhost_type(r) == &ConditionGhostType))
49 return (SGCondition*) naGhost_ptr(r);
55 static void conditionGhostDestroy(void* g)
57 SGCondition* cond = (SGCondition*)g;
58 if (!SGCondition::put(cond)) // unref
62 static naRef conditionPrototype;
64 naRef ghostForCondition(naContext c, const SGCondition* cond)
70 SGCondition::get(cond); // take a ref
71 return naNewGhost(c, &ConditionGhostType, (void*) cond);
74 static naRef f_condition_test(naContext c, naRef me, int argc, naRef* args)
76 SGCondition* cond = conditionGhost(me);
78 naRuntimeError(c, "condition.test called on non-condition object");
81 return naNum(cond->test());
84 static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
86 SGPropertyNode* node = ghostToPropNode(args[0]);
87 SGPropertyNode* root = globals->get_props();
89 root = ghostToPropNode(args[1]);
92 SGCondition* cond = sgReadCondition(root, node);
93 return ghostForCondition(c, cond);
96 // Table of extension functions. Terminate with zeros.
97 static struct { const char* name; naCFunction func; } funcs[] = {
98 { "_createCondition", f_createCondition },
103 naRef initNasalCondition(naRef globals, naContext c, naRef gcSave)
105 conditionPrototype = naNewHash(c);
106 hashset(c, gcSave, "conditionProto", conditionPrototype);
108 hashset(c, conditionPrototype, "test", naNewFunc(c, naNewCCode(c, f_condition_test)));
109 for(int i=0; funcs[i].name; i++) {
110 hashset(c, globals, funcs[i].name,
111 naNewFunc(c, naNewCCode(c, funcs[i].func)));