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"
#endif
#include <string.h>
#include "NasalCondition.hxx"
#include "NasalSys.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>
static void conditionGhostDestroy(void* g);
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());
}
typedef nasal::Ghost<SGConditionRef> NasalCondition;
//------------------------------------------------------------------------------
static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args)
{
SGPropertyNode* node = ghostToPropNode(args[0]);
SGPropertyNode* root = globals->get_props();
if (argc > 1) {
root = ghostToPropNode(args[1]);
SGPropertyNode* node = argc > 0
? ghostToPropNode(args[0])
: NULL;
SGPropertyNode* root = argc > 1
? ghostToPropNode(args[1])
: globals->get_props();
if( !node || !root )
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());
}
SGCondition* cond = sgReadCondition(root, node);
return ghostForCondition(c, cond);
}
// 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)
{
conditionPrototype = naNewHash(c);
naSave(c, conditionPrototype);
hashset(c, conditionPrototype, "test", naNewFunc(c, naNewCCode(c, f_condition_test)));
for(int i=0; funcs[i].name; i++) {
hashset(c, globals, funcs[i].name,
naNewFunc(c, naNewCCode(c, funcs[i].func)));
}
nasal::Ghost<SGConditionRef>::init("Condition")
.method("test", &SGCondition::test);
nasal::Hash(globals, c).set("_createCondition", f_createCondition);
return naNil();
}

View file

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

View file

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