| CARVIEW |
Javascript
my projects
My Javascript Projects
JavaScript Native Interface ( JSNI )
JSNI ( JavaScript Native Interface ) is, like JNI for Java, a way to call symbols in a native modules with JavaScript language.
JSNI use the powerfull libffi library (libffi_msvc from ctype project) and spidermonkey API. The aim of JSNI project is to allow to write this:
function MyAlert( text, caption ) {
var ret = new NativeData;
ret.PU32.Alloc();
new NativeModule( 'C:\\WINDOWS\\SYSTEM32\\User32').Proc('MessageBoxA')( ret.PU32, DWORD( 0 ), SZ(text), SZ( caption || "Alert" ), DWORD( 1 ) ); // using ordinal value of MessageBoxA
return ret.PU32[0];
}
Or things like this:
function Sleep( time ) {
if ( !Sleep.proc ) {
Sleep.proc = new NativeModule('kernel32', true ).Proc('Sleep');
Sleep.arg1 = new NativeData();
Sleep.arg1.PU32.Alloc()[0] = time
Sleep.ret = new NativeData().VOID;
}
Sleep.proc( Sleep.ret, Sleep.arg1.PU32 );
}
Or
exec('nspr.js');
var file = new File('testfile2.txt');
file.Open(File.RDONLY);
print( file.Read(10) );
file.Close();
Misc:
- Documentation
- Project hosting - google code
CPP to JS object wrapper
Unfortunately, SwigJS ( SWIG for JavaScript ) development has been stoped. So I decided to write my own wrapping system to export my C++ classes to JS classes.
This is a set of helper macros & classes to wrap javascript object around C++ classes. The idea is strongly based on wxJs's one.
eg.
... // C++ Socket class definition
...
INIT_CLASS(JsSocket,Socket)
// javascript side
BEGIN_OBJECT_PROPERTY_MAP
PROPERTY_READONLY(isConnected)
PROPERTY_READONLY(HasError)
PROPERTY_READONLY(LastErrorMessage)
END_PROPERTY_MAP
BEGIN_OBJECT_FUNCTION_MAP
FUNCTION(Connect,2)
FUNCTION(Send,1)
FUNCTION(Receive,0)
FUNCTION(Close,0)
END_FUNCTION_MAP
BEGIN_CLASS_FUNCTION_MAP
FUNCTION(WaitForReceive,2)
FUNCTION(HostToIp,1)
END_FUNCTION_MAP
DEFINE_PROPERTY(isConnected)
DEFINE_GETTER_FUNCTION(LastErrorMessage)
DEFINE_GETTER_FUNCTION(HasError)
DEFINE_FUNCTION(NO_RETURN ,ARG_2 ,Connect)
DEFINE_FUNCTION(NO_RETURN ,ARG_1 ,Send)
DEFINE_FUNCTION(HAS_RETURN,NO_ARG,Receive)
DEFINE_FUNCTION(NO_RETURN ,NO_ARG,Close)
DEFINE_FUNCTION(HAS_RETURN,NO_ARG,HasError)
DEFINE_FUNCTION(HAS_RETURN,ARG_1,HostToIp)
...
Once this is done, it is very simple to use in javascript:
LoadModule('socket');
s = new Socket();
s.Connect( 'localhost', 80 );
s.HasError && Print( s.LastErrorMessage() );
s.Send( 'GET\n\r\n\r' );
Socket.WaitForReceive( 1000, s );
Print( s.Receive() );
Script Host
The Script Host is a small executable file that run javascript programs. The Script Host has a global object that has some basic functions: Load, LoadModule, Print, Error and CollectGarbage (to make tests).
Some features of the Script Host:
- lightweight
The binary executable file is about 60KB - Minimalist internal API
Load, LoadModule, Print, Error and CollectGarbage is enough, everything else can be added using dynamic libraries - manage precompiled javascript files (using XDR system) to speed up overall execution
Load('IRCBot.js'); // loads IRCBot.js or IRCBot.jsxrd if available
... - module based architecture (.dll/.so)
eg.
LoadModule('socket');
LoadModule('wxJS','wxJS_Init');
...
Misc:
- Project hosting - google code
JavaScript IRC bot
The aim of this project is to provide a good IRC bot that is very fast, fully and easily customizable.
This is an alternative to the eggdrop project, that use TCL as scripting language.
The main difference is that my bot is 100% javascript written.
The "main" function looks like this:
var client = new IRCClient( 'euroserv.fr.quakenet.org', 6667 );
client.AddModule( new DefaultModule( 'souboks_bot' ) );
client.AddModule( new ChanModule( '#tremulous' ) );
client.Create();
Features:
- module based.
- server event listeners.
- flood control mechanism.
Misc:
- Project hosting - google code
My del.icio.us bookmark