#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
use alloc::string::String;
use codec::{Decode, Encode};
use sp_core::RuntimeDebug;
use sp_std::vec::Vec;
use sp_wasm_interface::ReturnValue;
#[derive(Clone, Copy, Debug)]
pub enum Instantiate {
Version1,
Version2,
}
#[derive(Encode, Decode, RuntimeDebug)]
#[codec(crate = codec)]
pub struct HostError;
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
#[codec(crate = codec)]
pub enum ExternEntity {
#[codec(index = 1)]
Function(u32),
#[codec(index = 2)]
Memory(u32),
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
#[codec(crate = codec)]
pub struct Entry {
pub module_name: String,
pub field_name: String,
pub entity: ExternEntity,
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
#[codec(crate = codec)]
pub struct EnvironmentDefinition {
pub entries: Vec<Entry>,
}
pub const MEM_UNLIMITED: u32 = -1i32 as u32;
pub const ERR_OK: u32 = 0;
pub const ERR_MODULE: u32 = -1i32 as u32;
pub const ERR_OUT_OF_BOUNDS: u32 = -2i32 as u32;
pub const ERR_EXECUTION: u32 = -3i32 as u32;
pub const ERROR_GLOBALS_OK: u32 = 0;
pub const ERROR_GLOBALS_NOT_FOUND: u32 = u32::MAX;
pub const ERROR_GLOBALS_OTHER: u32 = u32::MAX - 1;
#[derive(Clone, Copy, PartialEq, Encode, Decode, Debug)]
#[codec(crate = codec)]
pub struct WasmReturnValue {
pub gas: i64,
pub inner: ReturnValue,
}
impl WasmReturnValue {
pub const ENCODED_MAX_SIZE: usize = 8 + ReturnValue::ENCODED_MAX_SIZE;
}
pub const GLOBAL_NAME_GAS: &str = "gear_gas";
#[cfg(test)]
mod tests {
use super::*;
use codec::Codec;
use std::fmt;
fn roundtrip<S: Codec + PartialEq + fmt::Debug>(s: S) {
let encoded = s.encode();
assert_eq!(S::decode(&mut &encoded[..]).unwrap(), s);
}
#[test]
fn env_def_roundtrip() {
roundtrip(EnvironmentDefinition { entries: vec![] });
roundtrip(EnvironmentDefinition {
entries: vec![Entry {
module_name: "kernel".to_string(),
field_name: "memory".to_string(),
entity: ExternEntity::Memory(1337),
}],
});
roundtrip(EnvironmentDefinition {
entries: vec![Entry {
module_name: "env".to_string(),
field_name: "abort".to_string(),
entity: ExternEntity::Function(228),
}],
});
}
}