繁体   English   中英

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

[英].js created (by Webassembly and emscripten) works only once

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

我正在使用这种方式(可以在向标准HTML程序提供stdin上找到 ):

我评论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>

文字区

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

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

和一个按钮

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

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

来自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() )。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM