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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! A stack-based interpreter to execute instructions of WIT adapters.

mod instructions;
pub mod stack;
pub mod wasm;

pub use instructions::Instruction;

use crate::errors::{InstructionResult, InterpreterResult};
use crate::IValue;
use stack::Stack;

use futures::future::BoxFuture;

use std::{convert::TryFrom, marker::PhantomData};

/// Represents the `Runtime`, which is used by an adapter to execute
/// its instructions.
pub(crate) struct Runtime<
    'invocation,
    'instance,
    'store_ref,
    'store_param,
    Instance,
    Export,
    LocalImport,
    Memory,
    MemoryView,
    Store,
> where
    Export: wasm::structures::Export + 'instance,
    LocalImport: wasm::structures::LocalImport<Store> + 'instance,
    Memory: wasm::structures::Memory<MemoryView, Store> + 'instance,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance:
        wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store> + 'instance,
    Store: wasm::structures::Store,
{
    /// The invocation inputs are all the arguments received by an
    /// adapter.
    invocation_inputs: &'invocation [IValue],

    /// Each runtime (so adapter) has its own stack instance.
    stack: Stack<IValue>,

    /// The WebAssembly module instance. It is used by adapter's
    /// instructions.
    wasm_instance: &'instance mut Instance,
    store: &'store_ref mut <Store as it_memory_traits::Store>::ActualStore<'store_param>,

    /// Phantom data.
    _phantom: PhantomData<(Export, LocalImport, Memory, MemoryView, Store)>,
}

/// Type alias for an executable instruction. It's an implementation
/// details, but an instruction is a boxed dyn AsyncExecutableInstructionImpl instance.
pub(crate) type AsyncExecutableInstruction<
    Instance,
    Export,
    LocalImport,
    Memory,
    MemoryView,
    Store,
> = Box<
    dyn AsyncExecutableInstructionImpl<Instance, Export, LocalImport, Memory, MemoryView, Store>
        + Send
        + Sync,
>;

pub(crate) type SyncExecutableInstruction<
    Instance,
    Export,
    LocalImport,
    Memory,
    MemoryView,
    Store,
> = Box<
    dyn Fn(
            &mut Runtime<Instance, Export, LocalImport, Memory, MemoryView, Store>,
        ) -> InstructionResult<()>
        + Send
        + Sync,
>;

/// Type alias for an executable instruction. It's an implementation
/// details, but an instruction is a boxed closure instance.
pub(crate) enum ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView, Store>
where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    Sync(SyncExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView, Store>),
    Async(AsyncExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView, Store>),
}

pub(crate) trait AsyncExecutableInstructionImpl<
    Instance,
    Export,
    LocalImport,
    Memory,
    MemoryView,
    Store,
> where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    fn execute<'args>(
        &'args self,
        runtime: &'args mut Runtime<Instance, Export, LocalImport, Memory, MemoryView, Store>,
    ) -> BoxFuture<'args, InstructionResult<()>>;
}

/// An interpreter is the central piece of this crate. It is a set of
/// executable instructions. Each instruction takes the runtime as
/// argument. The runtime holds the invocation inputs, [the
/// stack](stack), and [the WebAssembly instance](wasm).
///
/// When the interpreter executes the instructions, each of them can
/// query the WebAssembly instance, operates on the stack, or reads
/// the invocation inputs. At the end of the execution, the stack
/// supposedly contains a result. Since an interpreter is used by a
/// WIT adapter to execute its instructions, the result on the stack
/// is the result of the adapter.
///
/// # Example
///
/// ```rust,ignore
/// use std::{cell::Cell, collections::HashMap, convert::TryInto};
/// use wasmer_interface_types::{
///     interpreter::{
///         instructions::tests::{Export, Instance, LocalImport, Memory, MemoryView},
/// //      ^^^^^^^^^^^^ This is private and for testing purposes only.
/// //                   It is basically a fake WebAssembly runtime.
///         stack::Stackable,
///         Instruction, Interpreter,
///     },
///     types::IType,
///     values::IValue,
/// };
///
/// // 1. Creates an interpreter from a set of instructions. They will
/// //    be transformed into executable instructions.
/// let interpreter: Interpreter<Instance, Export, LocalImport, Memory, MemoryView> = (&vec![
///     Instruction::ArgumentGet { index: 1 },
///     Instruction::ArgumentGet { index: 0 },
///     Instruction::CallCore { function_index: 42 },
/// ])
///     .try_into()
///     .unwrap();
///
/// // 2. Defines the arguments of the adapter.
/// let invocation_inputs = vec![IValue::I32(3), IValue::I32(4)];
///
/// // 3. Creates a WebAssembly instance.
/// let mut instance = Instance {
///     // 3.1. Defines one function: `fn sum(a: i32, b: i32) -> i32 { a + b }`.
///     locals_or_imports: {
///         let mut hashmap = HashMap::new();
///         hashmap.insert(
///             42,
///             LocalImport {
///                 // Defines the argument types of the function.
///                 inputs: vec![IType::I32, IType::I32],
///
///                 // Defines the result types.
///                 outputs: vec![IType::I32],
///
///                 // Defines the function implementation.
///                 function: |arguments: &[IValue]| {
///                     let a: i32 = (&arguments[0]).try_into().unwrap();
///                     let b: i32 = (&arguments[1]).try_into().unwrap();
///
///                     Ok(vec![IValue::I32(a + b)])
///                 },
///             },
///         );
///     },
///     ..Default::default()
/// };
///
/// // 4. Executes the instructions.
/// let run = interpreter.run(&invocation_inputs, &mut instance);
///
/// assert!(run.is_ok());
///
/// let stack = run.unwrap();
///
/// // 5. Read the stack to get the result.
/// assert_eq!(stack.as_slice(), &[IValue::I32(7)]);
/// ```
pub struct Interpreter<Instance, Export, LocalImport, Memory, MemoryView, Store>
where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    executable_instructions:
        Vec<ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView, Store>>,
}

impl<Instance, Export, LocalImport, Memory, MemoryView, Store>
    Interpreter<Instance, Export, LocalImport, Memory, MemoryView, Store>
where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    fn iter(
        &self,
    ) -> impl Iterator<
        Item = &ExecutableInstruction<Instance, Export, LocalImport, Memory, MemoryView, Store>,
    > + '_ {
        self.executable_instructions.iter()
    }

    /// Runs the interpreter, such as:
    ///   1. Create a fresh stack,
    ///   2. Create a fresh stack,
    ///   3. Execute the instructions one after the other, and
    ///      returns the stack.
    pub async fn run(
        &self,
        invocation_inputs: &[IValue],
        wasm_instance: &mut Instance,
        wasm_store: &mut <Store as wasm::structures::Store>::ActualStore<'_>,
    ) -> InterpreterResult<Stack<IValue>> {
        let mut runtime = Runtime {
            invocation_inputs,
            stack: Stack::new(),
            wasm_instance,
            store: wasm_store,
            _phantom: PhantomData,
        };

        for executable_instruction in self.iter() {
            match &executable_instruction {
                ExecutableInstruction::Sync(instruction) => instruction(&mut runtime)?,
                ExecutableInstruction::Async(instruction) => {
                    instruction.execute(&mut runtime).await?
                }
            }
        }

        Ok(runtime.stack)
    }
}
/*
/// Transforms a `Vec<Instruction>` into an `Interpreter`.
impl<Instance, Export, LocalImport, Memory, MemoryView, Store> TryFrom<Vec<Instruction>>
    for Interpreter<Instance, Export, LocalImport, Memory, MemoryView, Store>
where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    type Error = ();

    fn try_from(instructions: Vec<Instruction>) -> Result<Self, Self::Error> {
        let executable_instructions = instructions
            .into_iter()
            .map(|instruction| match instruction {
                Instruction::ArgumentGet { index } => {
                    instructions::argument_get(index, instruction)
                }

                Instruction::CallCore { function_index } => {
                    instructions::call_core(function_index, instruction)
                }

                Instruction::BoolFromI32 => instructions::bool_from_i32(instruction),
                Instruction::S8FromI32 => instructions::s8_from_i32(instruction),
                Instruction::S8FromI64 => instructions::s8_from_i64(instruction),
                Instruction::S16FromI32 => instructions::s16_from_i32(instruction),
                Instruction::S16FromI64 => instructions::s16_from_i64(instruction),
                Instruction::S32FromI32 => instructions::s32_from_i32(instruction),
                Instruction::S32FromI64 => instructions::s32_from_i64(instruction),
                Instruction::S64FromI32 => instructions::s64_from_i32(instruction),
                Instruction::S64FromI64 => instructions::s64_from_i64(instruction),
                Instruction::I32FromBool => instructions::i32_from_bool(instruction),
                Instruction::I32FromS8 => instructions::i32_from_s8(instruction),
                Instruction::I32FromS16 => instructions::i32_from_s16(instruction),
                Instruction::I32FromS32 => instructions::i32_from_s32(instruction),
                Instruction::I32FromS64 => instructions::i32_from_s64(instruction),
                Instruction::I64FromS8 => instructions::i64_from_s8(instruction),
                Instruction::I64FromS16 => instructions::i64_from_s16(instruction),
                Instruction::I64FromS32 => instructions::i64_from_s32(instruction),
                Instruction::I64FromS64 => instructions::i64_from_s64(instruction),
                Instruction::U8FromI32 => instructions::u8_from_i32(instruction),
                Instruction::U8FromI64 => instructions::u8_from_i64(instruction),
                Instruction::U16FromI32 => instructions::u16_from_i32(instruction),
                Instruction::U16FromI64 => instructions::u16_from_i64(instruction),
                Instruction::U32FromI32 => instructions::u32_from_i32(instruction),
                Instruction::U32FromI64 => instructions::u32_from_i64(instruction),
                Instruction::U64FromI32 => instructions::u64_from_i32(instruction),
                Instruction::U64FromI64 => instructions::u64_from_i64(instruction),
                Instruction::I32FromU8 => instructions::i32_from_u8(instruction),
                Instruction::I32FromU16 => instructions::i32_from_u16(instruction),
                Instruction::I32FromU32 => instructions::i32_from_u32(instruction),
                Instruction::I32FromU64 => instructions::i32_from_u64(instruction),
                Instruction::I64FromU8 => instructions::i64_from_u8(instruction),
                Instruction::I64FromU16 => instructions::i64_from_u16(instruction),
                Instruction::I64FromU32 => instructions::i64_from_u32(instruction),
                Instruction::I64FromU64 => instructions::i64_from_u64(instruction),
                Instruction::PushI32 { value } => instructions::push_i32(value),
                Instruction::PushI64 { value } => instructions::push_i64(value),

                Instruction::StringLiftMemory => instructions::string_lift_memory(instruction),
                Instruction::StringLowerMemory => instructions::string_lower_memory(instruction),
                Instruction::StringSize => instructions::string_size(instruction),

                Instruction::ByteArrayLiftMemory => {
                    instructions::byte_array_lift_memory(instruction)
                }
                Instruction::ByteArrayLowerMemory => {
                    instructions::byte_array_lower_memory(instruction)
                }
                Instruction::ByteArraySize => instructions::byte_array_size(instruction),

                Instruction::ArrayLiftMemory { ref value_type } => {
                    let value_type = value_type.clone();
                    instructions::array_lift_memory(instruction, value_type)
                }
                Instruction::ArrayLowerMemory { ref value_type } => {
                    let value_type = value_type.clone();
                    instructions::array_lower_memory(instruction, value_type)
                }
                Instruction::RecordLiftMemory { record_type_id } => {
                    instructions::record_lift_memory(record_type_id as _, instruction)
                }
                Instruction::RecordLowerMemory { record_type_id } => {
                    instructions::record_lower_memory(record_type_id as _, instruction)
                }
                Instruction::Dup => instructions::dup(instruction),
                Instruction::Swap2 => instructions::swap2(instruction),
            })
            .collect();

        Ok(Interpreter {
            executable_instructions,
        })
    }
}
*/
/// Transforms a `Vec<Instruction>` into an `Interpreter`.
impl<Instance, Export, LocalImport, Memory, MemoryView, Store> TryFrom<Vec<Instruction>>
    for Interpreter<Instance, Export, LocalImport, Memory, MemoryView, Store>
where
    Export: wasm::structures::Export,
    LocalImport: wasm::structures::LocalImport<Store>,
    Memory: wasm::structures::Memory<MemoryView, Store>,
    MemoryView: wasm::structures::MemoryView<Store>,
    Instance: wasm::structures::Instance<Export, LocalImport, Memory, MemoryView, Store>,
    Store: wasm::structures::Store,
{
    type Error = ();

    fn try_from(instructions: Vec<Instruction>) -> Result<Self, Self::Error> {
        let executable_instructions = instructions
            .into_iter()
            .map(|instruction| match instruction {
                Instruction::ArgumentGet { index } => {
                    instructions::argument_get(index, instruction)
                }

                Instruction::CallCore { function_index } => {
                    instructions::call_core(function_index, instruction)
                }

                Instruction::BoolFromI32 => instructions::bool_from_i32(instruction),
                Instruction::S8FromI32 => instructions::s8_from_i32(instruction),
                Instruction::S8FromI64 => instructions::s8_from_i64(instruction),
                Instruction::S16FromI32 => instructions::s16_from_i32(instruction),
                Instruction::S16FromI64 => instructions::s16_from_i64(instruction),
                Instruction::S32FromI32 => instructions::s32_from_i32(instruction),
                Instruction::S32FromI64 => instructions::s32_from_i64(instruction),
                Instruction::S64FromI32 => instructions::s64_from_i32(instruction),
                Instruction::S64FromI64 => instructions::s64_from_i64(instruction),
                Instruction::I32FromBool => instructions::i32_from_bool(instruction),
                Instruction::I32FromS8 => instructions::i32_from_s8(instruction),
                Instruction::I32FromS16 => instructions::i32_from_s16(instruction),
                Instruction::I32FromS32 => instructions::i32_from_s32(instruction),
                Instruction::I32FromS64 => instructions::i32_from_s64(instruction),
                Instruction::I64FromS8 => instructions::i64_from_s8(instruction),
                Instruction::I64FromS16 => instructions::i64_from_s16(instruction),
                Instruction::I64FromS32 => instructions::i64_from_s32(instruction),
                Instruction::I64FromS64 => instructions::i64_from_s64(instruction),
                Instruction::U8FromI32 => instructions::u8_from_i32(instruction),
                Instruction::U8FromI64 => instructions::u8_from_i64(instruction),
                Instruction::U16FromI32 => instructions::u16_from_i32(instruction),
                Instruction::U16FromI64 => instructions::u16_from_i64(instruction),
                Instruction::U32FromI32 => instructions::u32_from_i32(instruction),
                Instruction::U32FromI64 => instructions::u32_from_i64(instruction),
                Instruction::U64FromI32 => instructions::u64_from_i32(instruction),
                Instruction::U64FromI64 => instructions::u64_from_i64(instruction),
                Instruction::I32FromU8 => instructions::i32_from_u8(instruction),
                Instruction::I32FromU16 => instructions::i32_from_u16(instruction),
                Instruction::I32FromU32 => instructions::i32_from_u32(instruction),
                Instruction::I32FromU64 => instructions::i32_from_u64(instruction),
                Instruction::I64FromU8 => instructions::i64_from_u8(instruction),
                Instruction::I64FromU16 => instructions::i64_from_u16(instruction),
                Instruction::I64FromU32 => instructions::i64_from_u32(instruction),
                Instruction::I64FromU64 => instructions::i64_from_u64(instruction),
                Instruction::PushI32 { value } => instructions::push_i32(value),
                Instruction::PushI64 { value } => instructions::push_i64(value),

                Instruction::StringLiftMemory => instructions::string_lift_memory(instruction),
                Instruction::StringLowerMemory => instructions::string_lower_memory(instruction),
                Instruction::StringSize => instructions::string_size(instruction),

                Instruction::ByteArrayLiftMemory => {
                    instructions::byte_array_lift_memory(instruction)
                }
                Instruction::ByteArrayLowerMemory => {
                    instructions::byte_array_lower_memory(instruction)
                }
                Instruction::ByteArraySize => instructions::byte_array_size(instruction),

                Instruction::ArrayLiftMemory { ref value_type } => {
                    let value_type = value_type.clone();
                    instructions::array_lift_memory(instruction, value_type)
                }
                Instruction::ArrayLowerMemory { ref value_type } => {
                    let value_type = value_type.clone();
                    instructions::array_lower_memory(instruction, value_type)
                }
                Instruction::RecordLiftMemory { record_type_id } => {
                    instructions::record_lift_memory(record_type_id as _, instruction)
                }
                Instruction::RecordLowerMemory { record_type_id } => {
                    instructions::record_lower_memory(record_type_id as _, instruction)
                }
                Instruction::Dup => instructions::dup(instruction),
                Instruction::Swap2 => instructions::swap2(instruction),
            })
            .collect();

        Ok(Interpreter {
            executable_instructions,
        })
    }
}