WebAssembly / WebAssembly/spec

[js-api] Can we get a constant for page size?

Open
#1,433 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
3.5k
Forks
539
Avg merge
2d 1h
Merged PRs (30d)
13

Description

Currently in order to prepare some memory in the JS API we need to specify the size of the memory in page sizes. However if we already have some memory in say a ArrayBuffer, in order to actually be able to store that in WASM memory we need to convert it's size using the page size:

const PAGE_SIZE = 2**16;

function allocateMemory(buffer) {
    const memory = new WebAssembly.Memory({
        initial: Math.ceil(buffer.byteLength / PAGE_SIZE),
    });
    const memBytes = new Uint8Array(memory.buffer);
    memBytes.set(new Uint8Array(buffer), 0);
    return memory;
}

For users who are manually intiailizing memories, the exact value of the page size is fairly immaterial (and I for one have to look it up everytime). It would be nice if this were just exposed as a constant so we can just use it without remembering:

const { PAGE_SIZE } = WebAssembly.Memory;

The change for this would be pretty trivial, just add the following line to Memory in the IDL:

interface Memory {
    const unsigned long PAGE_SIZE = 65536;
    // ...
}

I'm not sure if Wasm64 is intended to have a different page size, if so it would probably be better to call it PAGE_SIZE_32.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the WebAssembly.Memory interface in the IDL and inspect the JavaScript API and specification tests that cover Memory. Determine whether the constant should apply to current memories and possible Wasm64 behavior, then add coverage showing the exposed page-size value is 65536.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, wasm
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.