Skip to content

How to read the real byteOffset? #928

Closed
@zie1ony

Description

@zie1ony

I try to read an array of bytes on my host:

@external("env", "read_bytes")
declare function read_bytes(
  ptr: i32,
  size: i32,
): void;

export function call(): void {
    var value = new Uint8Array(3);
    value[0] = 1;
    value[1] = 2;
    value[2] = 3;
    read_bytes(
        value.byteOffset, // returns 0
        value.byteLength
    );

    var value2 = new Uint8Array(3);
    value2[0] = 4;
    value2[1] = 5;
    value2[2] = 6;
    read_bytes(
        value2.byteOffset,  // returns 0
        value2.byteLength
    );
}

I expected byteOffset to return a point in WASM memory where structure starts, but it doesn't work like that.

I have the solution, that works fine in Rust:

let bytes: [u8; 32] = [255u8; 32];
let ptr = bytes.as_ptr();
let size = bytes.len();

unsafe {
    read_bytes(ptr, size);
}

Is it possible to have something similar in AssemblyScript?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions