mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-28 22:22:02 +00:00
test
This commit is contained in:
49
src/builder/data.rs
Normal file
49
src/builder/data.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use super::invoke::{Identity, Invoke};
|
||||||
|
use elements;
|
||||||
|
|
||||||
|
pub struct DataSegmentBuilder<F=Identity> {
|
||||||
|
callback: F,
|
||||||
|
// todo: add mapper once multiple memory refs possible
|
||||||
|
mem_index: u32,
|
||||||
|
offset: elements::InitExpr,
|
||||||
|
value: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DataSegmentBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
DataSegmentBuilder::with_callback(Identity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> DataSegmentBuilder<F> {
|
||||||
|
pub fn with_callback(callback: F) -> Self {
|
||||||
|
DataSegmentBuilder {
|
||||||
|
callback: callback,
|
||||||
|
mem_index: 0,
|
||||||
|
offset: elements::InitExpr::empty(),
|
||||||
|
value: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn offset(mut self, opcode: elements::Opcode) -> Self {
|
||||||
|
self.offset = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(mut self, value: Vec<u8>) -> Self {
|
||||||
|
self.value = value;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> DataSegmentBuilder<F> where F: Invoke<elements::DataSegment> {
|
||||||
|
pub fn build(self) -> F::Result {
|
||||||
|
self.callback.invoke(
|
||||||
|
elements::DataSegment::new(
|
||||||
|
self.mem_index,
|
||||||
|
self.offset,
|
||||||
|
self.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -487,4 +487,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn data() {
|
||||||
|
let module = module()
|
||||||
|
.data()
|
||||||
|
.offset(::elements::Opcode::I32Const(16))
|
||||||
|
.value(vec![0u8, 15, 10, 5, 25])
|
||||||
|
.build()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert_eq!(module.data_section().expect("data section to exist").entries().len(), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user