Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Walrus to v0.21 #4037

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
* `UnwrapThrowExt for Result` now makes use of the required `Debug` bound to display the error as well.
[#4035](https://github.com/rustwasm/wasm-bindgen/pull/4035)

* MSRV of CLI tools bumped to v1.76. This does not affect libraries like `wasm-bindgen`, `js-sys` and `web-sys`!
[#4037](https://github.com/rustwasm/wasm-bindgen/pull/4037)

### Fixed

* Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ log = "0.4"
rustc-demangle = "0.1.13"
serde_json = "1.0"
tempfile = "3.0"
walrus = "0.20.2"
walrus = "0.21"
wasm-bindgen-externref-xform = { path = '../externref-xform', version = '=0.2.92' }
wasm-bindgen-multi-value-xform = { path = '../multi-value-xform', version = '=0.2.92' }
wasm-bindgen-shared = { path = "../shared", version = '=0.2.92' }
Expand Down
60 changes: 53 additions & 7 deletions crates/cli-support/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
//! functions.

use crate::descriptor::{Closure, Descriptor};
use anyhow::Error;
use anyhow::{bail, Error};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use walrus::ImportId;
use std::collections::HashMap;
use walrus::{ConstExpr, ElementItems, ElementKind, ImportId, RefType};
use walrus::{CustomSection, FunctionId, Module, TypedCustomSectionId};
use wasm_bindgen_wasm_interpreter::Interpreter;

Expand Down Expand Up @@ -87,7 +87,7 @@ impl WasmBindgenDescriptorsSection {
// Find all functions which call `wbindgen_describe_closure`. These are
// specially codegen'd so we know the rough structure of them. For each
// one we delegate to the interpreter to figure out the actual result.
let mut element_removal_list = HashSet::new();
let mut element_removal_list = HashMap::new();
let mut func_to_descriptor = HashMap::new();
for (id, local) in module.funcs.iter_local() {
let mut find = FindDescribeClosure {
Expand All @@ -106,9 +106,55 @@ impl WasmBindgenDescriptorsSection {
// For all indirect functions that were closure descriptors, delete them
// from the function table since we've executed them and they're not
// necessary in the final binary.
for (segment, idx) in element_removal_list {
log::trace!("delete element {}", idx);
module.elements.get_mut(segment).members[idx] = None;
for (segment, idxs) in element_removal_list {
let segment = module.elements.get_mut(segment);

let items = match &mut segment.items {
ElementItems::Functions(items) => items,
ElementItems::Expressions(_, items) => {
for idx in idxs {
log::trace!("delete element {}", idx);
items[idx] = ConstExpr::RefNull(RefType::Funcref)
}

continue;
}
};

let (table, offset) = match &segment.kind {
ElementKind::Active {
table,
offset: ConstExpr::Value(Value::I32(n)),
} => (*table, *n),
_ => bail!("somehow found a closure in an unexpected element segment"),
};

let mut to_insert = Vec::new();

for idx in idxs.into_iter().rev() {
log::trace!("delete element {}", idx);

items.remove(idx);

// Last item, no need to do anything fancy.
if items.len() == idx {
continue;
}

let block = items.split_off(idx);
let offset = offset + idx as i32 + 1;
let offset = ConstExpr::Value(Value::I32(offset));

to_insert.push((offset, block));
}

for (offset, block) in to_insert.into_iter().rev() {
let id = module.elements.add(
ElementKind::Active { table, offset },
ElementItems::Functions(block),
);
module.tables.get_mut(table).elem_segments.insert(id);
}
}

// And finally replace all calls of `wbindgen_describe_closure` with a
Expand Down
55 changes: 34 additions & 21 deletions crates/cli-support/src/externref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::wit::{AdapterKind, Instruction, NonstandardWitSection};
use crate::wit::{AdapterType, InstructionData, StackChange, WasmBindgenAux};
use anyhow::Result;
use std::collections::HashMap;
use walrus::{ir::Value, ElementKind, InitExpr, Module};
use walrus::ElementItems;
use walrus::{ir::Value, ConstExpr, ElementKind, Module};
use wasm_bindgen_externref_xform::Context;

pub fn process(module: &mut Module) -> Result<()> {
Expand Down Expand Up @@ -397,11 +398,22 @@ pub fn force_contiguous_elements(module: &mut Module) -> Result<()> {
// Here we take a look at all element segments in the module to see if we
// need to split them.
for segment in module.elements.iter_mut() {
// If this segment has all-`Some` members then it's already contiguous
// and we can skip it.
if segment.members.iter().all(|m| m.is_some()) {
continue;
}
let (ty, items) = match &mut segment.items {
ElementItems::Expressions(ty, items) => {
// If this segment has no null reference members then it's already
// contiguous and we can skip it.
if items
.iter()
.all(|item| !matches!(item, ConstExpr::RefNull(_)))
{
continue;
}

(*ty, items)
}
// Function index segments don't have holes.
ElementItems::Functions(_) => continue,
};

// For now active segments are all we're interested in since
// passive/declared have no hope of being MVP-compatible anyway.
Expand All @@ -410,7 +422,7 @@ pub fn force_contiguous_elements(module: &mut Module) -> Result<()> {
let (table, offset) = match &segment.kind {
ElementKind::Active {
table,
offset: InitExpr::Value(Value::I32(n)),
offset: ConstExpr::Value(Value::I32(n)),
} => (*table, *n),
_ => continue,
};
Expand All @@ -425,16 +437,13 @@ pub fn force_contiguous_elements(module: &mut Module) -> Result<()> {
// offset.
let mut commit = |last_idx: usize, block: Vec<_>| {
let new_offset = offset + (last_idx - block.len()) as i32;
let new_offset = InitExpr::Value(Value::I32(new_offset));
new_segments.push((table, new_offset, segment.ty, block));
let new_offset = ConstExpr::Value(Value::I32(new_offset));
new_segments.push((table, new_offset, ty, block));
};
for (i, id) in segment.members.iter().enumerate() {
match id {
// If we find a function, then we either start a new block or
// push it onto the existing block.
Some(id) => block.get_or_insert(Vec::new()).push(Some(*id)),
None => {
let block = match block.take() {
for (i, expr) in items.iter().enumerate() {
match expr {
ConstExpr::RefNull(_) => {
let block: Vec<_> = match block.take() {
Some(b) => b,
None => continue,
};
Expand All @@ -449,21 +458,25 @@ pub fn force_contiguous_elements(module: &mut Module) -> Result<()> {
commit(i, block);
}
}
// If we find a function, then we either start a new block or
// push it onto the existing block.
_ => block.get_or_insert(Vec::new()).push(*expr),
}
}

// If there's no trailing empty slots then we commit the last block onto
// the new segment list.
if let Some(block) = block {
commit(segment.members.len(), block);
commit(items.len(), block);
}
segment.members.truncate(truncate);
items.truncate(truncate);
}

for (table, offset, ty, members) in new_segments {
let id = module
.elements
.add(ElementKind::Active { table, offset }, ty, members);
let id = module.elements.add(
ElementKind::Active { table, offset },
ElementItems::Expressions(ty, members),
);
module.tables.get_mut(table).elem_segments.insert(id);
}
Ok(())
Expand Down
53 changes: 43 additions & 10 deletions crates/cli-support/src/multivalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::wit::{Adapter, NonstandardWitSection};
use crate::wit::{AdapterKind, Instruction, WasmBindgenAux};
use anyhow::{anyhow, Error};
use walrus::ir::Value;
use walrus::{FunctionId, InitExpr, Module};
use walrus::{ConstExpr, FunctionId, Module};
use wasm_bindgen_multi_value_xform as multi_value_xform;
use wasm_bindgen_wasm_conventions as wasm_conventions;

Expand Down Expand Up @@ -125,19 +125,36 @@ fn resolve_table_entry(module: &Module, func_index: u32) -> FunctionId {
let elem = module.elements.get(id);
let offset = match elem.kind {
walrus::ElementKind::Active { offset, .. } => match offset {
InitExpr::Value(Value::I32(value)) => value as u32,
ConstExpr::Value(Value::I32(value)) => value as u32,
_ => panic!("table offset was not an i32 value"),
},
_ => panic!("found non-active element section for function table"),
};
elem.members.iter().enumerate().find_map(|(i, &func_id)| {

let find = |(i, func_id): (usize, Option<&FunctionId>)| {
let table_index = i as u32 + offset;
if table_index == func_index {
func_id
func_id.cloned()
} else {
None
}
})
};
match &elem.items {
walrus::ElementItems::Functions(items) => {
items.iter().map(Some).enumerate().find_map(find)
}
walrus::ElementItems::Expressions(_, items) => items
.iter()
.map(|expr| {
if let ConstExpr::RefFunc(id) = expr {
Some(id)
} else {
None
}
})
.enumerate()
.find_map(find),
}
})
.expect("function in function table is not initialized")
}
Expand All @@ -154,15 +171,31 @@ fn set_table_entry(module: &mut Module, func_index: u32, new_id: FunctionId) {
let elem = module.elements.get_mut(id);
let offset = match elem.kind {
walrus::ElementKind::Active { offset, .. } => match offset {
InitExpr::Value(Value::I32(value)) => value as u32,
ConstExpr::Value(Value::I32(value)) => value as u32,
_ => panic!("table offset was not an i32 value"),
},
_ => panic!("found non-active element section for function table"),
};
for (i, func_id) in elem.members.iter_mut().enumerate() {
let table_index = i as u32 + offset;
if table_index == func_index {
*func_id = Some(new_id);
match &mut elem.items {
walrus::ElementItems::Functions(items) => {
items.iter_mut().enumerate().for_each(|(i, func_id)| {
let table_index = i as u32 + offset;
if table_index == func_index {
*func_id = new_id;
}
})
}
walrus::ElementItems::Expressions(_, items) => {
items.iter_mut().enumerate().for_each(|(i, func_id)| {
let table_index = i as u32 + offset;
if table_index == func_index {
assert!(
matches!(func_id, ConstExpr::RefFunc(_)),
"didn't find a function at the expected position"
);
*func_id = ConstExpr::RefFunc(new_id);
}
})
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/cli-support/src/wit/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::descriptor::VectorKind;
use crate::wit::{AuxImport, WasmBindgenAux};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use walrus::{FunctionId, ImportId, TypedCustomSectionId};
use walrus::{FunctionId, ImportId, RefType, TypedCustomSectionId};

#[derive(Default, Debug)]
pub struct NonstandardWitSection {
Expand Down Expand Up @@ -344,8 +344,8 @@ impl AdapterType {
walrus::ValType::I64 => AdapterType::I64,
walrus::ValType::F32 => AdapterType::F32,
walrus::ValType::F64 => AdapterType::F64,
walrus::ValType::Externref => AdapterType::Externref,
walrus::ValType::Funcref | walrus::ValType::V128 => return None,
walrus::ValType::Ref(RefType::Externref) => AdapterType::Externref,
walrus::ValType::Ref(_) | walrus::ValType::V128 => return None,
})
}

Expand All @@ -356,7 +356,9 @@ impl AdapterType {
AdapterType::F32 => walrus::ValType::F32,
AdapterType::F64 => walrus::ValType::F64,
AdapterType::Enum(_) => walrus::ValType::I32,
AdapterType::Externref | AdapterType::NamedExternref(_) => walrus::ValType::Externref,
AdapterType::Externref | AdapterType::NamedExternref(_) => {
walrus::ValType::Ref(RefType::Externref)
}
_ => return None,
})
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ureq = { version = "2.7", default-features = false, features = [
"brotli",
"gzip",
] }
walrus = { version = "0.20.2", features = ['parallel'] }
walrus = { version = "0.21", features = ['parallel'] }
wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.92" }
wasm-bindgen-shared = { path = "../shared", version = "=0.2.92" }

Expand All @@ -43,8 +43,8 @@ diff = "0.1"
predicates = "1.0.0"
rayon = "1.0"
tempfile = "3.0"
wasmparser = "0.102.0"
wasmprinter = "0.2.54"
wasmparser = "0.212"
wasmprinter = "0.212"

[[test]]
name = "reference"
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/tests/reference/add.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
(export "memory" (memory 0))
(export "add_u32" (func $add_u32))
(export "add_i32" (func $add_i32))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/anyref-empty.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
(export "memory" (memory 0))
(export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after export) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/anyref-import-catch.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
(export "__wbindgen_add_to_stack_pointer" (func $__wbindgen_add_to_stack_pointer))
(export "__externref_table_dealloc" (func $__externref_table_dealloc))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/anyref-nop.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
(export "foo" (func $foo))
(export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/builder.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
(export "memory" (memory 0))
(export "__wbg_classbuilder_free" (func $__wbg_classbuilder_free))
(export "classbuilder_builder" (func $classbuilder_builder))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/constructor.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
(export "memory" (memory 0))
(export "__wbg_classconstructor_free" (func $__wbg_classconstructor_free))
(export "classconstructor_new" (func $classconstructor_new))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/empty.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(module $reference_test.wasm
(memory (;0;) 16)
(export "memory" (memory 0))
(@custom "target_features" (after export) "\02+\0fmutable-globals+\08sign-ext")
)

2 changes: 2 additions & 0 deletions crates/cli/tests/reference/enums.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
(export "memory" (memory 0))
(export "enum_echo" (func $enum_echo))
(export "option_enum_echo" (func $option_enum_echo))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
)

Loading