1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 15:32:28 +02:00

Remove undue dependencies on vAPI

Whether WebAssembly can be enabled or not should be
decided at a higher level.
This commit is contained in:
Raymond Hill 2021-08-08 11:41:05 -04:00
parent 19f59df22d
commit 22768ddcd0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 19 additions and 22 deletions

View File

@ -19,8 +19,6 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* globals WebAssembly vAPI */
'use strict'; 'use strict';
/******************************************************************************* /*******************************************************************************
@ -457,20 +455,28 @@ const HNTrieContainer = class {
} }
async enableWASM(wasmModuleFetcher, path) { async enableWASM(wasmModuleFetcher, path) {
if ( typeof WebAssembly !== 'object' ) { return false; } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; } 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); const module = await getWasmModule(wasmModuleFetcher, path);
if ( module instanceof WebAssembly.Module === false ) { return false; } if ( module instanceof WASM.Module === false ) { return false; }
const memory = new WebAssembly.Memory({ initial: 2 }); const memory = new WASM.Memory({ initial: 2 });
const instance = await WebAssembly.instantiate(module, { const instance = await WASM.instantiate(module, {
imports: { imports: {
memory, memory,
growBuf: this.growBuf.bind(this, 24, 256) growBuf: this.growBuf.bind(this, 24, 256)
} }
}); });
if ( instance instanceof WebAssembly.Instance === false ) { if ( instance instanceof WASM.Instance === false ) { return false; }
return false;
}
this.wasmMemory = memory; this.wasmMemory = memory;
const curPageCount = memory.buffer.byteLength >>> 16; const curPageCount = memory.buffer.byteLength >>> 16;
const newPageCount = roundToPageSize(this.buf.byteLength) >>> 16; const newPageCount = roundToPageSize(this.buf.byteLength) >>> 16;
@ -536,10 +542,7 @@ const HNTrieContainer = class {
resizeBuf(bufLen, char0) { resizeBuf(bufLen, char0) {
bufLen = roundToPageSize(bufLen); bufLen = roundToPageSize(bufLen);
if ( if ( bufLen === this.buf.length && char0 === this.buf32[CHAR0_SLOT] ) {
bufLen === this.buf.length &&
char0 === this.buf32[CHAR0_SLOT]
) {
return; return;
} }
const charDataLen = this.buf32[CHAR1_SLOT] - this.buf32[CHAR0_SLOT]; const charDataLen = this.buf32[CHAR1_SLOT] - this.buf32[CHAR0_SLOT];
@ -773,12 +776,6 @@ const getWasmModule = (( ) => {
return wasmModulePromise; 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, // The wasm module will work only if CPU is natively little-endian,
// as we use native uint32 array in our js code. // as we use native uint32 array in our js code.
const uint32s = new Uint32Array(1); const uint32s = new Uint32Array(1);

View File

@ -337,7 +337,7 @@ try {
vAPI.net.suspend(); vAPI.net.suspend();
} }
if ( µb.hiddenSettings.disableWebAssembly !== true ) { if ( vAPI.canWASM && µb.hiddenSettings.disableWebAssembly !== true ) {
const wasmModuleFetcher = function(path) { const wasmModuleFetcher = function(path) {
return fetch(`${path}.wasm`, { mode: 'same-origin' }).then( return fetch(`${path}.wasm`, { mode: 'same-origin' }).then(
globals.WebAssembly.compileStreaming globals.WebAssembly.compileStreaming

View File

@ -1208,7 +1208,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
const psl = globals.publicSuffixList; const psl = globals.publicSuffixList;
// WASM is nice but not critical // WASM is nice but not critical
if ( this.hiddenSettings.disableWebAssembly !== true ) { if ( vAPI.canWASM && this.hiddenSettings.disableWebAssembly !== true ) {
const wasmModuleFetcher = function(path) { const wasmModuleFetcher = function(path) {
return fetch( `${path}.wasm`, { return fetch( `${path}.wasm`, {
mode: 'same-origin' mode: 'same-origin'