Howto:Extend Nasal
Nasal internals |
---|
Memory Management (GC) |
This article is dedicated to describing how to write custom C/C++ extension functions in order to extend the Nasal scripting interpreter in FlightGear, for example in order to expose new or existing FlightGear APIs to the Nasal scripting engine, so that Nasal scripts can access additional FlightGear internals.
Status (cppbind)
As of 03/2013, this article should be considered deprecated, we have Nasal/CppBind, a new C++-based API for exposing C/C++ functions/data structures to Nasal and vice versa in simgear/simgear/nasal/cppbind. Usually, there should be no need to use the bare Nasal APIs anymore for exposing functions/data to Nasal or Nasal-space types to C++, except for people working on the cppbind framework itself.
The cppbind framework is much more generic and high level than the bare APIs documented here, it includes unit testing support and makes use of modern C++ features like templates and STL support, including SimGear specific types like SGPath/SGGeod etc, its overhead is fairly small (not just performance, but also LoC to create new bindings). The cppbind framework is already extensively used by the Canvas system and the NasalPositioned bindings, both of which are a good place to look for code examples.
You will find that most of the "old" code in $FG_SRC/Scripting still uses those C-APIs for interacting with the Nasal engine. Only the new code, #include'ing <simgear/nasal/cppbind>, uses boost templates to hide the low level details.
Most of the code in the Nasal subsystem itself (FGNasalSys) also still uses the legacy C APIs - this is just to explain the two approaches, to avoid unnecessary confusion. Overall, it's a good idea to favor cppbind over the low-level approach documented here, it isn't only much more elegant, but also saves you tons of typing and time, too
The new cppbind framework makes heavy use of modern C++, so that C++ data structures and classes/methods (including templates) can now be easily exposed to Nasal space and vice versa.
Implementation examples are to be found in $FG_SRC/Scripting. For example, see the most recent addition, which exposes SimGear's HTTP client code to Nasal space with less than 140 lines of code in $FG_SRC/Scripting/NasalHTTP.?xx There's still plenty of "old" code that doesn't yet use the cppbind framework - check the header inclusions at the top of the file.
Some of the more straightforward things to play with would be exposing useful SG/FG classes to Nasal space, such as the SGSubsystem interface to register scripted SGSubsystems, or the autopilot system.
For more technical Nasal questions (C API, internals etc), you'll probably want to refer to the forum, in particular: Philosopher, TheTom, Zakalawe or Hooray - TheTom and Zakalawe can also provide help on using cppbind, having both used it extensively while developing/extending the Canvas system.
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).
Also, you should have experience compiling FlightGear (see Building FlightGear for Linux instructions or Building Flightgear - Windows for Windows specific instructions).
In addition, you may want to check out the FlightGear Integration Docs.
Nasal
Introductory information on the Nasal programming language itself can be found at Nasal scripting language. Information on writing simple Nasal scripts can be found at Howto: Write simple scripts in Nasal. Useful Nasal snippets can be found at Nasal Snippets.
If you have any Nasal specific questions, you will want to check out the Nasal FAQ, feel free to ask new questions or help answer and refine existing ones.
All Nasal related articles can be found in Category:Nasal.
Note FlightGear's version of the Nasal interpreter is maintained in the SimGear git repository, inside the simgear/simgear/nasal folder, the most important header file detailing the internal Nasal API is nasal.h, you will want to check this out for the latest changes and information. |
You will probably also want to check out the simgear/simgear/nasal folder for specific examples on using the various Nasal APIs that are not yet covered here completely.
Caution As of 05/2009, this article is work in progress, and none of the examples have so far been tested/compiled.
Your help in improving and updating this article is appreciated, thanks! |
Another, simpler, option to add new C++ code to FlightGear is illustrated here: Howto:Add new fgcommands to FlightGear.
Intro
In FlightGear, the simplest way to add new extension functions is to look at the existing functions at flightgear/src/Scripting/NasalSys.cxx.
There is a static table of function pointers (named funcs[]) referencing extension functions, along with their corresponding names in Nasal: flightgear/src/Scripting/NasalSys.cxx (line 797). The following is a copy of the extension function list, taken in 11/2015:
// Table of extension functions. Terminate with zeros.
static struct { const char* name; naCFunction func; } funcs[] = {
{ "getprop", f_getprop },
{ "setprop", f_setprop },
{ "print", f_print },
{ "logprint", f_logprint },
{ "_fgcommand", f_fgcommand },
{ "settimer", f_settimer },
{ "maketimer", f_makeTimer },
{ "_setlistener", f_setlistener },
{ "removelistener", f_removelistener },
{ "addcommand", f_addCommand },
{ "removecommand", f_removeCommand },
{ "_cmdarg", f_cmdarg },
{ "_interpolate", f_interpolate },
{ "rand", f_rand },
{ "srand", f_srand },
{ "abort", f_abort },
{ "directory", f_directory },
{ "resolvepath", f_resolveDataPath },
{ "finddata", f_findDataDir },
{ "parsexml", f_parsexml },
{ "parse_markdown", f_parse_markdown },
{ "md5", f_md5 },
{ "systime", f_systime },
{ 0, 0 }
};
(Note, as of 06/2012, some of these functions have meanwhile been moved to a different file and are initialized there: NasalPositioned.cxx)
So, the basic format is "name" (string), function_pointer - whereas "name" refers to the internal name used by Nasal and its scripts, and "function_pointer" has to use the right function signature and is a pointer to the implementation of the Nasal function in C/C++ code space:
// The function signature for an extension function:
typedef naRef (*naCFunction)(naContext ctx, naRef me, int argc, naRef* args);
You will need to add your new extension function to this list of static functions, preferably following the existing naming convention (i.e. "f_" prefix).
If your extension functions are likely to be fairly low level, and will thus be provided with a more abstract wrapper in Nasal space, these functions should use a prepended undercore ("_"), such as the _fgcommand()
, _setlistener()
, _cmdarg()
and _interpolate()
functions.
Extension function signature
These extension functions look like:
static naRef f_your_function(naContext c, naRef me, int argc, naRef* args)
{
// ...
}
So, this is basically the boilerplate that you'll need to use in order to expose a new function (i.e. you can just copy & paste this into the NasalSys.cxx file and change the function's name accordingly).
If you don't have anything else to return, you can just return naNil(), so that a function named "f_cool" looks like this:
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
SG_LOG(SG_GENERAL, SG_ALERT, "Nasal:cool() got executed!");
return naNil();
}
In order to make this function known to the Nasal interpreter, you'll want to extend the previously mentioned list of Nasal extension functions (in src/Scripting/NasalSys.cxx), to read:
// ....
{ "airportinfo", f_airportinfo },
{ "cool", f_cool }, //this is where we add our new function
{ 0, 0 }
};
Once you have made these modifications and rebuilt FlightGear (running make in the Scripting sub folder and relinking FG should suffice), you can simply invoke your new function, of course it will not yet do anything useful, though:
#cooltest.nas (to be saved in $FG_ROOT/Nasal or run via the Nasal console)
cool();
So, in order to make it slightly more interesting, we are going to change the return statement to return something else, instead of nil:
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
const char* cool="cool";
naRef retval;
naStr_fromdata(retval, cool, strlen(cool) );
return retval;
}
So, after rebuilding the FlightGear binary, whenever you call the new "cool" API function, it will always return a "cool" string, which you could for example print out using a script like the following:
#cooltest.nas (to be saved in $FG_ROOT/Nasal)
var result=cool();
print(result);
Argument processing
Consider the callback signature:
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
The arguments to the function are passed in in the args array. The number of arguments is passed via the argc parameter (this is basically consistent with the standard signature of main in C/C++).
So, if you know that you require a certain number of arguments, you can also directly check argc to to match your requirements and show an error message, return nil, or throw an exception using naRuntimeError():
// Throw an error from the current call stack. This function makes a
// longjmp call to a handler in naCall() and DOES NOT RETURN. It is
// intended for use in library code that cannot otherwise report an
// error via the return value, and MUST be used carefully. If in
// doubt, return naNil() as your error condition. Works like
// printf().
void naRuntimeError(naContext c, const char* fmt, ...);
The "me" reference is set if the function was called as a method call on an object (e.g. object.your_function() instead of just your_function(), in which case "me" would be set to the object (Nasal hash)).
The naRef objects can be manipulated using the functions in nasal.h. For the latest copy of the file, see simgear/simgear/nasal/nasal.h
Basically, you can check the type of the reference with the following naIs*() functions:
int naIsNil(naRef r);
int naIsNum(naRef r);
int naIsString(naRef r);
int naIsScalar(naRef r);
int naIsVector(naRef r);
int naIsHash(naRef r);
int naIsCode(naRef r);
int naIsFunc(naRef r);
int naIsCCode(naRef r);
And then get the appropriate value out with things like naNumValue(), naStr_data(), etc...
So, if we're now to change the interface of our earlier "cool" function in the example, to only return "cool" if we are passed at least one numerical parameter, and otherwise return "uncool", it could look like this:
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
const char* cool="cool";
const char* uncool="uncool";
const char* result;
naRef retval;
if (!argc || !isNaNum(args[0]))
result=uncool;
else
result=cool;
return naStr_fromdata(retval, result, strlen(result) );
}
You can test this, by running a simple Nasal script like the following:
# test new cool() function:
print( cool("foo") );
print( cool(100) );
A common way to evaluate parameters passed to Nasal extension functions in C, looks like this:
static naRef f_foo (naContext c, naRef me, int argc, naRef* args) {
naRef param1 = argc > 0 ? args[0] : naNil();
naRef param2 = argc > 1 ? args[1] : naNil();
naRef param3 = argc > 2 ? args[2] : naNil();
//further parameter processing
return naNil;
}
This will basically look for 3 parameters, and assign them accordingly - if they are not available, they will just be assigned nil using the naNil() calls (the next step would be to check if the parameters have the right types using the naIs* helpers mentioned above).
Creating Nasal data structures
There are also functions to create Nasal data structures (hash tables, vectors, etc...) which you can return to the caller simply by returning the resulting naRef:
naRef naNil();
naRef naNum(double num);
naRef naNewString(naContext c);
naRef naNewVector(naContext c);
naRef naNewHash(naContext c);
naRef naNewFunc(naContext c, naRef code);
naRef naNewCCode(naContext c, naCFunction fptr);
String manipulation API
// String utilities:
int naStr_len(naRef s);
char* naStr_data(naRef s);
naRef naStr_fromdata(naRef dst, const char* data, int len);
naRef naStr_concat(naRef dest, naRef s1, naRef s2);
naRef naStr_substr(naRef dest, naRef str, int start, int len);
naRef naInternSymbol(naRef sym);
Vector manipulation API
// Vector utilities:
int naVec_size(naRef v);
naRef naVec_get(naRef v, int i);
void naVec_set(naRef vec, int i, naRef o);
int naVec_append(naRef vec, naRef o);
naRef naVec_removelast(naRef vec);
void naVec_setsize(naRef vec, int sz);
Hash manipulation API
// Hash utilities:
int naHash_size(naRef h);
int naHash_get(naRef hash, naRef key, naRef* out);
naRef naHash_cget(naRef hash, char* key);
void naHash_set(naRef hash, naRef key, naRef val);
void naHash_cset(naRef hash, char* key, naRef val);
void naHash_delete(naRef hash, naRef key);
void naHash_keys(naRef dst, naRef hash);
// Retrieve the specified member from the object, respecting the
// "parents" array as for "object.field". Returns zero for missing
// fields.
int naMember_get(naRef obj, naRef field, naRef* out);
int naMember_cget(naRef obj, const char* field, naRef* out);
A common way to set up hash field members easily, is using a simple macro like:
#define HASHSET(s,l,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,l),n)
//... do your hash setup here
#undef HASHSET
As you can see, this allows you to easily set up hash fields.
This will call the naHash_set() helper, with the target hash being its first argument, the key name coming next and the naRef (n) being last.
The macro can be even further simplified by automatically computing the length of the hash key using strlen() instead of the explicit length argument:
#define HASHSET(s,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,strlen(s)),n)
//... do your hash setup here
#undef HASHSET
Conversion & comparison routines
Some useful conversion/comparison routines include:
int naEqual(naRef a, naRef b);
int naStrEqual(naRef a, naRef b);
int naTrue(naRef b);
naRef naNumValue(naRef n);
naRef naStringValue(naContext c, naRef n);
Wrapping C++ classes as Nasal objects with cppbind
Please see: simgear/simgear/nasal/cppbind
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.
Ghost utilities:
typedef struct naGhostType {
void(*destroy)(void*);
const char* name;
} naGhostType;
naRef naNewGhost(naContext c, naGhostType* t, void* ghost);
naGhostType* naGhost_type(naRef ghost);
void* naGhost_ptr(naRef ghost);
int naIsGhost(naRef r);
This needs to be specifically implemented for each new "ghost" type.
For an example of how something like this is done in the FlightGear context, you may want to check out $FG_SRC/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.
Locking a Nasal context (threading)
// Acquires a "modification lock" on a context, allowing the C code to
// modify Nasal data without fear that such data may be "lost" by the
// garbage collector (nasal data on the C stack is not examined in
// GC!). This disallows garbage collection until the current thread
// can be blocked. The lock should be acquired whenever nasal objects
// are being modified. It need not be acquired when only read access
// is needed, PRESUMING that the Nasal data being read is findable by
// the collector (via naSave, for example) and that another Nasal
// thread cannot or will not delete the reference to the data. It
// MUST NOT be acquired by naCFunction's, as those are called with the
// lock already held; acquiring two locks for the same thread will
// cause a deadlock when the GC is invoked. It should be UNLOCKED by
// naCFunction's when they are about to do any long term non-nasal
// processing and/or blocking I/O. Note that naModLock() may need to
// block to allow garbage collection to occur, and that garbage
// collection by other threads may be blocked until naModUnlock() is
// called. It must also be UNLOCKED by threads that hold a lock
// already before making a naCall() or naContinue() call -- these
// functions will attempt to acquire the lock again.
void naModLock();
void naModUnlock();
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.
There is a minimal API to handle this:
// "Save" this object in the context, preventing it (and objects
// referenced by it) from being garbage collected.
void naSave(naContext ctx, naRef obj);
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:
// This mechanism is here to allow naRefs to be passed to
// locations "outside" the interpreter. Normally, such a
// reference would be garbage collected unexpectedly. By passing
// it to gcSave and getting a key/handle, it can be cached in a
// globals.__gcsave hash. Be sure to release it with gcRelease
// when done.
int gcSave(naRef r);
void gcRelease(int key);