-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Embed io.js in a C++ 3d game engine? #537
Comments
I think the answer depends on whether you want to use features that exist in io.js but not in V8, like networking, file system access, the module system, etc. Building io.js as a static or shared library isn't currently officially supported but it's on the roadmap (see nodejs/roadmap#9.) For now, you can try patching node.gyp by hand: set the type of the 'iojs' target in node.gyp to 'shared_library' ('static_library' won't work) and drop src/node_main.cc from the sources list. The easy entry point is node::Start() whereas the node::CreateEnvironment() functions are more flexible. You can find their definitions in src/node.h. Good luck! |
Thank you very much. I'll try now! Just, where is the gyp command? |
GYP is bundled with io.js (it's in tools/gyp) but you don't have to deal with it directly. Edit node.gyp, then run configure and make, and the build system will take care of it. EDIT: Or, if you're using MSVC, use vcbuild.bat. |
Oh yes it's in VCBuild. BTW the v1.x branch can't compile, I was froced to switch to tag v1.0.3 |
Hi, it's building as |
I think you may have to change RuntimeLibrary from 0 to 3 in the msvs_settings section in common.gypi. |
Thank you! I tried in node.gyp it wasn't working... |
How do you use |
Probably best explained through working code. Take a look at how src/node.cc sets up a v8::Context before calling CreateEnvironment().
You can add a lib/_third_party_main.js to node.gyp that io.js will pick up on a rebuild. There are other options but they are complicated. Take a look at src/node.js and lib/module.js and go from there. |
Thank you. There is just the argv and argc I don't know what to put. So I understand that node.js is called before our script. Useful. Do I need to rebuild the lib each time I edit node.js or third_party? Is there a way that I specify third_party script folder in my engine directory (io.js is in a subfolder)? And in my engine (so not in io.js, I'd rather avoid rebuilding everything in order to add a single function), how do I expose some functions that third_party_main.js or another custom script will be able to access in order to interact with my engine? PS: I'll try to add a section in the wiki when I've understood enough |
argv should look like
Yes. If you want to load code from disk, you can make lib/third_party_main.js do that, of course.
I'm not sure I understand the question. If you want to expose your own C++ functions, you can set them on the global object with your_context->Global(). third_party_main.js can then cache and delete them. |
So "iojs" is a constant? How do I load the script? I didn't see the line in node::Start, so I assume the script path is in argv, no? EDIT: I understood, I must do so : char* argv[] = { "iojs", "C:/OgreSDK/Projects/whitedrop/dist/media/scripts/hello.js", nullptr };
node::Start(2, argv);
So I need to rebuild it everytime, but I can tell to third party script to load an external script, am I right?
So I have to have some code that looks like node::Start, and just after this line I can inject things to my context? |
Oh, a last question, sorry. Where are the scripts in lib/ stored once built? |
@vinz243 they are stored directly in binary |
Oh yes I just saw :). How can I do so in my exe? |
you can use tools/js2c.py script. It compiles js files into an |
Thank you very much! |
Sorry guys :D I have a problem. I get this: This line concerned is: V8::InitializePlatform(new node::Platform(4)); In my Interface.cpp, taken from https://github.com/iojs/io.js/blob/v1.x/src/node.cc#L3638 #include <node_v8_platform.h> I get:
Any idea? |
@vinz243 You need to provide your own implementation of the v8::Platform class. io.js can't do it for you because it's not in control of V8 initialization. You could change |
I will try to copy both files for now, thank you! |
Oh no wait it needs utils and everything. So I think for the moment I'll just add NODE_EXTERN evn tough it's not recommended. |
With NODE_EXTERN it works. And I finally managed to expose a test API! :) |
Hi,
I'm trying to code a 3D game engine, and for the scripting, I wanted to use V8, but I thought it would be better to directly use node.js. But io.js seems more suited since the engine is libre and it supports ES6. Does it seems reasonable to you? Where should I start to implement a javacript API? I mean, there are already a handful good tutorials about V8, but I'd like it to integrate seamlessly with io.js.
Thanks
The text was updated successfully, but these errors were encountered: