fn inject_helper_functions(module: Module) -> Module {
    let mut builder = builder::from_module(module);
    let import_sig = builder.push_signature(builder::signature().build_sig());

    builder.push_import(
        builder::import()
            .module("__")
            .field("out_of_instructions")
            .external()
            .func(import_sig)
            .build(),
    );

    let import_sig = builder.push_signature(
        builder::signature()
            .with_param(ValueType::I32)
            .with_param(ValueType::I32)
            .with_result(ValueType::I32)
            .build_sig(),
    );
    builder.push_import(
        builder::import()
            .module("__")
            .field("update_available_memory")
            .external()
            .func(import_sig)
            .build(),
    );
    let mut module = builder.build();
    // We know, we have at least two imports, because we pushed them above, now
    // let's move them to the first two positions respectively, so that we can
    // increase all other function indices unconditionally.
    let entries = module.import_section_mut().unwrap().entries_mut();
    let last = entries.pop().unwrap();
    debug_assert!(last.module() == "__" && last.field() == "update_available_memory");
    entries.insert(0, last);
    let last = entries.pop().unwrap();
    debug_assert!(last.module() == "__" && last.field() == "out_of_instructions");
    entries.insert(0, last);

    // We lift all call references by 2
    for section in module.sections_mut() {
        match section {
            Section::Code(ref mut code_section) => {
                for func_body in code_section.bodies_mut() {
                    let code = func_body.code_mut();
                    code.elements_mut().iter_mut().for_each(|instr| {
                        if let Instruction::Call(ref mut call_index) = instr {
                            *call_index += 2
                        }
                    });
                }
            }
            Section::Export(ref mut export_section) => {
                for export in export_section.entries_mut() {
                    if let Internal::Function(ref mut func_index) = export.internal_mut() {
                        *func_index += 2
                    }
                }
            }
            Section::Element(ref mut elements_section) => {
                for segment in elements_section.entries_mut() {
                    for func_index in segment.members_mut() {
                        *func_index += 2
                    }
                }
            }
            Section::Start(ref mut func_index) => *func_index += 2,
            _ => {}
        }
    }
    module
}
// Injects two system api functions:
//   * `out_of_instructions` which is called, whenever a message execution runs
//     out of instructions.
//   * `update_available_memory` which is called after a native `memory.grow` to
//     check whether the canister has enough available memory according to its
//     memory allocation.
//
// Note that these functions are injected as the first two imports, so that we
// can increment all function indices unconditionally by two. (If they would be
// added as the last two imports, we'd need to increment only non imported
// functions, since imported functions precede all others in the function index
// space, but this would be error-prone).
Injects two system api functions:
`out_of_instructions` which is called, whenever a message execution runs
out of instructions.
`update_available_memory` which is called after a native `memory.grow` to
check whether the canister has enough available memory according to its
memory allocation.

Note that these functions are injected as the first two imports, so that we
can increment all function indices unconditionally by two. (If they would be
added as the last two imports, we'd need to increment only non imported
functions, since imported functions precede all others in the function index
space, but this would be error-prone).
Injects two system api functions : ` out_of_instructions ` which is called whenever a message execution runs out of instructions . ` update_available_memory ` which is called after a native ` memory . grow ` to check whether the canister has enough available memory according to its memory allocation . Note that these functions are injected as the first two imports so that we can increment all function indices unconditionally by two . ( If they would be added as the last two imports we ' d need to increment only non imported functions since imported functions precede all others in the function index space but this would be error - prone ) .
fn inject_helper_functions(module: Module) -> Module {
    let mut builder = builder::from_module(module);
    let import_sig = builder.push_signature(builder::signature().build_sig());
    builder.push_import(
        builder::import()
            .module("__")
            .field("out_of_instructions")
            .external()
            .func(import_sig)
            .build(),
    );
    let import_sig = builder.push_signature(
        builder::signature()
            .with_param(ValueType::I32)
            .with_param(ValueType::I32)
            .with_result(ValueType::I32)
            .build_sig(),
    );
    builder.push_import(
        builder::import()
            .module("__")
            .field("update_available_memory")
            .external()
            .func(import_sig)
            .build(),
    );
    let mut module = builder.build();
    // We know, we have at least two imports, because we pushed them above, now
    // let's move them to the first two positions respectively, so that we can
    // increase all other function indices unconditionally.
    let entries = module.import_section_mut().unwrap().entries_mut();
    let last = entries.pop().unwrap();
    debug_assert!(last.module() == "__" && last.field() == "update_available_memory");
    entries.insert(0, last);
    let last = entries.pop().unwrap();
    debug_assert!(last.module() == "__" && last.field() == "out_of_instructions");
    entries.insert(0, last);
    // We lift all call references by 2
    for section in module.sections_mut() {
        match section {
            Section::Code(ref mut code_section) => {
                for func_body in code_section.bodies_mut() {
                    let code = func_body.code_mut();
                    code.elements_mut().iter_mut().for_each(|instr| {
                        if let Instruction::Call(ref mut call_index) = instr {
                            *call_index += 2
                        }
                    });
                }
            }
            Section::Export(ref mut export_section) => {
                for export in export_section.entries_mut() {
                    if let Internal::Function(ref mut func_index) = export.internal_mut() {
                        *func_index += 2
                    }
                }
            }
            Section::Element(ref mut elements_section) => {
                for segment in elements_section.entries_mut() {
                    for func_index in segment.members_mut() {
                        *func_index += 2
                    }
                }
            }
            Section::Start(ref mut func_index) => *func_index += 2,
            _ => {}
        }
    }
    module
}
fn inject_helper_functions ( module : Module ) -> Module { let mut builder = builder :: from_module ( module ) ; let import_sig = builder . push_signature ( builder :: signature ( ) . build_sig ( ) ) ; builder . push_import ( builder :: import ( ) . module ( " " ) . field ( " " ) . external ( ) . func ( import_sig ) . build ( ) , ) ; let import_sig = builder . push_signature ( builder :: signature ( ) . with_param ( ValueType :: I32 ) . with_param ( ValueType :: I32 ) . with_result ( ValueType :: I32 ) . build_sig ( ) , ) ; builder . push_import ( builder :: import ( ) . module ( " " ) . field ( " " ) . external ( ) . func ( import_sig ) . build ( ) , ) ; let mut module = builder . build ( ) ; // We know, we have at least two imports, because we pushed them above, now // let's move them to the first two positions respectively, so that we can // increase all other function indices unconditionally. let entries = module . import_section_mut ( ) . unwrap ( ) . entries_mut ( ) ; let last = entries . pop ( ) . unwrap ( ) ; debug_assert ! ( last module ( ) " " last field ( ) " " ) ; entries . insert ( 0 , last ) ; let last = entries . pop ( ) . unwrap ( ) ; debug_assert ! ( last module ( ) " " last field ( ) " " ) ; entries . insert ( 0 , last ) ; // We lift all call references by 2 for section in module . sections_mut ( ) { match section { Section :: Code ( ref mut code_section ) => { for func_body in code_section . bodies_mut ( ) { let code = func_body . code_mut ( ) ; code . elements_mut ( ) . iter_mut ( ) . for_each ( | instr | { if let Instruction :: Call ( ref mut call_index ) = instr { * call_index += 2 } } ) ; } } Section :: Export ( ref mut export_section ) => { for export in export_section . entries_mut ( ) { if let Internal :: Function ( ref mut func_index ) = export . internal_mut ( ) { * func_index += 2 } } } Section :: Element ( ref mut elements_section ) => { for segment in elements_section . entries_mut ( ) { for func_index in segment . members_mut ( ) { * func_index += 2 } } } Section :: Start ( ref mut func_index ) => * func_index += 2 , _ => { } } } module }
Injects two system api functions:
 `out_of_instructions` which is called, whenever a message execution runs
out of instructions.
Injects two system api functions : ` out_of_instructions ` which is called whenever a message execution runs out of instructions .
module
Module
module
Module