wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.87k stars 551 forks source link

stopping VM instances from the host side #478

Closed ghost closed 4 years ago

ghost commented 6 years ago

Consider an infinite loop.

Maybe it would stem from a malicious "joke" mod the user downloaded, maybe from the user himself when trying to create some macro script or whatever.

In that case you'd probably be able to shut down your application/game gracefully, which by extension would mean stopping the blocking VM instance. By gracefully, I mean really anything from not blocking execution at all to stopping a level, showing an error message and going back to the main menu to closing the application without crashing the window manager in the process.

Apparently, some ways to implement this (but probably not limited to) are:

Some of those possibilities have probably drawbacks like reduced instruction throughput and some probably can't be realized from the main thread, which probably rules out stopping the VM manually by GUI.

That being said, I didn't find anything like it in the documentation. What is Wrens take on this?

mhermier commented 6 years ago

AFAIK, wren VM is single threaded and don't support instrumentation from an external thread. And there is no simple solution to this halting/suspend problem, since most of the solutions can lead to memory leaks. Maybe the more interesting solution is a stop flag/count. That way, we should be able to perform debugging and stepping, that are also crucial for creating debugging tools.

ruby0x1 commented 6 years ago

Keep in mind that we've already implemented debugging with stepping and will be upstreaming it soon. You wouldn't typically want debugging support enabled for something like user facing mods though because it will cost performance, but also because it will also be able to introspect state, view module sources (if string based) and such.

You could probably put an external mutex around vm->fiber->error and set it that way which will stop the vm loop from what I see.

mhermier commented 6 years ago

Nice I wait to see stepping in action, and try to do a debugger yummy.

It is not really possible to do so, because if the user decide it is ok to infinite loop and we might need to be able to resume. So I really like to see how is your implementation of stepping, and its impact on speed.

ruby0x1 commented 6 years ago

I put a link to the video in the above comment but since thread is a different topic best to discuss the debugger in it's own thread when we post soon.

luodaoyi commented 1 year ago

Hi, I want a Hook interface like lua script "lua_sethook"


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

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