Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i stop wren script running? #1165

Open
luodaoyi opened this issue Apr 12, 2023 · 3 comments
Open

How can i stop wren script running? #1165

luodaoyi opened this issue Apr 12, 2023 · 3 comments

Comments

@luodaoyi
Copy link

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);
	}
}
@CrazyInfin8
Copy link
Contributor

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.

@luodaoyi
Copy link
Author

luodaoyi commented Apr 12, 2023

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

@mhermier
Copy link
Contributor

mhermier commented Apr 12, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants