Julien Pierru wrote:
> I am trying to add a function to nasal, I have tried to look at the
> files indicated by the nasal website, but I can't seem to figure out
> how exactly I am supposed to go about it.

A C/C++ extension function you mean?  The simplest way to do it is to
look at the existing functions in src/Scripting/NasalSys.cxx.  There
is a static table of function pointers at the bottom of the file
referencing extension functions that look like:

  static naRef f_your_function(naContext c, naRef me, int argc, naRef* args)
  {
      // ...
  }

The arguments to the function are passed in in the args array.  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).

The naRef objects can be manipulated using the functions in nasal.h.
Basically, you can check the type of the reference with the naIs*()
functions, and then get the appropriate value out with things like
naNumValue(), naStr_data(), etc...

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.  If you don't have anything else to
return, you can just return naNil().

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*, which has a callback that you need to implement
to deal with what happens when the Nasal interpreter garbage collects
your object.

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
(naSave()), but it doesn't have a corrresponding call to release a
saved object.  Just don't do this, basically.  Always keep your Nasal
data in Nasal space.

Andy

* That started as an acronym for "Garbage-collectable Handle to an
  OutSide Thing", but then I decided it was dumb. :)


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to