Looking at the C code snippet below, there is a struct defined as follows
typedef struct {
ValueType type;
union {
bool boolean;
double number;
Obj* obj;
} as;
} Value;
The ValueType
enum used in the struct is defined as:
typedef enum {
VALUE_BOOL,
VALUE_NIL,
VALUE_NUMBER,
VALUE_OBJ
} ValueType;
To interact with this C code using the WebAssembly (WASM) technology, an external JavaScript file named js_library.js
is created. Inside this file, there is a function called accessStruct with the signature defined in C as:
extern void accessStruct(Value value);
The implementation of the accessStruct function in JavaScript is shown below:
function accessStruct(value) {
console.log(Module.getValue(value, "i32"));
}
However, attempting to access the remaining struct fields by incrementing the pointer does not yield the expected results. An example code snippet demonstrating this issue is given below:
function accessStruct(value) {
console.log(Module.getValue(value, "i32"));
const v = value + 32;
console.log(Module.getValue(v, "i64"));
}
Given the fact that ValueType
occupies 4 bytes (or 32 bits), it was assumed that accessing a double value should be possible through the line of code
Module.getValue(v, "i64")
. However, incorrect values are returned which leads to confusion and raises the question - where is the mistake?