Skip to content

Commit

Permalink
fix: wasm loading in editor native engine && fix gulp build (cocos#…
Browse files Browse the repository at this point in the history
…15012)

* fix: wasm loading in editor native engine

* fix: gulp build workflow
  • Loading branch information
PPpro authored May 10, 2023
1 parent ad6d3c3 commit 4811334
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 247 deletions.
492 changes: 280 additions & 212 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@babel/core": "^7.13.10",
"@babel/preset-env": "7.8.7",
"@cocos/babel-preset-cc": "2.2.0",
"@cocos/build-engine": "4.4.1-alpha.10",
"@cocos/build-engine": "4.4.1",
"@types/fs-extra": "^5.0.4",
"@types/jest": "^24.9.1",
"@types/yargs": "^12.0.14",
Expand Down
10 changes: 10 additions & 0 deletions pal/wasm/wasm-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { EDITOR } from 'internal:constants';
import { native } from '../../cocos/native-binding/index';

export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise<any> {
// NOTE: when it's in EDITOR, wasmUrl is a url with `external:` protocol.
if (EDITOR) {
return Editor.Message.request('engine', 'query-engine-info').then((info) => {
const externalRoot = `${info.native.path}/external/`;
wasmUrl = wasmUrl.replace('external:', externalRoot);
const arrayBuffer = native.fileUtils.getDataFromFile(wasmUrl);
return WebAssembly.instantiate(arrayBuffer, importObject);
}) as Promise<any>;
}
wasmUrl = `/src/cocos-js/${wasmUrl}`;
const arrayBuffer = native.fileUtils.getDataFromFile(wasmUrl);
return WebAssembly.instantiate(arrayBuffer, importObject);
Expand Down
7 changes: 5 additions & 2 deletions platforms/native/builtin/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
console.info('injectWebAssembly ...');

const oldWebAssemblyInstantiate = WebAssembly.instantiate;
const oldWebAssemblyCompile = WebAssembly.compile;

WebAssembly.compile = function(bufferSource) {
return new Promise((resolve, reject)=>{
if (!bufferSource) {
reject('WebAssembly.compile: Invalid buffer source!');
}
else {
} else if (CC_EDITOR) {
// FIX EDITOR ERROR: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread.
resolve(oldWebAssemblyCompile.call(WebAssembly, bufferSource));
} else {
resolve(new WebAssembly.Module(bufferSource));
}
});
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-engine/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/build-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/build-engine",
"version": "4.4.1-oh.3",
"version": "4.4.1",
"description": "",
"repository": {
"type": "git",
Expand Down
30 changes: 0 additions & 30 deletions scripts/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,36 +591,6 @@ async function doBuild({
rollupOptions.perf = true;
}

const bulletAsmJsModule = await nodeResolveAsync('@cocos/bullet/bullet.cocos.js');
const wasmBinaryPath = ps.join(bulletAsmJsModule, '..', 'bullet.wasm.wasm');
if (ammoJsWasm === true) {
rpVirtualOptions['@cocos/bullet'] = `
import wasmBinaryURL from '${pathToAssetRefURL(wasmBinaryPath)}';
export const bulletType = 'wasm';
export default wasmBinaryURL;
`;
} else if (ammoJsWasm === 'fallback') {
rpVirtualOptions['@cocos/bullet'] = `
export async function initialize(isWasm) {
let ammo;
if (isWasm) {
ammo = await import('${pathToAssetRefURL(wasmBinaryPath)}');
} else {
ammo = await import('${filePathToModuleRequest(bulletAsmJsModule)}');
}
return ammo.default;
}
export const bulletType = 'fallback';
export default initialize;
`;
} else {
rpVirtualOptions['@cocos/bullet'] = `
import Bullet from '${filePathToModuleRequest(bulletAsmJsModule)}';
export const bulletType = 'asmjs';
export default Bullet;
`;
}

const rollupBuild = await rollup.rollup(rollupOptions);

const timing = rollupBuild.getTimings?.();
Expand Down

0 comments on commit 4811334

Please sign in to comment.