@@ -7,15 +7,19 @@ import throttle from 'common/throttle';
77import { povChances } from './winningChances' ;
88import { sanIrreversible } from './util' ;
99
10- function sharedWasmMemory ( initial : number , maximum : number ) : WebAssembly . Memory | undefined {
10+ function sharedWasmMemory ( initial : number , maximum : number ) : WebAssembly . Memory {
11+ return new WebAssembly . Memory ( { shared : true , initial, maximum } as WebAssembly . MemoryDescriptor ) ;
12+ }
13+
14+ function sendableSharedWasmMemory ( initial : number , maximum : number ) : WebAssembly . Memory | undefined {
1115 // Atomics
1216 if ( typeof Atomics !== 'object' ) return ;
1317
1418 // SharedArrayBuffer
1519 if ( typeof SharedArrayBuffer !== 'function' ) return ;
1620
1721 // Shared memory
18- const mem = new WebAssembly . Memory ( { shared : true , initial, maximum } as WebAssembly . MemoryDescriptor ) ;
22+ const mem = sharedWasmMemory ( initial , maximum ) ;
1923 if ( ! ( mem . buffer instanceof SharedArrayBuffer ) ) return ;
2024
2125 // Structured cloning
@@ -51,7 +55,7 @@ export default function (opts: CevalOpts): CevalCtrl {
5155 const source = Uint8Array . from ( [ 0 , 97 , 115 , 109 , 1 , 0 , 0 , 0 ] ) ;
5256 if ( typeof WebAssembly === 'object' && typeof WebAssembly . validate === 'function' && WebAssembly . validate ( source ) ) {
5357 technology = 'wasm' ; // WebAssembly 1.0
54- const sharedMem = sharedWasmMemory ( 8 , 16 ) ;
58+ const sharedMem = sendableSharedWasmMemory ( 8 , 16 ) ;
5559 if ( sharedMem ) {
5660 technology = 'hce' ;
5761
@@ -98,7 +102,7 @@ export default function (opts: CevalOpts): CevalCtrl {
98102 const hovering = prop < Hovering | null > ( null ) ;
99103 const isDeeper = prop ( false ) ;
100104
101- const workerOpts = {
105+ const protocolOpts = {
102106 minDepth,
103107 variant : opts . variant . key ,
104108 threads : ( technology == 'hce' || technology == 'nnue' ) && ( ( ) => Math . min ( parseInt ( threads ( ) ) , maxThreads ) ) ,
@@ -218,24 +222,24 @@ export default function (opts: CevalOpts): CevalCtrl {
218222
219223 if ( ! worker ) {
220224 if ( technology == 'nnue' )
221- worker = new ThreadedWasmWorker ( workerOpts , {
225+ worker = new ThreadedWasmWorker ( protocolOpts , {
222226 baseUrl : 'vendor/stockfish-nnue.wasm/' ,
223227 module : 'Stockfish' ,
224228 downloadProgress : throttle ( 200 , mb => {
225229 downloadProgress ( mb ) ;
226230 opts . redraw ( ) ;
227231 } ) ,
228232 version : '2732f2' ,
233+ wasmMemory : sharedWasmMemory ( 2048 , growableSharedMem ? 32768 : 2048 ) ,
229234 } ) ;
230235 else if ( technology == 'hce' )
231- worker = new ThreadedWasmWorker (
232- workerOpts ,
233- officialStockfish ( opts . variant . key )
234- ? { baseUrl : 'vendor/stockfish.wasm/' , module : 'Stockfish' }
235- : { baseUrl : 'vendor/stockfish-mv.wasm/' , module : 'StockfishMv' }
236- ) ;
236+ worker = new ThreadedWasmWorker ( protocolOpts , {
237+ baseUrl : officialStockfish ( opts . variant . key ) ? 'vendor/stockfish.wasm/' : 'vendor/stockfish-mv.wasm/' ,
238+ module : officialStockfish ( opts . variant . key ) ? 'Stockfish' : 'StockfishMv' ,
239+ wasmMemory : sharedWasmMemory ( 1024 , growableSharedMem ? 32768 : 1088 ) ,
240+ } ) ;
237241 else
238- worker = new WebWorker ( workerOpts , {
242+ worker = new WebWorker ( protocolOpts , {
239243 url : technology == 'wasm' ? 'vendor/stockfish.js/stockfish.wasm.js' : 'vendor/stockfish.js/stockfish.js' ,
240244 } ) ;
241245 }
0 commit comments