Sitelet https://github.com/nodejs/node/issues/537
Skip to content

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

Description

@vinz243

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

Activity

  1. bnoordhuis commented on Jan 21, 2015

    @bnoordhuis
    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!

  2. vinz243 commented on Jan 21, 2015

    @vinz243
    Author

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

  3. bnoordhuis commented on Jan 21, 2015

    @bnoordhuis
    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.

  4. vinz243 commented on Jan 21, 2015

    @vinz243
    Author

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

  5. vinz243 commented on Jan 21, 2015

    @vinz243
    Author

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

  6. bnoordhuis commented on Jan 21, 2015

    @bnoordhuis
    Member

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

  7. vinz243 commented on Jan 21, 2015

    @vinz243
    Author

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

  8. vinz243 commented on Jan 22, 2015

    @vinz243
    Author

    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)

  9. bnoordhuis commented on Jan 22, 2015

    @bnoordhuis
    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.

  10. vinz243 commented on Jan 23, 2015

    @vinz243
    Author

    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

  11. bnoordhuis commented on Jan 23, 2015

    @bnoordhuis
    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.

  12. vinz243 commented on Jan 23, 2015

    @vinz243
    Author

    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?

  13. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

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

  14. vkurchatkin commented on Jan 24, 2015

    @vkurchatkin
    Contributor

    @vinz243 they are stored directly in binary

  15. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

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

  16. vkurchatkin commented on Jan 24, 2015

    @vkurchatkin
    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 .

  17. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

    Thank you very much!

  18. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

    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?

  19. bnoordhuis commented on Jan 24, 2015

    @bnoordhuis
    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.

  20. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

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

  21. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

    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.

  22. vinz243 commented on Jan 24, 2015

    @vinz243
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions