From: QueensGambit Date: Sun, 23 Aug 2020 17:56:19 +0000 (+0200) Subject: updated ffish.js for initial npm version X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=29f35add8e3cd4990fbf4f1baf26fc882cf019b4;p=fairystockfish.git updated ffish.js for initial npm version + added ffish.info() with test + added ES6 compile instructions to README.md + updated package.json --- diff --git a/src/ffishjs.cpp b/src/ffishjs.cpp index 41ecdb4..73eb6b6 100644 --- a/src/ffishjs.cpp +++ b/src/ffishjs.cpp @@ -206,10 +206,16 @@ private: } }; +// returns the version of the Fairy-Stockfish binary +std::string info() { + return engine_info(); +} + bool Board::sfInitialized = false; // binding code EMSCRIPTEN_BINDINGS(ffish_js) { + function("info", &info); class_("Board") .constructor<>() .constructor() diff --git a/tests/js/README.md b/tests/js/README.md index f216efa..8068e2e 100644 --- a/tests/js/README.md +++ b/tests/js/README.md @@ -1,15 +1,21 @@ # ffish.js -**ffish.js** is a high performance JavaScript library which supports all chess variants of _FairyStockfish_. +A high performance WebAssembly chess variant library based on _Fairy-Stockfish_. It is built using emscripten/Embind from C++ source code. * https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html +## Install instructions + +```bash +npm install ffish +``` + ## Build instuctions ```bash -cd ../../src +cd Fairy-Stockfish/src ``` ```bash emcc -O3 --bind ffishjs.cpp \ @@ -45,7 +51,7 @@ xboard.cpp \ Load the API in JavaScript: ```javascript -const ffish = require('./ffish.js'); +const ffish = require('ffish'); ``` Create a new variant board from its default starting position: @@ -96,3 +102,57 @@ npm install ```bash node index.js ``` + +## Compile as ES6/ES2015 module + +Some environments such as [vue-js](https://vuejs.org/) may require the library to be exported + as a ES6/ES2015 module. + +```bash +cd Fairy-Stockfish/src +``` +```bash +emcc -O3 --bind \ +-s ENVIRONMENT='web,worker' -s EXPORT_ES6=1 -s MODULARIZE=1 -s USE_ES6_IMPORT_META=0 \ +ffishjs.cpp \ +benchmark.cpp \ +bitbase.cpp \ +bitboard.cpp \ +endgame.cpp \ +evaluate.cpp \ +material.cpp \ +misc.cpp \ +movegen.cpp \ +movepick.cpp \ +parser.cpp \ +partner.cpp \ +pawns.cpp \ +piece.cpp \ +position.cpp \ +psqt.cpp \ +search.cpp \ +thread.cpp \ +timeman.cpp \ +tt.cpp \ +uci.cpp \ +syzygy/tbprobe.cpp \ +ucioption.cpp \ +variant.cpp \ +xboard.cpp \ +-o ../tests/js/ffish.js +``` + +Later the module can be imported as follows: + +```javascript +import Module from './ffish.js'; +let ffish = null; + +new Module().then(loadedModule => { + ffish = loadedModule; + console.log(`initialized ${ffish} ${loadedModule}`); + } +}); + +``` +References: [emscripten/#10114](https://github.com/emscripten-core/emscripten/issues/10114) diff --git a/tests/js/package.json b/tests/js/package.json index 150c016..a454074 100644 --- a/tests/js/package.json +++ b/tests/js/package.json @@ -1,22 +1,33 @@ { - "name": "ffish_test", - "version": "1.0.0", - "description": "Testing server for ffish.js", - "main": "index.js", + "name": "ffish", + "version": "0.1.4", + "description": "A high performance WebAssembly chess variant library based on Fairy-Stockfish", + "main": "ffish.js", "scripts": { - "test": "mocha" + "test": "mocha", + "dev": "node index" }, - "author": "Fabian Fichter, Johannes Czech", + "author": [ + { + "name": "Fabian Fichter", + "url": "https://github.com/ianfab" + }, + { + "name": "Johannes Czech", + "url": "https://github.com/QueensGambit" + } + ], + "keywords": ["wasm", "library", "chess-variants", "fairy-stockfish"], "license": "GPL-3.0", - "dependencies": { + "homepage": "https://github.com/ianfab/Fairy-Stockfish/tree/master/tests/js#readme", + "dependencies": {}, + "devDependencies": { + "chai": "^4.2.0", "chess": "^0.4.3", "chess.js": "^0.11.0", "crazyhouse.js": "0.0.8", "express": "^4.17.1", + "mocha": "^8.0.1", "performance": "^1.4.0" - }, - "devDependencies": { - "chai": "^4.2.0", - "mocha": "^8.0.1" } } diff --git a/tests/js/test.js b/tests/js/test.js index 5b29098..a0a6462 100644 --- a/tests/js/test.js +++ b/tests/js/test.js @@ -198,3 +198,9 @@ describe('board.isGameOver()', function () { chai.expect(board.isGameOver()).to.equal(true); }); }); + +describe('ffish.info()', function () { + it("it returns the version of the Fairy-Stockfish binary", () => { + chai.expect(ffish.info()).to.be.a('string'); + }); +});