简体   繁体   中英

Terminate a WebWorker (Javascript)

I am writing a language for educational purpose, which should run in web browsers. The language compiles to JavaScript.

So there is the problem of endless loops and recursion. Until that moment I thought the solution would be to have the runtime implemented as a WebWorker and a button on the ui, to terminate it if it runs to long.

I just found out, while testing my language, that the web worker isn't really terminated, by calling : runtime.terminate() ;

At least not until his last job has finished, i haven't tested what happens if it would be finished. Even in the documentation I found so far, its stated that it stops execution immediately.

So even if the user can continue working that way, the running worker in the background can use up resources and lead to at least a not responding browser.

So I need any way to terminate the execution in case of an endless loop or recursion, in response to a user event. As I din't find any way to put a thread to sleep in JavaScript, this doesn't work either, or is there something like sleep for web workers?

Have you got any suggestions what I could do?

I would do it like this:

1) Have your compiler generate code such that instead of executing the program from start to finish, it breaks it up into segments - for example, the body of a loop or the body of a function could be a segment.

2) Each segment should end with a timeoutId = setTimeout(nextSegment, 0) . This allows the web worker to receive and respond to asynchronous events in between executing segments.

3) When you want to terminate the web worker, use window.postMessage() to send it an asynchronous message.

4) Inside the web worker, have an event handler that, upon receiving that message, does a clearTimeout(timeoutId) .

Thanks for the response, I have thought about this way, so actually I wouldn't need the web worker for it. So the downside of this approach is:

1) That I would have to implement the loops as recursive function calls ...

And the bigger problem 2) The segment size has to be limited to one command, that this approach actually works, so I would have to wrap each command into a function that calls the next one, which would be a huge overhead...

Thats why I am looking for a way around, if there is any?

Yes I'm writing a compiler.

As working on this approach I must mention, it might be that the overhead for the user might not be much, but the impact on compilation is really huge... It's like building a VM on top of Javascript...

What I got to this point is that additional to the above mentioned points I need also to:

-need an complex indexing system for function naming -take care of variable allocation -have to solve expressions in series -...

That's why I am looking for a more elegant variant...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM