DLR-FT/wasm-interpreter

Refactor WasmReader

Open

#58 opened on Jul 29, 2024

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Rust (5 forks)auto 404
coreenhancementhelp wantedpriority-medium

Repository metrics

Stars
 (37 stars)
PR merge metrics
 (PR metrics pending)

Description

The WasmReader is currently used to (1) store a reference to the WASM bytecode and (2) an index for the current location in the bytecode. This issue proposes a refactor for the WasmReader. Its replacement will be a more generic WasmCursor that is just a newtype wrapper around (2):

/// Newtype for an index into the WASM bytecode
pub struct WasmCursor(pub usize);

impl WasmCursor {
    /// Its methods now need to specify the WASM bytecode as an additional argument
    pub fn peek_u8(&self, wasm: &[u8]) -> Result<u8> { .. }
}

Pros

  1. Better separation of responsibilities: The cursor should not need store the bytecode. This also comes with the removal of the lifetime annotation on the struct
  2. Less memory usage: 1 * usize instead of 3 * usize. In theory, even a u32 would suffice, as WASM only allows for 32-bit indexing.
  3. WasmCursor can be used as the program counter / instruction index

Cons

  1. Now the WASM bytecode has to be specified as an additional argument for most methods
  2. More overhead for the developer, who now has to provide 1 additional argument for every method call

Contributor guide