// POINTER 1
const P_BUFFER_SIZE = 2;
// GET THE POINTER TO WASM MEMORY FROM RUST
const p_mem_pointer = get_wasm_p_buffer_pointer(); // <- needs to be multiple of 4
// CREATE THE MEMORY BUFFER
const P_BUFFER = new Uint32Array(rust_WASM.memory.buffer, p_mem_pointer, P_BUFFER_SIZE);
Now I'm not familiar how pointer allocation etc. works. To me it seems I'm just unlucky to not get a pointer which is a multiple of 4. However, for a couple of months I haven't gotten this error, so I had to be very lucky...
So what is the actual issue here, and how can I resolve it?
The error is given at JS side, at line P_BUFFER = new Uint32Array(...);, where the message is:
RangeError: start offset of Uint32Array should be a multiple of 4 at new Uint32Array ()
This "pointer being a multiple" thing is called alignment. The specific pointer may have any non-null value permitted by its alignment, so your code may work or not work depending on phases of the moon.
If you need specific alignment, you must use alignment rules and modifiers. Specifically, array [T; N] has the same alignment as T. Since P_BUFFER: [u8; P_BUFFER_SIZE], its alignment is the same as u8, i.e. usually 1, while you need alignment of 4. The easiest way to do it is to follow the hint in the name of Uint32Array and to use an array of u32, which has alignment at least 4.
Note that P_BUFFER in my example is 4 bytes long (equal to std::mem::size_of::<u32>()), while in your example it's 2 bytes long. Since the array in my example always has a size multiple of 4, you can't get it smaller.
It is actually possible to make an array of size 2 and alignment 4, via the alignment attribute:
#[repr(align(4))]
struct AlignedArray([u8; 2]);
However, I would not recommend you to do it. The function you call is Uint32Array, so it expects an array of u32. Providing an array of wrong size will either result in a new error, or will silently corrupt your data (I don't work with WASM, so don't know which of those will happen).