Robot: Cosmetic changes
Some interesting ideas for extending Nasal this way have been collected at [[Proposals:Nasal related]].
= Pre-Requisites =
In order to work through this article, you will likely need to be interested in FlightGear core development, need to be somewhat familiar with C/C++, as well as with some basic Nasal (given that Nasal itself is implemented in ANSI C, basic C knowledge will mostly do for starters-C++ knowledge will only be required in order to understand the FlightGear side of things).
= Argument Processing =
Consider the callback signature:
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
naRef naStringValue(naContext c, naRef n);
= Passing Pointers to Nasal scripts = Things get more complicated if you need to pass a handle to a C/C++ object into a Nasal script.
There, you need to use a wrapped handle type called a ghost ("Garbage-collectable Handle to an OutSide Thing"), which has a callback that you need to implement to deal with what happens when the Nasal interpreter garbage collects your object.
For an example how something like this is done in the FlightGear context, you may want to check out $FG_SRC/Scripting/nasal-props.cxx, which wraps the SGPropertyNode class ([[SimGear]]) inside a nasal ghost, so that the C++ class is exposed to Nasal scripts.
= Things to avoid =
And just for completeness: things get *really* complicated if you need to *store* a naRef you got from a Nasal script in a place where the interpreter can't see it.
But this API doesn't yet have a corresponding call to release a saved object. Just don't do this, basically.
Always keep your Nasal data in Nasal space.
If you really need to do this, you may however want to check out the gcSave/gcRelease methods of the FGNasalSys class:
int gcSave(naRef r);
void gcRelease(int key);