There are two different ways of handling the Node.js event loop.
By calling node::lib::ProcessEvents(), the Node.js event loop will be run once, handling the next pending event. The return value of the call specifies whether there are more events in the queue.
By calling node::lib::RunEventLoop(callback), the C++ host program gives up the control of the thread and allows the Node.js event loop to run until no more events are in the queue or node::lib::StopEventLoop() is called. The callback parameter in the RunEventLoop function is called once per event loop. This allows the C++ programmer to react on changes in the Node.js state and e.g. terminate Node.js preemptively.
In the following, simple examples demonstrate the usage of Node.js as a library. For more complex examples, including handling of the event loop, see the node-embed repository.
This example evaluates multiple lines of JavaScript code in the global Node context. The result of console.log is piped to the stdout.
node::lib::Initialize("example01");
node::lib::Evaluate("var helloMessage = 'Hello from Node.js!';");
node::lib::Evaluate("console.log(helloMessage);");This example evaluates a JavaScript file and lets Node handle all pending events until the event loop is empty.
node::lib::Initialize("example02");
node::lib::Run("cli.js");
while (node::lib::ProcessEvents()) { }This example uses the fs module to check whether a specific file exists.
node::lib::Initialize("example03");
auto fs = node::lib::IncludeModule("fs");
v8::Isolate *isolate = node::lib::internal::isolate();
// Check if file cli.js exists in the current working directory.
auto result = node::lib::Call(fs, "existsSync", {v8::String::NewFromUtf8(isolate, "cli.js")});
auto file_exists = v8::Local<v8::Boolean>::Cast(result)->BooleanValue();
std::cout << (file_exists ? "cli.js exists in cwd" : "cli.js does NOT exist in cwd") << std::endl;