libjspp

Anand Rathi

Template based C++ Wraper for Spidermonkey 1.8.5

General description

Libjspp allows easy interfacing & interacting of C++ with javascript[currently spidermonkey 1.8.5] Non intrusive to C++ & javascript world at same time. Useful for applications who want to Embed & Extend in Object oriented manner. Very thin interface, By using C++ templates. App can create multiple javascript engines per process or per thread and export native copyright @ anand rathi 2012 functions , class, variables. Cont...

 

General description

Multi threaded – support for each engine per thread. Dynamically define Cached & Non Cached property. Caching of properties defined in native & anonymous object allow direct local acess without need of conversions. Caching of strings avoids trivial & frequents memory allocations.
Cont...
copyright @ anand rathi 2012

General description

Javascript world is agnoustic about to fact that class / object was by C++ Exported C++ class objects is agnoustic to fact that they are Proxy to Javascript object.

copyright @ anand rathi 2012

Use Cases

This Library has two ways to export

1) Anonymous Object: Creating named Non C++ objects inside Javascript engine from C++ dynamically & defining properties and attaching native C++ methods as functions of that object. 2) Export Native Class: Exporting existing C++ class as javascript class which includes exposing
− − − −

Exposing native constructor [currently only One] Exposing native methods Exposing native properties User can create as many instances these ProxyObjects
copyright @ anand rathi 2012

C++ Code for creating object called 'MyObject'  void testfunc() { std::cout << "testfunc\n";} void testfunca(std::string a) {std::cout << "testfunc a= " << a << "\n"; } void testfuncb(std::string& a) {  std::cout << "testfunc a= " << a << "\n"; } void testfuncc(int& a) { std::cout << "testfunc a= " << a << "\n"; } void testfuncd(double& a) {  std::cout << "testfunc a= " << a << "\n"; } TestAnonymous() { ///Create new Javascript Engine JSEngine _js;  int iret = _js.Init(); if(iret!=0) { printf("init failed\n"); } defConsole(_js); ///Create new Anonymous Object MyObject as property of global object AnonymousJSObject *pdj = new AnonymousJSObject("MyObject",_js); std::string sval = "1234 6"; ///Create new Anonymous Object MyObject as property of global object pdj­>SetPropertyVal( "x", "1234", false , false); pdj­>SetPropertyVal( "y","4321", false, false); ///Define New Externel Properties Externel properties are cached variables.  pdj­>DefPropertyValExternalString("shx", sval); pdj­>SetPropertyValExternalString("shx", "1234 1234 1234 1234 1234 1234 1234  1234 1234 12345 13"); ///Define new functions and add them to the 'MyObject' pdj­>addFunc<testfunc>("testfunc", testfunc); pdj­>addFunc<void, std::string, testfunca>("testfunca", testfunca); pdj­>addFunc<void, std::string&, testfuncb>("testfuncb", testfuncb); pdj­>addFunc<void, int&, testfuncc>("testfuncc", testfuncc); pdj­>addFunc<void, double&, testfuncd>("testfuncd", testfuncd); ///Compile and execute script in the test.js file' int eret = _js.CompileExecuteFile("/home/libjss/test.js"); } Cont... copyright @ anand rathi 2012

Example: Anonymous Object use case

Javascript Code using 'MyObject'  CONSOLE.writeln(MyObject.x); CONSOLE.writeln(MyObject.y); CONSOLE.writeln(MyObject.shx); MyObject.shx="5432 5432 5432 5432 5432 5432 5432 5432 5432 54321 53" ; MyObject.shx="5432 5432 5432 5432 5432 5432 5432 5432 48" ; MyObject.testfunc(); MyObject.testfunca(" test string"); MyObject.testfuncb(" test string b"); MyObject.testfuncc(1234); MyObject.testfuncd(111.222); CONSOLE.writeln('hello \n') ; Cont...

copyright @ anand rathi 2012

Example: Export Native Class use case
C++ Code for creating object called ''  class Cc { public: Cc(){ printf("In default constructor of c \n"); } Cc(int pi) {  printf("In constructor of c %d\n",pi ); i=pi; d=3.33; s="444"; } Cc(std::string i) { printf("In constructor of c %s\n",i.c_str() );} Cc(std::string & i) { printf("In constructor of c %s\n",i.c_str() ); } char c; int i; double d; std::string s; int sfunc(std::string& a) { std::cout << a << "\n"; } }; bool TestJSScriptExportClass(void) {JSEngine _js; int iret = _js.Init(); if(iret!=0) { printf("init failed\n"); } defConsole(_js); ///Export Native C++ class and its properties & function exporterCLass<Cc> *pec = exporterCLass<Cc>::GetInstance("testclass"); pec­>setConstructor(init<int >()). add<int, std::string&, &Cc::sfunc>("sfunc", &Cc::sfunc) .setVariable< int , &Cc::i >("ivar") .setVariable< double, &Cc::d >("dvar") .setVariable< std::string, &Cc::s >("svar"); ///Export Native C++ class and its properties & function pec­>registerEngine(_js); copyright @ anand rathi 2012 int eret = _js.CompileExecuteFile("/home/libcdr/TestJSScriptExportClass.js"); }Cont...

Javascript Code creating and using Export Native Class // CONSOLE.writeln('hello \n') ; var tObj = new testclass(2); tObj.sfunc("Hello JS\n") CONSOLE.write('tObj.ivar=') ; CONSOLE.writeln(tObj.ivar) ; CONSOLE.write('tObj.dvar=') ; CONSOLE.writeln(tObj.dvar) ; CONSOLE.write('tObj.svar=') ; CONSOLE.writeln(tObj.svar) ; CONSOLE.writeln('done\n') ;

copyright @ anand rathi 2012

Future work ..cont
• Export more complex class properties, function Parameter currently only std::string, int, double are mapped example
class A { std::string s; }; class B { A peropertyA }; JSEngine _js; exporterCLass<A> *pecA = exporterCLass<A>::GetInstance("A"); pecA­>setVariable< std::string, &A::s >("s") pecA­>registerEngine(_js); exporterCLass<B> *pecB = exporterCLass<B>::GetInstance("B"); pecB­>setVariable< exporterCLass<A>, &B::A >("peropertyA") ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ In javascript var tObj = new B(); tObj.peropertyA = “Hello”;  

copyright @ anand rathi 2012

... Future work
• Adding capablity to Invoke Javascript function from C++ • Adding capablity to acess Javascript properties from C++ • Javascript as Inversion of control • Upto 10 or more parameters in functions & constructors • Modules to wrap various other C/C++ libraries database , http server, curl and more goodies
copyright @ anand rathi 2012

Current build Environment
• Gcc/g++ 4.7 • Linux • Spidermonkey 1.8.5

copyright @ anand rathi 2012

Contact
• scriptwrap@yahoo.com.au • http://netya.weebly.com/contact.html

copyright @ anand rathi 2012

Sign up to vote on this title
UsefulNot useful