-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BUG * fix: wechat webassembly support * replace createInstantiateWasmFunc in better way * revert miniprogram build target * Update rollup.config.js * Update backend_wasm.ts Co-authored-by: Ping Yu <4018+pyu10055@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { walk } from 'estree-walker'; | ||
|
||
function createInstantiateWasmFunc(path) { | ||
return function (imports, callback) { | ||
WebAssembly.instantiate(path, imports).then(function (output) { | ||
callback(output.instance, output.module); | ||
}); | ||
return {}; | ||
}; | ||
} | ||
|
||
/** | ||
* WebAssembly has changed to WXWebAssembly after WeChat 8.0 | ||
* 0. not simd or thread support. | ||
* 1. only load local wasm file is allowed. | ||
* 2. WebAssembly.validate not working so env registerFlag doesn't work | ||
* @see https://developers.weixin.qq.com/community/develop/doc/000e2c019f8a003d5dfbb54c251c00?jumpto=comment&commentid=000eac66934960576d0cb1a7256c | ||
*/ | ||
export function patchWechatWebAssembly() { | ||
return { | ||
transform(code, file) { | ||
// remove node imports | ||
if ( | ||
file.endsWith('tfjs-backend-wasm-threaded-simd.worker.js') || | ||
file.endsWith('tfjs-backend-wasm-threaded-simd.js') | ||
) { | ||
code = code.replace(`require("worker_threads")`, 'null'); | ||
code = code.replace(`require("perf_hooks")`, 'null'); | ||
} | ||
|
||
// it is not a nice way, but WebAssembly.validate not working and SIMD is not support in WXWebAssembly | ||
// tf.env().set('WASM_HAS_SIMD_SUPPORT', false) will be done in tfjs-wechat or application code | ||
// so does the WASM_HAS_MULTITHREAD_SUPPORT | ||
if (file.endsWith('backend_wasm.ts')) { | ||
const ast = this.parse(code); | ||
walk(ast, { | ||
enter(node) { | ||
if ( | ||
node.type === 'FunctionDeclaration' && | ||
node.id && | ||
node.id.name === 'createInstantiateWasmFunc' | ||
) { | ||
code = code.replace( | ||
code.slice(node.start, node.end), | ||
createInstantiateWasmFunc.toString(), | ||
); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
code = code.replace(/WebAssembly\./g, `WXWebAssembly.`); | ||
code = code.replace(/typeof WebAssembly/g, `typeof WXWebAssembly`); | ||
return { code, map: null }; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters