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;
- // Struct that is treated like a class in JS such that 'Point::x' => 'instance.x'
- // is called a 'value_object'
- // 'value_objects' are treated as dictionaries in JS. You can even define new structs this way
- //
- // For example: 'let x = {x: 44, y: 66}' => 'Point pt = Point {44, 66}'
- struct Point
- {
- int x;
- int y;
- };
- Point PointFactory(int x, int y)
- {
- return Point {x, y};
- }
- int getX(Point pt)
- {
- return pt.x;
- }
- EMSCRIPTEN_BINDINGS(Module)
- {
- value_object<Point>("Point") // <-- Register 'Point' as a value object such that
- .field("x", &Point::x) // 'Point::x' => 'instance.x' &
- .field("y", &Point::y); // 'Point::y' => 'instance.y'
- function("PointFactory", &PointFactory); // <-- Register method to return a new 'Point' struct instance
- function("getX", &getX);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement