[go: nahoru, domu]

Skip to content
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 6 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tfjs-backend-wasm/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import node from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import visualizer from 'rollup-plugin-visualizer';
import {getBrowserBundleConfigOptions} from '../rollup.config.helpers';
import {patchWechatWebAssembly} from './scripts/patch-wechat-webassembly'

const PREAMBLE = `/**
* @license
Expand Down Expand Up @@ -143,7 +144,8 @@ module.exports = cmdOptions => {
freeze: false
},
ignore: ['fs', 'path', 'worker_threads', 'perf_hooks', 'os'],
tsCompilerOptions: {target: 'es5'}
tsCompilerOptions: {target: 'es5'},
plugins: [ patchWechatWebAssembly() ]
}));
} else {
const browserBundles = getBrowserBundleConfigOptions(
Expand Down
57 changes: 57 additions & 0 deletions tfjs-backend-wasm/scripts/patch-wechat-webassembly.js
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);
Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and test case updated

tf.env().set('WASM_HAS_SIMD_SUPPORT', false);
tf.env().set('WASM_HAS_MULTITHREAD_SUPPORT', false);

https://github.com/deepkolos/tfjs-wxmp-wasm-test/blob/ebe95a1a036c1a29ab8a7a980d5f2d2099a1ea99/pages/index/index.js#L15

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 };
},
};
}
2 changes: 2 additions & 0 deletions tfjs-backend-wasm/src/backend_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export class BackendWasm extends KernelBackend {
}

function createInstantiateWasmFunc(path: string) {
// this will be replace by rollup plugin patchWechatWebAssembly in minprogram's output
// and the rest code will be remove by treeshaking
// tslint:disable-next-line:no-any
return (imports: any, callback: any) => {
util.fetch(path, {credentials: 'same-origin'}).then((response) => {
Expand Down