Project Information
Members
Featured
Downloads
Wiki pages
Links
|
Jslibs is a standalone JavaScript development runtime environment for using JavaScript as a general-purpose scripting language. jslibs uses SpiderMonkey library that is Gecko's JavaScript engine (SpiderMonkey is used in various Mozilla products, including Firefox). The latest version of jslibs uses the new TraceMonkey/JaegerMonkey library with tracing/method JIT enabled. jslibs provides a set of native modules that contains various general purpose classes and functions. Some of these modules are simple wrappers for familiar libraries such as: zlib, SQLite, FastCGI, NSPR (Netscape Portable Runtime) , ODE (Open Dynamics Engine) , libpng, libjpeg, librsvg, SDL, libiconv, OpenGL, OpenAL, ogg vorbis, libTomCrypt, libffi (Foreign function interface) , ... Other modules provide tools for enhancing JavaScript programming : Print(), Load(), Exec(), Seal(), Expand(), Buffer class, Blob class, Sandbox class, ... The jslibs distribution comes with a small standalone command line access program (jshost) that can be used to run JavaScript files. Note that the modules are quite independent from jshost and can be used in any project that embeds SpiderMonkey. A Windows binary (without console) is also available (jswinhost). jslibs is available under GNU GPL 2.0 license. You can access the source code through the Subversion repository using the 'Source' tab. Project documentation is available in the 'Wiki' tab. In this section, you can find modules and classes API. If you would like to report a defect or request an enhancement, click the 'Issues' tab and read the existing issues. If no existing issue is relevant, you may enter a new one. You can also use the discussion group to propose new features. Feel free to ask any question or make any comments on this project in the discussion group or the mailing list. You can contact me at soubok+jslibs@gmail.com or leave a comment.
SVN revision status
(use svn checkout --revision #### http://jslibs.googlecode.com/svn/trunk/ ./jslibs or svn update --revision ####)
Some code snippetsCapture an image from a webcam (module jsvideoinput) LoadModule('jsio'); LoadModule('jsimage'); LoadModule('jsvideoinput'); var vi = new VideoInput('QuickCam', 800, 600, 30); // 800x600 at 30fps var img = vi.GetImage(); new File('myImage.png').content = EncodePngImage(img); Create a JavaScript thread (module jstask) LoadModule('jsstd'); LoadModule('jstask'); function MyTask( request ) { for ( var i = 0; i < 200000; i++); // working... return 'r#' + request; } var myTask = new Task(MyTask); for ( var i = 0; i < 10; i++ ) myTask.Request(i); while ( !myTask.idle ) Print( myTask.Response(), ', ' ); Create a PNG from SVG (modules jsimage, jssvg, jsio) LoadModule('jsio'); LoadModule('jsimage'); LoadModule('jssvg'); var svgSource = <svg width="100%" height="100%"> <rect x="0" y="0" width="100" height="100" fill="#FF4422" /> <circle cx="50" cy="50" r="25" stroke="black" stroke-width="5" fill="none"/> </svg> var svg = new SVG(); svg.Write(svgSource); svgimage = svg.RenderImage(); new File('myImage.png').content = EncodePngImage( svgimage ); Display the content of a file (module jsio) Print( new File('test.txt').content ); Compress a string (module jsz) var compressedText = new Z(Z.DEFLATE)('This text will be compressed', true); Call MessageBoxA in User32.dll (module jsffi / jsni) function Alert( text, caption ) { var ret = new NativeData().PU32.Alloc(); new NativeModule('C:\\WINDOWS\\SYSTEM32\\User32').Proc('MessageBoxA')( ret, DWORD( 0 ), SZ(text), SZ( caption || 'Alert' ), DWORD( 1 ) ); return ret[0]; } One-line database query (module jssqlite) var myDatabaseVersion = new Database('myDatabase').Exec('PRAGMA user_version'); Non-blocking sockets (module jsio) var soc = new Socket(); soc.Connect( 'localhost', 8080 ); soc.writable = function(s) { delete soc.writable; s.Write('GET\r\n'); } soc.readable = function(s) { Print( s.Read() ); } while(!endSignal) Poll([soc],100); Encrypt data using blowfish (module jscrypt) var rnd = new Prng('yarrow'); rnd.AutoEntropy(128); var key = new Hash('sha256')('this is a secret key'); var IV = rnd(Crypt.BlockLength('blowfish')); var crypt = new Crypt( 'ctr', 'blowfish', key, IV ); var plainText = 'secret string'; var cipherData = crypt.Encrypt(plainText); Draw "Hello world" in hello.png (module jsfont) var f = new Font('arial.ttf'); f.size = 100; var img = f.Draw('Hello world'); var file = new File('hello.png'); file.content = EncodePngImage(img); Ogg Vorbis player (module jsaudio + jssound) LoadModule('jsio'); LoadModule('jsstd'); LoadModule('jssound'); LoadModule('jsaudio'); var decoder = new OggVorbisDecoder(new File('41_30secOgg-q0.ogg').Open(File.RDONLY)); Oal.Open(); var src = Oal.GenSource(); var pcm, decodeSize = 512; while ( (pcm = decoder.Read(decodeSize)) && !endSignal ) { Oal.SourceQueueBuffers(src, Oal.Buffer(pcm)); if ( Oal.GetSourceInteger(src, Oal.SOURCE_STATE) == Oal.INITIAL ) Oal.PlaySource(src); decodeSize = pcm.frames * 2; } var totalTime = decoder.frames/decoder.rate; var currentTimeOffset = Oal.GetSourceReal(src, Oal.SEC_OFFSET); for ( i = 0; !endSignal && i < totalTime - currentTimeOffset; i++ ) Sleep(1000); Oal.Close(); Use Open Dynamics Engine (module jsode) var world = new World(); world.gravity = [0,0,-0.81]; var body1 = new Body(world); var geo1 = new GeomBox(); geo1.body = body1; var body2 = new Body(world); var geo2 = new GeomBox(); geo2.body = body2; var joint = new JointHinge(world); joint.body1 = body1; joint.body2 = body2; joint.anchor = [4,4,0]; joint.axis = [1,0,0]; joint.loStop = 1; joint.hiStop = 1.5; body1.linearVel = [0,0,19]; body1.angularVel = [1,1,0]; ... Load a jpeg image (module jsimage) var texture = new Jpeg(new File('R0010235.JPG').Open(File.RDONLY)).Load().Trim([10,10,20,20]); Print( texture.width+'x'+texture.height+'x'+texture.channels, '\n' ); A configurable systray shortcut launcher (module jswinshell) LoadModule('jsstd'); LoadModule('jswinshell'); var s = new Systray(); s.icon = new Icon( 0 ); s.menu = { add:'Add', exit:'Exit', s1:{ separator:true } }; s.onmousedown = s.PopupMenu; s.oncommand = function( id, button ) { switch ( id ) { case 'exit': return true; case 'add': var fileName = FileOpenDialog( 'executable files|*.exe;*.com;*.cmd;*.bat|all files|*.*' ); if ( !fileName ) return; s.menu[fileName] = { icon: ExtractIcon( fileName ), text: fileName.substr( fileName.lastIndexOf( '\\' ) + 1 ) }; break; default: if ( button == 1 ) // left-click to run. CreateProcess( id ); else // right-click to remove. if ( MessageBox( 'Remove item: ' + id + '? ', 'Question', MB_YESNO) == IDYES ) delete s.menu[id]; } } do { Sleep(100) } while ( !s.ProcessEvents() ); A server-side script using E4X with the FastCGI module (module jsfastcgi) 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> ); A minimalist HTTP server (modules jsstd, jsio) LoadModule('jsstd'); LoadModule('jsio'); const CRLF = '\r\n'; var descList = []; var serv = new Socket(Socket.TCP); serv.Bind(8081); serv.Listen(); descList.push(serv); function Respond() { this.httpHeader += this.Read(); if ( this.httpHeader.indexOf(CRLF+CRLF) == -1 ) return; Print('Received: \n' + this.httpHeader + '\n'); descList.splice(descList.indexOf(this), 1); var writeOp = this.Write( 'HTTP/1.0 200 OK' + CRLF + 'Content-Type: text/html; charset=utf-8' + CRLF + 'Cache-Control: no-cache, must-revalidate' + CRLF + 'Pragma: no-cache' + CRLF + CRLF + '<html><body>Hello from <a href="http://jslibs.googlecode.com/">jslibs</a> at ' + new Date() + '</body></html>' ); this.Close(); }; serv.readable = function () { var desc = this.Accept(); desc.httpHeader = ''; desc.readable = Respond; descList.push(desc); } Print('HTTP server minimal example. Point a web browser at http://localhost:8081. CTRL+C to exit\n'); while ( !endSignal ) Poll(descList, 50);
Leave a comment.
|