Skip to content

Commit

Permalink
Remove globals usage from hntrie.js (#3842)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjethani committed Aug 21, 2021
1 parent 7186bd2 commit 5d2c295
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"vAPI": false,
"webext": false,
"µBlock": false,
"URLSearchParams": false
"URLSearchParams": false,
"WebAssembly": false
},
"laxbreak": true,
"newcap": false,
Expand Down
24 changes: 7 additions & 17 deletions src/js/hntrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals self */
/* globals WebAssembly */

'use strict';

Expand Down Expand Up @@ -450,28 +450,18 @@ const HNTrieContainer = class {
}

async enableWASM(wasmModuleFetcher, path) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
const globals = (( ) => {
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; }
if ( typeof WebAssembly === 'undefined' ) { return false; }
if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; }
const module = await getWasmModule(wasmModuleFetcher, path);
if ( module instanceof WASM.Module === false ) { return false; }
const memory = new WASM.Memory({ initial: 2 });
const instance = await WASM.instantiate(module, {
if ( module instanceof WebAssembly.Module === false ) { return false; }
const memory = new WebAssembly.Memory({ initial: 2 });
const instance = await WebAssembly.instantiate(module, {
imports: {
memory,
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;
const curPageCount = memory.buffer.byteLength >>> 16;
const newPageCount = roundToPageSize(this.buf.byteLength) >>> 16;
Expand Down

0 comments on commit 5d2c295

Please sign in to comment.