hxcpp icon indicating copy to clipboard operation
hxcpp copied to clipboard

Embind

Open damoebius opened this issue 6 years ago • 4 comments

Embind is used to bind C++ functions and classes to JavaScript, so that the compiled code can be used in a natural way by “normal” JavaScript. According to this documentation how can i use Embind in my Haxe code ? https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html

#include <emscripten/bind.h>

using namespace emscripten;

float lerp(float a, float b, float t) {
    return (1 - t) * a + t * b;
}

EMSCRIPTEN_BINDINGS(my_module) {
    function("lerp", &lerp);
}

damoebius avatar Jun 18 '18 12:06 damoebius

Have you considered automatically generating bindings using Haxe build macro?

First analyse your haxe class, and generate equivalent binding in c++ like this :

EMSCRIPTEN_BINDINGS(my_class_example) {
  class_<MyClass>("MyClass")
    .constructor<int, std::string>()
    .function("incrementX", &MyClass::incrementX)
    .property("x", &MyClass::getX, &MyClass::setX)
    .class_function("getStringFromInstance", &MyClass::getStringFromInstance)
    ;
}

I did something similar for Wren bindings with Wrenegade

darmie avatar Apr 27 '19 12:04 darmie

It depends on what you are trying to do.

  1. Write haxe, compile to hxcpp, compile to js - then using from native js
  2. Embed a js interpreter inside a hxcpp host.
  3. Use hxcpp build tool to compile a NDLL to be js callable

I. have decided not to really support this because I can't think of a use case where compiling haxe straight to js would not be the better solution. 2. I am not supporting really because this is what cppia is for. I you can do it with macros, then I highly recommend this solution. 3. This is how I have implemented NME in Js. It gets tricky with garbage collection, but it is possible.

hughsando avatar Apr 27 '19 12:04 hughsando

Some native dependencies won't be available from browser, the steps highlighted by @hughsando seems like it will only work in nodejs environment.

Imagine I build a game engine that uses Wren for scripting, if I want the game to work in the browser, I need a way to make the wren VM run in the browser. This is where emscripten comes in.

darmie avatar Apr 27 '19 13:04 darmie

Yes - makes sense - and also why I like to script in haxe.

hughsando avatar Apr 27 '19 14:04 hughsando