Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Determine how to wire up isolate state #164

Closed
vsmenon opened this issue May 4, 2015 · 16 comments
Closed

Determine how to wire up isolate state #164

vsmenon opened this issue May 4, 2015 · 16 comments

Comments

@vsmenon
Copy link
Contributor

vsmenon commented May 4, 2015

The dart2js isolate library code relies on some trampoline code at startup to set isolate globalState.

I'm doing the following right now, but it'd be nice not to need this.

--- a/lib/src/codegen/html_codegen.dart
+++ b/lib/src/codegen/html_codegen.dart
@@ -90,7 +90,8 @@ Node _cssInclude(String cssUrl) =>
 Node _invokeMain(String mainLibraryName) {
   var code = mainLibraryName == null
       ? 'console.error("dev_compiler error: main was not generated");'
-      : '$mainLibraryName.main();';
+      : 'library__isolate_helper.startRootIsolate($mainLibraryName.main, []);';
   return parseFragment('<script>$code</script>\n');
 }
@vsmenon
Copy link
Contributor Author

vsmenon commented May 4, 2015

Also needed this:

--- a/tool/input_sdk/private/isolate_helper.dart
+++ b/tool/input_sdk/private/isolate_helper.dart
@@ -157,10 +157,10 @@ void startRootIsolate(entry, args) {
 // TODO(eub, sigmund): move the "manager" to be entirely in JS.
 // Running any Dart code outside the context of an isolate gives it
 // the chance to break the isolate abstraction.
-_Manager get _globalState => JS("_Manager", "init.globalState");
+_Manager get _globalState => JS("_Manager", "dart.globalState");

 set _globalState(_Manager val) {
-  JS("void", "init.globalState = #", val);
+  JS("void", "dart.globalState = #", val);
 }

@sigmundch per the TODO. :-)

@jmesserly
Copy link
Contributor

is angular using isolates? Or it gets pulled in as part of something else (async?)

@sigmundch
Copy link
Contributor

dart2js uses the notion of an isolate always, similar to the vm: all code starts in a root isolate. So even if it's not used explicitly, there is some bootstraping code that will set up the initial isolate before running main.

@jmesserly
Copy link
Contributor

yeah, that makes sense. I wonder if we can make it lazy in that case?

@sigmundch
Copy link
Contributor

Possibly. Depending how we define what's the lazy trigger. Would that be when you spawn a second isolate?

One use-case I'm thinking about is when we have 2 separately compiled apps in the same page. I have to look deeper into it, but it's possible that we use isolates in dart2js as the mechanism to support that.

@jmesserly
Copy link
Contributor

One use-case I'm thinking about is when we have 2 separately compiled apps in the same page.

Unless such a use case makes sense for JavaScript code too, it's something we should not try to do. That way lies interop madness :)

If it is something that makes sense for JS, it would probably be best to express it as a JS->JS transpiler phase. (Which we could totally integrate into our tools. No issues there.)

@jmesserly
Copy link
Contributor

Would that be when you spawn a second isolate?

Maybe. Although to be honest, I'm not entirely clear on what we mean by isolate.

I definitely don't want to create abstracts that don't make sense in JS (our old DOM isolates come to mind, plus the general way Dart code was isolated from the JS world). Also don't want users to pay for abstractions they aren't using. On the other hand, we definitely want to expose Web Workers and IFrames. (though we probably get them mostly for free)

@vsmenon
Copy link
Contributor Author

vsmenon commented May 5, 2015

@jmesserly We appear to hit this in the context of async, zones, and stack traces.

More generally, we may want to rethink the model of multiple isolates and the DOM. Previously, we kind of took the Dartium notion that we already had separate JS and Dart isolates and ran with it.

@jmesserly
Copy link
Contributor

makes sense. yeah, Zones are super awesome. I wonder if we can reuse https://github.com/angular/zone.js ... the nice thing is it's opt in (e.g. add it to the page if zones are needed), so you don't pay for it if you don't use it

@vsmenon
Copy link
Contributor Author

vsmenon commented May 8, 2015

A related issue which I'll just include here: spawnFunction effectively assumes all code is delivered in a single script - see isolate_helper.dart, computeThisScript. This won't work as is in DDC.

@vsmenon
Copy link
Contributor Author

vsmenon commented May 8, 2015

Another related issue: js_helper.dart, convertDartClosureToJS wraps a Dart closure to record the source isolate. Not sure we need that either.

@jmesserly
Copy link
Contributor

removing angular label as (I think) a decent workaround is landed.

@jmesserly
Copy link
Contributor

What are the next steps here? Should subject should be something like "refactor private/isolate_helper.dart to only support worker isolates, not DOM isolates" ... ?

@vsmenon
Copy link
Contributor Author

vsmenon commented Jun 22, 2015

I think there a few issues here to sort out:

(1) What do we need to support the Dart event loop on a DOM thread? I believe we currently rely on this code to ensure that certain tasks run before we yield back to the browser's own event loop.

(2) Do we really need to support multiple DOM isolates in the same frame? In general, I think this is a bad idea. This is only partially supported in Dartium.

(3) Does spawnFunction make sense in the browser? If so, how do we support it? This is at least somewhat related to the module question - how do we tell the worker what code to load? dart2js assumes that the current script tag's src attribute contains all code.

@jmesserly
Copy link
Contributor

Ah nice!

So for (1), I filed #233 to track
for (3), filed #234
Also I agree on (2). Filed #235 for cleanup

If that's good, perhaps we can close this one, since IIRC you fixed the original issue (need to have dart2js isolate globalState set or things go haywire in SDK).

@vsmenon
Copy link
Contributor Author

vsmenon commented Jun 22, 2015

Awesome - thanks! Closing this one.

@vsmenon vsmenon closed this as completed Jun 22, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants