From 1b55ab5f4032c6f3f1a4d07c0b9babd3744f1c37 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Sun, 22 Jun 2014 15:37:48 +0200 Subject: [PATCH] Nasal: expose SGCondition using nasal::Ghost and improved error checking. --- src/Scripting/NasalCondition.cxx | 96 ++++++++------------------------ src/Scripting/NasalCondition.hxx | 6 -- src/Scripting/NasalSys.hxx | 1 - 3 files changed, 24 insertions(+), 79 deletions(-) diff --git a/src/Scripting/NasalCondition.cxx b/src/Scripting/NasalCondition.cxx index f47a52a51..a28842b45 100644 --- a/src/Scripting/NasalCondition.cxx +++ b/src/Scripting/NasalCondition.cxx @@ -22,94 +22,46 @@ # include "config.h" #endif -#include - #include "NasalCondition.hxx" #include "NasalSys.hxx" #include
-#include +#include +#include #include -static void conditionGhostDestroy(void* g); - -naGhostType ConditionGhostType = { conditionGhostDestroy, "condition" }; +typedef nasal::Ghost NasalCondition; -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) +//------------------------------------------------------------------------------ +static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args) { - SGCondition* cond = (SGCondition*)g; - if (!SGCondition::put(cond)) // unref - delete cond; -} + SGPropertyNode* node = argc > 0 + ? ghostToPropNode(args[0]) + : NULL; + SGPropertyNode* root = argc > 1 + ? ghostToPropNode(args[1]) + : globals->get_props(); -static naRef conditionPrototype; + if( !node || !root ) + naRuntimeError(c, "createCondition: invalid argument(s)"); -naRef ghostForCondition(naContext c, const SGCondition* cond) -{ - if (!cond) { - return naNil(); + try + { + return nasal::to_nasal(c, sgReadCondition(root, node)); } - - 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"); + catch(std::exception& ex) + { + naRuntimeError(c, "createCondition: %s", ex.what()); } - - return naNum(cond->test()); } -static naRef f_createCondition(naContext c, naRef me, int argc, naRef* args) +//------------------------------------------------------------------------------ +naRef initNasalCondition(naRef globals, naContext c) { - SGPropertyNode* node = ghostToPropNode(args[0]); - SGPropertyNode* root = globals->get_props(); - if (argc > 1) { - root = ghostToPropNode(args[1]); - } - - 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 } -}; + nasal::Ghost::init("Condition") + .method("test", &SGCondition::test); + nasal::Hash(globals, c).set("_createCondition", f_createCondition); -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))); - } - return naNil(); } diff --git a/src/Scripting/NasalCondition.hxx b/src/Scripting/NasalCondition.hxx index c16b2b5d1..6f4ffe2bd 100644 --- a/src/Scripting/NasalCondition.hxx +++ b/src/Scripting/NasalCondition.hxx @@ -23,12 +23,6 @@ #include -// forward decls -class SGCondition; - -naRef ghostForCondition(naContext c, const SGCondition* cond); - naRef initNasalCondition(naRef globals, naContext c); #endif // of SCRIPTING_NASAL_CONDITION_HXX - diff --git a/src/Scripting/NasalSys.hxx b/src/Scripting/NasalSys.hxx index bbb2ace90..e487a48eb 100644 --- a/src/Scripting/NasalSys.hxx +++ b/src/Scripting/NasalSys.hxx @@ -29,7 +29,6 @@ class FGNasalModuleListener; namespace simgear { class BufferedLogCallback; } SGPropertyNode* ghostToPropNode(naRef ref); -SGCondition* conditionGhost(naRef r); class FGNasalSys : public SGSubsystem { -- 2.39.5