简体   繁体   English

使用 go 和 Vue + Vite 编写的 wasm 函数的设置

[英]What setup to use wasm functions written in go with Vue + Vite

The Goal目标

How to use wasm functions written in Go with Vue + Vite?如何在 Vue + Vite 中使用用 Go 编写的 wasm 函数? ( ideally in a pinia's actions store ) (最好在 pinia 的行动商店)

How not to succeed不成功怎么办

On the Go side following the wiki with vanilla js it works perfectly and I'm able to play with the syscall/js package ( so I guess my.wasm is not completely crap )在 Go 方面,跟随带有香草 js 的wiki ,它运行良好,我可以使用 syscall/js package (所以我猜 my.wasm 并不完全是废话)

On the Vitesse(Vue + Vite) side following the Vite doc I'm able to run a wasm function add.wasm ( taken from here )Vite 文档之后的 Vitesse(Vue + Vite) 方面,我可以运行 wasm function add.wasm (取自此处

But unfortunately with the.wasm I wrote and compiled myself with go v1.19 I get the following error:但不幸的是,我使用 the.wasm 编写并使用 go v1.19 自己编写和编译,我收到以下错误:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="go" error: module is not an object or function

If I try to import the wasm_exec.js directly in the store and also get:如果我尝试直接在商店中导入wasm_exec.js并获得:

Failed to resolve import "go" from wasm_exec.js or The requested module '/src/store/wasm_exec.js' does not provide an export named 'Go' Failed to resolve import "go" from wasm_exec.jsThe requested module '/src/store/wasm_exec.js' does not provide an export named 'Go'

I tried to use the vite-plugin-wasm without success.我尝试使用vite-plugin-wasm没有成功。

The issue might be related to the fact that wasm written in go needs to use a wasm_exec.js then a Go() instance in order to make the WebAssembly.instantiateStreaming() work..该问题可能与用 go 编写的 wasm 需要使用 wasm_exec.js 然后使用 Go() 实例以使WebAssembly.instantiateStreaming()工作的事实有关。

Or it's completely something else that I don't get.或者这完全是我没有得到的其他东西。

It'll be more understandable with my demo我的演示会更容易理解

I'm out of ideas on how to make it work.. Is the issue in go?我不知道如何使它工作。go 中的问题是什么? vite?维特? Wasm?瓦斯姆? Can it be realated to this issue or this one ?它可以解决这个问题这个问题吗? If anybody has experience using is combinaison ( go wasm vue vite ) can you share your experience/code/docs?如果有人有使用组合的经验( go wasm vue vite )你能分享你的经验/代码/文档吗?

Solved解决了

After further research I found my error...经过进一步研究,我发现了我的错误......

As the error messages suggested, the problem was due to the integration of wasm_exec.js.正如错误消息所暗示的,问题是由于集成了 wasm_exec.js。

I had ended up using tinygo in order to reduce the size of the.wasm generated: For a simple hello world ~1MB with standard go1.19 vs ~10KB with tinygo… ( btw I will be curious to know the reason behind this massive difference.. because of the garbage collector??? )我最终使用 tinygo 来减少生成的.wasm 的大小:对于一个简单的 hello world,标准 go1.19 约 1MB,而 tinygo 约 10KB……(顺便说一句,我很想知道这种巨大差异背后的原因..因为垃圾收集器???)

But as they mention in the doc the tinyo wasm_exec.js is different from the standard go one:但正如他们在文档中提到的,tinyo wasm_exec.js 与标准的 go 不同:

Execution of the contents require a few JS helper functions which are called from WebAssembly.内容的执行需要一些从 WebAssembly 调用的 JS 辅助函数。 We have defined these in tinygo/targets/wasm_exec.js .我们在tinygo/targets/wasm_exec.js中定义了这些。 It is based on $GOROOT/misc/wasm/wasm_exec.js from the standard library, but is slightly different.它基于标准库中的$GOROOT/misc/wasm/wasm_exec.js ,但略有不同。 Ensure you are using the same version of wasm_exec.js as the version of tinygo you are using to compile.确保您使用的 wasm_exec.js 版本与您用于编译的 tinygo 版本相同。

By mistake I mixed the wasm_exec.js from go with a.wasm form tinygo… or vice versa idk… and it didn't worked..错误地,我将wasm_exec.js中的 wasm_exec.js 与 tinygo 形式的 a.wasm 混合在一起……反之亦然 idk ……但它没有用。

To patch it I use the 2 files from tinygo ( didn't try only with go1.19 ) and I import them into the header of the index.html为了修补它,我使用来自 tinygo 的 2 个文件(没有尝试仅使用 go1.19 )并将它们导入索引的 header 中index.html

Vite handle rest and I'm able to call the wasm functions from everywhere, including from the pinia stores. Vite 手柄 rest 并且我可以从任何地方调用 wasm 函数,包括在 pinia 商店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Vue 和 Vite 中使用 process.env 时出错 - Error when I use process.env with Vue and Vite 使用 Vue 3 + Vite 不匹配路由器路径 - Router path not matched using Vue 3 + Vite Firebase 函数使用什么记录器? - What logger to use for Firebase Functions? rust:相当于用 Go 编写的这种并发代码? - rust: equivalent of such concurrent code written in Go? Vite + Vue3 + CapacitorJS + iOS 上的 Firebase 未通过身份验证 - Vite + Vue3 + CapacitorJS + Firebase on iOS is not passing through Authentication 在 NodeJS Cloud Functions 中使用本地模块的最佳方式是什么? - What is the best way to use local module in NodeJS Cloud Functions? 有没有办法知道在 S3 表中写入的最后一个分区是什么,用于 AWS Glue 作业中的下推谓词? - Is there a way to know what was the last partition written in S3 table to use for a push down predicate in AWS Glue Job? Go 中的封装使用问题 - Problems to use packages in Go 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions? 在 firestore (Vue) 中使用 Firebase uid - Use Firebase uid in firestore (Vue)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM