1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::mem;

use wasm_context::{
    FunctionKind,
    FnTy,
    Import,
    Export,
    Instruction,
    Context
};

pub fn process( ctx: &mut Context ) {
    let already_done = ctx.functions.iter().any( |(_, function)| {
        match function {
            &FunctionKind::Import { import: Import { ref module, ref field }, .. } if module == "env" && field == "__web_on_grow" => {
                true
            },
            _ => false
        }
    });

    if already_done {
        return;
    }

    let type_index = ctx.get_or_add_fn_type( FnTy { params: vec![], return_type: None } );
    let on_grow_function_index = ctx.add_function( FunctionKind::Import {
        type_index,
        export: Export::none(),
        import: Import {
            module: "env".to_owned(),
            field: "__web_on_grow".to_owned()
        },
        name: Some( "__web_on_grow".to_owned() )
    });

    ctx.patch_code( |instructions| {
        let should_process = instructions.iter().any( |instruction| {
            match instruction {
                &Instruction::GrowMemory( _ ) => true,
                _ => false
            }
        });

        if !should_process {
            return;
        }

        let mut new_instructions = Vec::with_capacity( instructions.len() );
        let mut old_instructions = Vec::new();
        mem::swap( instructions, &mut old_instructions );

        for instruction in old_instructions {
            let should_insert = match &instruction {
                &Instruction::GrowMemory( _ ) => true,
                &_ => false
            };

            new_instructions.push( instruction );
            if should_insert {
                new_instructions.push( Instruction::Call( on_grow_function_index ) );
            }
        }

        *instructions = new_instructions;
    });
}