In version 1.12, you could set a emcc compiler flag
RUNTIME_TYPE_INFO = 1 and it would emit a javascript object containing
the size and layouts of the types.
Example of the struct info...
C:typedef struct SFLocation {
double x;
double y;
SFLinearUnit unit;
bool empty;
} SFLocation;
JS (NOTE - this is the feature that is no longer available): "SFLocation" : {
"alignSize" : 8,
"fields" : [ "double", "double", "i32", "i8" ],
"flatIndexes" : [ 0, 8, 16, 20 ],
"flatSize" : 24,
"packed" : false
}
So when I need to call a C function that has this interface...
C:SFLocation ScreenGetCursorLocation(Screen * this);
JS:function _ScreenGetCursorLocation($agg$result, $this)
I have to allocate memory for
$agg$result somewhere. The two problems I have at runtime without the type info is...
1. I don't know the size of the struct, so I don't know how much memory to allocate.
2. I can't retrieve values from the struct because I don't know the
offsets or field types anymore (emscripten's "getValue" function
requires an 'address' and 'type' as arguments).
Is there a better way of doing this now with the newer compiler that I am not aware of?
Here is how I used to be able to do it:
var GetCursorLocation = Module.cwrap("ScreenGetCursorLocation", "number", ["number", "number"]);
var metadata = Runtime.typeInfo["SFLocation"];
var locationPtr = _malloc(metadata.flatSize);
GetCursorLocation(locationPtr, screen);
var x = getValue(locationPtr + metadata.flatIndexes[0], metadata.fields[0]);
var y = getValue(locationPtr + metadata.flatIndexes[1], metadata.fields[1]);
// ETC, ETC
_free(locationPtr);