DLR-FT/wasm-interpreter

Refactor WasmReader

Open

#58 创建于 2024年7月29日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Rust (5 fork)auto 404
coreenhancementhelp wantedpriority-medium

仓库指标

Star
 (37 star)
PR 合并指标
 (PR 指标待抓取)

描述

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

贡献者指南