Last active
December 16, 2017 17:31
-
-
Save pepyakin/92edcce0d4706973a811cf2bbb12b88e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate parity_wasm; | |
use std::rc::Rc; | |
use parity_wasm::elements::Module; | |
use parity_wasm::interpreter::{ | |
ModuleInstance, HostModule, HostModuleBuilder, Error | |
}; | |
mod tictactoe { | |
use std::collections::HashMap; | |
#[derive(Copy, Clone, Debug)] | |
pub enum Error { | |
OutOfRange, | |
AlreadyOccupied, | |
} | |
#[derive(Copy, Clone, Debug, PartialEq)] | |
pub enum Player { | |
X, | |
O | |
} | |
impl Player { | |
pub fn into_i32(maybe_player: Option<Player>) -> i32 { | |
match maybe_player { | |
None => 0, | |
Some(Player::X) => 1, | |
Some(Player::O) => 2, | |
} | |
} | |
} | |
#[derive(Debug)] | |
pub struct Game { | |
board: [Option<Player>; 9], | |
} | |
impl Game { | |
pub fn new() -> Game { | |
Game { | |
board: [None; 9], | |
} | |
} | |
pub fn set(&mut self, idx: i32, player: Player) -> Result<(), Error> { | |
if idx < 0 || idx > 9 { | |
return Err(Error::OutOfRange); | |
} | |
if self.board[idx as usize] != None { | |
return Err(Error::AlreadyOccupied); | |
} | |
self.board[idx as usize] = Some(player); | |
Ok(()) | |
} | |
pub fn get(&self, idx: i32) -> Result<Option<Player>, Error> { | |
if idx < 0 || idx > 9 { | |
return Err(Error::OutOfRange); | |
} | |
Ok(self.board[idx as usize]) | |
} | |
} | |
} | |
struct Runtime<'a> { | |
player: tictactoe::Player, | |
game: &'a mut tictactoe::Game, | |
} | |
fn instantiate<'a, 'b>( | |
module: &Module, | |
env: &HostModule<Runtime<'a>>, | |
runtime: &'b mut Runtime<'a>, | |
) -> Rc<ModuleInstance<Runtime<'a>>> { | |
let instance = ModuleInstance::instantiate(module) | |
.with_import("env", &*env) | |
.run_start(runtime) | |
.expect("Instantiate module successfully"); | |
instance | |
} | |
fn env_host_module<'a>() -> HostModule<Runtime<'a>> { | |
let mut builder = HostModuleBuilder::<Runtime>::new(); | |
builder.with_func1( | |
"set", | |
|state: &mut Runtime, idx: i32| -> Result<Option<()>, Error> { | |
state.game.set(idx, state.player); | |
Ok(None) | |
}, | |
); | |
builder.with_func1( | |
"get", | |
|state: &mut Runtime, idx: i32| -> Result<Option<i32>, Error> { | |
let val: i32 = tictactoe::Player::into_i32(state.game.get(idx).unwrap()); | |
Ok(Some(val)) | |
}, | |
); | |
builder.build() | |
} | |
fn play<'a>(x_module: &Module, o_module: &Module, host_module: &HostModule<Runtime<'a>>, game: &'a mut tictactoe::Game) { | |
let x_instance = { | |
let mut runtime = Runtime { | |
player: tictactoe::Player::X, | |
game, | |
}; | |
instantiate(x_module, host_module, &mut runtime); | |
}; | |
let o_instance = { | |
let mut runtime = Runtime { | |
player: tictactoe::Player::O, | |
game, | |
}; | |
instantiate(o_module, host_module, &mut runtime); | |
}; | |
} | |
fn main() { | |
let env_host_module = env_host_module(); | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment