I have the following C struct
typedef struct {
ValueType type;
union {
bool boolean;
double number;
Obj* obj;
} as;
} Value;
where ValueType
is an enum as follows
typedef enum {
VALUE_BOOL,
VALUE_NIL,
VALUE_NUMBER,
VALUE_OBJ
} ValueType;
Using WASM, I define an external JS file called js_library.js
within which is a function called accessStruct with the following signature defined in C.
extern void accessStruct(Value value);
I implement the function in JS as follows
function accessStruct(value) {
console.log(Module.getValue(value, "i32"));
}
I can accurately log the value of the ValueType
field as an integer. However, I was under the impression that if I simply increment the pointer by the required offset, I could access the remaining struct fields since they need to be laid out next to each other in memory, in the order specified.
So, I tried
function accessStruct(value) {
console.log(Module.getValue(value, "i32"));
const v = value + 32;
console.log(Module.getValue(v, "i64"));
}
ValueType
occupies 4 bytes = 32 bits. If the ValueType
is a double, I should be able to access the double value through the Module.getValue(v, "i64");
line of code but I get garbage back.
Where am I going wrong?