Javascript C++ binding? - javascript

I have some C++ code that I want to expose to client side of a web app. Ideally, I want to write Javascript wrapper objects for my C++ classes so that I can use them clientside.
Has this been done before?. Does anyone have a link to show how this may be achieved?

There is a library to convert C++ code to javascript, it might help:
emscripten

Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey 1 . 8 . 5 and more
SpiderMonkey? is Mozilla Project's Javascript/ECMAScript engine.
Libjspp allows C++ developers to embed SpiderMonkey? simply and easily into their applications. Libjspp allows to run multiple Javascript Engines within same process which suits one engine per thread para dime which is helpful in achieving true parallisim. Also Libjspp no way stops user from running multiple threads within engine.
http://code.google.com/p/libjspp/

I guess that RPC is what you want. You'll need to wrap your functions on the server side using some sort of framework. I've not yet used it, but this one looks promising.
On the client side you use proxy objects to dispatch the function calls. The communication is handled usually either via XML-RPC or JSON-RPC. I used this client side framework and was quite content but I'm sure you'll find many others.

This is an old topi, however, I was in the exact situation right now, and all of the solutions I found on the net complicated or outdated.
Recently, I ran across a library which supports V8 engine (including the new isolation API, which makes 90% of the libraries I found outdated) and provides great exposure and interaction API.
https://github.com/QuartzTechnologies/v8bridge
I hope that my solution will help anybody.

There's a relatively new library for doing this called nbind. Maybe that would suit you? It looks very good to me, and I'm just about to start using it.

I think you want a C++ JSON parser. You should be able to find one here http://www.json.org/. It may not do all you want because it just serializes and deserializes C++ objects without any behavior, but it should be good enough. See https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser for some discussion.

If the C++ code has to be on the client, then there is no simple way to do this for a web app. A solution may involve coding plugins for the browsers you want to support, which may then be accessed from javascript code.
If, for example, you need this for a client application, that is another case. Such a thing has been done and involves linking your application to (or running from outside) with for example chromium library, or any other javascript execution engine. That way you can create bindings to C++ classes and use such objects from javascript and vice-versa. Note that this is also not a trivial solution and may be a big effort to implement (also requires additional resources).

You could for example wrap the C++ classes in PHP or Python, and then implement an API over HTTP to access the required functions.
Or if you insist on exposing the functions as JavaScript you could try using Node.js, and create an C++ add-on to wrap you classes. See the Node.js documentation here: http://nodejs.org/api/addons.html#addons_wrapping_c_objects
But either way, I don't think avoid creating some sort of API (HTTP SOAP, XML RPC) to access the functions on your server.

Though QML is not exactly Javascript, Qt is not plain C++, but what they do together seem just like what you need

Related

Can I call a c++ function from JavaScript?

There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.
A few options I can think of:
1) Find a different library in JavaScript.
2) Port the C++ code over to JavaScript. How easy this is depends on
how complex the C++ code is or how much of it you need.
3) Wrap the C++ code in a web service on a server and call it using AJAX. I'm not personally familiar with any C++ web service frameworks, but barring that, you could create Java, Python, or .NET bindings for the library (or just for the portion that you need), or bindings in another language. Then you would write your web service in whatever language you chose.
3B) Alternative to #3 - If the library has a command-line interface, or if there exists a command-line program that uses the library, your web service could call that and you wouldn't have to write a language binding. Note however that there are performance & security problems to be aware of with this option.
4) If you have the C++ source code for the library, you could try to compile the library to JavaScript using Emscripten.
I would probably try those in that order, roughly. I might try #3 last cause it could be the trickiest.
Also, if you do #3 or #3B, you'll want to be sure your use of the library is thread-safe.
Disclaimer: I've never tried any of these except #3B.
You'd be better off creating a 'C' wrapper for the C++ library and calling that from the Javascript.
C++ is notoriously difficult to interface with due to the language standard lacking a tight ABI definition. C does not have this limitation. For this reason, 'C' ends up being the lingua franca when you want two programming languages to talk with each other.
Usually browsers doesn't allow that, because that's very insecure.
You might compile C++ in asm.js and use it as JavaScript library.
Alternatively you can create browser extension, which will run or call desired code.
Yes its possible but before you can call them from javascript. You do this by creating an interface file to tell javascript about the interface.

How to implement a wrapper for the JavaScript API of NodeJS in C++

I saw this website: AppJS , and if you go to the end of it, you'll see the Section "We need your help!". Therein is explained that you could help by making wrappers for a specific platform (whichever you want, be it Windows, Linux, Mac...) that should be an interface for the JavaScript language so that the JavaScript can call those functions (and obviously to create cross-platform apps).
So my question is how can I write something in C++, compile it, and then call that function from outside, specifically from JavaScript?
What should I know? Does the AppJS or the NodeJS have some kind of module programmed that allows the communication between your own compiled C++ code and the JS part?
Basically how does this wrapping work?
Edit:
Probably, in the next couple of links there is information about the topic.
http://pravinchavan.wordpress.com/2013/11/08/c-binding-with-node-js/
http://es.slideshare.net/nsm.nikhil/writing-native-bindings-to-nodejs-in-c
Rather than "wrapping" the word that would fit the most would be "binding" I guess.
Pthread (POSIX) is a "wrapper" but not a "binding".
NodeJS is a library for V8, the Google open source JavaScript engine, written in C++. I think you should look into V8 for more information.
You need to create an addon with the help of v8 jscript, a c++ library and libuv, a C event loop library. Read more on
http://nodejs.org/api/addons.html

Is there an CDI-like mechanism thinkable for JavaScript

Before diving into JavaScript I was doing a lot of Java and especially liked the concept of context dependency injection. I used Google Guice a lot.
I know that JavaScript is not statically typed. And there are modulization concepts like used by requireJs or Node.js (even did something myself).
But is it thinkable or are there already solutions to inject dependencies based on a loosely defined interface.. (hope that isn't too stupid to ask)
The closest thing I have seen to what you are asking for (I think) is Architect from tim caswell.
It allows you to register modules abilities and requirements, so you can essentially request something that can preform an action, and have Architect check all the available modules to see if any of them support such an action.
You may also want to take a look at Errai (http://www.jboss.org/errai). It brings the client and sever together using the same programming paradigm by using the GWT javascript compiler bringing CDI to the browser (they're also working on JPA in the browser as well).

Integrating ECMA Script into a C# program, what is the fastest technique?

I'm trying to execute JavaScript in a C# program, and I'm trying to determine:
Would it be better to create a state machine for JavaScript, or should it be compiled into MSIL before executing it?
I'm really looking for techniques rather than solutions. Some attempt to compile the JavaScript, there by treating it as a programming language, others use a state machine and treat it as it was meant to be : a scripting language.
Any documentation on the methods used for executing is welcome also.
The easiest way to run JS in a C# program is to use one of the existing .Net JS runtimes: IronJS, Jurassic, and Jint. If you're interested in adding another .Net-based interpreter to the mix, examining their code is where I'd start; If you're embedding JS in your program then using a preexisting one is probably the way to go.
You haven't explained your scenario - how much javascript, how often does it get executed, how often does it change, how closely integrated it needs to be with the C# logic, and so on.
If it rarely changes, then it may be smart to compile it into MSIL via one of the alternatives.
If it changes "constantly" then it might make sense to stand up a JS compiler and send it JS as necessary. It's something like a REPL for Javascript, that you'd use programmatically. Node.js offers a server that can do REPL operations; it could listen on a port, you send it JS to evaluate, and it sends back the result. There's a free w3sockets component that could be used from cscript.exe to do something similar: for example you could build a "host" Javascript shell that listens on a socket, executes the js it receives, then sends back the result.
If it needs to be closely integrated with the C# code, then you'll have to be smarter about this. One way to do it might be to host the JS in a Windows Script Component and have the C# code invoke the JS logic via COM. You can also do the converse - have the JS host invoke the C# layer via COM to gather input and deliver results.
EDIT
A better way is to use the IActiveScript stuff - this is the official way Microsoft makes it possible to host scripting within an application. There's a winforms example published here.
OR better, see parse and execute JS by C#
Using that code I put together a demonstration of how to parse and invoke Javascript from a .NET app, using IActiveScript.
You can download a VS2010 project for this app here.

What is "native JavaScript"?

Is there anything called "Native JavaScript"? I have seen this term in a job description I am going to be interviewing.
Is regular JavaScript is same as Native JavaScript?
The term "native" is very overused in JavaScript.
Colloquially, it is used as in Johan's answer: no JQuery, Moo, Dojo.
Analogous to the JNI for Java, Google's GWT and similar I-compile-down-to-JavaScript talks about the underlying implementation as being native.
The original use of native in JS, I believe, refers to objects built and defined in ECMAScript as opposed to the environment. JavaScript, as an ECMAScript language, is not intended to be self-sufficient; it is embedded in a host environment such as a Web browser, Photoshop, Acroread, etc. When you write a web client program, you will use objects such as Math, Function, Array, Window, and Button. The first three are native (independent of host environment), while the last two are non-native (supplied by the host environment). This is kind of the opposite of cdhowie's answer, which is a good answer BTW. Just interesting, though!
I'm sure there are other interpretations. My guess is that if you see this in a job description, it's probably not the last one: that definition is too academic. :)
Here is the official definition from the ECMAScript-262 Standard, Fifth Edition:
4.3.6 native object --- object in an ECMAScript implementation
whose semantics are fully defined by this specification rather
than by the host environment. NOTE Standard native objects are
defined in this specification. Some native objects are built-in;
others may be constructed during the course of execution of an
ECMAScript program.
In other words the built-ins like Math, Object, String, RegExp are native, as are any objects I make with object literals or function definitions. But host objects are the opposite. Sorry for the ramble.
Native JS, I think, it's example below:
$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")[$],_$_:++$,$_$$:
({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$$_:(!""+"")[$],$__:++$,$_$:
++$,$$__:({}+"")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};$.$_=
($.$_=$+"")[$.$_$]+($._$=$.$_[$.__$])+($.$$=($.$+"")[$.__$])+
((!$)+"")[$._$$]+($.__=$.$_[$.$$_])+($.$=(!""+"")[$.__$])+
($._=(!""+"")[$._$_])+$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+
(!""+"")[$._$$]+$.__+$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];
$.$($.$($.$$+"\""+$.$$_$+$._$+$.$$__+$._+"\\"+$.__$+$.$_$+$.$_$+
$.$$$_+"\\"+$.__$+$.$_$+$.$$_+$.__+".\\"+$.__$+$.$$_+$.$$$+
"\\"+$.__$+$.$$_+$._$_+"\\"+$.__$+$.$_$+$.__$+$.__+$.$$$_+
"(\\\"\\"+$.__$+$.__$+$.___+$.$$$_+(![]+"")[$._$_]+(![]+"")[$._$_]+
$._$+", \\"+$.__$+$.$$_+$.$$$+$._$+"\\"+$.__$+$.$$_+$._$_+
(![]+"")[$._$_]+$.$$_$+"!\\\")\\"+$.$$$+$._$$+"\"")())();
Native JavaScript is the JavaScript implemented in the browser.
Browser implementation is followed by ECMA standard and some extra method. For example, if you familiar with jQuery and want to select element by ID
$('#myId')
Native JavaScript will be
document.getElementById('myId')
Now, if you need to do some simple tasks. You might not or should not load a huge jQuery Library and execute all its functionality. Rather 10 lines of native Javascript will do the job instead of 5 lines of jQuery code
I belief native Javascript reveres to 'vanilla' javascript, so no jQuery, MooTools, etc. Just plain old javascript
Not to complicate things too much more as #Ray's answer is correct, but a new JavaScript framework for building mobile applications has emerged called NativeScript https://www.nativescript.org/
If not confusing enough, this framework allows you to write pure "vanilla" JavaScript, but call Native (iOS/Android/WindowsPhone) APIs.
It is almost like writing a "Native" app (think Objective-C/Java/C#) but using a single language (JavaScript) which is interpreted at run-time on the device.
I suspect as this library's popularity grows "Native JavaScript" may be used as a label to describe code using this framework (esp. since that's how I got to this question via Google)
Native JavaScript is all JavaScript code . Not any kind of framework codes.
It's faster and loads in the browser.
You can use for simple browser task, that does not require any database interface, or any back-end large computation work.
Using framework makes codes larger and makes browser slow.
The only context I know for the term "native JavaScript" involves writing JavaScript objects and/or functions that are implemented by the JavaScript provider and not written in JavaScript itself. For example, many of the functions you call on the window object are implemented by the browser in its native language (C++ for Firefox/Mozilla, etc.). This might be what they mean...
They might also mean vanilla/pure JavaScript (without frameworks like jQuery or Prototype). Consider contacting them and asking for clarification.
It is the same, perhaps just wondering if you've done more than just jayesh etc.
No , Native Javascript to me means 3rd party js functions that can do things that regular js cant , kind of like the Java-JNI relationship . Google JNEXT and JSNI for more info.
Again , I do not know the real context of the question , If i were you , I'd ask for clarification.

Categories

Resources