Update the walrus dependency (#2125)

This commit updates the `walrus` crate used in `wasm-bindgen`. The major
change here is how `walrus` handles element segments, exposing segments
rather than trying to keep a contiugous array of all the elements and
doing the splitting itself. That means that we need to do mroe logic
here in `wasm-bindgen` to juggle indices, segments, etc.
This commit is contained in:
Alex Crichton
2020-05-06 12:57:02 -05:00
committed by GitHub
parent dc54c0fb25
commit 8e3d6fe619
20 changed files with 242 additions and 120 deletions

View File

@ -10,5 +10,5 @@ description = "Utilities for working with Wasm codegen conventions (usually esta
edition = "2018"
[dependencies]
walrus = "0.14.0"
walrus = "0.16.0"
anyhow = "1.0"

View File

@ -6,13 +6,13 @@
//! * The shadow stack pointer
//! * The canonical linear memory that contains the shadow stack
#![deny(missing_docs, missing_debug_implementations)]
use anyhow::{anyhow, bail, Error};
use walrus::{ir::Value, GlobalId, GlobalKind, InitExpr, MemoryId, Module, ValType};
use anyhow::{anyhow, bail, Result};
use walrus::{
ir::Value, ElementId, FunctionId, GlobalId, GlobalKind, InitExpr, MemoryId, Module, ValType,
};
/// Get a Wasm module's canonical linear memory.
pub fn get_memory(module: &Module) -> Result<MemoryId, Error> {
pub fn get_memory(module: &Module) -> Result<MemoryId> {
let mut memories = module.memories.iter().map(|m| m.id());
let memory = memories.next();
if memories.next().is_some() {
@ -55,3 +55,40 @@ pub fn get_shadow_stack_pointer(module: &Module) -> Option<GlobalId> {
_ => None,
}
}
pub struct FunctionTableEntry {
pub element: ElementId,
pub idx: usize,
pub func: Option<FunctionId>,
}
/// Looks up a function table entry by index in the main function table.
pub fn get_function_table_entry(module: &Module, idx: u32) -> Result<FunctionTableEntry> {
let table = module
.tables
.main_function_table()?
.ok_or_else(|| anyhow!("no function table found in module"))?;
let table = module.tables.get(table);
for &segment in table.elem_segments.iter() {
let segment = module.elements.get(segment);
let offset = match &segment.kind {
walrus::ElementKind::Active {
offset: InitExpr::Value(Value::I32(n)),
..
} => *n as u32,
_ => continue,
};
let idx = (idx - offset) as usize;
match segment.members.get(idx) {
Some(slot) => {
return Ok(FunctionTableEntry {
element: segment.id(),
idx,
func: slot.clone(),
})
}
None => continue,
}
}
bail!("failed to find `{}` in function table", idx);
}