4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/nasal/nasal.h>
10 class FGNasalSys : public SGSubsystem
14 virtual ~FGNasalSys();
16 virtual void update(double dt) { /* noop */ }
18 // Simple hook to run arbitrary source code. Returns a bool to
19 // indicate successful execution. Does *not* return any Nasal
20 // values, because handling garbage-collected objects from C space
21 // is deep voodoo and violates the "simple hook" idea.
22 bool parseAndRun(const char* sourceCode);
24 // Slightly more complicated hook to get a handle to a precompiled
25 // Nasal script that can be invoked via a call() method. The
26 // caller is expected to delete the FGNasalScript returned from
27 // this function. The "name" argument specifies the "file name"
28 // for the source code that will be printed in Nasal stack traces
30 FGNasalScript* parseScript(const char* src, const char* name=0);
32 // Implementation of the settimer extension function
33 void setTimer(naRef args);
35 // Returns a ghost wrapper for the current _cmdArg
38 // Callbacks for command and timer bindings
39 virtual bool handleCommand(const SGPropertyNode* arg);
42 friend class FGNasalScript;
45 // FGTimer subclass for handling Nasal timer callbacks.
46 // See the implementation of the settimer() extension function for
50 virtual void timerExpired();
56 void loadPropertyScripts();
57 void initModule(const char* moduleName, const char* fileName,
58 const char* src, int len);
59 void readScriptFile(SGPath file, const char* lib);
60 void hashset(naRef hash, const char* key, naRef val);
62 naRef parse(const char* filename, const char* buf, int len=0);
63 naRef genPropsModule();
64 naRef propNodeGhost(SGPropertyNode* handle);
66 // This mechanism is here to allow naRefs to be passed to
67 // locations "outside" the interpreter. Normally, such a
68 // reference would be garbage collected unexpectedly. By passing
69 // it to gcSave and getting a key/handle, it can be cached in a
70 // globals.__gcsave hash. Be sure to release it with gcRelease
73 void gcRelease(int key);
78 SGPropertyNode* _cmdArg;
83 public: void handleTimer(NasalTimer* t);
88 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
91 naCall(_nas->_context, _code, naNil(), naNil(), naNil());
92 return naGetError(_nas->_context) == 0;
96 friend class FGNasalSys;
102 #endif // __NASALSYS_HXX