~ 3.5 Minute Read.
Web XR in C++… what?
Let’s get into some more technical details for those who are interested:
To pass data between C++ and JS with emscripten, you will want to use the heap. While passing smaller data via function arguments and return values is trivial, you can pass entire structures by allocating heap memory:
var yourStruct = Module._malloc(SIZEOF_YOUR_STRUCT);
setValue(yourStruct + 0, 42.0, 'double') setValue(yourStruct + 8, 42.0, 'float') setValue(yourStruct + 12, 42, 'i64') setValue(yourStruct + 20, 42, 'i32') setValue(yourStruct + 24, 42, 'i16') setValue(yourStruct + 28, 42, 'i8')
or in C/C++ as you would usually do (I hope).
I hope this helps someone out there, I know it would have saved be a minute to have this a little more condensed and in one place.
- This will probably be a blog post of its own sometime.
- I just noticed it’s missing an origin trial token may not run!
- In emscripten’s libraries you will often see
makeSetValue, which is meant to work with the JSON library which stores the offsets of the members of all the C structs, though.
Written in 60 minutes, edited in 5 minutes.