-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wasm] fix #5021: wechat webassembly support #5056
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa47bdb
fix: wechat webassembly support
deepkolos 0cdb0ed
replace createInstantiateWasmFunc in better way
deepkolos 62dc0bf
revert miniprogram build target
deepkolos 6b2c544
Merge branch 'master' into master
pyu10055 054cad1
Update rollup.config.js
pyu10055 028cb5a
Update backend_wasm.ts
pyu10055 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +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() { | ||
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') | ||
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')) { | ||
code = code.replace(`env().getAsync('WASM_HAS_SIMD_SUPPORT')`, 'false') | ||
code = code.replace(`env().getAsync('WASM_HAS_MULTITHREAD_SUPPORT')`, 'false') | ||
code = code.replace( | ||
`function createInstantiateWasmFunc(path) {`, | ||
`function createInstantiateWasmFunc(path) { | ||
return function (imports, callback) { | ||
WebAssembly.instantiate(path, imports).then(function (output) { | ||
callback(output.instance, output.module); | ||
}); | ||
return {}; | ||
}`) | ||
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.`, `WXWebAssembly.`) | ||
code = code.replace(`typeof WebAssembly`, `typeof WXWebAssembly`) | ||
return { code }; | ||
} | ||
} | ||
code = code.replace(/WebAssembly\./g, `WXWebAssembly.`); | ||
code = code.replace(/typeof WebAssembly/g, `typeof WXWebAssembly`); | ||
return { code, map: null }; | ||
}, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pyu10055 a better way to replace createInstantiateWasmFunc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and test case updated
https://github.com/deepkolos/tfjs-wxmp-wasm-test/blob/ebe95a1a036c1a29ab8a7a980d5f2d2099a1ea99/pages/index/index.js#L15