Skip to content
WebAssembly port of the strong chess engine Stockfish
Branch: master
Clone or download

Readme.md

stockfish.wasm

WebAssembly port of the strong chess engine Stockfish. See it in action on https://lichess.org/analysis.

npm version

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() {
  // WebAssembly 1.0
  var source = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
  if (typeof WebAssembly !== 'object' || !WebAssembly.validate(source)) return false;

  // SharedArrayBuffer
  if (typeof SharedArrayBuffer !== 'function') return false;

  // Atomics
  if (typeof Atomics !== 'object') return false;

  // Shared memory
  if (!(new WebAssembly.Memory({shared: true, initial: 8, maximum: 8}).buffer instanceof SharedArrayBuffer)) return false;

  // Structured cloning
  try {
    // You have to make sure nobody cares about this message!
    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>
var sf = Stockfish();
sf.addMessageListener(function (line) {
  console.log(line);
});

sf.postMessage('uci');
</script>

License

Thanks to the Stockfish team for sharing the engine under the GPL3.

You can’t perform that action at this time.