1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-25 11:52:51 +01:00

Remove globals usage from hntrie.js (#3842)

This commit is contained in:
Manish Jethani 2021-08-21 17:03:53 +05:30 committed by GitHub
parent 7186bd24e5
commit 5d2c295600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 18 deletions

View File

@ -12,7 +12,8 @@
"vAPI": false, "vAPI": false,
"webext": false, "webext": false,
"µBlock": false, "µBlock": false,
"URLSearchParams": false "URLSearchParams": false,
"WebAssembly": false
}, },
"laxbreak": true, "laxbreak": true,
"newcap": false, "newcap": false,

View File

@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* globals self */ /* globals WebAssembly */
'use strict'; 'use strict';
@ -450,28 +450,18 @@ const HNTrieContainer = class {
} }
async enableWASM(wasmModuleFetcher, path) { async enableWASM(wasmModuleFetcher, path) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis if ( typeof WebAssembly === 'undefined' ) { return false; }
const globals = (( ) => { if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; }
if ( typeof self !== 'undefined' ) { return self; }
// jshint ignore:start
if ( typeof globalThis !== 'undefined' ) { return globalThis; }
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 WASM.Module === false ) { return false; } if ( module instanceof WebAssembly.Module === false ) { return false; }
const memory = new WASM.Memory({ initial: 2 }); const memory = new WebAssembly.Memory({ initial: 2 });
const instance = await WASM.instantiate(module, { const instance = await WebAssembly.instantiate(module, {
imports: { imports: {
memory, memory,
growBuf: this.growBuf.bind(this, 24, 256) growBuf: this.growBuf.bind(this, 24, 256)
} }
}); });
if ( instance instanceof WASM.Instance === false ) { return false; } if ( instance instanceof WebAssembly.Instance === 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;