What's new? | Help | Directory | Sign in
Google
jslibs
standalone Javascript development environment with general purpose native libraries.
  
  
  
  
    
Search
for
Updated Dec 10, 2007 by soubok
Labels: Featured
About  

Purpose

JavaScript has always been considered as a second-rate language in spite of its powerful features. These features like exceptions, closures, garbage collection, generator, ..., can make JavaScript an efficient and modern general-purpose scripting language like python and ruby.

The main issue is that there are no or only few ways to execute JavaScript code outside the web browser. jslibs is based on Spidermonkey, the mozilla's JavaScript engine. Usually SpiderMonkey is used to enable scripting capabilities of existing projects but not for a standalone development environment.

jslibs is composed of a set of general-purpose libraries and a host to execute JavaScript source files.

The aim of jslibs is to be simple to use, fast, safe and lightweight.

Modules

In jslibs a module is the name given to a lynalicaly linked library that content set of related classes an functions. A module can be a simple wrapper to an existing library.

Modules are loaded using the LoadModule() function witch is the only function that is provided by the script host (jshost and jswinhost).

jslibs modules

Wrappers to third party libraries

By default, modules are loaded in the global namespace, however, it is possible to load a module in a custom namespace: Load a module in the global namespace (default):

LoadModule('jssqlite');
...
var db = new Database('myDatabase');

Load a module in a custom namespace:

var sqlite = {}; // create the namespace
LoadModule.apply(sqlite, 'jssqlite'); // load the module inside the namespace
...
var db = new sqlite.Database('myDatabase');

Standard module

SpiderMonkey is only a language library, and, for example, it does not provide any standard input or output access. To display something on the screen, at least a Print function is needed.

jsstd module provide a minimal set of basic programing functions like Print(), Expand(), Seal(), ... .

Another important basic function is Exec() that allows you to load (compile and execute) other scripts. The Exec() function saves the compiled version of the script to the disk, to speeds up following loads of the same script. The format used to store the compiled script version is XDR (External Data Representation XDR IETF standard)

Simplicity of use

Using these libraries is very simple and the modules can be load at run-time, this allows a good modularity of the code. Each module is loaded only once even if you call LoadModule several times with the same argument.

Deflate a string using zlib:

LoadModule('jsz');
deflatedText = new Z(Z.DEFLATE)('This text will be deflated', true);

Query the version of a sqlite database file:

LoadModule('jssalite');
myDatabaseVersion = new Database('myDatabase').Exec('PRAGMA user_version');

Server-Side scripting

One interesting feature of jslibs is its server-side scripting capability.

Using jshost as a FastCGI program allow server-side applications development. Because jshost is a generic script host, it needs a FastCGI support script to run FastCGI programs:

LoadModule('jsfastcgi');
while ( Accept() >= 0 )
    try {
        Exec(GetParam('SCRIPT_FILENAME'));
    } catch(ex) {
        var errorMessage = ex.name + ': ' + ex.message + ' (' + ex.fileName + ':' + ex.lineNumber + ')';
        Log( errorMessage );
        Write( 'Status: 500\r\n\r\n' + errorMessage );
    }

Then, server-side programs looks like:

Write( "Content-type: text/html\r\n\r\n" );
function CGIVariableList() {
    var fcgiParams = GetParam();
    var list = <ul/>;
    for ( var k in fcgiParams )
        list += <li><b>{k} = </b><pre>{fcgiParamsk}</pre></li>;
    return list;
}

Write(
<html>
    <head><title>teswt</title></head>
    <body>
        <H1>HELLO WORLD</H1>
        <p>CGI/1.1 variables:</p> {CGIVariableList()}
    </body>
</html>
);

Note that like in the previous example, the use of E4X can make HTML page easy to generate.

Configuration

Each module can use the global configuration object (configuration) to get some informations about the execution environment.

This object is optional an neither module nor host should rely on it.

Safe mode

Internaly, jslibs supports two execution modes: safe mode and unsafe mode. This mode is transmitted to other modules using the configuration object.

Modules can use this information to make more tests and assertions in its code. The safe mode is like a run-time debug mode.

Standard output and error

The global configuration object also stores two javascript functions to write, output messages and error messages. By default, jshost bind these functions do stdout and stderr, and jswinhost do not define them. These functions can be overrided by redefining them. eg.

LoadModule('jswinshell');
_configuration.stderr = MessageBox;
LoadModule('jsstd');

Global object

The global namespace object is also defined with 'global'

Using jslibs in a third-party application

(TBD)

supported platforms

Currently, the supported platforms are Windows and Linux, and MacOSX soon.

License

jslibs is an open-source project under the GNU GPL2 license.

External Links


Sign in to add a comment