stockfish.wasm
WebAssembly port of the strong chess engine
Stockfish. See it in action
on https://lichess.org/analysis.
Requirements
Uses the latest WebAssembly threading proposal.
- Available as an Origin Trial in Chrome 70.
- Behind a flag since Chrome 70: chrome://flags/#enable-webassembly-threads
- Behind a flag in Firefox Nightly:
javascript.options.shared_memory
in about:config.
See stockfish.js for a more
portable but single-threaded version.
Feature detection:
function wasmThreadsSupported() {
var source = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
if (typeof WebAssembly !== 'object' || !WebAssembly.validate(source)) return false;
if (typeof SharedArrayBuffer !== 'function') return false;
if (typeof Atomics !== 'object') return false;
if (!(new WebAssembly.Memory({shared: true, initial: 8, maximum: 8}).buffer instanceof SharedArrayBuffer)) return false;
try {
window.postMessage(new WebAssembly.Module(source), '*');
} catch (e) {
return false;
}
return true;
}
Current limitations
- Maximum size of hashtable determined at compile time (currently 16 MB).
- Maximum number of threads determined at compile time (currently 8). Blocked
on reserving more memory at runtime.
- Only one instance per
window
. - No Syzygy tablebase support.
Building
Assuming em++ (>= 1.38.27) is available:
npm run-script prepare
Usage
Requires stockfish.js
, stockfish.wasm
, stockfish.js.mem
and
stockfish.worker.js
(total size ~600K) to be served from the same directory.
<script src="stockfish.js"></script>
<script>
Stockfish().then(function(sf) {
sf.addMessageListener(function (line) {
console.log(line);
});
sf.postMessage('uci');
});
</script>
License
Thanks to the Stockfish team for sharing the engine under the GPL3.