Nasal for C++ programmers: Difference between revisions

Line 14: Line 14:
* providing a full OOP interface (using simgear/nasal/cppbind) (see $FG_SRC/Scripting/NasalPositioned.?xx)
* providing a full OOP interface (using simgear/nasal/cppbind) (see $FG_SRC/Scripting/NasalPositioned.?xx)


Even if you should know C or C++ already, Nasal probably remains the most accessible and the most powerful method for customizing the simulator in the beginning, simply because it is extremely easy and fast to get started, you don't need an "integrated development environment", you don't need to install compilers and you don't need to satisfy any 3rd party dependencies; bottom line being: if you can run FlightGear, you can also run Nasal and directly create new code.
Even if you should know C or C++ already, Nasal probably remains the most accessible and the most powerful method for customizing the simulator in the beginning, simply because it is extremely easy and fast to get started, you don't need an "integrated development environment", you don't need to install compilers and you don't need to satisfy any 3rd party dependencies; bottom line being: if you can run FlightGear, you can also run Nasal and directly create new features.


In addition, Nasal code is fairly abstract code, too. Once you start looking at some existing Nasal scripts, you will see that it is also fairly high level code, much more high level than C++ - so Nasal has a much higher density of code, too. Nasal's role in FlightGear really is like JavaScript's role in Firefox, where it is also used for many/most core-related logics (CSS/XUL).  
In addition, Nasal code is fairly abstract code, too. Once you start looking at some existing Nasal scripts, you will see that it is also fairly high level code, much more high level than C++ or C- so Nasal has a much higher density of code, too. Nasal's role in FlightGear really is like JavaScript's role in Firefox or Chrome, where it is also used for many/most core-related logics (CSS/XUL).  


Nasal is an extension language, a scripting language that is much more high level than C++ and specifically meant to be used for extending a host application (FlightGear). C++ on the other hand is a systems programming language (most APIs in Nasal are FlightGear specific).
Nasal is an extension language, a scripting language that is much more high level than C++ and specifically meant to be used for extending a host application (FlightGear). C++ on the other hand is a systems programming language (most APIs in Nasal are FlightGear specific).
Line 22: Line 22:
Nasal's power is due to the fact that it is not as low level as C++ code, and that it is very easy for non-programmers to get started adding custom features to FlightGear, without requiring a background in software development or programming.  
Nasal's power is due to the fact that it is not as low level as C++ code, and that it is very easy for non-programmers to get started adding custom features to FlightGear, without requiring a background in software development or programming.  


Aircraft developers or other base package contributors don't need to be familiar with C++ programming, they don't even need an IDE or a working build environment. They can just program scripts in a text editor and drop them into the base package, and things will simply work. That is because FlightGear is the runtime environment for Nasal scripts.
Aircraft developers or other base package contributors don't need to be familiar with C++ programming, they don't even need an IDE or a working build environment. They can just write scripts in a text editor and drop them into the base package, and things will simply work. That is because FlightGear is the runtime environment for Nasal scripts.


Obviously, an experienced programmer can make use of more complex Nasal features.
Obviously, an experienced programmer can make use of more complex Nasal features, and come up with much more optimized code and sophisticated features.


Basically, there really is nothing in either language that you cannot also do in the other language - ever heard of "turing completeness"?
Basically, there really is nothing in either language that you cannot also do in the other language - ever heard of "turing completeness"?


Of course, some things are better handled in C++ space, while others are better (=more easily) done in Nasal.
Of course, some things are better handled in C++ space, while others are better (=more easily) done in Nasal, usually you'll want to implement performance-critical stuff in C/C++ and then make it accessible to scripting space.


It really boils down to what needs to be done, usually new features can be implemented (or at least prototyped) with just some Nasal code, sometimes new Nasal functions may be required to interface to the rest of FlightGear.
It really boils down to what needs to be done, usually new features can be implemented (or at least prototyped) with just some Nasal code, sometimes new Nasal functions may be required to interface to the rest of FlightGear.
Line 34: Line 34:
=== Some words on plugins ===
=== Some words on plugins ===


Experienced C++ developers may be looking for an SDK to create plugins for FlightGear. This is a discussion that has been brought up frequently by new FlightGear users who would like to create new modules for FlightGear.
Experienced C++ developers may be looking for an SDK to create plugins for FlightGear. This is a discussion that has been brought up frequently by new FlightGear users who would like to create new features/modules for FlightGear.


FlightGear doesn't have such an SDK, and the general consensus is that FlightGear doesn't need an SDK for creating binary plugins. FlightGear is an open source flight simulator, developers are invited to directly contribute to FlightGear itself.
FlightGear doesn't have such an SDK, and the general consensus is that FlightGear doesn't need an SDK for creating binary plugins. FlightGear is an open source flight simulator, developers are invited to directly contribute to FlightGear itself.
Line 43: Line 43:


Not to mention, that plugin developers need to be able to build/compile their modules before using them.
Not to mention, that plugin developers need to be able to build/compile their modules before using them.
Nasal scripts on the other hand "just work" by dropping them into the base package folder.
Nasal scripts on the other hand "just work" by dropping them into the base package folder, in its current form, there's nothing platform-specific about Nasal code - if it runs on one machine, it will also run another - regardless of your CPU, GPU or operating system.


Instead, FlightGear is usually extended by either using its support for Nasal scripting or by adding new C++ code to the source tree, or a combination of both approaches (so that Nasal scripts can call C++ code).
Instead, FlightGear is usually extended by either using its support for Nasal scripting or by adding new C++ code to the source tree, or a combination of both approaches (so that Nasal scripts can call C++ code).
Line 81: Line 81:


As far as speed goes, the last time any benchmarking for Nasal was done, it was about as fast as Perl 5 or Python 2.2 at most things.  It's garbage collector was faster, its symbol lookup about the same or slightly faster, and its bytecode interpreter somewhat slower.
As far as speed goes, the last time any benchmarking for Nasal was done, it was about as fast as Perl 5 or Python 2.2 at most things.  It's garbage collector was faster, its symbol lookup about the same or slightly faster, and its bytecode interpreter somewhat slower.
i


Meanwhile, the GC has been identified as a potential bottleneck in FlightGear, see [[How the Nasal GC works]] for details.
=== Thread safety ===
=== Thread safety ===
Unlike almost all other script interpreters (and unlike the FlightGear/Nasal interface itself currently) , Nasal is thread safe and scalable when called from multiple CPU threads (as opposed to the user space interpreter threads implemented by Ruby for example).  
Unlike almost all other script interpreters (and unlike the FlightGear/Nasal interface itself currently) , Nasal is thread-safe and scalable when called from multiple CPU threads (as opposed to the user space interpreter threads implemented by Ruby or the GIL used by Python for example).  


No special treatment is required (as for perl, which clones a separate interpreter with separate data for each thread and uses locking around specifically-designated shared data) and the threads can be scheduled simultaneously. There is no global lock on the interpreter, as used by Python or Lua. The only limit on scalability is garbage collection, which must block all interpreter threads before running.  
No special treatment is required (as for perl, which clones a separate interpreter with separate data for each thread and uses locking around specifically-designated shared data) and the threads can be scheduled simultaneously. There is no global lock on the interpreter, as used by Python or Lua. The only limit on scalability is garbage collection, which must block all interpreter threads before running.