Changes

Jump to: navigation, search

Howto:Extend Nasal

594 bytes added, 23:13, 12 December 2014
m
Copyediting
 
{{Template:Nasal Internals}}
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.
<!-- Some interesting ideas for extending Nasal this way have been collected at [[Proposals:Nasal related]]. Article was deleted 1 July 2014. /Johan G, 12 December 2014 -->
== Status (cppbind) ==
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 during the last months.
== Scope ==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. Some interesting ideas for extending Nasal this way have been collected at [[Proposals:Nasal related]]. = Pre-Requisites 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).
In addition, you may want to check out the [http://www.plausible.org/nasal/flightgear.html 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]]. <!-- A Nasal Style Guide is available at [[Nasal Style Guide]] (Note that as of 05/2009, this is work in progress). Was deleted 9 April 2013 due to being a skeleton stub for years. /Johan G, 13 dec 2014 -->
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 the [http://wiki.flightgear.org/index.php/Category:Nasal Nasal category].
'''{{Note:''' |FlightGear's version of the Nasal interpreter is maintained in the [http://www.simgear.org SimGear] git repository, inside the {{Git file|gitorious|fg/simgear|next|simgear/nasal|pre=$SG_SRC/}} folder, the most important header file detailing the internal Nasal API is "[http://simgear.org/doxygen/nasal_8h-source.html nasal.h]", you will want to check this out for the latest changes and information.}}
You will probably also want to check out the {{Git file|gitorious|fg/simgear|next|simgear/nasal|pre=$SG_SRC/}} folder for specific examples on using the various Nasal APIs that are not yet covered here completely.
'''Important:''' {{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 in {{Git file|gitorious|fg/flightgear|next|src/Scripting/NasalSys.cxx|747|pre=$FG_SRC/}}.
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:
<syntaxhighlight lang="ccpp">
// The function signature for an extension function:
typedef naRef (*naCFunction)(naContext ctx, naRef me, int argc, naRef* args);
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 function signature ==
These extension functions look like:
<syntaxhighlight lang="ccpp">
static naRef f_your_function(naContext c, naRef me, int argc, naRef* args)
{
If you don't have anything else to return, you can just return naNil(), so that a function named "f_cool" looks like this:
<syntaxhighlight lang="ccpp">
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
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:
<syntaxhighlight lang="cpp">
// ....
{ "airportinfo", f_airportinfo },
{ 0, 0 }
};
</syntaxhighlight>
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:
<syntaxhighlight lang="phpnasal">
#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:
<syntaxhighlight lang="ccpp">
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
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:
<syntaxhighlight lang="phpnasal">
#cooltest.nas (to be saved in $FG_ROOT/Nasal)
var result=cool();
</syntaxhighlight>
== Argument Processing processing ==
Consider the callback signature:
<syntaxhighlight lang="ccpp">
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
</syntaxhighlight>
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():
<syntaxhighlight lang="ccpp">
// 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
Basically, you can check the type of the reference with the following naIs*() functions:
<syntaxhighlight lang="ccpp">
int naIsNil(naRef r);
int naIsNum(naRef r);
int naIsCCode(naRef r);
</syntaxhighlight>
 
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:
<syntaxhighlight lang="ccpp">
static naRef f_cool (naContext c, naRef me, int argc, naRef* args)
{
You can test this, by running a simple Nasal script like the following:
<syntaxhighlight lang="phpnasal">
# test new cool() function:
print( cool("foo") );
A common way to evaluate parameters passed to Nasal extension functions in C, looks like this:
<syntaxhighlight lang="ccpp">
static naRef f_foo (naContext c, naRef me, int argc, naRef* args) {
naRef param1 = argc > 0 ? args[0] : 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 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:
<syntaxhighlight lang="ccpp">
naRef naNil();
naRef naNum(double num);
</syntaxhighlight>
=== String Manipulation manipulation API ===<syntaxhighlight lang="cpp">
// String utilities:
int naStr_len(naRef s);
naRef naStr_substr(naRef dest, naRef str, int start, int len);
naRef naInternSymbol(naRef sym);
</syntaxhighlight>
 === Vector Manipulation manipulation API ===<syntaxhighlight lang="cpp">
// Vector utilities:
int naVec_size(naRef v);
naRef naVec_removelast(naRef vec);
void naVec_setsize(naRef vec, int sz);
</syntaxhighlight>
 === Hash Manipulation manipulation API ===<syntaxhighlight lang="cpp">
// Hash utilities:
int naHash_size(naRef h);
int naMember_get(naRef obj, naRef field, naRef* out);
int naMember_cget(naRef obj, const char* field, naRef* out);
</syntaxhighlight>
A common way to set up hash field members easily, is using a simple macro like:
<syntaxhighlight lang="Ccpp">
#define HASHSET(s,l,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,l),n)
//... do your hash setup here
The macro can be even further simplified by automatically computing the length of the hash key using strlen() instead of the explicit length argument:
<syntaxhighlight lang="Ccpp">
#define HASHSET(s,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,strlen(s)),n)
//... do your hash setup here
</syntaxhighlight>
== Conversion & Comparison comparison routines ==
Some useful conversion/comparison routines include:
<syntaxhighlight lang="cpp">
int naEqual(naRef a, naRef b);
int naStrEqual(naRef a, naRef b);
naRef naNumValue(naRef n);
naRef naStringValue(naContext c, naRef n);
</syntaxhighlight>
== Wrapping C++ Classes classes as Nasal Objects (objects with cppbind) ==
Please see: {{Git file|gitorious|fg/simgear|next|simgear/nasal/cppbind|pre=$SG_SRC/}}
<!--
Also, the implementation of the SGPropertyNode bindings in [https://gitorious.org/fg/flightgear/blobs/next/src/Scripting/nasal-props.cxx nasal-props.cxx] contains additional examples.
-->
== Passing Pointers 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:
<syntaxhighlight lang="ccpp">
typedef struct naGhostType {
void(*destroy)(void*);
For an example of 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.
== Locking a Nasal context (threading) ==<syntaxhighlight lang="cpp">
// 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
void naModLock();
void naModUnlock();
</syntaxhighlight>
== 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:
<syntaxhighlight lang="cpp">
// "Save" this object in the context, preventing it (and objects
// referenced by it) from being garbage collected.
void naSave(naContext ctx, naRef obj);
</syntaxhighlight>
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:
<syntaxhighlight lang="cpp">
// This mechanism is here to allow naRefs to be passed to
// locations "outside" the interpreter. Normally, such a
int gcSave(naRef r);
void gcRelease(int key);
</syntaxhighlight>
[[Category:Core developer documentation]]
[[Category:Nasal howto|Extend Nasal]]

Navigation menu