Is it possible to include NodeJS as a scripting engine inside an executable?

Go To StackoverFlow.com

4

I've an application written in C. I would like to execute user defined scripts written in JavaScript in this application and allow these scripts to work with my internal C variables mapped to JavaScript namespace.

Is it possible to use NodeJS to compile it as a scripting engine?

I know that I can do vice versa: run NodeJS and use my C code as a library for NodeJS with proper binding of C variables to JS. But this is not acceptable, since my application has a GUI and many other modules that are included as libs and it would be hard to rewrite the code to run as a NodeJS lib.

I also don't want to run NodeJS as an external executable each time I need to run a script due to performance reasons. I need to keep NodeJS in memory and run scripts in the same namespace during the whole process cycle.

Maybe there is some special edition of NodeJS intended for such purpose? Or I can compile it as such?

2012-04-04 00:24
by PoltoS
Maybe you want to use V8 without nodejs - stewe 2012-04-04 00:32
I've heard NodeJS to be an adoption of V8 for server-side. How is it far from original V8? Is there an example of using Google V8 engine inside something else (like I want)? [of course except Chrome - PoltoS 2012-04-04 00:37
Added example below. Currently the latest version of node uses v8 version 3.6.6.2 - stewe 2012-04-04 00:43


1

What you need is V8 javascript engine. Take a look here for more details...

This document discusses these concepts further and introduces others that are key to embedding V8 within your own C++ application.

What you can use nodejs for is perhaps to look at it's source code on how to build on top of V8 engine.

And here is a quick example.

2012-04-04 00:34
by Aleksandar Vucetic
Is there an "easy" example of such integration? NodeJS is a quite heavy one to learn about V8 integration - PoltoS 2012-04-04 00:40
I updated my answer with a link to getting started. Just go through Google's documentation and I think that you will find everything you need there - Aleksandar Vucetic 2012-04-04 00:43
Great! It seems t be exactly what I wanted. Could you also give me an advice on hungry of V8? Can I run it on embedded platforms? Like ARM 500 MHz with 128 Mb of memory? Is there a recommended minimal hardware - PoltoS 2012-04-04 00:46
And another question: NodeJS has a built in web server. V8 does not. I need a webserver. Shall I use something like libmicrohttpd or it is still possible to include some part of NodeJS to serve HTTP - PoltoS 2012-04-04 00:50
I'm really not sure about that...regarding hardware, it runs on phones, so don't see a reason why it wouldn't run on 500Mhz AR - Aleksandar Vucetic 2012-04-04 00:51


4

Here is an example executing some javascript using v8:

int main(int argc, char* argv[]) {

  // Create a string containing the JavaScript source code.
  String source = String::New("'Hello' + ', World'");

  // Compile the source code.
  Script script = Script::Compile(source);

  // Run the script to get the result.
  Value result = script->Run();

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);
  return 0;
}

From: https://developers.google.com/v8/get_started

2012-04-04 00:42
by stewe


0

Why not explore the internals of Node and sort of "copy" what it's doing. Then you can build your app on top of V8. Node.JS is, after all, just a layer on top of V8 and several libraries (I/O) to provide additional functionality.

2012-04-04 00:37
by Joseph
This would either not allow use of nodejs's many useful libraries or necessitate keeping in lockstep with nodejs as it evolves to keep compatibility with its libraries - hippietrail 2012-11-28 04:26