通过Webassembly和emscripten创建的.js只能运行一次

时间:2019-02-22 15:37:28

标签: javascript emscripten webassembly

我正在为一个项目使用Webassembly和emscripten,并且该网页运行良好。在其中,我将带有信息的文本区域发送到(通过Webassembly和emscripten创建的).js进行处理,但是,哦,这是一个问题!!,当我修改文本区域中的内容并重新提交时,只能工作一次!到js,它什么都不做。当我重新加载页面时,它可以再次工作(仅一次)。

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

我评论run();在剧本结尾

// 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>

io textareas

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

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

和一个按钮

<button onclick="execEmscript();">run</button>

1 个答案:

答案 0 :(得分: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=0命令指定emcc(这样就不必注释掉run())。

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