From 22768ddcd0b29620737af96d6ac4fd46c4dfff5c Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 8 Aug 2021 11:41:05 -0400 Subject: [PATCH] Remove undue dependencies on vAPI Whether WebAssembly can be enabled or not should be decided at a higher level. --- src/js/hntrie.js | 37 +++++++++++++++++-------------------- src/js/start.js | 2 +- src/js/storage.js | 2 +- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/js/hntrie.js b/src/js/hntrie.js index 63e91dc4b..8208ad92e 100644 --- a/src/js/hntrie.js +++ b/src/js/hntrie.js @@ -19,8 +19,6 @@ Home: https://github.com/gorhill/uBlock */ -/* globals WebAssembly vAPI */ - 'use strict'; /******************************************************************************* @@ -457,20 +455,28 @@ const HNTrieContainer = class { } async enableWASM(wasmModuleFetcher, path) { - if ( typeof WebAssembly !== 'object' ) { return false; } - if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; } + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis + const globals = (( ) => { + // jshint ignore:start + if ( typeof globalThis !== 'undefined' ) { return globalThis; } + if ( typeof self !== 'undefined' ) { return self; } + if ( typeof global !== 'undefined' ) { return global; } + // jshint ignore:end + return {}; + })(); + const WASM = globals.WebAssembly || null; + if ( WASM === null ) { return false; } + if ( this.wasmMemory instanceof WASM.Memory ) { return true; } const module = await getWasmModule(wasmModuleFetcher, path); - if ( module instanceof WebAssembly.Module === false ) { return false; } - const memory = new WebAssembly.Memory({ initial: 2 }); - const instance = await WebAssembly.instantiate(module, { + if ( module instanceof WASM.Module === false ) { return false; } + const memory = new WASM.Memory({ initial: 2 }); + const instance = await WASM.instantiate(module, { imports: { memory, growBuf: this.growBuf.bind(this, 24, 256) } }); - if ( instance instanceof WebAssembly.Instance === false ) { - return false; - } + if ( instance instanceof WASM.Instance === false ) { return false; } this.wasmMemory = memory; const curPageCount = memory.buffer.byteLength >>> 16; const newPageCount = roundToPageSize(this.buf.byteLength) >>> 16; @@ -536,10 +542,7 @@ const HNTrieContainer = class { resizeBuf(bufLen, char0) { bufLen = roundToPageSize(bufLen); - if ( - bufLen === this.buf.length && - char0 === this.buf32[CHAR0_SLOT] - ) { + if ( bufLen === this.buf.length && char0 === this.buf32[CHAR0_SLOT] ) { return; } const charDataLen = this.buf32[CHAR1_SLOT] - this.buf32[CHAR0_SLOT]; @@ -773,12 +776,6 @@ const getWasmModule = (( ) => { return wasmModulePromise; } - if ( typeof WebAssembly !== 'object' ) { return; } - - // Soft-dependency on vAPI so that the code here can be used outside of - // uBO (i.e. tests, benchmarks) - if ( typeof vAPI === 'object' && vAPI.canWASM !== true ) { return; } - // The wasm module will work only if CPU is natively little-endian, // as we use native uint32 array in our js code. const uint32s = new Uint32Array(1); diff --git a/src/js/start.js b/src/js/start.js index c8ad67521..2cbc57acf 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -337,7 +337,7 @@ try { vAPI.net.suspend(); } - if ( µb.hiddenSettings.disableWebAssembly !== true ) { + if ( vAPI.canWASM && µb.hiddenSettings.disableWebAssembly !== true ) { const wasmModuleFetcher = function(path) { return fetch(`${path}.wasm`, { mode: 'same-origin' }).then( globals.WebAssembly.compileStreaming diff --git a/src/js/storage.js b/src/js/storage.js index 4b15eaac0..3be81945e 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1208,7 +1208,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { const psl = globals.publicSuffixList; // WASM is nice but not critical - if ( this.hiddenSettings.disableWebAssembly !== true ) { + if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) { const wasmModuleFetcher = function(path) { return fetch( `${path}.wasm`, { mode: 'same-origin'