GithubHelp home page GithubHelp logo

classicvalues / stockfish.wasm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lichess-org/stockfish.wasm

1.0 1.0 0.0 12.65 MB

WebAssembly port of the strong chess engine Stockfish

Home Page: https://npmjs.com/package/stockfish.wasm

License: GNU General Public License v3.0

Makefile 3.74% C++ 94.08% JavaScript 0.40% Shell 1.54% HTML 0.23%

stockfish.wasm's Introduction

stockfish.wasm

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

npm version CI Passively maintained

Superceded by Stockfish with NNUE

This port is branched from SF_classical. See https://github.com/hi-ogawa/Stockfish for a WebAssembly port with NNUE support.

Requirements

Uses the latest WebAssembly threading proposal. Requires these HTTP headers on the top level response:

Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

And the following header on the included files:

Cross-Origin-Embedder-Policy: require-corp

Chromium based (desktop only)

  • Since Chromium 79: Full support.
  • Chromium 74: Supports treading, but cannot allocate additional memory. The default allocation suffices for up to 2 threads and 16 MB hash.
  • Chromium 70 to 73: Needs flag chrome://flags/#enable-webassembly-threads or Origin Trial.

Firefox

  • Since Firefox 79: Full support.
  • Firefox 72 to 78: Structured cloning can no longer be enabled with flags, except on nightlies.
  • Firefox 71: Requires javascript.options.shared_memory and dom.postMessage.sharedArrayBuffer.withCOOP_COEP to be enabled in about:flags.
  • Firefox 68 to 70: Requires javascript.options.shared_memory to be enabled in about:flags

Other browsers

No support.

See stockfish.js for a more portable but single-threaded version.

Feature detection

function wasmThreadsSupported() {
  // WebAssembly 1.0
  const source = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
  if (
    typeof WebAssembly !== "object" ||
    typeof WebAssembly.validate !== "function"
  )
    return false;
  if (!WebAssembly.validate(source)) return false;

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

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

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

  // Structured cloning
  try {
    // You have to make sure nobody cares about these messages!
    window.postMessage(mem, "*");
  } catch (e) {
    return false;
  }

  // Growable shared memory (optional)
  try {
    mem.grow(8);
  } catch (e) {
    return false;
  }

  return true;
}

Current limitations

  • Hashtable: 1024 MB. You may want to check navigator.deviceMemory before allocating.
  • Threads: 32. You may want to check navigator.hardwareConcurrency. May be capped lower (e.g., dom.workers.maxPerDomain in Firefox).
  • Can hang when UCI protocol is misused. (Do not send invalid commands or positions. While the engine is searching, do not change options or start additional searches).
  • No NNUE support.
  • No Syzygy tablebase support.

Building

Assuming em++ (^2.0.13) is available:

npm run-script prepare

Usage

Requires stockfish.js, stockfish.wasm and stockfish.worker.js (total size ~400K, ~150K gzipped) to be served from the same directory.

<script src="stockfish.js"></script>
<script>
  Stockfish().then((sf) => {
    sf.addMessageListener((line) => {
      console.log(line);
    });

    sf.postMessage("uci");
  });
</script>

Or from recent node (v14.4.0 tested) with flags --experimental-wasm-threads --experimental-wasm-bulk-memory:

const Stockfish = require("stockfish.wasm"); // the module, not the file

Stockfish().then((sf) => {
  // ...
});

License

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

stockfish.wasm's People

Contributors

31m059 avatar ajithcj avatar ceebo avatar elbertoone avatar fauziakram avatar glinscott avatar hxim avatar joergoster avatar jromang avatar jundery avatar locutus2 avatar lucasart avatar mcostalba avatar miguel-l avatar mjz1977 avatar niklasf avatar pb00068 avatar r-peleg avatar rocky640 avatar sfisgod avatar snicolet avatar stefano80 avatar syzygy1 avatar tomtor avatar unaiic avatar uriblass avatar vizvezdenec avatar vondele avatar voyagerone avatar zamar avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.