Você está na página 1de 13

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++Codeforcreatingobjectcalled'MyObject' voidtestfunc(){ std::cout<<"testfunc\n";} voidtestfunca(std::stringa){std::cout<<"testfunca="<<a<<"\n";} voidtestfuncb(std::string&a){ std::cout<<"testfunca="<<a<<"\n";} voidtestfuncc(int&a){ std::cout<<"testfunca="<<a<<"\n";} voidtestfuncd(double&a){ std::cout<<"testfunca="<<a<<"\n";} TestAnonymous(){ ///Create newJavascriptEngine JSEngine_js; intiret=_js.Init(); if(iret!=0){printf("initfailed\n");} defConsole(_js); ///Create newAnonymousObjectMyObjectaspropertyofglobalobject AnonymousJSObject*pdj=newAnonymousJSObject("MyObject",_js); std::stringsval="12346"; ///Create newAnonymousObjectMyObjectaspropertyofglobalobject pdj>SetPropertyVal("x","1234",false,false); pdj>SetPropertyVal("y","4321",false,false); ///Define New Externel PropertiesExternelpropertiesarecachedvariables. pdj>DefPropertyValExternalString("shx",sval); pdj>SetPropertyValExternalString("shx","1234123412341234123412341234 123412341234513"); ///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' interet=_js.CompileExecuteFile("/home/libjss/test.js"); }Cont... copyright @ anand rathi 2012

Example: Anonymous Object use case

JavascriptCodeusing'MyObject' CONSOLE.writeln(MyObject.x); CONSOLE.writeln(MyObject.y); CONSOLE.writeln(MyObject.shx); MyObject.shx="5432543254325432543254325432543254325432153"; MyObject.shx="5432543254325432543254325432543248"; MyObject.testfunc(); MyObject.testfunca("teststring"); MyObject.testfuncb("teststringb"); MyObject.testfuncc(1234); MyObject.testfuncd(111.222); CONSOLE.writeln('hello\n'); Cont...

copyright @ anand rathi 2012

Example: Export Native Class use case


C++Codeforcreatingobjectcalled'' classCc{ public: Cc(){printf("Indefaultconstructorofc\n");} Cc(intpi){ printf("Inconstructorofc%d\n",pi); i=pi; d=3.33; s="444"; } Cc(std::stringi){printf("Inconstructorofc%s\n",i.c_str());} Cc(std::string&i){printf("Inconstructorofc%s\n",i.c_str());} charc; inti; doubled; std::strings; intsfunc(std::string&a){std::cout<<a<<"\n";} }; boolTestJSScriptExportClass(void) {JSEngine_js; intiret=_js.Init(); if(iret!=0){printf("initfailed\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 interet=_js.CompileExecuteFile("/home/libcdr/TestJSScriptExportClass.js"); }Cont...

JavascriptCodecreatingandusingExportNativeClass // CONSOLE.writeln('hello\n'); vartObj=newtestclass(2); tObj.sfunc("HelloJS\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 { AperopertyA }; 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") Injavascript vartObj=newB(); 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

Você também pode gostar