创建的.js(由Webassembly和emscripten创建)只能运行一次

问题描述 投票:-1回答:1

我正在使用Webassembly和emscripten进行项目,网页运行良好。在其中我发送一个textarea信息到.js创建(由Webassembly和emscripten),以进行处理,但是,哦,有问题!!,只有一次,当我修改它在textarea并重新提交到js,它什么都不做。当我重新加载页面时,它再次工作(只有一次)。

我正在使用这种方式(在Providing stdin to an emscripten HTML program?上找到):

我评论run();在emscript的最后

// in my emscript 

// shouldRunNow refers to calling main(), not run().
var shouldRunNow = true;
if (Module['noInitialRun']) {
   shouldRunNow = false;
}
//run(); // << here
// {{POST_RUN_ADDITIONS}}

.

 result = areaInput(); \\and add areaInput in result

在我的文件中添加以下代码以激活emscript中的run()

<script>
var message;
var point = -1;
function getArea(){
   message = document.getElementById('input').value.split('\n');
}
function areaInput(){
  if(point >= message.length - 1){
    return null;
  }
  point += 1;
  return message[point];
}
function execEmscript(){
  window.console = {
     log: function(str){
        document.getElementById("output").value += "\n" + str;
    }
 }
getArea();
run();
}
</script>

我textareas

<textarea id="input" cols="80" rows="30"></textarea>

<textarea id="output" cols="80" rows="30"></textarea>

和一个按钮

<button onclick="execEmscript();">run</button>
javascript emscripten webassembly
1个回答
1
投票

也许这些设置会有所帮助:

来自src/settings.js

// Whether we will run the main() function. Disable if you embed the generated
// code in your own, and will call main() yourself at the right time (which you
// can do with Module.callMain(), with an optional parameter of commandline args).
var INVOKE_RUN = 1;

// If 0, the runtime is not quit when main() completes (allowing code to
// run afterwards, for example from the browser main event loop). atexit()s
// are also not executed, and we can avoid including code for runtime shutdown,
// like flushing the stdio streams.
// Set this to 1 if you do want atexit()s or stdio streams to be flushed
// on exit.
var EXIT_RUNTIME = 0;

在您的Emscripten版本中,您可以默认使用EXIT_RUNTIME = 1。该文件中的其他选项也很有趣。

所以尝试指定-s INVOKE_RUN=0 -s EXIT_RUNTIME=0emcc命令(你不需要注释掉run()然后)。

但是你的程序可能不会期望你多次调用main()。这可能是通过设置EXPORTED_FUNCTIONS导出一些其他C函数并从你的JS调用它来解决(不确定,但你可能首先需要调用main())。

© www.soinside.com 2019 - 2024. All rights reserved.