wren icon indicating copy to clipboard operation
wren copied to clipboard

How can i stop wren script running?

Open luodaoyi opened this issue 1 year ago • 3 comments

Like lua hook:

...
lua_sethook(luaVm, Lua_YeildHook, LUA_MASKLINE, 0);
....

void Lua_YeildHook(lua_State* _L, lua_Debug* ar)
{
	if (bStop) {
		lua_yield(_L, 0);
	}
}

luodaoyi avatar Apr 12 '23 08:04 luodaoyi

I don't think there is currently a way to force a running wren script to terminate prematurely. However, in a fork I created, I hacked in a function to exit the VM here.

Essentially I added a new boolean value to the WrenVM struct called earlyExit which is checked each instruction cycle for the VM. If earlyExit was ever set to true while the VM is still executing code, it raises a runtime error. This way, in another thread, you can set this value (in my implementation using a function called wrenEarlyExit) to effectively stop the VM by the next instruction.

I don't know if this is actually the most optimal way to do this. You'll need another thread running to call the function to stop the VM. I haven't really tested to see whether this is thread safe to begin with, and I also don't know if stopping the VM prematurely might corrupt it or put it in an unusable state. From the few examples I've done with this, the VM seems to interpret and call functions afterwards without issue, though your mileage may vary.

CrazyInfin8 avatar Apr 12 '23 08:04 CrazyInfin8

I don't think there is currently a way to force a running wren script to terminate prematurely. However, in a fork I created, I hacked in a function to exit the VM here.

Essentially I added a new boolean value to the WrenVM struct called earlyExit which is checked each instruction cycle for the VM. If earlyExit was ever set to true while the VM is still executing code, it raises a runtime error. This way, in another thread, you can set this value (in my implementation using a function called wrenEarlyExit) to effectively stop the VM by the next instruction.

I don't know if this is actually the most optimal way to do this. You'll need another thread running to call the function to stop the VM. I haven't really tested to see whether this is thread safe to begin with, and I also don't know if stopping the VM prematurely might corrupt it or put it in an unusable state. From the few examples I've done with this, the VM seems to interpret and call functions afterwards without issue, though your mileage may vary.

This solution is very good! but have to edit wren code ..

luodaoyi avatar Apr 12 '23 08:04 luodaoyi

From a callback it is not possible. From wren you can yield or abort.

mhermier avatar Apr 12 '23 16:04 mhermier