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

Embed io.js in a C++ 3d game engine? #537

Closed
vinz243 opened this issue Jan 21, 2015 · 22 comments
Closed

Embed io.js in a C++ 3d game engine? #537

vinz243 opened this issue Jan 21, 2015 · 22 comments

Comments

@vinz243
Copy link

vinz243 commented Jan 21, 2015

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

@bnoordhuis
Copy link
Member

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!

@vinz243
Copy link
Author

vinz243 commented Jan 21, 2015

Thank you very much. I'll try now! Just, where is the gyp command?
https://github.com/whitedrop/io.js#windows

@bnoordhuis
Copy link
Member

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.

@vinz243
Copy link
Author

vinz243 commented Jan 21, 2015

Oh yes it's in VCBuild. BTW the v1.x branch can't compile, I was froced to switch to tag v1.0.3

@vinz243
Copy link
Author

vinz243 commented Jan 21, 2015

Hi, it's building as /MTd. How can I build it as /MDd?

@bnoordhuis
Copy link
Member

I think you may have to change RuntimeLibrary from 0 to 3 in the msvs_settings section in common.gypi.

@vinz243
Copy link
Author

vinz243 commented Jan 21, 2015

Thank you! I tried in node.gyp it wasn't working...

@vinz243 vinz243 closed this as completed Jan 21, 2015
@vinz243
Copy link
Author

vinz243 commented Jan 22, 2015

How do you use node::CreateEnvironment? And how do I add some functions or classes in a module (ie require('engine')), without rewriting io.js source code (in my code)

@vinz243 vinz243 reopened this Jan 22, 2015
@bnoordhuis
Copy link
Member

How do you use node::CreateEnvironment?

Probably best explained through working code. Take a look at how src/node.cc sets up a v8::Context before calling CreateEnvironment().

And how do I add some functions or classes in a module (ie require('engine')), without rewriting io.js source code (in my code)

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.

@vinz243
Copy link
Author

vinz243 commented Jan 23, 2015

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

@bnoordhuis
Copy link
Member

There is just the argv and argc I don't know what to put.

argv should look like char* argv[] = {"iojs", "arg1", "arg2", nullptr} and argc is the number of elements in the array minus the trailing nullptr.

Do I need to rebuild the lib each time I edit node.js or third_party?

Yes. If you want to load code from disk, you can make lib/third_party_main.js do that, of course.

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?

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.

@vinz243
Copy link
Author

vinz243 commented Jan 23, 2015

argv should look like char* argv[] = {"iojs", "arg1", "arg2", nullptr} and argc is the number of elements in the array minus the trailing nullptr.

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);

Yes. If you want to load code from disk, you can make lib/third_party_main.js do that, of course.

So I need to rebuild it everytime, but I can tell to third party script to load an external script, am I right?

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 I have to have some code that looks like node::Start, and just after this line I can inject things to my context?

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

Oh, a last question, sorry. Where are the scripts in lib/ stored once built?

@vkurchatkin
Copy link
Contributor

@vinz243 they are stored directly in binary

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

Oh yes I just saw :). How can I do so in my exe?

@vkurchatkin
Copy link
Contributor

you can use tools/js2c.py script. It compiles js files into an .h file that you can include in your C or C++ (or maybe Objective-C) code .

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

Thank you very much!

@vinz243 vinz243 closed this as completed Jan 24, 2015
@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

Sorry guys :D I have a problem. I get this: error C2039: 'Platform' : is not a member of 'node'

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
If I add this line:

#include <node_v8_platform.h>

I get:

error LNK2019: unresolved external symbol "public: __thiscall node::Platform::Platform(unsigned int)" (??0Platform@node@@QAE@I@Z) referenced in function "public: void __thiscall Scribe::V8Interface::initialize(void)" (?initialize@V8Interface@Scribe@@QAEXXZ)   

Any idea?

@vinz243 vinz243 reopened this Jan 24, 2015
@bnoordhuis
Copy link
Member

@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 class Platform : ... to class NODE_EXTERN Platform : ... in node_v8_platform.h (NODE_EXTERN is in node.h) but keep in mind that node::Platform is an internal class with no promise of API or ABI stability; it may change at any time.

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

I will try to copy both files for now, thank you!

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

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.

@vinz243
Copy link
Author

vinz243 commented Jan 24, 2015

With NODE_EXTERN it works. And I finally managed to expose a test API! :)

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

3 participants