收藏
回答

worker如何使用webassembly胶水js?

我用Emscripten把c函数编译生成hello.js和hello.wasm两个文件,可以在page/index.js里面require使用,但是在worker中使用就会遇到问题:

官网文档中说worker不能加载其目录以外的文件,实测也的确如此:

const hello_debug=require('../pages/index/hello.js')
报错:
thirdScriptError
worker uncaught third Error
module "pages/index/hello.js" is not defined
Error: module "pages/index/hello.js" is not defined

同时:

"在 Worker 内使用 WXWebAssembly 时,.wasm 文件需要放置在 worker 目录外,因为 worker 目录只会打包 .js 文件,非 .js 文件会被忽略"

我将胶水js放在worker目录下,wasm文件放在pages下,

const hello_debug=require('./hello.js')
报错:
thirdScriptError
worker uncaught third Error
module "pages/index/hello.js" is not defined
Error: module "pages/index/hello.js" is not defined


请问该如何在worker中加载webassembly的胶水js文件呢?这个报错原因是啥呢,谢谢!

回答关注问题邀请回答
收藏

2 个回答

  • sine
    sine
    2021-11-22

    胶水js放在worker目录下,wasm文件放在worker目录外,这个用法没问题。

    具体原因得看看代码,可以通过代码片段功能展示一下代码: https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

    2021-11-22
    有用 1
    回复 7
    • 汤圆🌕
      汤圆🌕
      2021-11-23
      感谢回复,代码片段在这边:
      https://developers.weixin.qq.com/s/HbOu51mC7rvc


      目前我可以用指令emcc hello.cc -s STANDALONE_WASM -o hellonew.wasm的形式生存不包含胶水js的wasm文件并在worker中调用。


      但是如果普通的emcc hello.cc -o hello_glue.js,生成的wasm和胶水js在导入的时候会报错,如代码片段所示,请问该如何修改才能在小程序中使用呢?(能在普通的web中使用)。
      谢谢!
      2021-11-23
      回复
    • sine
      sine
      2021-11-23回复汤圆🌕
      我打开了你的demo,看到报错内容并不是 Error: module "pages/index/hello.js" is not defined。而是  XMLHttpRequest is not a constructor
      2021-11-23
      1
      回复
    • sine
      sine
      2021-11-23回复sine
      XMLHttpRequest is not a constructor 的原因是小程序不支持 XMLHttpRequest,需要手动适配一下
      2021-11-23
      回复
    • sine
      sine
      2021-11-23回复sine
      2021-11-23
      回复
    • 汤圆🌕
      汤圆🌕
      2021-11-24回复sine
      不好意思,原问题贴错了log,的确是您所说的这个报错. 但是worker中不支持wx. API,该如何修改呢?
      请问能否分享基于WXWebAssembly的可在worker中用的最简胶水js呢?网上之前的代码大多基于改版之前的WebAssembly,目前小程序不支持了.


      具体的情况我在这个提问中提出了:
      https://developers.weixin.qq.com/community/develop/doc/000ca895518d6870211df985c56000
      万分感谢!
      2021-11-24
      回复
    查看更多(2)
  • 疯狂的代码
ᯤ⁶ᴳ
    疯狂的代码 ᯤ⁶ᴳ
    2023-12-13

    我想问一下,你编译出来运行Wasm卡不卡,我这里很卡,不知道为什么,谢谢,很急

    2023-12-13
    有用
    回复
登录 后发表内容