2

I am having C/C++ based application. I want to run nodejs script from my C code. The nodejs script looks as below.

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://google.com');
  await page.screenshot({path: 'some.png'});
  await browser.close();
})();

This script is generated in C code and available in memory. I can dump it to a file and using system() api, I can run the script. I want to avoid that. Is it possible to run the script, present in memory directly using nodejs without dumping it to file.

While executing, if any exception occurs I need to report it back to my C/C++ application through a callback. Is this possible?

EDIT

I know there is a similar question / answer at Calling JavaScript from C++ with node.js , but here still .js file will be in disk and it is executed. Here my question is, how to run nodjs from C/C++ application where the script is available in memory.

Austin
  • 1,709
  • 20
  • 40
  • 2
    Possible duplicate of [Calling JavaScript from C++ with node.js](https://stackoverflow.com/questions/29165155/calling-javascript-from-c-with-node-js) – Alan Birtles Aug 07 '19 at 06:00
  • @Alan Birtles, Thanks for pointing. I hope in that answer, still a .js file is used. My question is about to avoid .js file and execute the in-memory script. For second part of my question, probably the answer may be useful, I need to check though. – Austin Aug 07 '19 at 06:24
  • You mean the whole script is in form of a character buffer in C/C++ code which needs to be run? Why is everything in buffer which part of code is dynamic? – Vishal-L Aug 07 '19 at 06:40
  • As far as I can tell the answer to that question is still valid which is basically node.js only has an addon API so the only way is to execute node from the command line. – Alan Birtles Aug 07 '19 at 06:42
  • see also https://stackoverflow.com/questions/5525162/how-to-embed-node-js-interpreter-into-c-c – Alan Birtles Aug 07 '19 at 06:45
  • and https://github.com/metacall/core – Alan Birtles Aug 07 '19 at 06:49
  • @Alan Birtles, still I am not clear. Using addons, I can make a node module, and can call addon module apis from nodejs. In my case, process starts with c/c++ executable, and I need to call nodejs function/api. Can you please write the answer with an example code. – Austin Aug 14 '19 at 03:38
  • 1
    I don't think it's easily possible, your best option looks like the meta call library – Alan Birtles Aug 14 '19 at 06:42
  • @AlanBirtles I did some more R&D. I am starting nodjs script from command line. Using N-API, I am able to execute simple javascripts from c/c++ code. But when I try to execute puppeteer script as shown in question above, it gets exception at require line (1st line). Any idea, why this happens. – Austin Aug 14 '19 at 15:21
  • is puppeteer installed in the environment you are running the script under? – Alan Birtles Aug 14 '19 at 15:26
  • Yes, puppeteer is installed. In index.js (that I run from command line), require('puppeteer'); works fine. But when I keep it in a string and executes through napi_run_script api, it gets exception. – Austin Aug 14 '19 at 16:08
  • @AlanBirtles, is it true that whatever can be run by V8, only those syntax / function etc should be there in string that I run as javascript through napi_run_script. Nodejs does more than V8, like DOM handling etc, can those be part of JS that can be executed by napi_run_script. – Austin Aug 14 '19 at 16:38
  • @AlanBirtles, did you use metacall anytime. I gave a try, though I could build, but execution result not satisfactory. – Austin Aug 16 '19 at 00:45
  • No, i just found it through googling your problem – Alan Birtles Aug 16 '19 at 06:08

0 Answers0