Skip to content

Javascript Interop #1

@csjh

Description

@csjh

Goals:

  • Vite plugin, enabling import { func } from "./something.mite"
    • func should be able to return arbitrary numerical-based struct/num/etc
      • arbitrary numerical-based struct would be (wasm/mite-side) heap allocated
        • maybe a special part of the heap with sizeof(struct)-sized chunks for faster alloc?
      • automatically allocated (memcpy) w/ returned stack variables
      • property accessing is just getter/setter wrapping dataview.set<somenum> or nested struct
      • free'd with FinalizationRegistry
  • Typescript type gen
  • Handle v128 somehow
  • Should generate something like
declare const wasm_memory: WebAssembly.Memory;
declare const _free: (ptr: number) => void;
const freedom = new FinalizationRegistry<number>((ptr) => _free(ptr));

class MyStruct {
    #view: DataView;
    static sizeof = 12;

    constructor(ptr: number) {
        this.#view = new DataView(wasm_memory.buffer, ptr, MyStruct.sizeof);
        freedom.register(this, ptr);
    }

    get x(): number {
        return this.#view.getUint32(0, true);
    }
    set x(value: number) {
        this.#view.setUint32(0, value, true);
    }

    get y(): bigint {
        return this.#view.getBigInt64(0, true);
    }
    set y(value: bigint) {
        this.#view.setBigInt64(0, value, true);
    }
}

for

struct MyStruct {
    u32 x;
    i64 y;
}
  • special case primitive arrays into their corresponding TypedArray representation
  • otherwise just use something like Array.from({ length: returned_length }, (_, i) => new MyStruct(base + i * MyStruct.sizeof))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions