Giter Site home page Giter Site logo

lichess-org / stockfish.js Goto Github PK

View Code? Open in Web Editor NEW
183.0 17.0 26.0 13.13 MB

The strong open source chess engine Stockfish compiled to JavaScript and WebAssembly using Emscripten

License: GNU General Public License v3.0

Makefile 2.36% JavaScript 0.15% C++ 96.07% Shell 1.23% Dockerfile 0.19%
chess stockfish emscripten lichess webassembly

stockfish.js's Introduction

stockfish.js

The strong open source chess engine Stockfish compiled to JavaScript and WebAssembly using Emscripten. See it in action for local computer analysis on lichess.org.

npm Passively maintained

Maintained with bugfixes to keep supporting older browsers, but active development is happening on stockfish.wasm.

Releases

About 1.4MB uncompressed, 250 KB gzipped.

Building

Install Emscripten and uglifyjs, then:

./build.sh

Or using Docker:

docker run --user $(id -u):$(id -g) --volume $(pwd):/home/builder/stockfish.js:rw niklasf/emscripten-for-stockfish

Usage

var wasmSupported = typeof WebAssembly === 'object' && WebAssembly.validate(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));

var stockfish = new Worker(wasmSupported ? 'stockfish.wasm.js' : 'stockfish.js');

stockfish.addEventListener('message', function (e) {
  console.log(e.data);
});

stockfish.postMessage('uci');

Changes to original Stockfish

  • Expose as web worker.
  • Web workers are inherently single threaded. Limit to one thread.
  • Break down main iterative deepening loop to allow interrupting search.
  • Limit total memory to 32 MB.
  • Disable Syzygy tablebases.
  • Disable benchmark.

Acknowledgements

Thanks to @nmrugg for doing the same thing with Stockfish 6, to @ddugovic for his multi-variant Stockfish fork and to the Stockfish team for ... Stockfish.

stockfish.js's People

Contributors

ajithcj avatar ceebo avatar ddugovic avatar elbertoone avatar glinscott avatar hxim avatar ianfab avatar iivec avatar jcalovski avatar joergoster avatar jromang avatar jundery avatar lakinwecker avatar locutus2 avatar lucasart avatar mbootsector avatar mcostalba avatar niklasf avatar pb00068 avatar ppigazzini avatar r-peleg avatar rocky640 avatar snicolet avatar stefano80 avatar syzygy1 avatar tomtor avatar uriblass avatar vondele avatar voyagerone avatar zamar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stockfish.js's Issues

Error on build with latest version of emscripten

Following instructions from the readme and emscripten, I get this error

stockfish.js git/ddugovic  
❯ ./build.sh


Config:
debug: 'no'
sanitize: 'no'
optimize: 'yes'
arch: 'any'
bits: '32'
kernel: 'Darwin'
os: ''
prefetch: 'no'
popcnt: 'no'
sse: 'no'
pext: 'no'

Flags:
CXX: em++
CXXFLAGS: -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto
LDFLAGS:  -s TOTAL_MEMORY=33554432 --memory-init-file 0 -s NO_EXIT_RUNTIME=1 -s EXPORTED_FUNCTIONS=[_main, _uci_command] -s EXTRA_EXPORTED_RUNTIME_METHODS=[ccall] --pre-js pre.js -s WASM=0 -s LEGACY_VM_SUPPORT=1 -O3 -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto

Testing config sanity. If this fails, try 'make help' ...

/Library/Developer/CommandLineTools/usr/bin/make ARCH=js COMP=emscripten all
em++ -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto   -c -o bitbase.o bitbase.cpp
em++ -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto   -c -o bitboard.o bitboard.cpp
clang-13: error: unsupported option '-mdynamic-no-pic' for target 'wasm32-unknown-emscripten'
clang-13: error: unsupported option '-mdynamic-no-pic' for target 'wasm32-unknown-emscripten'
em++: error: '/Users/robertwebb/emsdk/upstream/bin/clang++ -DEMSCRIPTEN -fignore-exceptions -flto=full -Xclang -iwithsysroot/include/SDL -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=2 -D__EMSCRIPTEN_minor__=0 -D__EMSCRIPTEN_tiny__=14 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -flegacy-pass-manager -Werror=implicit-function-declaration --sysroot=/Users/robertwebb/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/compat -Wall -Wcast-qual -std=c++11 -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto -c bitboard.cpp -o bitboard.o' failed (1)
make[1]: *** [bitboard.o] Error 1
make[1]: *** Waiting for unfinished jobs....
clang-13: error: unsupported option '-mdynamic-no-pic' for target 'wasm32-unknown-emscripten'
clang-13: error: unsupported option '-mdynamic-no-pic' for target 'wasm32-unknown-emscripten'
em++: error: '/Users/robertwebb/emsdk/upstream/bin/clang++ -DEMSCRIPTEN -fignore-exceptions -flto=full -Xclang -iwithsysroot/include/SDL -target wasm32-unknown-emscripten -D__EMSCRIPTEN_major__=2 -D__EMSCRIPTEN_minor__=0 -D__EMSCRIPTEN_tiny__=14 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -flegacy-pass-manager -Werror=implicit-function-declaration --sysroot=/Users/robertwebb/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/compat -Wall -Wcast-qual -std=c++11 -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DNDEBUG -O3 -mdynamic-no-pic -DNO_PREFETCH -flto -c bitbase.cpp -o bitbase.o' failed (1)
make[1]: *** [bitbase.o] Error 1
make: *** [build] Error 2
                                    

This is probably due to latest emscripten being incompatible with this build, going to try to find either a different stockfish js or the correct version of emscripten to use.

Web Assembly Engine Can't Find .wasm File

So i'm having an issue in my code where the stockfish.wasm.js file can't find the stockfish.wasm file.

myFunctions.loadChessEngine = function() {
    if(!lozzaObjectURL) {
        lozzaObjectURL = URL.createObjectURL(new Blob([GM_getResourceText('stockFish.js')], {type: 'application/javascript'}));
    }
    if(!wasmObjectURL) {
       wasmObjectURL = URL.createObjectURL(new Blob([GM_getResourceText('stockFish.wasm.js')], {type: 'application/javascript'}));
    }
    if(!wasmFile) {
      wasmFile = URL.createObjectURL(new Blob([GM_getResourceText('stockFish.wasm')], {type: 'application/wasm'}));
    }
    console.log(lozzaObjectURL);
    console.log(wasmObjectURL);
    var wasmSupported = typeof WebAssembly === 'object' && WebAssembly.validate(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
    console.log('Webassembly supported: '+wasmSupported);
    if(lozzaObjectURL && wasmObjectURL) {
        engine.engine = new Worker(wasmSupported ? wasmObjectURL : lozzaObjectURL);
        //engine.engine = new Worker(lozzaObjectURL);

        engine.engine.onmessage = e => {
            parser(e);
        };
        engine.engine.onerror = e => {
            console.log("Worker Error: "+e);
        };

        engine.engine.postMessage('ucinewgame');
    }
    console.log('loaded chess engine');
}

(The code tag wasn't working on GitHub for me. :( )
I'm currently trying to load the files through blob and stockfish.wasm.js can't find the stockfist.wasm file.

Not recognizing "eval" token

The engine doesn't seem to be responding to eval UCI command, is there a specific UCI sequence needed for debug output?

safari js error with unoptimized build

optimize=no optimize=yes
Chrome 57
Firefox 49
Safari 9.1.3 :-(

The js error:

TypeError: Module['___cxa_is_pointer_type'] is not a function.
(In 'Module['___cxa_is_pointer_type'](throwntype)', 'Module['___cxa_is_pointer_type']' is undefined)

Built with OS X 10.11.6, LLVM 8.0.0, emscripten 1.37.1. I was trying to debug the wonky elapsed clock in safari, but it's difficult to trace through without optimize=no.

Error with WebAssembly engine

When I try to use the web assembly version I get an error in the browser

image

This is how I serve the wasm files with NestJs

@Get('webworker-wasm.js')
private getWebworkerWasmSfjs(req: Request, res: Response) {
    Logger.Info('Serving file ../../node_modules/stockfish.js/stockfish.wasm.js');

    fs.readFile('../../node_modules/stockfish.js/stockfish.wasm.js', 'utf8', (err: any, data: string) => {
        // Buffer.concat(data)
        if (err) {
            console.log(err);
        }

        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Request-Method', '*');
        res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
        res.setHeader('Access-Control-Allow-Headers', '*');
        // res.setHeader('Content-Type', 'application/wasm');
        res.setHeader('Content-Type', 'application/javascript');

        // res.writeHead(200, {'Content-Type': 'application/wasm'});
        res.status(200).end(data);
    });
}

@Get('stockfish.wasm')
private getWebworkerWasmSf(req: Request, res: Response) {
    Logger.Info('Serving file ../../node_modules/stockfish.js/stockfish.wasm');

    fs.readFile('../../node_modules/stockfish.js/stockfish.wasm', 'binary', (err: any, data: Buffer) => {
        // Buffer.concat(data)
        if (err) {
            console.log(err);
        }

        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Request-Method', '*');
        res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
        res.setHeader('Access-Control-Allow-Headers', '*');
        res.setHeader('Content-Type', 'application/wasm');

        // res.writeHead(200, {'Content-Type': 'application/wasm'});
        res.status(200).end(data);
    });
}

In my browser I execute it as follows

 engine = new Worker(`http://localhost:${this.port}/files/webworker-wasm.js`);
 engine.postMessage('uci');
 engine.postMessage('setoption name Move Overhead value 200');
 engine.postMessage('position fen rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR b KQkq - 0 1');
engine.postMessage('go  wtime 59000 btime 60000');

Maybe I'm serving the files wrong or am I using wrong options. Any suggestions?

em++:Permission denied while running with docker

cannot run with docker. Getting this permission error inside docker container.

vjuneja$ docker run --volume $PWD:/home/builder/stockfish.js --user $(id -u) niklasf/emscripten-for-stockfish
Makefile:613: recipe for target '.depend' failed
make: [.depend] Error 127 (ignored)

Config:
debug: 'no'
sanitize: 'no'
optimize: 'yes'
arch: 'any'
bits: '32'
kernel: 'Linux'
os: 'GNU/Linux'
prefetch: 'no'
popcnt: 'no'
sse: 'no'
pext: 'no'

Flags:
CXX: em++
CXXFLAGS: -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DUSELONGESTPV -DNDEBUG -O3 -DNO_PREFETCH
LDFLAGS:  -s TOTAL_MEMORY=33554432 --memory-init-file 0 -s NO_EXIT_RUNTIME=1 -s EXPORTED_FUNCTIONS=[_main, _uci_command] --pre-js pre.js --post-js post.js -O3

Testing config sanity. If this fails, try 'make help' ...

make ARCH=js COMP=emscripten all
make[1]: Entering directory '/home/builder/stockfish.js/src'
Makefile:613: recipe for target '.depend' failed
make[1]: [.depend] Error 127 (ignored)
em++ -Wall -Wcast-qual -std=c++11  -DANTI -DATOMIC -DCRAZYHOUSE -DHORDE -DKOTH -DRACE -DTHREECHECK -DUSELONGESTPV -DNDEBUG -O3 -DNO_PREFETCH   -c -o bitbase.o bitbase.cpp
make[1]: execvp: em++: Permission denied
<builtin>: recipe for target 'bitbase.o' failed
make[1]: *** [bitbase.o] Error 127
make[1]: Leaving directory '/home/builder/stockfish.js/src'
Makefile:491: recipe for target 'build' failed
make: *** [build] Error 2
vjuneja$

Tried running assudo as well.

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.