language

Raw Memory

Reading and writing memory by address, for the ports and hot loops that need it, and how to see where a project does.

Raw-memory operations work on addresses rather than typed references: load/store, changetype, memory.copy/fill/repeat, the v128 load/store family, call_indirect and atomic.*. Any function may use them.

peek.as
export function peek(p: usize): i32 {
    return load<i32>(p);
}

The compiler checks none of them. A wrong address reads or overwrites whatever lives there in the script’s own memory, or traps past its end.

Seeing where they are

A build counts the raw-memory operations it compiles from the project’s own scripts and from each library. When there are any, rive logs one line naming them, and logs it again only when the counts change.

build log
raw memory operations: this project 1, draco 85

The JSON report of a --verify or a --publish (--format=json) carries the same counts as rawMemory, with a null library for the project’s own scripts. The standard library’s own operations are not counted, since wrapping them is its job. Neither is unchecked() element access, which skips a bounds check without using one of these operations. A count covers the code the build compiles, so a library function nothing calls adds nothing, and neither does a throw message once a publish strips it.

What to watch for

Addresses below 1024 are reserved: nothing rasc places lives there. An optimized build folds an address’s added constant into the access, so a load or store whose address wraps below 1024 may trap once optimized. load<i32>(p + 16) with p at -8 reads address 8 in a debug build and traps in an optimized one.

A raw pointer into an object is good for one call only. Objects may move between calls, so take the pointer where you use it and never keep it in a field. An async function or a generator outlives the call that started it, so W3019 warns of a usize it still holds at an await or a yield.

Older sources mark these functions unsafe, which the compiler once required. The word still parses and does nothing, and W1016 asks for its removal.