cawfree / react-native-webassembly

⚛️ 🏎 WebAssembly for React Native powered by JSI.
https://twitter.com/cawfree
MIT License
329 stars 6 forks source link

Memory allocation in Webassembly #21

Open tainnhan opened 1 year ago

tainnhan commented 1 year ago

Hello,

I'm currently trying to integrate rust code into my expo app. After compiling my rust code into wasm I have a sign_bg.wasm.d.ts. The function signature look like this:

/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function add(a: number, b: number): number;
export function greet(a: number, b: number, c: number): void;
export function generate_eddsa_signature(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_exn_store(a: number): void;
        const module = await WebAssembly.instantiate<{
          memory: WebAssembly.Memory;
          __wbindgen_add_to_stack_pointer: (a: number) => number;
          __wbindgen_malloc: (a: number, b: number) => number;
          __wbindgen_realloc: (
            a: number,
            b: number,
            c: number,
            d: number
          ) => number;
          __wbindgen_free: (a: number, b: number, c: number) => void;
        }>(wasmBytes);

After this initializitation I use

let ptr = module.instance.exports. __wbindgen_malloc(5,1);
console.log(ptr); // gives me 1114120 

I checked my size of memory

let length = module.instance.exports.memory.byteLength;
console.log(length) // gives me 1114112

How can the allocated memory of my pointer be outside of the boundary of my Assembly Memory? Do I misunderstand here something?

cawfree commented 12 months ago

Hey @tainnhan thanks for raising.

Did you manage to determine where the extra two bytes are coming from?