Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <emscripten/bind.h>
- // Bring Emscripten namespace into scope for the sake of readability
- using namespace emscripten;
- class Vertex
- {
- double x;
- double y;
- public:
- Vertex(int x, int y): x(x), y(y)
- {}
- double getX() const { return this->x; } // <--- Getters for 'properties' must be 'const' methods.
- void setX(double x) { this->x = x; } // Notice how this is different from returning a const
- double getY() const { return this->y; } // value. 'const' keyword declared this way tells the compiler
- // to throw an error if any attributes get changed inside of the method
- void setY(double y) { this->y = y; }
- void invert()
- {
- x = -x;
- y = -y;
- }
- static Vertex VertexFactory(double x, double y)
- {
- return Vertex(x, y);
- }
- };
- EMSCRIPTEN_BINDINGS(Module) {
- class_<Vertex>("Vertex")
- .constructor<double, double>() // <--- Register contructor that takes 2 doubles as arguments. You can also do '.constructor(&func)' to register an external function as a constructor
- .property("x", &Vertex::getX, &Vertex::setX) // <-- Register a 'property'. 'Properties' are JS variables
- .property("y", &Vertex::getY, &Vertex::setY) // that call getters and setters when a variable is accessed.
- // So when 'Vertex.x = 4' is called, setX() will be called to
- // mutate 'x'
- .function("invert", &Vertex::invert) // <-- Register an instance function (ie. a function that is called per instance)
- .class_function("VertexFactory", &Vertex::VertexFactory); // <-- Register a static function
- // (ie. a function that can be directly called
- // from the class)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement