2013-04-07 16 views
11

Tutorial emscripten daje dobre wyjaśnienie, jak współdziałać z funkcji C: https://github.com/kripken/emscripten/wiki/Interacting-with-codeInterakcje z C++ klas emscripten

Ale w jaki sposób interakcji z klas C++:

  • wezwać konstruktora utworzyć object
  • usunąć oBJ
  • Zapobiegaj martwy kod eliminację klas i jego metod
+2

ten powinien zostać rozwiązany szybko przez 'embind'. Myślę, że możesz rzucić okiem na https://github.com/kripken/emscripten/tree/master/tests/embind, ale nie wiesz, jak aktualny jest. – abergmeier

+0

Widziałem powyższy komentarz po napisaniu mojej odpowiedzi. Wygląda na to, że są teraz jakieś dokumenty na ten temat [tutaj] (https://github.com/imvu/emscripten/wiki/embind). Przyjrzę się użyciu 'embind', kiedy dostanę szansę. – lakenen

+0

Krótka przyczyna tego jest trudna, google na C++ mangling nazwy. – meawoppl

Odpowiedz

11

Sprawdź to: https://github.com/kripken/emscripten/wiki/embind

Przykład:

C++ Kod:

#include <emscripten/bind.h> 

using namespace emscripten; 

class MyClass { 
public: 
    MyClass(int x, std::string y) 
     : x(x) 
     , y(y) 
    {} 

    void incrementX() { 
     ++x; 
    } 

    int getX() const { return x; } 
    void setX(int x_) { x = x_; } 

    static std::string getStringFromInstance(const MyClass& instance) { 
     return instance.y; 
    } 

private: 
    int x; 
    std::string y; 
}; 

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

kod JS:

var instance = new Module.MyClass(10, "hello"); 
instance.incrementX(); 
instance.x; // 12 
instance.x = 20; // 20 
Module.MyClass.getStringFromInstance(instance); // "hello" 
instance.delete(); 
+0

Awsome :) Dzięki milionowi :) – Mortennobel

+0

Embind nie działa na Emscripten 1.16.0 – FacePalm

6

Sposób, w jaki to zrobiłem, polegał na utworzeniu funkcji "proxy", które wykonują niezbędne operacje. Na przykład:

class HelloWorld 
{ 
    int x; 
    public: 
    HelloWorld() { x = 0; } 
    ~HelloWorld() {} 
    void setX(int v) { x = v; } 
    int getX() { return x; } 
    // ... 
}; 


//compile using "C" linkage to avoid name obfuscation 
extern "C" { 
    //constructor, returns a pointer to the HelloWorld object 
    void *HW_constructor() { 
    return new HelloWorld(); 
    } 

    void HW_setX(HelloWorld *hw, int x) { 
    hw->setX(x); 
    } 

    int HW_getX(HelloWorld *hw) { 
    return hw->getX(); 
    } 

    void HW_destructor(HelloWorld *hw) { 
    delete hw; 
    } 
}; 

Następnie w JS, trzeba zbudować klona swojego przedmiotu, który wywołuje funkcji proxy (irytujące, wiem, ale ja nie znam lepszego rozwiązania w tej chwili):

// get references to the exposed proxy functions 
var HW_constructor = Module.cwrap('HW_constructor', 'number', []); 
var HW_destructor = Module.cwrap('HW_destructor', null, ['number']); 
var HW_setX = Module.cwrap('HW_setX', null, ['number', 'number']); 
var HW_getX = Module.cwrap('HW_getX', 'number', ['number']); 

function HelloWorld() { 
    this.ptr = HW_constructor(); 
} 

HelloWorld.prototype.destroy = function() { 
    HW_destructor(this.ptr); 
}; 

HelloWorld.prototype.setX = function (x) { 
    HW_setX(this.ptr, x); 
}; 

HelloWorld.prototype.getX = function() { 
    return HW_getX(this.ptr); 
}; 

WAŻNE Pamiętaj, aby to zadziałało, trzeba dodać następującą flagę polecenia EMCC, aby poinformować go, aby nie pozbawić się metody proxy jako martwy kod (Uwaga: podkreślenia tutaj jest celowy i ważny!):

emcc helloworld.cpp -o helloworld.js \ 
    -s EXPORTED_FUNCTIONS="['_HW_constructor','_HW_destructor','_HW_setX','_HW_getX']" 

EDYCJA: Stworzyłem gist dla ludzi, aby wypróbować kod.

+0

Dzięki - dobra robota .. ale miałem nadzieję na bardziej eleganckie rozwiązanie ;-) – Mortennobel

+0

Tak, to samo tutaj haha. Daj mi znać, jeśli znajdziesz taki! :) – lakenen

Powiązane problemy