chore: add getPagePointer to enable zero-copy memory reads from JS#175
Conversation
Implements the approach suggested in tomusdrw#113: expose a WASM-side function that returns the linear memory pointer for an already-allocated page buffer, letting JS copy bytes directly without any extra WASM allocation. - `Memory.getPagePointer(pageIndex)` – core method; returns 0 on page/access fault - `getPagePointer(page)` – exported from api-debugger (deprecates `getMemory`) - `pvmGetPagePointer(pvmId, page)` – exported from api-utils (deprecates `pvmReadMemory`) - `Assert.isNotEqual` helper added to test.ts - 5 new tests in memory.test.ts covering fault cases and data correctness 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR adds a zero-copy memory-access API across three layers: Memory.getPagePointer(pageIndex: u32): usize, api-debugger.getPagePointer(page: u32): usize, and pvmGetPagePointer(pvmId: u32, page: u32): usize. Each returns the WASM linear memory pointer (byte offset) for a page's backing buffer or 0 when the page is missing or not readable. Tests for page-pointer behavior were added, and an Assert.isNotEqual helper was introduced for tests. getMemory remains but its docs were updated to recommend the pointer-based approach. Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@assembly/memory.test.ts`:
- Around line 242-285: Add a test that covers the branch where a page exists in
the pages map but lacks read permission: use MemoryBuilder.setData with
Access.None at RESERVED_MEMORY to allocate a page with no read access, build the
Memory via new MemoryBuilder().build(), compute pageIndex as RESERVED_MEMORY >>
PAGE_SIZE_SHIFT, call getPagePointer(pageIndex) and assert it equals <usize>0;
this targets the !page.can(Access.Read) branch in getPagePointer.
- Around line 260-273: Rename the test case to match its setup and assertion:
update the test name string in the test that constructs a MemoryBuilder, calls
builder.setData(Access.Read, pageAddress, ...), builds mem, computes pageIndex
and calls mem.getPagePointer(pageIndex) so it reflects that this is verifying a
read-only (or readable) page returns a non-zero pointer (e.g. "getPagePointer
returns non-zero for read-only page"); leave the body and assertion
(assert.isNotEqual(ptr, <usize>0)) unchanged and only change the test name
string to match the intent.
In `@assembly/memory.ts`:
- Around line 132-145: The JS examples in the JSDoc use the signed right-shift
operator (address >> PAGE_SIZE_SHIFT) which yields negative page indices for
addresses >= 0x80000000; update these to the unsigned right-shift (address >>>
PAGE_SIZE_SHIFT) in the snippet in assembly/memory.ts and the equivalent JSDoc
snippets for pvmGetPagePointer (assembly/api-utils.ts) and getPagePointer
(assembly/api-debugger.ts) so page calculations are correct across the full
32-bit address space.
|
paiechali |
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@assembly/memory.test.ts`:
- Line 301: The assertion uses redundant pointer arithmetic in the test: replace
load<u8>(ptr + 0) with load<u8>(ptr) to remove the unnecessary "+ 0" noise;
update the assertion in assembly/memory.test.ts where the variable ptr and the
call to load<u8> are used so the message and behavior remain the same but the
expression is simplified.
---
Duplicate comments:
In `@assembly/memory.test.ts`:
- Around line 242-259: Add a test that covers the branch where a page exists but
is not readable: use MemoryBuilder to build a Memory, create/insert a Page entry
into the Memory.pages map at a non-reserved index with Access.None (so
page.can(Access.Read) returns false), then call Memory.getPagePointer(pageIndex)
and assert it returns <usize>0; reference MemoryBuilder, Memory.getPagePointer,
the pages map, page.can, and Access.None to locate the code to modify.
Implements the approach suggested in #113: expose a WASM-side function that returns the linear memory pointer for an already-allocated page buffer, letting JS copy bytes directly without any extra WASM allocation.
closes #113