Cha*_*ohr 7 javascript c webassembly
我试图允许用户在 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");
}
Run Code Online (Sandbox Code Playgroud)
使用 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,1);
},
Run Code Online (Sandbox Code Playgroud)
其中“waitret()”首先被调用以提供快速退出的入口点,其中堆栈由“stacksetup”保存……而waittask()在JavaScript中调用“waitout()”,以努力改变堆栈和快捷方式出服务员。
需要注意的是,我已经调用了 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();
}
Run Code Online (Sandbox Code Playgroud)
和 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;
}
},
Run Code Online (Sandbox Code Playgroud)
我得到了类似的行为 - mymain() 函数直接通过,然后当回调被调用时,我得到这个错误:
Uncaught RuntimeError: indirect call to null
jssleep file:///home/cnlohr/git/wasm_integrated/c-test.html:793
Run Code Online (Sandbox Code Playgroud)