SHARE
TWEET

Untitled

quantumech Apr 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <emscripten/bind.h>
  2.  
  3. // Bring Emscripten namespace into scope for the sake of readability
  4. using namespace emscripten;
  5.  
  6. // Struct that is treated like a class in JS such that 'Point::x' => 'instance.x'
  7. // is called a 'value_object'
  8. // 'value_objects' are treated as dictionaries in JS. You can even define new structs this way
  9. //
  10. // For example: 'let x = {x: 44, y: 66}' => 'Point pt = Point {44, 66}'
  11. struct Point
  12. {
  13.     int x;
  14.     int y;
  15. };
  16.  
  17. Point PointFactory(int x, int y)
  18. {
  19.     return Point {x, y};
  20. }
  21.  
  22. int getX(Point pt)
  23. {
  24.     return pt.x;
  25. }
  26.  
  27. EMSCRIPTEN_BINDINGS(Module)
  28. {
  29.     value_object<Point>("Point")   // <-- Register 'Point' as a value object such that
  30.         .field("x", &Point::x)     //     'Point::x' => 'instance.x' &
  31.         .field("y", &Point::y);    //     'Point::y' => 'instance.y'
  32.  
  33.     function("PointFactory", &PointFactory); // <-- Register method to return a new 'Point' struct instance
  34.  
  35.     function("getX", &getX);
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top