Skip to content

Commit

Permalink
Working wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmv committed Aug 6, 2023
1 parent 0c5be58 commit d8f8ee3
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 128 deletions.
42 changes: 23 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::Serialize;
use std::{
collections::HashMap,
fmt::Debug,
rc::Rc,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Expand Down Expand Up @@ -39,13 +40,13 @@ struct SearchState {
nodes_searched: usize,
max_depth: usize,
max_transposition_table_depth: usize,
stop: Arc<AtomicBool>,
stop: Box<dyn Fn() -> bool>,
}

struct Interrupted;

impl SearchState {
pub fn new(stop: Arc<AtomicBool>) -> SearchState {
pub fn new(stop: Box<dyn Fn() -> bool>) -> SearchState {
SearchState {
transposition_table: HashMap::new(),
next_transposition_table: HashMap::new(),
Expand All @@ -63,7 +64,7 @@ impl SearchState {
mut alpha: i32,
mut beta: i32,
) -> Result<SearchResult, Interrupted> {
if self.stop.load(Ordering::Relaxed) {
if (self.stop)() {
return Err(Interrupted);
}
if depth >= self.max_depth || state.ended() {
Expand Down Expand Up @@ -170,10 +171,10 @@ pub struct PartialSearchResult {

pub fn find_best_move(
state: Board2,
stop: Arc<AtomicBool>,
stop: impl Fn() -> bool + 'static,
partial: impl Fn(PartialSearchResult),
) -> Option<Move> {
let mut search_state = SearchState::new(stop);
let mut search_state = SearchState::new(Box::new(stop));
let mut result: Option<SearchResult> = None;
loop {
search_state.next_depth();
Expand Down Expand Up @@ -201,39 +202,42 @@ pub fn find_best_move(

#[wasm_bindgen]
pub struct Engine {
stop: Arc<AtomicBool>,
stop: Rc<js_sys::Uint8Array>,
partial: js_sys::Function,
}

#[wasm_bindgen]
impl Engine {
#[wasm_bindgen(constructor)]
pub fn new(partial: js_sys::Function) -> Engine {
pub fn new(stop: js_sys::Uint8Array, partial: js_sys::Function) -> Engine {
Engine {
stop: Arc::new(AtomicBool::new(false)),
stop: Rc::new(stop),
partial,
}
}

pub fn find_best_move(&self, state: Vec<u8>) -> Option<String> {
self.stop.store(false, Ordering::Relaxed);
let state = Board2::from_positions(&state);
let m = find_best_move(state, self.stop.clone(), |result| {
let m = serde_json::to_string(&result).unwrap();
let m = JsValue::from_str(&m);
self.partial.call1(&JsValue::NULL, &m).unwrap();
});
let stop = self.stop.clone();
let m = find_best_move(
state,
move || js_sys::Atomics::load(&stop, 0).unwrap() != 0,
|result| {
let m = serde_json::to_string(&result).unwrap();
let m = JsValue::from_str(&m);
self.partial.call1(&JsValue::NULL, &m).unwrap();
},
);
m.map(|m| serde_json::to_string(&m).unwrap())
}

pub fn stop(&self) {
self.stop.store(true, Ordering::Relaxed);
}
}

#[wasm_bindgen_test]
fn test_basic_engine() {
let engine = Engine::new(js_sys::Function::default());
let engine = Engine::new(
js_sys::Uint8Array::new_with_length(1),
js_sys::Function::default(),
);
// does not terminate.
println!(
"{:?}",
Expand Down
22 changes: 12 additions & 10 deletions www/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello wasm-pack!</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<!-- <script src='./pkg/wynn.js'></script> -->
<script src="./index.js" type="module"></script>
</body>
</html>

<head>
<meta charset="utf-8">
<title>Wynn Game</title>
</head>

<body>
<script src="./index.js" type="module"></script>
<button onclick="doSearch()">Search</button>
</body>

</html>
18 changes: 17 additions & 1 deletion www/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
const engine = new Worker(new URL("./worker.js", import.meta.url));
const worker = new Worker(new URL("./worker.js", import.meta.url));

window.doSearch = function doSearch() {
const stop = new SharedArrayBuffer(1);
worker.postMessage({
search: [0, 1, 3, 4, 20, 21, 23, 24, 22, 2, 1],
stop,
});

setTimeout(() => {
new Uint8Array(stop)[0] = 1;
}, 2000);
}

worker.onmessage = (msg) => {
console.log(msg);
};
Loading

0 comments on commit d8f8ee3

Please sign in to comment.