Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Nodejs to work on standalone C++ application on Windows #1087

Closed
ehsanen opened this issue Feb 1, 2018 · 7 comments
Closed

Getting Nodejs to work on standalone C++ application on Windows #1087

ehsanen opened this issue Feb 1, 2018 · 7 comments

Comments

@ehsanen
Copy link

ehsanen commented Feb 1, 2018

  • Node.js 8.9.1:
  • Windows 10:
  • Scope (code, runtime):

We have embedded nodejs into our C++ crossplatform app. We faced a crash bug, which was cause by the lack of stdin when you do not run the node from command line. We noticed that there is already a pull request #11863 for this issue and we have these changes in our nodejs code. But this does not solve our problem as the PlatformInit() function in node.cc never gets called. Here is how we initialise node and v8 in our program:

platform_ = new node::NodePlatform(8, loop_, nullptr);
node::i18n::InitializeICUDirectory("");
v8::V8::InitializePlatform(platform_);
v8::V8::Initialize();

copied directly from SetUp() function in node_test_fixture.h.

interestingly we just face this problem on Windows not on macOs. We solved it now by adding this line

NODE_EXTERN void PlatformInit();

to node.h file in the node namespace, and then calling PlatformInit() before creating the NodePlatform. So now our initialisation looks like this:

node::PlatformInit(); // new line added
platform_ = new node::NodePlatform(8, loop_, nullptr);
node::i18n::InitializeICUDirectory("");
v8::V8::InitializePlatform(platform_);
v8::V8::Initialize();

Now we are wondering is our initialisation wrong at first place or is this really a bug and we need to create a pull request for it. Please advice.

@bnoordhuis
Copy link
Member

which was cause by the lack of stdin when you do not run the node from command line

Yes, PlatformInit() guards against that by ensuring that the stdio file descriptors are valid. As an embedder, the best way forward is probably to duplicate that logic; it's only a few lines of code.

Calling PlatformInit() is not a good idea. It's a grab bag of things for the standalone binary. The initialization it does may not be suitable for the embedding application.

@ehsanen
Copy link
Author

ehsanen commented Feb 1, 2018

thanks for the reply. I assume by duplicating the logic you mean to do what the PlatformInit does right now, somewhere in my own source code. But do I have access to all the variables and defined objects that PlatformInit uses? I don't have my windows laptop nearby right now, but will give it a shot soon

@bnoordhuis
Copy link
Member

You don't need anything specific. The logic is this: https://github.com/nodejs/node/blob/v9.5.0/src/node.cc#L4422-L4434

@ehsanen
Copy link
Author

ehsanen commented Feb 5, 2018

I've tested that today and it worked. I did what bnoordhuis has suggested, duplicating the logic that PlatformInit does in my code and avoid touching the nodejs source code.

@ehsanen ehsanen closed this as completed Feb 5, 2018
@DiegoBM
Copy link

DiegoBM commented Nov 4, 2018

ehsanen, could you please explain in more detail (hopefully with code example) how did you embed node.js without modifying node's source code? And how are you using it exactly (use the node loop?)

@viferga
Copy link

viferga commented Jan 9, 2019

@DiegoBM I have detailed in this issue (nodejs/node#23265 (comment)) how I have solved the problem of NodeJS embedding with examples and a working implementation.

@DiegoBM
Copy link

DiegoBM commented Jan 10, 2019

Thank you @viferga I finally solved my problem with a different solution, but if I consider integrating node.js again I'll consider your implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants