Advertisement
quantumech

Untitled

Apr 23rd, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  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 in JS like an array. 'returnX(Point(1,5))' => 'Module.returnX([1,5])'
  7. // This struct is treated like an array because it is being registered as a 'value_array'
  8. struct Point
  9. {
  10.     int x;
  11.     int y;
  12. };
  13.  
  14. int returnX(Point pt)
  15. {
  16.     return pt.x;
  17. }
  18.  
  19. EMSCRIPTEN_BINDINGS(Module)
  20. {
  21.     value_array<Point>("Point") // <-- Register 'value_array' called 'Point'. 'value_arrays'
  22.                                 //     are structs that are treated like arrays in JS.
  23.         .element(&Point::x)     // <-- Set 'Point::X' to be the first element
  24.         .element(&Point::y);    // <-- Set 'Point::Y' to be the second element
  25.  
  26.     function("returnX", &returnX); // <-- Register method to return the X value of 'Point' struct
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement