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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{
    errors::{InstructionError, InstructionErrorKind},
    interpreter::wasm::structures::{FunctionIndex, TypedIndex},
    interpreter::Instruction,
};

executable_instruction!(
    call_core(function_index: u32, instruction: Instruction) -> _ {
        move |runtime| -> _ {
            let instance = &runtime.wasm_instance;
            let index = FunctionIndex::new(function_index as usize);

            let local_or_import = instance.local_or_import(index).ok_or_else(|| {
                InstructionError::from_error_kind(
                    instruction.clone(),
                    InstructionErrorKind::LocalOrImportIsMissing {
                        function_index,
                    },
                )
            })?;
            let inputs_cardinality = local_or_import.inputs_cardinality();

            let inputs = runtime.stack.pop(inputs_cardinality).ok_or_else(|| {
                InstructionError::from_error_kind(
                    instruction.clone(),
                    InstructionErrorKind::StackIsTooSmall {
                        needed: inputs_cardinality,
                    },
                )
            })?;

            super::check_function_signature(&**instance, local_or_import, &inputs)
                .map_err(|e| InstructionError::from_error_kind(instruction.clone(), e))?;

            log::debug!("call-core: calling {} with arguments: {:?}", local_or_import.name(), inputs);

            let outputs = local_or_import.call(runtime.store, &inputs).map_err(|_| {
                InstructionError::from_error_kind(
                    instruction.clone(),
                    InstructionErrorKind::LocalOrImportCall {
                        function_name: local_or_import.name().to_string(),
                    },
                )
            })?;

            log::debug!("call-core: call to {} succeeded with result {:?}", local_or_import.name(), outputs);

            for output in outputs.into_iter() {
                runtime.stack.push(output)
            }

            Ok(())
        }
    }
);

#[cfg(test)]
mod tests {
    test_executable_instruction!(
        test_call_core =
            instructions: [
                Instruction::ArgumentGet { index: 0 },
                Instruction::ArgumentGet { index: 1 },
                Instruction::CallCore { function_index: 42 },
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I32(4),
            ],
            instance: Instance::new(),
            stack: [IValue::I32(12)],
    );

    test_executable_instruction!(
        test_call_core__invalid_local_import_index =
            instructions: [
                Instruction::CallCore { function_index: 42 },
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I32(4),
            ],
            instance: Default::default(),
            error: r#"`call-core 42` the local or import function `42` doesn't exist"#,
    );

    test_executable_instruction!(
        test_call_core__stack_is_too_small =
            instructions: [
                Instruction::ArgumentGet { index: 0 },
                Instruction::CallCore { function_index: 42 },
                //                                      ^^ `42` expects 2 values on the stack, only one is present
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I32(4),
            ],
            instance: Instance::new(),
            error: r#"`call-core 42` needed to read `2` value(s) from the stack, but it doesn't contain enough data"#,
    );

    test_executable_instruction!(
        test_call_core__invalid_types_in_the_stack =
            instructions: [
                Instruction::ArgumentGet { index: 0 },
                Instruction::ArgumentGet { index: 1 },
                Instruction::CallCore { function_index: 42 },
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I64(4),
                //              ^^^ mismatch with `42` signature
            ],
            instance: Instance::new(),
            error: r#"`call-core 42` the local or import function `42` has the signature `[I32, I32] -> []` but it received values of kind `[I32, I64] -> []`"#,
    );

    test_executable_instruction!(
        test_call_core__failure_when_calling =
            instructions: [
                Instruction::ArgumentGet { index: 0 },
                Instruction::ArgumentGet { index: 1 },
                Instruction::CallCore { function_index: 42 },
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I32(4),
            ],
            instance: Instance {
                locals_or_imports: {
                    let mut hashmap = HashMap::new();
                    hashmap.insert(
                        42,
                        LocalImport {
                            inputs: vec![IType::I32, IType::I32],
                            outputs: vec![IType::I32],
                            function: |_| Err(()),
                            //            ^^^^^^^ function fails
                        },
                    );

                    hashmap
                },
                ..Default::default()
            },
            error: r#"`call-core 42` failed while calling the local or import function `42`"#,
    );

    test_executable_instruction!(
        test_call_core__void =
            instructions: [
                Instruction::ArgumentGet { index: 0 },
                Instruction::ArgumentGet { index: 1 },
                Instruction::CallCore { function_index: 42 },
            ],
            invocation_inputs: [
                IValue::I32(3),
                IValue::I32(4),
            ],
            instance: Instance {
                locals_or_imports: {
                    let mut hashmap = HashMap::new();
                    hashmap.insert(
                        42,
                        LocalImport {
                            inputs: vec![IType::I32, IType::I32],
                            outputs: vec![IType::I32],
                            function: |_| Ok(vec![]),
                            //            ^^^^^^^^^^ void
                        },
                    );

                    hashmap
                },
                ..Default::default()
            },
            stack: [],
    );
}