User:Soubok~enwiki

From Wikipedia, the free encyclopedia

My home page[edit]

Try my web site

My project[edit]

I am working on the jslibs project that is a standalone JavaScript development environment for using JavaScript as a general-purpose scripting language.

jslibs provides a set of native modules that contains various general-purpose classes and functions. Some of these modules are simple wrappers to familiar libraries such as : zlib, SQLite, FastCGI, NSPR (Netscape Portable Runtime), ODE (Open Dynamics Engine), libpng, libjpeg, OpenGL, OpenAL, LibTomCrypt, libffi (Foreign function interface), ... Other modules provide tools to enhance JavaScript programming : Print(), Load(), Exec(), Seal(), Expand(), Buffer class, ... The jslibs distribution comes with a small standalone command-line access program (jshost) that can be used to run JavaScript files.

jslibs is based on Spidermonkey, the Mozilla JavaScript engine.


JavaScript has always been considered as a second-rate language in spite of its powerful features (see Douglas Crockford's article: "The World's Most Misunderstood Programming Language"). These features like closure (computer science), garbage collection (computer science), generator (computer science), ..., 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. 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 and lightweight.


In jslibs a module is the name given to a 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.


  • jsstd : provide basic development tools ( Print, Exec, Buffer class, ... )
  • jsprotex : provides tools to create procedural textures.
  • jswinshell : basic support of Windows shell API ( CreateProcess, Console, Systray, clipboard, MessageBox, ... )
  • jsobjex : extended JavaScript object that support add, del, get, set listener of a property.


  • jsz : support of zlib deflate and inflate.
  • jssqlite : support of SQLite database access.
  • jsfastcgi : support server-side applications using FastCGI communication with the web-server.
  • jsnspr : support of Files and non-blocking TCP/UDP Sockets using the Netscape Portable Runtime (NSPR) library.
  • jsode : support of dynamics 3D calculation using the ode the Open Dynamics Engine.
  • jsimage : support of png and jpeg image format using libpng and libjpeg.
  • jsgraphics : support of fast 3D transformations (using SSE instructions) and 3D drawing using the OpenGL library. See Vision Factory in links section.
  • jsaudio : support of 3D audio using OpenAL library.
  • jscrypt : support of RSA, AES, blowfish, twofish, ... ciphers using Tom St. Denis' LibTomCrypt and LibTomMath.
  • jsffi : support Foreign function interface with libffi.

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

var sqlite = {};
LoadModule.apply(sqlite, 'jssqlite');


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 to load (compile and execute) other scripts. The Exec() function saves to the disk the compiled version of the script (using the External Data Representation XDR IETF standard) to speeds up following loads of the same script.


Using these libraries is quite simple and the modules can be load at run-time, this allows a good modularity of the code.

Deflate a string using zlib:

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

Query the version of a sqlite database file:

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


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>{fcgiParams[k]}</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.


Internaly, jslibs supports two execution modes: safemode and unsafemode. This mode is transmitted to other modules using a global object (_configuration). Each module can use this information to make more tests and assertions in its code. the safe mode is like a run-time debug mode.


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


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