How to Embed Node.Js Interpreter into C/C++

How to embed Node.js interpreter into C/C++?

It probably is, V8 is written in C++, node.js can run on V8, but unless you have an extremely good reason why you would run javascript through C++ you are probably much better served finding an appropriate C++ library and implementing the needed functionality directly in C++. The task of integrating scripting languages and native code is usually not trivial. E.g. V8 documentation. Qt offers a pretty decent integration between c++ and javascript and it still not trivial to move objects back and forth between script and code.

Embedding Node.js in Python

What you want to do is not supported. Unlike, say, the CPython interpreter, or even the V8 JavaScript interpreter, Node.js was not designed to be embedded, has no interface for doing so, and no serious plan to change that.

I can't find any official documentation on that, but there are numerous threads like this one that give the relevant information.

But that doesn't mean it's impossible. The top layer of Node isn't that complicated, and really you'd just need to patch out a few parts of it to do different things. And, in fact, people have tried to do exactly that, in projects like tacnode. I don't know if any of them are ready for prime time or not, but it may be worth looking at them, especially if you're willing and able to contribute if they're incomplete.

Of course that only gets you embedding in C or C++; you still need to embed in Python. But wrapping a C shared library so you can use it in Python (well, CPython and PyPy) is a long-solved problem; Python has had extension modules since almost the beginning, as well as ctypes and cffi if you don't want to write any C code. And there are third-party projects like Cython to let you write almost-Python code that directly calls your shared library as if it were native C, and compiles to a Python extension module.

So, it's almost certainly doable, but it's probably not going to be trivial, or packaged and ready to go out of the box.

Use Node.js as an interpreter

As I got from this question the problem of immediate linking with node.js is still unsolved. Actually the workaround may be running it in a separated process like an ordinary command line application. You may save your script to file, pass it as cmdline argument, then obtain std output from the node.js executable.

How to set nodejs interpreter arguments from javascript source

If one will have such someScript.js:

#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"

function test(a, ...args) {
console.log(args);
}

test('a', 'b', 'c');

Then "sh someScript.js" command will use interpreter and arguments specified in the someScript.js.

If one will mark someScript.js executable (chmod u+x someScript.js). Then ./someScript.js command also will work.

This article was helpful:
http://sambal.org/2014/02/passing-options-node-shebang-line/

Also if you use Windows EOL there will be a bug with '\r' adding to last script argument. If there are no arguments there will be one ('\r').

UPDATE:

Some shells allow to use

#! /usr/bin/env node --harmony

instead of

#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"


Related Topics



Leave a reply



Submit