4

I'm in the process of creating a web application, written in Dart, that requires the creation of independent paths of execution, also written in Dart. (Were we not working in the browser, I would call them "threads.)

Normally, I would utilize Dart's (very robust) isolates to perform this task, but I am restricted in that I need to be able to arbitrarily terminate any of these "threads" at any time, without their cooperation.

If anyone is curious, this is because untrusted, 3rd party code must be run in these "threads," and I need to be able to terminate them if they are not responding, as well as for other reasons.

As I understand, this is impossible using dart:isolate. (Right?)

Anyway, I have had success compiling some Dart code to Javascript with dart2js and executing it in a Worker, but I don't know how I can properly invoke the postMessage function and respond to the onmessage event, like I would in JS. I messed around a little bit with the internals of dart2js and managed to tweak it (specifically the js_backend stuff) such that I am able to execute the postMessage function from my code, but responding to the onmessage event with a Dart handler is beyond my understanding of the workings of dart2js.

I looked into the js.dart library, but that works by creating script tags on the page, and that is obviously not possible in a worker.

I know this is a really weird use case, but does anyone have any idea how I would leverage the internals of dart2js to respond and reply to messages to the worker?

Zane Kaminski
  • 529
  • 4
  • 13
  • I may be wrong, but isn't that generally impossible using WebWorker? – MarioP Sep 20 '13 at 13:03
  • I also would like to be able to terminate Dart's isolates from the main "root" isolate. And/or be able to create Web Workers and kill them from the root isolate. Background: I'm writing a game and I'd like to be able to run user written artificial intelligence in separate isolates, and if the AI is buggy it should be killed, rather than the AI killing the browser. – KajMagnus Sep 20 '13 at 17:23
  • @MarioP the following docs gives me the impression that workers can indeed be killed: http://www.w3.org/TR/workers/#kill-a-worker *"User agents may invoke the "kill a worker" processing model on a worker at any time, e.g. in response to user requests"* – KajMagnus Sep 20 '13 at 17:28
  • I know that I can kill a worker. The real question is as to how I can call postMessage and respond to the onmessage event in Dart. – Zane Kaminski Sep 21 '13 at 00:41

1 Answers1

3

The Worker class seems to support what you want.

tomaszkubacki
  • 3,181
  • 3
  • 23
  • 36
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275