如何解决如何在WASM中制作协程?
我试图允许用户在C语言中调用“ main”,并让“ main”等待requestAnimationFrame继续执行。即main可能有while(1)循环,并且在main中会有一个“ waitForAnimationFrame” ...或允许像sleep(...)这样的函数得以实现。
我一直在研究emscripten实际如何执行此操作的内部工作,但看起来不太适合我的目的。该页面介绍了许多与我想做的事情非常接近的事情,但是当以这种方式实现时,它们似乎都没有真正起作用。 https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html
更具体地说,似乎最好的方法是保存堆栈,然后通过WASM中的虚拟函数“返回”,然后在javascript中发生事件时,我可以调用虚拟函数但返回到正确的状态地点。但是,这将需要似乎不是实际STACKTOP
一部分或无法通过stackSave
和stackRestore
使用的调用堆栈。最值得注意的是,无论我做什么setThrew
似乎都没有用。即它会立即返回。
我也尝试使用一些启动/停止展开功能,但是由于从Javascript返回WASM时出现错误,因此无法在那里进行开发。
这里有什么想法吗?我真的无法像某些人希望的那样使main异步,并且期望它仍然可以正常工作。
编辑:好的,这是我正在测试的一些示例。
示例1:保存/恢复堆栈
在C中具有这两个功能:
void waittask()
{
volatile int sentinel = 85755555;
consolelog("waittask1");
waitout();
consolelog("waittask2");
}
void waitret()
{
volatile int sentinel = 12994423;
consolelog("waitret1");
stacksetup(sentinel);
stacksetup(sentinel);
consolelog("waitret2");
}
具有JavaScript函数:
stacksetup : function() {
console.log( "STACK SETUP" );
STACK_QUICKRET = g_wa.instance.exports.stackSave();
memory_words = new Int32Array(g_wa.instance.exports.memory.buffer);
console.log( "VALUE: " + memory_words[(STACK_QUICKRET>>2)] );
console.log( "VALUE: " + memory_words[(STACK_QUICKRET>>2)-1] );
console.log( "STACK:" + STACK_QUICKRET);
g_wa.instance.exports.setThrew(0,1);
},waitout : function()
{
console.log( "WAIT OUT" );
setTimeout( function() {
console.log( "Restoring" );
g_wa.instance.exports.stackRestore( STACK_DORET );
g_wa.instance.exports.waitret();
},1000 );
STACK_DORET = g_wa.instance.exports.stackSave();
g_wa.instance.exports.stackRestore( STACK_QUICKRET );
g_wa.instance.exports.setThrew(0,
首先调用“ waitret()”以提供快速退出的入口点,然后通过“ stacksetup”保存堆栈...然后使用waittask()在JavaScript中调用“ waitout()”将堆栈和快捷方式更改为waitret。
重要的是要注意,我已经两次调用了stacksetup,所以我可以检查堆栈以查看调用的时间是否有任何差异。由于它是从两个不同的位置调用的,因此人们希望堆栈是不同的,但事实并非如此,因为WASM似乎将调用堆栈与数据堆栈分开存储。
上面的代码将始终通过函数从其返回的位置返回,而不是回调到我希望其返回的位置。
其他尝试:
使用C函数
void mymain()
{
debug(0);
sprintf( ct,"PART 1 %f\n",OGGetAbsoluteTime() );
writestr( ct );
debug(1);
jssleep(1000,&DATA_ADDR,_STACKTOP,&DATA_ADDR);
debug(2);
}
void Wmain( int argc,char ** argv )
{
int ss;
stack_start = &ss;
mymain();
}
还有JavaScript函数
jssleep : function(ms,DATA_ADDR,stack_start,stack_end )
{
var sp = wasmExports.stackSave();
if (!sleeping) {
// We are called in order to start a sleep/unwind.
console.log('sleep... ('+ms+','+DATA_ADDR+',<' + sp + ',' + stacktop + '>' + stack_start + "," + stack_end + ')');
console.log( instance );
// Fill in the data structure. The first value has the stack location,// which for simplicity we can start right after the data structure itself.
memory_words[DATA_ADDR >> 2] = sp;
// The end of the stack will not be reached here anyhow.
memory_words[DATA_ADDR + 4 >> 2] = stacktop;
wasmExports.asyncify_start_unwind(DATA_ADDR);
sleeping = true;
// Resume after the proper delay.
setTimeout(function() {
console.log('timeout ended,starting to rewind the stack');
wasmExports.asyncify_start_rewind(DATA_ADDR);
console.log('calling back into main' );
// The code is now ready to rewind; to start the process,enter the
// first function that should be on the call stack.
wasmExports.stackRestore(sp);
// wasmExports.setThrew(0,1);
wasmExports.mymain(0);
console.log( "should not not get called\n" );
},ms);
} else {
// We are called as part of a resume/rewind. Stop sleeping.
console.log('...resume');
wasmExports.asyncify_stop_rewind();
sleeping = false;
}
},
我得到类似的行为-mymain()函数通过,然后在调用回调时出现此错误:
Uncaught RuntimeError: indirect call to null
jssleep file:///home/cnlohr/git/wasm_integrated/c-test.html:793
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。