Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

node.js

I want to use Node.js scripts in my C/C++ applications. Some people suggested me to start with v8, libev and libeio; but it means rewriting Node.js from scratch.

So, is it possible to embed Node.js into C or C++?

like image 941
Jeff Avatar asked Apr 02 '11 18:04

Jeff


People also ask

Is Nodejs written in C?

Node. js is written in C++. C and C++ are different languages, with different strengths and weaknesses. C is strongest in getting the most direct access and closest control over hardware--the Linux Kernel, for example, is in C.

Can JavaScript run C code?

js can dynamically load an external C or C++ DLL file at runtime and utilize its API to perform some operations written inside it from a JavaScript program.

CAN node js be compiled?

Standard Node. js is built against V8, which compiles every Javascript code snippet into native instructions. You may use --print_code flag in the command line to see which scripts are getting compiled, and compiled into what. Hope this helps.

Can you run Nodejs code in browser?

Thanks to some creative engineers, it is now feasible to use Node. js modules in browsers, but not directly. Being able to call Node. js modules from JavaScript running in the browser has many advantages because it allows you to use Node.


1 Answers

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:

  #!/usr/bin/node   var myAppMain = require('libmyApp');   myAppMain.withConfig(filename,   function(err, cnf) {      if (err) throw err; // parser or file access error      cnf.evalMe();   }); 

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!

UPDATE: There is a quite useful comment on line 66 of src/node.js:

  // 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.

like image 101
errordeveloper Avatar answered Sep 22 '22 21:09

errordeveloper