如何将参数传递给Wasm?

huangapple go评论62阅读模式
英文:

How to pass args to wasm?

问题

#include <iostream>

int main(int argc, char** argv) {
  if (argc > 1)
    printf("%s", argv[1]);
}

构建:

emcc ./test.cpp -sENVIRONMENT=shell -o test.js

将命令行参数传递到C++代码中。

英文:

I use emcc to complie a C++ code into js.
But how to pass args to C++ code?

#include&lt;iostream&gt;

int main(int argc, char** argv) {
  if (argc &gt; 1)
    printf(&quot;%s&quot;, argv[1]);
}

build :

emcc ./test.cpp -sENVIRONMENT=shell -o test.js

pass commadline args into c code

答案1

得分: 2

这个stackexchange答案似乎与您的用例相似:

在运行您的函数之前,放置以下代码:

Module[&#39;arguments&#39;].push(&#39;first_param&#39;);
Module[&#39;arguments&#39;].push(&#39;second_param&#39;);
int main(int argc, char *argv[])
{
    assert(argc == 3);
    assert(strcmp(argv[1], &quot;first_param&quot;) == 0);
    assert(strcmp(argv[2], &quot;second_param&quot;) == 0);
}

文档参考

编辑:

所以,最终我有时间安装了d8,并使其正常工作。

对我来说,在生成的test.js中,有这一行:

if (Module[&#39;arguments&#39;]) arguments_ = Module[&#39;arguments&#39;];legacyModuleProp(&#39;arguments&#39;, &#39;arguments_&#39;);

您必须在此之前放置参数,所以它应该如下所示:

Module[&#39;arguments&#39;] = [];
Module[&#39;arguments&#39;].push(&#39;first_param&#39;);
Module[&#39;arguments&#39;].push(&#39;second_param&#39;);

if (Module[&#39;arguments&#39;]) arguments_ = Module[&#39;arguments&#39;];legacyModuleProp(&#39;arguments&#39;, &#39;arguments_&#39;);

然后使用emcc进行编译,并使用d8运行。我遇到了另一个错误,因为我没有在printf调用中添加换行符,所以输出没有打印,这似乎也发生在您的代码中。幸运的是,有一个关于标准输出没有完全刷新的错误消息,所以我明白了这个问题。

英文:

It seems that this stackexchange answer is similar to your use-case:

Place

Module[&#39;arguments&#39;].push(&#39;first_param&#39;);
Module[&#39;arguments&#39;].push(&#39;second_param&#39;);

before you run your function.

int main(int argc, char *argv[])
{
    assert(argc == 3);
    assert(strcmp(argv[1], &quot;first_param&quot;) == 0);
    assert(strcmp(argv[2], &quot;second_param&quot;) == 0);
}

Documentation reference

Edit:

So, I finally had time to install d8, and I made it work.

For me, in the generated test.js, there is this line:

if (Module[&#39;arguments&#39;]) arguments_ = Module[&#39;arguments&#39;];legacyModuleProp(&#39;arguments&#39;, &#39;arguments_&#39;);

You have to place the arguments before this, so it should look like:

Module[&#39;arguments&#39;] = [];
Module[&#39;arguments&#39;].push(&#39;first_param&#39;);
Module[&#39;arguments&#39;].push(&#39;second_param&#39;);

if (Module[&#39;arguments&#39;]) arguments_ = Module[&#39;arguments&#39;];legacyModuleProp(&#39;arguments&#39;, &#39;arguments_&#39;);

You then compile with emcc and run with d8. I got another error because I did not end my printf call without a newline, so the output was not printing, a thing that seems to also happen in your code. Fortunately, there was an error message about the stdout not fully flushing, so I got to understand that was the problem.

huangapple
  • 本文由 发表于 2023年3月7日 11:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定