1
0
Fork 0

Nasal: expose SGCondition using nasal::Ghost and improved error checking.

This commit is contained in:
Thomas Geymayer 2014-06-22 15:37:48 +02:00
parent f7f7be77e5
commit 1b55ab5f40
3 changed files with 27 additions and 82 deletions

View file

@ -22,94 +22,46 @@
# include "config.h" # include "config.h"
#endif #endif
#include <string.h>
#include "NasalCondition.hxx" #include "NasalCondition.hxx"
#include "NasalSys.hxx" #include "NasalSys.hxx"
#include <Main/globals.hxx> #include <Main/globals.hxx>
#include <simgear/sg_inlines.h> #include <simgear/nasal/cppbind/Ghost.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/props/condition.hxx> #include <simgear/props/condition.hxx>
static void conditionGhostDestroy(void* g); typedef nasal::Ghost<SGConditionRef> NasalCondition;
naGhostType ConditionGhostType = { conditionGhostDestroy, "condition" };
static void hashset(naContext c, naRef hash, const char* key, naRef val)
{
naRef s = naNewString(c);
naStr_fromdata(s, (char*)key, strlen(key));
naHash_set(hash, s, val);
}
SGCondition* conditionGhost(naRef r)
{
if ((naGhost_type(r) == &ConditionGhostType))
{
return (SGCondition*) naGhost_ptr(r);
}
return 0;
}
static void conditionGhostDestroy(void* g)
{
SGCondition* cond = (SGCondition*)g;
if (!SGCondition::put(cond)) // unref
delete cond;
}
static naRef conditionPrototype;
naRef ghostForCondition(naContext c, const SGCondition* cond)
{
if (!cond) {
return naNil();
}
SGCondition::get(cond); // take a ref
return naNewGhost(c, &ConditionGhostType, (void*) cond);
}
static naRef f_condition_test(naContext c, naRef me, int argc, naRef* args)
{
SGCondition* cond = conditionGhost(me);
if (!cond) {
naRuntimeError(c, "condition.test called on non-condition object");
}
return naNum(cond->test());
}
//------------------------------------------------------------------------------
static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args) static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
{ {
SGPropertyNode* node = ghostToPropNode(args[0]); SGPropertyNode* node = argc > 0
SGPropertyNode* root = globals->get_props(); ? ghostToPropNode(args[0])
if (argc > 1) { : NULL;
root = ghostToPropNode(args[1]); SGPropertyNode* root = argc > 1
} ? ghostToPropNode(args[1])
: globals->get_props();
SGCondition* cond = sgReadCondition(root, node); if( !node || !root )
return ghostForCondition(c, cond); naRuntimeError(c, "createCondition: invalid argument(s)");
try
{
return nasal::to_nasal(c, sgReadCondition(root, node));
}
catch(std::exception& ex)
{
naRuntimeError(c, "createCondition: %s", ex.what());
}
} }
// Table of extension functions. Terminate with zeros. //------------------------------------------------------------------------------
static struct { const char* name; naCFunction func; } funcs[] = {
{ "_createCondition", f_createCondition },
{ 0, 0 }
};
naRef initNasalCondition(naRef globals, naContext c) naRef initNasalCondition(naRef globals, naContext c)
{ {
conditionPrototype = naNewHash(c); nasal::Ghost<SGConditionRef>::init("Condition")
naSave(c, conditionPrototype); .method("test", &SGCondition::test);
hashset(c, conditionPrototype, "test", naNewFunc(c, naNewCCode(c, f_condition_test))); nasal::Hash(globals, c).set("_createCondition", f_createCondition);
for(int i=0; funcs[i].name; i++) {
hashset(c, globals, funcs[i].name,
naNewFunc(c, naNewCCode(c, funcs[i].func)));
}
return naNil(); return naNil();
} }

View file

@ -23,12 +23,6 @@
#include <simgear/nasal/nasal.h> #include <simgear/nasal/nasal.h>
// forward decls
class SGCondition;
naRef ghostForCondition(naContext c, const SGCondition* cond);
naRef initNasalCondition(naRef globals, naContext c); naRef initNasalCondition(naRef globals, naContext c);
#endif // of SCRIPTING_NASAL_CONDITION_HXX #endif // of SCRIPTING_NASAL_CONDITION_HXX

View file

@ -29,7 +29,6 @@ class FGNasalModuleListener;
namespace simgear { class BufferedLogCallback; } namespace simgear { class BufferedLogCallback; }
SGPropertyNode* ghostToPropNode(naRef ref); SGPropertyNode* ghostToPropNode(naRef ref);
SGCondition* conditionGhost(naRef r);
class FGNasalSys : public SGSubsystem class FGNasalSys : public SGSubsystem
{ {