Helma Logo
main list history
previous version  overview  next version

Version 9 by hannes on 25. January 2009, 19:51

One of the cornerstones of Helma NG, and one that distinguishes it from every other JavaScript runtime out there, is how it handles modules and scopes.

== Modules

Helma NG handles JavaScript resources as Modules. If this evokes associations to *Python|http://docs.python.org/tut/node8.html* it is fully intentional, because NG Modules work and feel very much like *Python Modules|http://effbot.org/zone/import-confusion.htm*.

If a module wants to make use of another module, it needs to import the other module. Helma NG provides two global functions for this, one to import a module into a discrete namespace, and another to import some or all properties of a module into its own scope.

Now the special thing about Helma NG is that the two modules (one doing the import, the other being imported) are fully isolated from each other. The imported module does not have any way at all to access the importing module, and the importing module can only access the imported one via the namespace into which it imported it, or the properties and functions it explicitly imported from it. And what's more, you don't have to do anything special to accomplish this. You just write scripts with global functions and properties and never worry about stuff that is defined in other scripts.

The way Helma NG does this is by giving each Module its own top-level scope.

== Module Scope

<% this.image 'scopes-ng-small.png' %>

== Module naming

Module names are derived from a module's file name by removing the ".js" extension and replacing the path separators with dots. Thus, a module residing in file helma/http.js can be referred to as 'helma.http'. When a module is loaded, Helma NG defines a property called <code>__name__</code> and sets it to the module name.

Helma NG uses a module path to resolve module names. By default, the module path consists of the application directory and the modules directory in the Helma NG installation. It is possible to add directories and zip archives to the module path through command line arguments to the Helma NG runtime and shell. When asked to import a module, Helma walks along the module path until it finds a module matching the name. If no matching module can be found, an error is thrown.

Usually, Helma modules consist of only one JavaScript file. In some cases, it is preferable to split a single module over multiple files. For these cases, Helma provides a feature to load all the JavaScript files contained in a directory into one single module. Use a module name ending with '.\*' to use this, e.g. <code>'reallybigmodule.\*'</code>.

== importModule()import()

The global <code>importModule()</code> <code>import()</code> function allows to load a module into a namespace of the calling module. By default, the namespace is the same as the module name. For instance, the following statement will load the <code>helma.http</code> module and add it as <code>helma.http</code> to the scope of the calling module.


Optionally, importModule() takes a second argument that allows to override the namespace to place the imported module. The following call will put the imported module into <code>http</code>, dumping the 'helma'== prefix and making it more convenient to access:include()

  importModule(Another way of using the functionality of a module is to add one or more properties or functions from the module to one'helmas own top level scope.http' For example, you could use the following statement to add the Client constructor to your local module'http');s top level scope:

== importFromModule() include('helma.skin');

Another way of using the functionality of a module is to add one or more properties or functions from the module to one's own top level scope. For example, if you only need the Client class from the helma.http module, you could use the following statement to add the Client constructor to your local module's top level scope:

  importFromModule('helma.skin', 'render');

Now the really nice thing about this is that because of JavaScript's static scoping, the render function continues to 'live' in its original scope, which is the one defined by the helma.skin module. It can therefore call all global functions defined in the helma.skin module without using any prefix and without fear of unintentionally calling something defined elsewhere.

If a function imported using importFromModule() include() wants to access properties or functions of its importing scope, it can do by either accepting the scope as an argument to the function, or by using the <code>this</code> keyword, which contains the object the function was called on. In fact, the render() function in module helma.skin uses this to load a skin resource from a location relative to the importing module's file rather than itself.

== Module loading, sharing and caching

By default, Helma NG creates each module once per request. This means that if a module is loaded from several other modules in the same application, you be sure that you deal with the actual same module instance and scope throughout each request, but are isolated from other requests that may be running at the same time.

Sometimes it is desirable to have a module instantiated only once, and shared among all requests. You can achieve this by defining a property called <code>__shared__</code> in your module and giving it the value <code>true</code>:

  var __shared__ = true;

This will result in your module scope to be kept alive across and shared among requests.

== The shared global scope

== The per-thread global scope

== Metaprogramming

Modules automatically recognize functions following the *JSAdapter|http://blogs.sun.com/sundararajan/entry/self_javascript_and_jsadapter* syntax and will invoke these functions for property access if they are defined. For instance, if you define a function called <code>__get__</code> in your module, it will be called to lookup properties. You can access the "raw" properties defined in your modules in your JSAdapter functions.

  var bar = "BAR";
  function __get__(name) {
    if (name == "foobar") {
      return "foo" + bar;
    // leave this away if you don't want to expose "raw" properties
    return this[name];