fengari-lua / fengari

🌙 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser
MIT License
1.8k stars 64 forks source link

Timing out a script when someone uses while true do #159

Closed grilme99 closed 5 years ago

grilme99 commented 5 years ago

Hi!

I'm currently working on a website that will allow people to execute scripts. I have sandboxed everything, but I want to know the best way of preventing things like while true do with no wait crashing the thread? I'm fairly new to Fengari so I'd appreciate it if you broke some stuff down in your answer!

My friend used settimeout in JavaScript to make it so a script can only run for one second, but I don't want to do this because I want scripts to be able to run as long as needed. Is there a way to stop the script if it hangs for more than X time?

daurnimator commented 5 years ago

want to know the best way of preventing things like while true do with no wait crashing the thread?

You have all the same methods as normal Lua at your disposal. See e.g. https://stackoverflow.com/a/11425017/282536 That link does it from the lua side of things, but you can equally do it using the C/JS API.

My friend used settimeout in JavaScript to make it so a script can only run for one second

That wouldn't work if someone created a while (true) loop in javascript.

Your question might fit better on the Lua mailing list or e.g. reddit.