Skip to content

Commit

Permalink
Merge branch 'master' into glicko-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 19, 2024
2 parents 7e352ac + 095f604 commit 134478b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
20 changes: 8 additions & 12 deletions ui/ceval/src/engines/engines.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { BrowserEngineInfo, ExternalEngineInfo, EngineInfo, CevalEngine, Requires } from '../types';
import type { BrowserEngineInfo, ExternalEngineInfo, EngineInfo, CevalEngine } from '../types';
import type CevalCtrl from '../ctrl';
import { SimpleEngine } from './simpleEngine';
import { StockfishWebEngine } from './stockfishWebEngine';
import { ThreadedEngine } from './threadedEngine';
import { ExternalEngine } from './externalEngine';
import { storedStringProp, StoredProp } from 'common/storage';
import { isAndroid, isIOS, isIPad, getFirefoxMajorVersion, features, type Feature } from 'common/device';
import { isAndroid, isIOS, isIPad, features as browserSupport, type Feature } from 'common/device';
import { xhrHeader } from 'common/xhr';
import { lichessRules } from 'chessops/compat';
import { log } from 'common/permalog';
Expand All @@ -16,12 +16,8 @@ export class Engines {
localEngineMap: Map<string, WithMake>;
externalEngines: ExternalEngineInfo[];
selectProp: StoredProp<string>;
browserSupport: Requires[] = features().slice();

constructor(private ctrl: CevalCtrl) {
const firefox = getFirefoxMajorVersion();
if (!firefox || firefox > 113) this.browserSupport.push('allowLsfw');

this.localEngineMap = this.makeEngineMap();
this.localEngines = [...this.localEngineMap.values()].map(e => e.info);
this.externalEngines = this.ctrl.opts.externalEngines?.map(e => ({ tech: 'EXTERNAL', ...e })) ?? [];
Expand All @@ -47,7 +43,7 @@ export class Engines {
name: 'Fairy Stockfish 14+ NNUE',
short: 'FSF 14+',
tech: 'NNUE',
requires: ['simd', 'allowLsfw'],
requires: ['simd', 'dynamicImportFromWorker'],
variants: [key],
assets: {
version: 'sfw006',
Expand Down Expand Up @@ -75,7 +71,7 @@ export class Engines {
name: 'Stockfish 16 NNUE · 7MB',
short: 'SF 16 · 7MB',
tech: 'NNUE',
requires: ['simd', 'allowLsfw'],
requires: ['simd', 'dynamicImportFromWorker'],
minMem: 1536,
assets: {
version: 'sfw006',
Expand All @@ -91,7 +87,7 @@ export class Engines {
name: 'Stockfish 17 NNUE · 79MB',
short: 'SF 17 · 79MB',
tech: 'NNUE',
requires: ['simd', 'allowLsfw'],
requires: ['simd', 'dynamicImportFromWorker'],
minMem: 2560,
assets: {
version: 'sfw006',
Expand Down Expand Up @@ -126,7 +122,7 @@ export class Engines {
name: 'Fairy Stockfish 14+ HCE',
short: 'FSF 14+',
tech: 'HCE',
requires: ['simd', 'allowLsfw'],
requires: ['simd', 'dynamicImportFromWorker'],
variants: variants.map(v => v[0]),
assets: {
version: 'sfw006',
Expand Down Expand Up @@ -214,8 +210,8 @@ export class Engines {
]
.filter(
e =>
e.info.requires.every((req: Requires) => this.browserSupport.includes(req)) &&
!(e.info.obsoletedBy && this.browserSupport.includes(e.info.obsoletedBy as Feature)),
e.info.requires.every((req: Feature) => browserSupport().includes(req)) &&
!(e.info.obsoletedBy && browserSupport().includes(e.info.obsoletedBy as Feature)),
)
.map(e => [e.info.id, { info: withDefaults(e.info as BrowserEngineInfo), make: e.make }]),
);
Expand Down
6 changes: 2 additions & 4 deletions ui/ceval/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface BaseEngineInfo {
minThreads?: number;
maxThreads?: number;
maxHash?: number;
requires?: Requires[];
requires?: Feature[];
}

export interface ExternalEngineInfo extends BaseEngineInfo {
Expand All @@ -46,14 +46,12 @@ export interface ExternalEngineInfo extends BaseEngineInfo {
export interface BrowserEngineInfo extends BaseEngineInfo {
minMem?: number;
assets: { root?: string; js?: string; wasm?: string; version?: string; nnue?: string[] };
requires: Requires[];
requires: Feature[];
obsoletedBy?: Feature;
}

export type EngineInfo = BrowserEngineInfo | ExternalEngineInfo;

export type Requires = Feature | 'allowLsfw'; // lsfw = lila-stockfish-web

export type EngineNotifier = (status?: {
download?: { bytes: number; total: number };
error?: string;
Expand Down
30 changes: 18 additions & 12 deletions ui/common/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const isApple: () => boolean = memoize<boolean>(

export const shareIcon: () => string = () => (isApple() ? licon.ShareIos : licon.ShareAndroid);

export type Feature = 'wasm' | 'sharedMem' | 'simd';
export type Feature = 'wasm' | 'sharedMem' | 'simd' | 'dynamicImportFromWorker';

export const hasFeature = (feat?: string): boolean => !feat || features().includes(feat as Feature);

Expand All @@ -111,17 +111,23 @@ export const features: () => readonly Feature[] = memoize<readonly Feature[]>(()
WebAssembly.validate(Uint8Array.from([0, 97, 115, 109, 1, 0, 0, 0]))
) {
features.push('wasm');
if (sharedMemoryTest()) {
features.push('sharedMem');
// i32x4.dot_i16x8_s, i32x4.trunc_sat_f64x2_u_zero
const sourceWithSimd = Uint8Array.from([
0, 97, 115, 109, 1, 0, 0, 0, 1, 12, 2, 96, 2, 123, 123, 1, 123, 96, 1, 123, 1, 123, 3, 3, 2, 0, 1, 7,
9, 2, 1, 97, 0, 0, 1, 98, 0, 1, 10, 19, 2, 9, 0, 32, 0, 32, 1, 253, 186, 1, 11, 7, 0, 32, 0, 253, 253,
1, 11,
]);
if (WebAssembly.validate(sourceWithSimd)) features.push('simd');
}
// i32x4.dot_i16x8_s, i32x4.trunc_sat_f64x2_u_zero
const sourceWithSimd = Uint8Array.from([
0, 97, 115, 109, 1, 0, 0, 0, 1, 12, 2, 96, 2, 123, 123, 1, 123, 96, 1, 123, 1, 123, 3, 3, 2, 0, 1, 7, 9,
2, 1, 97, 0, 0, 1, 98, 0, 1, 10, 19, 2, 9, 0, 32, 0, 32, 1, 253, 186, 1, 11, 7, 0, 32, 0, 253, 253, 1,
11,
]);
if (WebAssembly.validate(sourceWithSimd)) features.push('simd');
if (sharedMemoryTest()) features.push('sharedMem');
}
try {
new Worker(
URL.createObjectURL(
new Blob(["import('data:text/javascript,export default {}')"], { type: 'application/javascript' }),
),
).terminate();
features.push('dynamicImportFromWorker');
} catch {}
return Object.freeze(features);
});

Expand All @@ -143,7 +149,7 @@ function sharedMemoryTest(): boolean {
if (!(mem.buffer instanceof SharedArrayBuffer)) return false;

window.postMessage(mem.buffer, '*');
} catch (_) {
} catch {
return false;
}
return mem.buffer instanceof SharedArrayBuffer;
Expand Down
4 changes: 3 additions & 1 deletion ui/puzzle/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export default class Report {
// the `mate` key theme is not sent, as it is considered redubant with `mateInX`
ctrl.data.puzzle.themes.some((t: ThemeKey) => t.toLowerCase().includes('mate')) ||
// if the user has chosen to hide the dialog less than a week ago
this.tsHideReportDialog() > Date.now() - 1000 * 3600 * 24 * 7
this.tsHideReportDialog() > Date.now() - 1000 * 3600 * 24 * 7 ||
// dynamic import from web worker feature is shared by all stockfish 16+ WASMs
!ctrl.ceval.engines.active?.requires?.includes('dynamicImportFromWorker')
)
return;
const node = ctrl.node;
Expand Down

0 comments on commit 134478b

Please sign in to comment.