pub struct ModuleBuilder<F = Identity> { /* private fields */ }Expand description
Module builder
Implementations§
Source§impl ModuleBuilder
impl ModuleBuilder
Source§impl<F> ModuleBuilder<F>where
F: Invoke<Module>,
impl<F> ModuleBuilder<F>where
F: Invoke<Module>,
Sourcepub fn with_callback(callback: F) -> Self
pub fn with_callback(callback: F) -> Self
New module builder with bound callback
Sourcepub fn with_module(self, module: Module) -> Self
pub fn with_module(self, module: Module) -> Self
Builder from raw module
Sourcepub fn with_sections<I>(self, sections: I) -> Selfwhere
I: IntoIterator<Item = Section>,
pub fn with_sections<I>(self, sections: I) -> Selfwhere
I: IntoIterator<Item = Section>,
Fill module with sections from iterator
Sourcepub fn with_section(self, section: Section) -> Self
pub fn with_section(self, section: Section) -> Self
Add additional section
Sourcepub fn with_signatures(self, bindings: Vec<Signature>) -> Self
pub fn with_signatures(self, bindings: Vec<Signature>) -> Self
Binds to the type section, creates additional types when required
Sourcepub fn push_function(&mut self, func: FunctionDefinition) -> CodeLocation
pub fn push_function(&mut self, func: FunctionDefinition) -> CodeLocation
Push stand-alone function definition, creating sections, signature and code blocks
in corresponding sections.
FunctionDefinition can be build using builder::function builder
Sourcepub fn push_memory(&mut self, memory: MemoryDefinition) -> u32
pub fn push_memory(&mut self, memory: MemoryDefinition) -> u32
Push linear memory region
Sourcepub fn push_table(&mut self, table: TableDefinition) -> u32
pub fn push_table(&mut self, table: TableDefinition) -> u32
Push table
Sourcepub fn push_signature(&mut self, signature: Signature) -> u32
pub fn push_signature(&mut self, signature: Signature) -> u32
Push one function signature, returning it’s calling index. Can create corresponding type in type section.
Examples found in repository?
28fn main() {
29 let args = env::args().collect::<Vec<_>>();
30 if args.len() != 3 {
31 println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
32 return;
33 }
34
35 let mut module = sophon_wasm::deserialize_file(&args[1]).unwrap();
36
37 for section in module.sections_mut() {
38 match section {
39 &mut elements::Section::Code(ref mut code_section) => {
40 for ref mut func_body in code_section.bodies_mut() {
41 inject_nop(func_body.code_mut());
42 }
43 },
44 _ => { }
45 }
46 }
47
48 let mut build = builder::from_module(module);
49 let import_sig = build.push_signature(
50 builder::signature()
51 .param().i32()
52 .param().i32()
53 .return_type().i32()
54 .build_sig()
55 );
56 let build = build.import()
57 .module("env")
58 .field("log")
59 .external().func(import_sig)
60 .build();
61
62 sophon_wasm::serialize_to_file(&args[2], build.build()).unwrap();
63}Sourcepub fn push_signatures(&mut self, signatures: Vec<Signature>) -> Vec<u32>
pub fn push_signatures(&mut self, signatures: Vec<Signature>) -> Vec<u32>
Push signatures in the module, returning corresponding indices of pushed signatures
Sourcepub fn push_import(&mut self, import: ImportEntry) -> u32
pub fn push_import(&mut self, import: ImportEntry) -> u32
Push import entry to module. Not that it does not update calling indices in function bodies.
Sourcepub fn push_export(&mut self, export: ExportEntry) -> u32
pub fn push_export(&mut self, export: ExportEntry) -> u32
Push export entry to module.
Sourcepub fn function(self) -> FunctionBuilder<Self>
pub fn function(self) -> FunctionBuilder<Self>
Add new function using dedicated builder
Examples found in repository?
12fn main() {
13
14 // Example binary accepts one parameter which is the output file
15 // where generated wasm module will be written at the end of execution
16 let args = env::args().collect::<Vec<_>>();
17 if args.len() != 2 {
18 println!("Usage: {} output_file.wasm", args[0]);
19 return;
20 }
21
22 // Main entry for the builder api is the module function
23 // It returns empty module builder structure which can be further
24 // appended with various wasm artefacts
25 let module = builder::module()
26 // Here we append function to the builder
27 // function() function returns a function builder attached
28 // to the module builder.
29 .function()
30 // We describe signature for the function via signature()
31 // function. In our simple example it's just one input
32 // argument of type 'i32' without return value
33 .signature().with_param(elements::ValueType::I32).build()
34 // body() without any further arguments means that the body
35 // of the function will be empty
36 .body().build()
37 // This is the end of the function builder. When `build()` is
38 // invoked, function builder returns original module builder
39 // from which it was invoked
40 .build()
41 // And finally we finish our module builder to produce actual
42 // wasm module.
43 .build();
44
45 // Module structure can be serialzed to produce a valid wasm file
46 sophon_wasm::serialize_to_file(&args[1], module).unwrap();
47}Sourcepub fn with_export(self, entry: ExportEntry) -> Self
pub fn with_export(self, entry: ExportEntry) -> Self
With inserted export entry
Sourcepub fn with_import(self, entry: ImportEntry) -> Self
pub fn with_import(self, entry: ImportEntry) -> Self
With inserted import entry
Sourcepub fn import(self) -> ImportBuilder<Self>
pub fn import(self) -> ImportBuilder<Self>
Import entry builder
§Examples
use sophon_wasm::builder::module;
let module = module()
.import()
.module("env")
.field("memory")
.external().memory(256, Some(256))
.build()
.build();
assert_eq!(module.import_section().expect("import section to exist").entries().len(), 1);Examples found in repository?
28fn main() {
29 let args = env::args().collect::<Vec<_>>();
30 if args.len() != 3 {
31 println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
32 return;
33 }
34
35 let mut module = sophon_wasm::deserialize_file(&args[1]).unwrap();
36
37 for section in module.sections_mut() {
38 match section {
39 &mut elements::Section::Code(ref mut code_section) => {
40 for ref mut func_body in code_section.bodies_mut() {
41 inject_nop(func_body.code_mut());
42 }
43 },
44 _ => { }
45 }
46 }
47
48 let mut build = builder::from_module(module);
49 let import_sig = build.push_signature(
50 builder::signature()
51 .param().i32()
52 .param().i32()
53 .return_type().i32()
54 .build_sig()
55 );
56 let build = build.import()
57 .module("env")
58 .field("log")
59 .external().func(import_sig)
60 .build();
61
62 sophon_wasm::serialize_to_file(&args[2], build.build()).unwrap();
63}Sourcepub fn with_global(self, global: GlobalEntry) -> Self
pub fn with_global(self, global: GlobalEntry) -> Self
With global variable
Sourcepub fn with_table(self, table: TableType) -> Self
pub fn with_table(self, table: TableType) -> Self
With table
Sourcepub fn export(self) -> ExportBuilder<Self>
pub fn export(self) -> ExportBuilder<Self>
Export entry builder
§Examples
use sophon_wasm::builder::module;
use sophon_wasm::elements::Opcode::*;
let module = module()
.global()
.value_type().i32()
.init_expr(I32Const(0))
.build()
.export()
.field("_zero")
.internal().global(0)
.build()
.build();
assert_eq!(module.export_section().expect("export section to exist").entries().len(), 1);Sourcepub fn global(self) -> GlobalBuilder<Self>
pub fn global(self) -> GlobalBuilder<Self>
Glboal entry builder
§Examples
use sophon_wasm::builder::module;
use sophon_wasm::elements::Opcode::*;
let module = module()
.global()
.value_type().i32()
.init_expr(I32Const(0))
.build()
.build();
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);Sourcepub fn with_data_segment(self, segment: DataSegment) -> Self
pub fn with_data_segment(self, segment: DataSegment) -> Self
Add data segment to the builder
Sourcepub fn build(self) -> F::Result
pub fn build(self) -> F::Result
Build module (final step)
Examples found in repository?
28fn main() {
29 let args = env::args().collect::<Vec<_>>();
30 if args.len() != 3 {
31 println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
32 return;
33 }
34
35 let mut module = sophon_wasm::deserialize_file(&args[1]).unwrap();
36
37 for section in module.sections_mut() {
38 match section {
39 &mut elements::Section::Code(ref mut code_section) => {
40 for ref mut func_body in code_section.bodies_mut() {
41 inject_nop(func_body.code_mut());
42 }
43 },
44 _ => { }
45 }
46 }
47
48 let mut build = builder::from_module(module);
49 let import_sig = build.push_signature(
50 builder::signature()
51 .param().i32()
52 .param().i32()
53 .return_type().i32()
54 .build_sig()
55 );
56 let build = build.import()
57 .module("env")
58 .field("log")
59 .external().func(import_sig)
60 .build();
61
62 sophon_wasm::serialize_to_file(&args[2], build.build()).unwrap();
63}More examples
12fn main() {
13
14 // Example binary accepts one parameter which is the output file
15 // where generated wasm module will be written at the end of execution
16 let args = env::args().collect::<Vec<_>>();
17 if args.len() != 2 {
18 println!("Usage: {} output_file.wasm", args[0]);
19 return;
20 }
21
22 // Main entry for the builder api is the module function
23 // It returns empty module builder structure which can be further
24 // appended with various wasm artefacts
25 let module = builder::module()
26 // Here we append function to the builder
27 // function() function returns a function builder attached
28 // to the module builder.
29 .function()
30 // We describe signature for the function via signature()
31 // function. In our simple example it's just one input
32 // argument of type 'i32' without return value
33 .signature().with_param(elements::ValueType::I32).build()
34 // body() without any further arguments means that the body
35 // of the function will be empty
36 .body().build()
37 // This is the end of the function builder. When `build()` is
38 // invoked, function builder returns original module builder
39 // from which it was invoked
40 .build()
41 // And finally we finish our module builder to produce actual
42 // wasm module.
43 .build();
44
45 // Module structure can be serialzed to produce a valid wasm file
46 sophon_wasm::serialize_to_file(&args[1], module).unwrap();
47}