lichess-org / stockfish.js

The strong open source chess engine Stockfish compiled to JavaScript and WebAssembly using Emscripten
GNU General Public License v3.0
200 stars 29 forks source link

Web Assembly Engine Can't Find .wasm File #15

Closed Auzgame closed 1 year ago

Auzgame commented 1 year ago

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.

niklasf commented 1 year ago

Edited for code tags.

There's no way to pass settings (in particular wasmFile) to the worker, so I don't think this approach is possible with this library as is.

The newer https://github.com/lichess-org/stockfish.wasm and https://github.com/hi-ogawa/Stockfish instanciate the module directly, so they would accept options like wasmBinary or locateFile when instanciating the module:

https://github.com/lichess-org/lila/blob/c2c71cbc7fcba9f7b03f265ef539d7aae879a1df/ui/ceval/src/worker.ts#L167-L172