node.js is actually V8 embedded in a web server. Since your application probably isn't a web server, you can just embed V8 (or any other javascript engine) into it.
– MAKApr 2 '11 at 19:01

1

Technically node.js doesn't just do webservers. It can fulfill tons of different roles, but it's really good for HTTP servers (fileservers, webservers, etc). In either case, you're right about just using V8 instead.
– beatgammitApr 3 '11 at 5:27

5

@MAK node.js is not a web server. It integrates a web server module, but it certainly is not a web server. In C++ apps node.js brings loads of modules not available with just v8.
– CoyoteDec 10 '12 at 10:01

6 Answers
6

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.

You should first consider whether it would be sufficient to implement your application as a C++ module for Node and then glue the main part as a Node script.

Otherwise you may wish to "re-implement Node", by taking the core code as the example and
removing the parts which you don't need (e.g. HTTP module) and then putting your components
into it. The least painful way would be to do a sub-tree merge and ripping-out the build system, then adding prefixes in the build scripts to point to the directory where it lives.
Then you can stop certain parts from being built. However Node's build system contains several parts and it may be quite a difficult job to do.

You can also try re-packaging Node with your stuff loaded by default and changing the name of the executable. However, that is just a more complex way of taking the first approach I have described, you can just install a script in /usr/bin/ which will go as:

You can use a JSlint as your parser, then grep for dangerous calls and then eval(conf_script) or just use require(config.js), though you will need to add exports.someMethod = function (...) {...}. But require() is much safer in general, however you may wish to implement a pre-processor for your config which will substitute exports.someMethod = function (...) {...} instead of your functions and will append require('OnlyCallMySafeMethods') and reject any
attempts to require('fs') or other lib which you may be afraid of letting the someone to use.
This sort of safety is just an optional thing you may wish to have, it's all up to you really.
Though I suppose you might want to do the bit with exports.someMethod = .... substitution and have one require('myAppConfigLib) added on the top so the user will just use you API plus anything they may wish to put into their script/config!

// To allow people to extend Node in different ways, this hook allows
// one to drop a file lib/_third_party_main.js into the build
// directory which will be executed instead of Node's normal loading.

Please also note that the contents of src/ are being compiled to bytecode at build time.

I was just checking out js-git which is made for Node.js and also depends on a few other Node.js modules.

However, the same developer wrote a tool tim-task to wrap up some common Node.js functions, most importantly require, and to pack together some Node.js modules in such a way that it should not depend on Node.js anymore. He used it to make git-web-platform, i.e. js-git packed as a JS file which can be used in browsers. The resulted packed file looks like this. This can probably also be used with minor modifications just in pure V8.

This might be useful for you. Note however that this approach will be limited.