Skip to content

Commit 520ad45

Browse files
committed
Derived Clone and Copy for DataKind. Added locals to LocalFunction.
1 parent 9d6c9de commit 520ad45

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/function_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ impl FunctionBuilder {
150150
/// let function_id = builder.finish(vec![], &mut module.funcs);
151151
/// # let _ = function_id;
152152
/// ```
153-
pub fn finish(self, args: Vec<LocalId>, funcs: &mut ModuleFunctions) -> FunctionId {
154-
let func = LocalFunction::new(args, self);
153+
pub fn finish(self, args: Vec<LocalId>, locals: Vec<LocalId>, funcs: &mut ModuleFunctions) -> FunctionId {
154+
let func = LocalFunction::new(args, locals, self);
155155
funcs.add_local(func)
156156
}
157157

158158
/// Returns the [crate::LocalFunction] built by this builder.
159-
pub fn local_func(self, args: Vec<LocalId>) -> LocalFunction {
160-
LocalFunction::new(args, self)
159+
pub fn local_func(self, args: Vec<LocalId>, locals: Vec<LocalId>) -> LocalFunction {
160+
LocalFunction::new(args, locals, self)
161161
}
162162
}
163163

src/module/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Data {
2828
}
2929

3030
/// The kind of data segment: passive or active.
31-
#[derive(Debug)]
31+
#[derive(Clone, Copy, Debug)]
3232
pub enum DataKind {
3333
/// An active data segment that is automatically initialized at some address
3434
/// in a static memory.
@@ -41,7 +41,7 @@ pub enum DataKind {
4141
}
4242

4343
/// The parts of a data segment that are only present in active data segments.
44-
#[derive(Clone, Debug)]
44+
#[derive(Clone, Copy, Debug)]
4545
pub struct ActiveData {
4646
/// The memory that this active data segment will be automatically
4747
/// initialized in.

src/module/functions/local_function/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ pub struct LocalFunction {
2121

2222
/// Arguments to this function, and the locals that they're assigned to.
2323
pub args: Vec<LocalId>,
24+
25+
/// Locals of this function, excluding arguments.
26+
pub locals: Vec<LocalId>,
2427
//
2528
// TODO: provenance: (InstrSeqId, usize) -> offset in code section of the
2629
// original instruction. This will be necessary for preserving debug info.
2730
}
2831

2932
impl LocalFunction {
3033
/// Creates a new definition of a local function from its components.
31-
pub(crate) fn new(args: Vec<LocalId>, builder: FunctionBuilder) -> LocalFunction {
32-
LocalFunction { args, builder }
34+
pub(crate) fn new(args: Vec<LocalId>, locals: Vec<LocalId>, builder: FunctionBuilder) -> LocalFunction {
35+
LocalFunction { args, locals, builder }
3336
}
3437

3538
/// Construct a new `LocalFunction`.
@@ -42,13 +45,15 @@ impl LocalFunction {
4245
id: FunctionId,
4346
ty: TypeId,
4447
args: Vec<LocalId>,
48+
locals: Vec<LocalId>,
4549
mut body: wasmparser::BinaryReader<'_>,
4650
on_instr_pos: Option<&(dyn Fn(&usize) -> InstrLocId + Sync + Send + 'static)>,
4751
mut validator: FuncValidator<ValidatorResources>,
4852
) -> Result<LocalFunction> {
4953
let mut func = LocalFunction {
5054
builder: FunctionBuilder::without_entry(ty),
5155
args,
56+
locals,
5257
};
5358

5459
let result: Vec<_> = module.types.get(ty).results().iter().cloned().collect();

src/module/functions/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ impl Module {
366366

367367
// Next up comes all the locals of the function.
368368
let mut reader = body.get_binary_reader();
369+
let mut locals = Vec::new();
369370
for _ in 0..reader.read_var_u32()? {
370371
let pos = reader.original_position();
371372
let count = reader.read_var_u32()?;
@@ -375,20 +376,21 @@ impl Module {
375376
for _ in 0..count {
376377
let local_id = self.locals.add(ty);
377378
let idx = indices.push_local(id, local_id);
379+
locals.push(local_id);
378380
if self.config.generate_synthetic_names_for_anonymous_items {
379381
let name = format!("l{}", idx);
380382
self.locals.get_mut(local_id).name = Some(name);
381383
}
382384
}
383385
}
384386

385-
bodies.push((id, reader, args, ty, validator));
387+
bodies.push((id, reader, args, locals, ty, validator));
386388
}
387389

388390
// Wasm modules can often have a lot of functions and this operation can
389391
// take some time, so parse all function bodies in parallel.
390392
let results = maybe_parallel!(bodies.(into_iter | into_par_iter))
391-
.map(|(id, body, args, ty, validator)| {
393+
.map(|(id, body, args, locals, ty, validator)| {
392394
(
393395
id,
394396
LocalFunction::parse(
@@ -397,6 +399,7 @@ impl Module {
397399
id,
398400
ty,
399401
args,
402+
locals,
400403
body,
401404
on_instr_pos,
402405
validator,

0 commit comments

Comments
 (0)