Difference between revisions of "Modules.nas"

From FlightGear wiki
Jump to: navigation, search
m (modules.setDebug())
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Nasal runtime loadable modules =
+
= Nasal runtime re-loadable modules =
 
+
 
== Introduction and motivation ==
 
== Introduction and motivation ==
 
Nasal (Flightgears integrated scripting language) comes with a limited number of [[Nasal library|core functions]].  
 
Nasal (Flightgears integrated scripting language) comes with a limited number of [[Nasal library|core functions]].  
Line 6: Line 5:
  
 
The latter are stored in the [[Fgdata]] repository under /Nasal and loaded automatically by FlightGear.
 
The latter are stored in the [[Fgdata]] repository under /Nasal and loaded automatically by FlightGear.
For more information on how and when these files are loaded see [[Nasal Initialization]].
+
Optional code, which is loaded only on demand, can be handled either by the legacy module system implemented in C++ or by modules.nas if run-time re-load support is required.
  
To avoid loading files needlessly modules.nas was written to allow run-time (re-)loadable Nasal modules, that are loaded on demand only.
+
For more information on how and when these files are loaded see [[Nasal Initialization]].
  
== modules.nas ==
+
== Differences between add-ons and modules ==
In ''modules.nas'' the class ''Module'' is defined.
+
A module object holds information about the path and filename of the Nasal script and supports unloading and reloading the code at runtime
+
(e.g. without restarting Flightgear as a whole) by tracking some critical resources like [[Listeners|listeners]] and [[Timers|timers]].
+
 
+
Parts of this functionality were added to the [[Addons|addons]] manager earlier and have now been extracted to avoid code duplication.
+
 
+
=== Differences between add-ons and modules ===
+
 
While there are many similarities between add-ons and modules, some words on the differences:  
 
While there are many similarities between add-ons and modules, some words on the differences:  
  
Line 26: Line 18:
 
Add-ons are selected by the user before launching FlightGear, thus they may or may not be available at runtime.
 
Add-ons are selected by the user before launching FlightGear, thus they may or may not be available at runtime.
  
== Nasal modules ==
 
Loadable modules are stored in subdirectories of FGDATA/Nasal/modules/ as these subdirectories will not be loaded automatically by Flightgear on start.
 
However, modules.nas will scan this directory to create a list of available modules (= list of subdirectories).
 
Each module correspondes to one subdirectory and the direcory name is also the module name.
 
  
A module must contain at least the file '''main.nas''' (default, other filename is possible)  
+
= HowTo / Examples =
 +
First some examples, the API documentation follows below.
 +
 
 +
== Nasal modules in an aircraft (e.g. for development) ==
 +
Nasal code for an aircraft is usually loaded by a XML declaration in the <aircraft>-set.xml file like this:
 +
<syntaxhighlight lang="xml">
 +
<PropertyList>
 +
    <nasal>
 +
        <foo> <!-- Nasal namespace foo -->
 +
            <file>Nasal/foo.nas</file>
 +
        </foo>
 +
        <bar> <!-- Nasal namespace bar -->
 +
            <file>Aircraft/Generic/foo.nas</file>
 +
        </bar>
 +
        <myAircraft> <!-- Nasal namespace myAircraft-->
 +
            <file>Nasal/efis_module.nas</file>
 +
        </myAircraft>
 +
    </nasal>
 +
</PropertyList>
 +
</syntaxhighlight>
 +
 
 +
This is fine unless you are developing a Nasal subsystem for the aircraft and have to restart FlightGear every time you edit a few lines.
 +
For this case you could consider to use the Module class (defined in FGDATA/Nasal/modules.nas) and make your code re-loadable at runtime,
 +
which means you do not have to restart whole FlightGear just for a few lines of edited Nasal code.
 +
 
 +
'''Example Nasal/efis_module.nas:'''
 +
<syntaxhighlight lang="nasal">
 +
#-- load EFIS as reloadable module
 +
var my_efis = modules.Module.new("myAircraft_EFIS"); # Module name
 +
my_efis.setDebug(1); # 0=(mostly) silent; 1=print setlistener and maketimer calls to console; 2=print also each listener hit, be very careful with this!
 +
my_efis.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal/EFIS");
 +
my_efis.setMainFile("myAircraft-efis.nas");
 +
my_efis.load();
 +
</syntaxhighlight>
 +
 
 +
Now add a menu item for easy reloading like this:
 +
{{note|
 +
The module name in the property path must match the name passed to modules.Module.new()
 +
|margin=10px |width=50%}}
 +
<syntaxhighlight lang="xml">
 +
<menubar>
 +
    <default>
 +
        <menu n=100>
 +
            <!-- normal aircraft menu -->
 +
        </menu>
 +
        <menu n=101>
 +
            <label>Aircraft Development</label>
 +
            <item>
 +
                <label>Reload EFIS</label>
 +
                <binding>
 +
                    <command>property-assign</command>
 +
                    <property>/nasal/modules/myAircraft_EFIS/reload</property>
 +
                    <value>1</value>
 +
                </binding>
 +
            </item>
 +
        </menu>
 +
    </default>
 +
</menubar>
 +
</syntaxhighlight>
 +
 
 +
{{tip|
 +
You can also call {{code| myAircraft.my_efis.reload();}} to trigger the reload. myAircraft is the namespace given in the <aircraft>-set.xml file, my_efis is the module object (see above).
 +
|margin=10px |width=50%}}
 +
 
 +
== Nasal frameworks ==
 +
Another usecase for Modules.nas is frameworks that need reload support.
 +
A framework usually provides more or less generic functionality that has to be adapted (configured) for a specific usecase (e.g. a specific aircraft).
 +
Reload support may become very convenient, if the customizing based on such framework involves many iterations of edit-reload-test.
 +
 
 +
First living example is the canvas_efis framework which can be included into an aircraft with a single line of code:
 +
 
 +
{{code|
 +
<nowiki>var efis_module = modules.load("canvas_efis");</nowiki>
 +
}}
 +
 
 +
(see also last chapter at the end of this article)
 +
 
 +
= Structure of Nasal reloadable modules =
 +
A module must contain at least the file '''main.nas''' (default, other filename is possible) and should contain the functions {{code|main()}} and {{code|unload()}}.
  
 
=== main() function ===
 
=== main() function ===
The main.nas file shall contain a function '''main''' that will be called on load with either the module object as parameter or the parameters passed to the module.load() function.
+
The main.nas file shall contain a function {{code|main()}} that will be called on load with either the module object as parameter or the parameters passed to the {{code|module.load()}} function.
  
 
=== unload() function ===
 
=== unload() function ===
 
If you want the module to be re-/un-loadable, you must make sure to track resources and remove them on onload.
 
If you want the module to be re-/un-loadable, you must make sure to track resources and remove them on onload.
For this the main.nas shall contain a function '''unload''' that removes any resources which were created by the module.
+
For this the main.nas shall contain a function {{code|unload()}} that removes any resources which were created by the module.
Exception: setlistener and maketimer are automatically tracked by Module class for you, timers will be stopped automatically, listeners will be removed automatically on unload().
+
  
Example: any canvas created by the module should have called its del() method here.
+
'''Example:''' any canvas created by the module should have called its {{code|del()}} method here.
 +
 
 +
{{note|Calls to {{code|setlistener()}} and {{code|maketimer()}} are automatically tracked by the Module class for you, timers will be stopped automatically, listeners will be removed automatically on {{code|unload()}}.}}
  
 
=== Rules for Module names ===
 
=== Rules for Module names ===
Line 47: Line 114:
 
# The name must contain at least one letter so it cannot be confused with a number
 
# The name must contain at least one letter so it cannot be confused with a number
 
# The name shall not match any existing .nas file or directory in FGDATA/Nasal to avoid namespace clashes
 
# The name shall not match any existing .nas file or directory in FGDATA/Nasal to avoid namespace clashes
 +
 +
{{note|
 +
Reloadable modules shipped with FlightGear are stored in subdirectories of FGDATA/Nasal/modules/ as these subdirectories will not be loaded automatically by Flightgear on start.
 +
However, modules.nas will scan this directory to create a list of available modules (list of subdirectories).
 +
Each module correspondes to one subdirectory and the direcory name is also the module name.
 +
}}
 +
 +
= modules.nas =
 +
In ''modules.nas'' the class ''Module'' is defined.
 +
A module object holds information about the path and filename of the Nasal script and supports unloading and reloading the code at runtime
 +
(e.g. without restarting Flightgear as a whole) by tracking some critical resources like [[Listeners|listeners]] and [[Timers|timers]].
 +
 +
{{note|Parts of this functionality were added to the [[Addons|addons]] manager earlier and have now been extracted to avoid code duplication.}}
 +
 +
{{caution| Within a module {{code|setlistener()}} is overloaded and the default value for the 4th argument (runtime) is changed to 0, so the listener will run only if the property value has changed.
 +
}}
  
 
== library functions ==
 
== library functions ==
Line 119: Line 202:
 
}}
 
}}
  
 +
=== setDebug() ===
 +
{{Nasal doc
 +
|syntax = mymod.setDebug(debug=1);
 +
|text = Activate debugging for this module. '''Must be called before calling load()!'''
 +
|param1 = debug
 +
|param1text =
 +
0: no debugging;
 +
1 (default if no argument given): print calls to redirected {{code|setlister()}} and {{code|maketimer()}};
 +
 +
2: listeners print property path when hit (Use with caution! Do not call {{code|setlistener()}} with runtime=1.)
 +
}}
  
 
=== load() ===
 
=== load() ===
Line 151: Line 245:
 
}}
 
}}
  
== HowTo / Examples ==
+
= Property tree interface for modules.nas =
 +
In the property tree there is a subtree /nasal to control modules and get some statistics.
 +
The properties available depend on the type of module ("load-once" or "reloadable", see [[Nasal Initialization]] for more information on the differences).
  
=== Nasal modules in an aircraft (e.g. for development) ===
+
== Reloadable modules / frameworks ==
Nasal code for an aircraft is usually loaded by a XML declaration in the <aircraft>-set.xml file like this:
+
Modules handled by modules.nas will have their properties in /nasal/modules/<moduleName> where <moduleName> is given by the developer when calling either
<syntaxhighlight lang="xml">
+
{{code|Module.new("<moduleName>")}} or {{code|modules.load("<moduleName>")}}.
<PropertyList>
+
In the latter case <moduleName> specifies the subdirectory FGDATA/Nasal/modules/<moduleName> in which some framework is stored.
    <nasal>
+
        <foo> <!-- Nasal namespace foo -->
+
            <file>Nasal/foo.nas</file>
+
        </foo>
+
        <bar> <!-- Nasal namespace bar -->
+
            <file>Aircraft/Generic/foo.nas</file>
+
        </bar>
+
        <myAircraft>
+
            <file>Nasal/efis_module.nas</file>
+
        </myAircraft>
+
    </nasal>
+
</PropertyList>
+
</syntaxhighlight>
+
  
This is fine unless you are developing a Nasal subsystem for the aircraft and have to restart FlightGear every time you edit a few lines.
+
{| class="wikitable"
For this case you could consider to use the Module class (defined in FGDATA/Nasal/modules.nas) and make your code re-loadable at runtime,
+
|-
which means you do not have to restart whole FlightGear just for a few lines of edited Nasal code.
+
! property !! type !! content
 +
|-
 +
| loaded || bool || true if module was loaded without errors
 +
|-
 +
| reload || bool || set this to true to trigger reload
 +
|-
 +
| listeners || int || Number of tracked listeners
 +
|-
 +
| listener-hits || int || If debugging is enabled, this prop shows the total number of hits to all tracked listeners.
 +
|-
 +
| timers || int || Number of tracked timers (maketimer).
 +
|}
  
'''Example Nasal/efis_module.nas:'''
+
== Legacy load-once modules ==
<syntaxhighlight lang="nasal">
+
A legacy load-once module is a direct (1st level) subdirectory of FGDATA/Nasal/ and its corresponding property tree is /nasal/<moduleName>/ where <moduleName> equals the name of the subdirectory.
#-- load EFIS as reloadable module
+
It is handled by C++ code and must have a corresponding entry in FGDATA/defaults.xml which defines a property "enabled" and optionally a property "module".
var my_efis = modules.Module.new("myAircraft_EFIS"); # Module name
+
If enabled is set to false in defaults.xml, the C++ code will setup a listener and load the module as soon as enabled is set to true.
my_efis.setDebug(1); # 0=(mostly) silent; 1=print setlistener and maketimer calls to console; 2=print also each listener hit, be very careful with this!
+
The property name "module" is a bit misleading, it is used to define into which namespace the files shall be loaded.
my_efis.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal/EFIS");
+
my_efis.setMainFile("myAircraft-efis.nas");
+
my_efis.load();
+
</syntaxhighlight>
+
  
Now add a menu item for easy reloading like this:
+
For each .nas file in the subdirectory a file[i] property is created holding the full path+filename.
{{note|
+
The module name in the property path must match the name passed to modules.Module.new()
+
|margin=10px |width=50%}}
+
<syntaxhighlight lang="xml">
+
<menubar>
+
    <default>
+
        <menu n=100>
+
            <!-- normal aircraft menu -->
+
        </menu>
+
        <menu n=101>
+
            <label>Aircraft Development</label>
+
            <item>
+
                <label>Reload EFIS</label>
+
                <binding>
+
                    <command>property-assign</command>
+
                    <property>/nasal/modules/myAircraft_EFIS/reload</property>
+
                    <value>1</value>
+
                </binding>
+
            </item>
+
        </menu>
+
    </default>
+
</menubar>
+
</syntaxhighlight>
+
  
{{tip|
+
The bool "loaded" property shows the status of the module.
You can also call {{code| myAircraft.my_efis.reload();}} to trigger the reload. myAircraft is the namespace given in the -set.xml file, my_efis is the module object (see above).
+
 
|margin=10px |width=50%}}
+
= Existing modules with reload support =
 +
Stable Nasal frameworks which support reloading can be added to ''FGDATA/Nasal/modules/<module_name>''.  
 +
This allows an aircraft developer to configure the framework for a specific aircraft and make use of the reload magic while developing the configuration.
  
== Existing modules ==
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
 
! Module name !! Desctiption !! time added  
 
! Module name !! Desctiption !! time added  
 
|-
 
|-
| canvas_efis || framework to manage canvas based EFIS screens || 01/2020
+
| [[Canvas EFIS Framework|canvas_efis]] || framework to manage canvas based EFIS screens || 02/2020
|-
+
| canvas_draw || library of drawing functions for canvas || 01/2020
+
 
|}
 
|}

Latest revision as of 13:53, 27 February 2020

Nasal runtime re-loadable modules

Introduction and motivation

Nasal (Flightgears integrated scripting language) comes with a limited number of core functions. The language was extended by C++ functions, as well as libraries written in Nasal (e.g. props, io, math, debug ...).

The latter are stored in the Fgdata repository under /Nasal and loaded automatically by FlightGear. Optional code, which is loaded only on demand, can be handled either by the legacy module system implemented in C++ or by modules.nas if run-time re-load support is required.

For more information on how and when these files are loaded see Nasal Initialization.

Differences between add-ons and modules

While there are many similarities between add-ons and modules, some words on the differences:

Modules are distributed with FlightGear as part of Fgdata. Add-ons have to be downloaded separately by a FlightGear user from wherever the author of the add-on publishes the add-on.

Modules can be loaded e.g. by an aircraft if the aircraft developer wants to make use of the module. Add-ons are selected by the user before launching FlightGear, thus they may or may not be available at runtime.


HowTo / Examples

First some examples, the API documentation follows below.

Nasal modules in an aircraft (e.g. for development)

Nasal code for an aircraft is usually loaded by a XML declaration in the <aircraft>-set.xml file like this:

<PropertyList>
    <nasal>
        <foo> <!-- Nasal namespace foo -->
            <file>Nasal/foo.nas</file>
        </foo>
        <bar> <!-- Nasal namespace bar -->
            <file>Aircraft/Generic/foo.nas</file>
        </bar>
        <myAircraft> <!-- Nasal namespace myAircraft-->
            <file>Nasal/efis_module.nas</file>
        </myAircraft>
    </nasal>
</PropertyList>

This is fine unless you are developing a Nasal subsystem for the aircraft and have to restart FlightGear every time you edit a few lines. For this case you could consider to use the Module class (defined in FGDATA/Nasal/modules.nas) and make your code re-loadable at runtime, which means you do not have to restart whole FlightGear just for a few lines of edited Nasal code.

Example Nasal/efis_module.nas:

#-- load EFIS as reloadable module
var my_efis = modules.Module.new("myAircraft_EFIS"); # Module name
my_efis.setDebug(1); # 0=(mostly) silent; 1=print setlistener and maketimer calls to console; 2=print also each listener hit, be very careful with this! 
my_efis.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal/EFIS");
my_efis.setMainFile("myAircraft-efis.nas");
my_efis.load();

Now add a menu item for easy reloading like this:

Note

The module name in the property path must match the name passed to modules.Module.new()

<menubar>
    <default>
        <menu n=100>
            <!-- normal aircraft menu -->
        </menu>
        <menu n=101>
            <label>Aircraft Development</label>
            <item>
                <label>Reload EFIS</label>
                <binding>
                    <command>property-assign</command>
                    <property>/nasal/modules/myAircraft_EFIS/reload</property>
                    <value>1</value>
                </binding>
            </item>
        </menu>
    </default>
</menubar>
Tip

You can also call myAircraft.my_efis.reload(); to trigger the reload. myAircraft is the namespace given in the <aircraft>-set.xml file, my_efis is the module object (see above).

Nasal frameworks

Another usecase for Modules.nas is frameworks that need reload support. A framework usually provides more or less generic functionality that has to be adapted (configured) for a specific usecase (e.g. a specific aircraft). Reload support may become very convenient, if the customizing based on such framework involves many iterations of edit-reload-test.

First living example is the canvas_efis framework which can be included into an aircraft with a single line of code:

var efis_module = modules.load("canvas_efis");

(see also last chapter at the end of this article)

Structure of Nasal reloadable modules

A module must contain at least the file main.nas (default, other filename is possible) and should contain the functions main() and unload().

main() function

The main.nas file shall contain a function main() that will be called on load with either the module object as parameter or the parameters passed to the module.load() function.

unload() function

If you want the module to be re-/un-loadable, you must make sure to track resources and remove them on onload. For this the main.nas shall contain a function unload() that removes any resources which were created by the module.

Example: any canvas created by the module should have called its del() method here.

Note  Calls to setlistener() and maketimer() are automatically tracked by the Module class for you, timers will be stopped automatically, listeners will be removed automatically on unload().

Rules for Module names

  1. Module names shall contain only letters (a-z, A-Z), numbers (0-9) and underscores ('_').
  2. The name must contain at least one letter so it cannot be confused with a number
  3. The name shall not match any existing .nas file or directory in FGDATA/Nasal to avoid namespace clashes
Note

Reloadable modules shipped with FlightGear are stored in subdirectories of FGDATA/Nasal/modules/ as these subdirectories will not be loaded automatically by Flightgear on start. However, modules.nas will scan this directory to create a list of available modules (list of subdirectories). Each module correspondes to one subdirectory and the direcory name is also the module name.

modules.nas

In modules.nas the class Module is defined. A module object holds information about the path and filename of the Nasal script and supports unloading and reloading the code at runtime (e.g. without restarting Flightgear as a whole) by tracking some critical resources like listeners and timers.

Note  Parts of this functionality were added to the addons manager earlier and have now been extracted to avoid code duplication.
Caution  Within a module setlistener() is overloaded and the default value for the 4th argument (runtime) is changed to 0, so the listener will run only if the property value has changed.

library functions

modules.isAvailable()

modules.isAvailable(module_name);

This function returns true, if there is a module (subdirectory) in FGDATA/Nasal/modules/ with the given name.

module_name
The name of the module (=subdirectory in Nasal/modules) to load.

Example

if (modules.isAvailable("foo_bar")) {
    modules.load("foo_bar");
}

modules.setDebug()

modules.setDebug(module_name, [debug=1]);

This function enables debugging for a module. It must be called before load()!

module_name
The name of the module (=subdirectory in Nasal/modules) to load.
debug
Defaults to 1 (true), use 0 to disable debug. If debug > 1, each listener hit will be printed, be careful not to listen to rapidly changing props, do not set runtime=1 in setlistener().

Example

var debug = 1;
modules.setDebug("foo_bar", debug);

modules.load()

modules.load(module_name, [namespace_name]);

This function attempts to load a module from FGDATA/Nasal/modules/

module_name
The name of the module (=subdirectory in Nasal/modules) to load.
namespace_name
Optional, load module to a different namespace.

Example

modules.load("foo_bar");

Methods of class Module

setFilePath()

mymod.setFilePath(path);

Configure where to look for the main file, e.g. aircraft (sub-)directory.

path
File path where the module is stored.

setMainFile()

mymod.setMainFile(filename);

Configure the nasal file to load.

filename
File that will be loaded by load().

Example

# if you develop a new nasal system for your aircraft, it might be handy to implement it as module
# so you can reload the file quickly without restarting FlightGear
var my_foo_sys = modules.Module.new("my_aircraft_foo");
my_foo_sys.setDebug(1);
my_foo_sys.setFilePath(getprop("/sim/aircraft-dir")~"/Nasal");
my_foo_sys.setMainFile("foo.nas");
my_foo_sys.load();

setNamespace()

mymod.setNamespace(namespace);

Configure the Nasal namespace to use. Be really carefull when using existing namespaces! unload() or reload() will destroy them!

namespace
The Nasal namespace the module code will be loaded into.

setDebug()

mymod.setDebug(debug=1);

Activate debugging for this module. Must be called before calling load()!

debug
0: no debugging;

1 (default if no argument given): print calls to redirected setlister() and maketimer();

2: listeners print property path when hit (Use with caution! Do not call setlistener() with runtime=1.)

load()

mymod.load([args]);

This function attempts to load the module into its namespace.

optional args
Arguments are passed to the main() function of the module. If empty, the module object will be passed to main().

unload()

mymod.unload();

This function attempts to remove tracked resources and remove the module by killing its namespace.

reload()

mymod.reload();

Shorthand, calls unload() and load().

get()

mymod.get(var_name);

Returns a variable from modules namespace.

var_name
The variable to get.

Example

var foo = modules.load("foo");
var bar = foo.get("bar"); # get variable "bar" defined in FGDATA/Nasal/modules/foo/main.nas (or a file included by this file)

Property tree interface for modules.nas

In the property tree there is a subtree /nasal to control modules and get some statistics. The properties available depend on the type of module ("load-once" or "reloadable", see Nasal Initialization for more information on the differences).

Reloadable modules / frameworks

Modules handled by modules.nas will have their properties in /nasal/modules/<moduleName> where <moduleName> is given by the developer when calling either Module.new("<moduleName>") or modules.load("<moduleName>"). In the latter case <moduleName> specifies the subdirectory FGDATA/Nasal/modules/<moduleName> in which some framework is stored.

property type content
loaded bool true if module was loaded without errors
reload bool set this to true to trigger reload
listeners int Number of tracked listeners
listener-hits int If debugging is enabled, this prop shows the total number of hits to all tracked listeners.
timers int Number of tracked timers (maketimer).

Legacy load-once modules

A legacy load-once module is a direct (1st level) subdirectory of FGDATA/Nasal/ and its corresponding property tree is /nasal/<moduleName>/ where <moduleName> equals the name of the subdirectory. It is handled by C++ code and must have a corresponding entry in FGDATA/defaults.xml which defines a property "enabled" and optionally a property "module". If enabled is set to false in defaults.xml, the C++ code will setup a listener and load the module as soon as enabled is set to true. The property name "module" is a bit misleading, it is used to define into which namespace the files shall be loaded.

For each .nas file in the subdirectory a file[i] property is created holding the full path+filename.

The bool "loaded" property shows the status of the module.

Existing modules with reload support

Stable Nasal frameworks which support reloading can be added to FGDATA/Nasal/modules/<module_name>. This allows an aircraft developer to configure the framework for a specific aircraft and make use of the reload magic while developing the configuration.

Module name Desctiption time added
canvas_efis framework to manage canvas based EFIS screens 02/2020