Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript ShadowRealm proposal integration #5339

Closed
wants to merge 2 commits into from

Conversation

littledan
Copy link
Contributor

@littledan littledan commented Mar 6, 2020

This patch gives the TC39 JavaScript ShadowRealm proposal semantics in HTML. ShadowRealms are given an environment settings object; in general, ShadowRealms inherit their settings from the outer environment.

When modules are used in ShadowRealms, module specifiers are interpreted with respect to the base URL of the surrounding environment, but when a module is imported within a ShadowRealm, it is a separate copy from what may be loaded in the surrounding environment or other ShadowRealms.

This patch implements the plan described earlier at tc39/proposal-shadowrealm#225 (comment).

The ShadowRealm proposal is currently at Stage 3.

  • At least two implementers are interested (and none opposed):
    • (none yet)
  • Tests are written and can be reviewed and commented upon at:
    • (none yet)
  • Implementation bugs are filed: (none yet)
    • Chrome: …
    • Firefox: …
    • Safari: …

(See WHATWG Working Mode: Changes for more details.)


/dom.html ( diff )
/embedded-content.html ( diff )
/form-elements.html ( diff )
/index.html ( diff )
/infrastructure.html ( diff )
/media.html ( diff )
/nav-history-apis.html ( diff )
/references.html ( diff )
/rendering.html ( diff )
/structured-data.html ( diff )
/webappapis.html ( diff )
/workers.html ( diff )

@littledan
Copy link
Contributor Author

I'm not sure if this may be missing something to give Realms sufficiently concrete behavior, so reviews will be appreciated if folks have time. In particular, in the February 2020 TC39 meeting, the Realm proposal champions announced that they would be seeking Stage 3 in the March 2020 meeting, and I think it'd be best if we had the HTML interactions basically worked out and agreed on before Stage 3. See also tc39/proposal-shadowrealm#225. (Note that the current rendered Realm spec text is a bit out of date; for now, you can cross-reference the source.)

@domenic
Copy link
Member

domenic commented Mar 6, 2020

/cc @bzbarsky. At a glance, the proposed semantics of inheriting from the "parent settings object" seem most likely to integrate well without major issues. (I'm glad these have a settings object at all; that wasn't clear from the proposal.)

Here is a mix of vague concerns and concrete questions that I've been thinking about in this space. I think this proposal gives reasonable answers to most of them, but perhaps not all (especially the vague concerns).

  • Right now every realm is a window or a worker. I think there are probably parts of the web and spec ecosystem that make this assumption, especially around the agent cluster business. Breaking that assumption (which could be stated something like, every realm either has a document or is only asynchronously available) could be tricky.

  • What happens if one of these realms is used for an entry realm/incumbent realm for a web platform API, like window.open() or worker.postMessage()? (I think they cannot end up as current or relevant realms since these realms do not have web platform APIs inside of them.)

  • How will these realms behave when faced with host-specific scenarios such as resolving modules, or reporting errors? E.g. what happens in terms of window.onerror reporting, and the reporting API, if you do jsRealmGlobal.eval("throw new Error('foo')")?

  • How will objects from these realms behave when faced with web platform features that depend on every object having a realm/global object pointer? (Those features might assume such a global is a window or a worker.) E.g., what happens when you take an object from one of these realms and feed it into call a user object's operation, such as when using addEventListener()?

  • How do these realms interact with "can run script" checks which currently depend on browsing context pointers and fully active status? Arguably these realms, since they are not attached to a fully active document, should never be able to run script.

  • How does this proposal mesh with the web platform's general move away from allowing synchronous cross-realm access (like iframe), in favor of asynchronous realm-creation APIs (like portals, workers/worklets, and iframe disallowdocumentaccess (Add disallowdocumentaccess iframe attribute to force a creation of a new agent cluster map #4606))? If we guard future powerful web platform features behind a "no sync cross-realm access allowed" opt-in, will we be able to prohibit the creation of these realms (e.g. from the host hook)? This also touches on the implementer interest question; it's not clear there's implementer support for adding a new way to synchronously create realms on the web.

@bzbarsky
Copy link
Contributor

bzbarsky commented Mar 6, 2020

@annevk @bholley

@bzbarsky
Copy link
Contributor

bzbarsky commented Mar 6, 2020

@EdgarChen

@littledan
Copy link
Contributor Author

Thanks for the prompt and detailed feedback. It looks like we have some more plumbing to do.

Overall, this leads to an editorial question for HTML experts: Would it be better to associate a "parent" Document/Worker/Worklet with each Realm (and reference it in all the appropriate places), or to deepen the use of environment settings objects, so that more logic indirects through their algorithms (thus avoiding assembling ever-larger switch statements for global type)? I'm fine with writing up either; the former sounds like less work/a smaller delta.

/cc @bzbarsky. At a glance, the proposed semantics of inheriting from the "parent settings object" seem most likely to integrate well without major issues. (I'm glad these have a settings object at all; that wasn't clear from the proposal.)

Note that the Realm champion group agreed on these semantics in tc39/proposal-shadowrealm#225. It wasn't clear to me either at first, so I asked.

Here is a mix of vague concerns and concrete questions that I've been thinking about in this space. I think this proposal gives reasonable answers to most of them, but perhaps not all (especially the vague concerns).

  • Right now every realm is a window or a worker. I think there are probably parts of the web and spec ecosystem that make this assumption, especially around the agent cluster business. Breaking that assumption (which could be stated something like, every realm either has a document or is only asynchronously available) could be tricky.

Thanks for pointing out a bunch of these cases below; I'll be happy to have help identifying more of them. Given successful past work on adding Worker and Worklet, I was hoping that we'll be able to figure everything out before too long. If we are left with errors or omissions (e.g., in unmaintained specs or earlier proposals), I'd want to make it clear to future spec readers and editors that the intention is to forward all behavior and state from the enclosing environment. Maybe we could write this in a note somewhere?

As far as agent clusters go, Realms created through the Realm API are in the same agent as their parent Realm. I'm not sure how/where to formalize/document this. Do you see any particular difficulties with integration with agent clusters?

  • What happens if one of these realms is used for an entry realm/incumbent realm for a web platform API, like window.open() or worker.postMessage()? (I think they cannot end up as current or relevant realms since these realms do not have web platform APIs inside of them.)

Yep, I see how this PR fails to handle this case. I think they should act like their parent Realm, which must (eventually, recursively) be something with already-defined semantics, e.g., a Window, Worker, or Worklet.

  • How will these realms behave when faced with host-specific scenarios such as resolving modules, or reporting errors? E.g. what happens in terms of window.onerror reporting, and the reporting API, if you do jsRealmGlobal.eval("throw new Error('foo')")?

Another good catch; again, it should forward to the error handler for the parent Realm (previous discussion).

  • How will objects from these realms behave when faced with web platform features that depend on every object having a realm/global object pointer? (Those features might assume such a global is a window or a worker.) E.g., what happens when you take an object from one of these realms and feed it into call a user object's operation, such as when using addEventListener()?

In general, my understanding is that we only end up using the "associated Realm" of functions and platform objects, so I'm failing to see where an addition mismatch would come from.

I'm having trouble finding where the "call a user object's operation" algorithm assumes that the global is a Window or Worker; I just see its use of the relevant exection context, which is defined in this PR. Maybe there's some other algorithm that runs into this issue?

  • How do these realms interact with "can run script" checks which currently depend on browsing context pointers and fully active status? Arguably these realms, since they are not attached to a fully active document, should never be able to run script.

Heh, I agree with that reading of this PR, which obviously isn't the intention. Again, this should forward up through the parent Realm.

  • How does this proposal mesh with the web platform's general move away from allowing synchronous cross-realm access (like iframe), in favor of asynchronous realm-creation APIs (like portals, workers/worklets, and iframe disallowdocumentaccess (Add disallowdocumentaccess iframe attribute to force a creation of a new agent cluster map #4606))? If we guard future powerful web platform features behind a "no sync cross-realm access allowed" opt-in, will we be able to prohibit the creation of these realms (e.g. from the host hook)? This also touches on the implementer interest question; it's not clear there's implementer support for adding a new way to synchronously create realms on the web.

I'm a big fan of new things in the web platform that allow splits into multiple processes/agent clusters--these mechanisms enable really strong isolation. I see the benefit of Realms to be complementary: they enable scenarios like emulating one environment on top of another, as well as creating sub-environments, e.g., for testing, as well as gradually migrating legacy code. So, I'm having trouble understanding what you're getting at with "general move"--these features seem complementary.

About implementer interest: maybe we could ask about this at the TC39 presentation where Realms is proposed for Stage 3? I wouldn't mind collecting positions and other kinds of feedback beforehand, but overall, I think the Realms proposal repository would be a better place to do so than this issue.

@domenic
Copy link
Member

domenic commented Mar 9, 2020

As far as agent clusters go, Realms created through the Realm API are in the same agent as their parent Realm. I'm not sure how/where to formalize/document this. Do you see any particular difficulties with integration with agent clusters?

Well, I think https://html.spec.whatwg.org/#integration-with-the-javascript-agent-formalism and https://html.spec.whatwg.org/#integration-with-the-javascript-agent-cluster-formalism make a lot of assumptions about everything being a window or worker (or worklet). Those would need extensive updates. And those sections have a bit of preexisting foundational shakiness which makes this hard:

In general, my understanding is that we only end up using the "associated Realm" of functions and platform objects, so I'm failing to see where an addition mismatch would come from.

Nope, we use it for user-provided objects, in https://heycam.github.io/webidl/#call-a-user-objects-operation step 4. This shows up in real-world code via something.addEventListener("event", { handleEvent() }) consulting the object's realm (not the handleEvent function's realm).

I'm having trouble finding where the "call a user object's operation" algorithm assumes that the global is a Window or Worker

I think you're right. I may have been thinking of the error reporting in https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke step 2.10.1, which builds on more shaky foundations (#958).

So, I'm having trouble understanding what you're getting at with "general move"--these features seem complementary.

The intention is to prevent creation of sync-accessible realms, so there's a conflict, not a complement.

About implementer interest: maybe we could ask about this at the TC39 presentation where Realms is proposed for Stage 3?

I think you'll separately need implementer interest for the web platform implications before we can consider this for HTML. Implementation in JavaScript engines is one thing, but exposure to the web is another, often involving a different constituency (depending on the browser engine).

@littledan
Copy link
Contributor Author

So, I'm having trouble understanding what you're getting at with "general move"--these features seem complementary.

The intention is to prevent creation of sync-accessible realms, so there's a conflict, not a complement.

Where can I find more documentation about this intention, and how it relates to the use cases I mentioned? It'd be good to forward this feedback to TC39!

@domenic
Copy link
Member

domenic commented Mar 9, 2020

I'm not sure it's documented centrally. It's more of a guiding principle in the development of new web APIs like workers, worklets, portals, etc. And disallowdocumentaccess allows us to "fix" the incorrect default that iframes have. Ideally we'll be able to move to a world where all realms are isolated from each other, with separate event loops. And there are very-early-stage experiments with rewarding this behavior (or punishing its opposite), e.g. never slow mode imagines a world where users have to give permission for a page to use slow APIs like sync realm creation, or you could use document policy to get that guarantee (and potentially get access to powerful features in exchange), etc.

You can also see it in the design of "meta-frameworks" like AMP which only allow executing JavaScript in a worker, and ensure all iframes are third-party so that they are isolated into separate event loops. These techniques are making their way into standards as well. (Although some of the AMP techniques are controversial, from what I can tell the ones about prohibiting performance worst-practices like document.write(), sync XHR, or sync realm creation are generally supported.)

@littledan
Copy link
Contributor Author

littledan commented Mar 9, 2020

Edit: Let's have the conversation about whether we should go ahead with Realms, or whether they contradict web platform goals, in tc39/proposal-shadowrealm#238. We can use this thread to work out the HTML integration on a technical level. I think the proposal repository is a better place to give this kind of high-level feedback because it is more likely to be visible to the people who are working on this proposal and the rest of TC39.

(Previous response collapsed in details)

Realms are definitely designed to have synchronous access to each other, sharing a common heap. Is this an "incorrect default"? Is the idea that Workers should supersede proposed Realm use cases?

I'd be interested to understand why sync realm creation is considered slow. (I'd imagine that the Realm API would be less overhead than iframe creation, but who knows.) It would indeed be unfortunate to introduce an API at the same time as introducing a mode which bans it.

@domenic
Copy link
Member

domenic commented Mar 9, 2020

I don't plan to engage much on the TC39 repository; I'll leave that to others.

Realms are definitely designed to have synchronous access to each other, sharing a common heap. Is this an "incorrect default"? Is the idea that Workers should supersede proposed Realm use cases?

Yes; I've been giving this feedback to the realms champions for years. (This is part of why I am not excited about engaging in TC39 on the subject.)

It would indeed be unfortunate to introduce an API at the same time as introducing a mode which bans it.

I agree.

@domenic domenic added the do not merge yet Pull request must not be merged per rationale in comment label Apr 6, 2020
@syg
Copy link
Contributor

syg commented Nov 6, 2020

I'm trying to wrap my head around the missing parts here if we keep with the forwarding settings object architecture. As I'm slowly doing this Domenic's complexity objections become more reasonable to me. Conceptually I still find the forwarding mechanic to be simple and generalizable, but a big effort seems to be needed to ensure that the resulting web specs remain actually coherent.

1. The invariant that all globals are windows/workers is broken.

An audit is needed here and in other web specs. This seems like the big outstanding TODO.

In messageEvent.source, for instance, the source attribute refers to "the Window object from which the message came", and doesn't refer to the global. With this PR that sentence becomes incoherent.

2. Implications of user Realms having their own module map

I'm not exactly clear what the implications are. Step 2 https://html.spec.whatwg.org/#fetch-a-single-module-script has an implicit assumption currently that each document (and thus event loop) has one fetch task per module map url key. This PR is morally equivalent in having the module map key be something like url+realm, is that right?

That might have implementation concerns, we should follow up with @yuki3. The user-realm module map entries are GCable, presumably, since user Realms have independent lifetimes, and the map would need to be additionally weakly keyed off the Realms, or something. Might be complicated.

3. The incumbent/entry settings object calculation

When determining the incumbent, the settings object is pulled off topmost execution context's Realm. In case the topmost execution context is a user Realm, it'll get that user Realm's forwarding settings object. That settings object behaves like the root settings object except in 3 cases AFAICT:

  1. Its Realm is the user Realm, not the root Realm
  2. It has its own execution context
  3. It has its own module map

I'm not seeing any obvious problems with this setup, e.g. the "prepare to run script" and "prepare to run callback" algorithms and their cleanup counterparts seem to be work as intended. Does this extend to all algorithms that use these concepts?

4. Error reporting

At first I thought userRealm.eval() would always be called by another user script, which ultimately must top out at a non-user realm. Uncaught exceptions from userRealm.eval propagate to the caller, which would eventually trigger the Runtime script error semantics on the root document.

But I guess that's wrong, as userRealm.eval could be posted to a task, and so error reporting needs to forward as well, which is not described in this PR.

5. Events and addEventListener() spec

This is beyond my expertise. Perusing https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener makes it seem like it might "just work" currently, since there's already a check if the callback Realm's global is a Window. An audit is also called for where places that have Realm and global-related logic should be explicitly reasoned through.

6. Realms cannot run any script since they do not have an active document.

My reading is that currently this PR is incorrect in the other way in that always allows a script to run in a user Realm, even if the root Document has disabled scripting.

Both https://html.spec.whatwg.org/#check-if-we-can-run-script and https://html.spec.whatwg.org/#concept-environment-script have checks for the global being a Window. In user realms' case, the global isn't a Window, so my reading is that step 1 of "check if we can run script" does not apply, step 2 does not apply since per the 3rd bullet point of "Scripting is enabled", scripting is enabled if the setting's global object is not a Window.

@bzbarsky
Copy link
Contributor

bzbarsky commented Nov 7, 2020

In messageEvent.source, for instance, the source attribute refers to "the Window object from which the message came"

Yeah, that should almost certainly refer to a WindowProxy. The actual Window might not even be in the same process in a modern browser.

But yes, the thing stored in source is not a global, and how to determine it in the user Realm case is not obvious.

@littledan
Copy link
Contributor Author

littledan commented Nov 7, 2020

These comments make me think that the environment settings object was the wrong layer to do this "forwarding" at, and instead, we should frame this integration as, "synthetic" Realms created with the Realm API refer to a "principal realm" which is used for all related web specs. This would avoid the need to do the large amount of refactoring that the approach in this PR implies. For example, for the source property, I was imagining that the source would be the outer Window or Worker where the Realm was created, for the current Realm which made the call to postMessage. I'm working on a new PR revision to frame Realm/HTML integration this way.

@leobalter
Copy link

Thanks! This helps me a lot putting some perspective on how I can work this out going through the details of this PR. I'll be in sync with @littledan to provide any support necessary.

@littledan
Copy link
Contributor Author

I've pushed a rebase and refactor of this PR, described in its commit message. This new version reflects the semantics that I was intending originally (that the Realms created with the Realm API never leak out into Web specs in a non-trivial way, that you're always deferring to their parent) but which I failed to express editorially, as has been pointed out above.

To answer the questions @syg listed above, in terms of this version:

1. The invariant that all globals are windows/workers is broken.

I guess this is already broken, since we added Worklets :) I fixed a little reference to that, as a drive-by. More types of global objects have been added in the past. I'm having trouble understanding what the invariant is here. This PR preserves the (modified) invariant that all principal global objects implement WindowProxy, WorkerGlobalScope or WorkletGlobalScope. However, it also exposes synthetic global objects which do not implement any WebIDL interface.

An audit is needed here and in other web specs. This seems like the big outstanding TODO.

This is true, but I think the scope is a bit reduced. We mostly have to worry about if other specs reach into JavaScript-internal data structures to get the [[Realm]] (fairly unlikely) or ask for the "current Realm" (more likely). In the latter case, this PR includes a note indicating that "current Realm" should be replaced by "current principal Realm" in other specs, generally. We'll have to do an audit of other specs for these two issues before landing.

In messageEvent.source, for instance, the source attribute refers to "the Window object from which the message came", and doesn't refer to the global. With this PR that sentence becomes incoherent.

The source comes from the incumbent settings object in https://html.spec.whatwg.org/#posting-messages:dom-messageevent-source . This version of the PR ensures that the incumbent Realm is always a principal Realm (so it is either a Window, Worker or Worklet).

2. Implications of user Realms having their own module map

I'm not exactly clear what the implications are. Step 2 https://html.spec.whatwg.org/#fetch-a-single-module-script has an implicit assumption currently that each document (and thus event loop) has one fetch task per module map url key. This PR is morally equivalent in having the module map key be something like url+realm, is that right?

Yes, the module map is used as a synchronization point, so either we need the realm as part of the module map key, or to give each realm its own separate module map. I think these are equivalent.

That might have implementation concerns, we should follow up with @yuki3. The user-realm module map entries are GCable, presumably, since user Realms have independent lifetimes, and the map would need to be additionally weakly keyed off the Realms, or something. Might be complicated.

The current PR hangs the module map off of the Realm, whether it's a principal or synthetic Realm, so no need for complicated GC semantics I think.

3. The incumbent/entry settings object calculation

When determining the incumbent, the settings object is pulled off topmost execution context's Realm. In case the topmost execution context is a user Realm, it'll get that user Realm's forwarding settings object. That settings object behaves like the root settings object except in 3 cases AFAICT:

1. Its Realm is the user Realm, not the root Realm

2. It has its own execution context

Note, the execution context ended up not being needed, so the synthetic realm settings object omits it.

3. It has its own module map

I'm not seeing any obvious problems with this setup, e.g. the "prepare to run script" and "prepare to run callback" algorithms and their cleanup counterparts seem to be work as intended. Does this extend to all algorithms that use these concepts?

Since no synthetic Realms don't implement any WebIDL interface and cannot be used in exposed, algorithms like "prepare to run script" and "prepare to run a callback" never need to run on it. And it's impossible to invoke these algorithms on synthetic realms in this PR version, by construction, as those algorithms expect an environment settings object, but synthetic realms do not have an environment settings object any more (instead, they have a synthetic realm settings object).

4. Error reporting

At first I thought userRealm.eval() would always be called by another user script, which ultimately must top out at a non-user realm. Uncaught exceptions from userRealm.eval propagate to the caller, which would eventually trigger the Runtime script error semantics on the root document.

But I guess that's wrong, as userRealm.eval could be posted to a task, and so error reporting needs to forward as well, which is not described in this PR.

The "target" for "report an error" is always a principal Realm, and "report an exception" will always find the corresponding principal, not synthetic, Realm, since it goes via the environment settings object. I'm not sure if this covers the path that you're thinking of; if it doesn't, the solution is probably through adding "the principal realm of" somewhere.

5. Events and addEventListener() spec

This is beyond my expertise. Perusing https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener makes it seem like it might "just work" currently, since there's already a check if the callback Realm's global is a Window. An audit is also called for where places that have Realm and global-related logic should be explicitly reasoned through.

This is beyond my expertise as well, but skimming the DOM spec, I think one thing that may need to be updated is WebIDL's associated Realm definition: the associated Realm of platform objects will always be a principal Realm, but for function objects, we'll need to reference function.[[Realm]]'s principal realm. This issue comes up in the inner invoke algorithm. I'm not really sure what the issue is that you're referring to with addEventListener--I think the Realms there are already all principal Realms by construction (since they have WebIDL interfaces implemented on them).

6. Realms cannot run any script since they do not have an active document.

My reading is that currently this PR is incorrect in the other way in that always allows a script to run in a user Realm, even if the root Document has disabled scripting.

Both https://html.spec.whatwg.org/#check-if-we-can-run-script and https://html.spec.whatwg.org/#concept-environment-script have checks for the global being a Window. In user realms' case, the global isn't a Window, so my reading is that step 1 of "check if we can run script" does not apply, step 2 does not apply since per the 3rd bullet point of "Scripting is enabled", scripting is enabled if the setting's global object is not a Window.

I'm having trouble understanding how this scenario would come up. It looks to me like the entrypoints to the "scripting is enabled"/"scripting is disabled" algorithms come from dealing with elements or documents, so they'll always pass in the environments settings object of a Window, Worker or Worklet. I think if we leave these algorithms as is, in terms of expecting an environment settings object as an argument, then this amounts to an implicit assertion that it's dealing with a principal Realm, as synthetic Realms no longer have an environment settings object.


One thing I could do is sprinkle the text with assertions that various realms being passed around are principal. I'm not sure whether or not this is necessary or helpful. Thanks for your reviews and patience as I figure out how to frame this PR.

@littledan
Copy link
Contributor Author

@domenic noted

However a major point of contention remains around the integration with the web platform's module system (tc39/proposal-shadowrealm#261). In current implementations the module map is tied to "real" realms, which come with associated security principals, fetch clients, HTTP cache partitions, etc. These "synthetic" realms want to create their own module maps, which will require significant rearchitecting, in both spec and implementation.

I'd like to understand what this means. This PR contains the spec update. Are there more spec updates expected? Could you elaborate on the implementation concerns? I'd like to understand what the problem is with forwarding to the relevant principal Realm, with the associated properties you mention.

@littledan
Copy link
Contributor Author

Discussion about exposing Web APIs from Realms' globals by default has been ongoing; see tc39/proposal-shadowrealm#284 (comment) . Tentatively, it seems reasonable to expose certain Web APIs (such as TextEncoder and atob) on Realms. This will take some more significant work on the HTML/DOM/WebIDL side, to determine exactly what to expose, and build the specification mechanics to do so; I'm not sure when we will get around to this.

@domenic
Copy link
Member

domenic commented Dec 10, 2020

To be clear, I'd expect that to be part of the work done before entering stage 3.

@leobalter
Copy link

To be clear, I'd expect that to be part of the work done before entering stage 3.

@domenic I agree at the point I want the thread for tc39/proposal-shadowrealm#284 (comment) to be resolved before Stage 3 to tell a clear direction for how this integration and implementation should go.

At the same time, I feel like TC39 should not exercise any decision making or governance over specific host APIs. The specifics of this work depends on the Web Platform side.

I believe we have the current options being at stake there:

  1. The proposed HostInitializeSyntheticRealm hook disallows adding properties ("_It is not expected that this hook would add properties to the Realm's global object.")
  2. The proposal allow properties initialization
    a. It might still disallow something specific like non-configurable properties
    b. It might just ignore item a

I'm in favor of 2, possibly 2.a. I recognize the item 1 is a no go for to make this integration possible and I believe it won't reflect other platforms as well such as Node, XS, etc.

The idea of not allowing any host properties seems, IMO, weakened by the fact we already have ECMAScript built-ins already. A synthetic realm is not super "clean" by nature.

Saying that, I hope we can find a good working direction for what should be added in this Web Platform integration, but I hope it doesn't become a blocker alone.

Base automatically changed from master to main January 15, 2021 07:57
@erights
Copy link

erights commented Jan 24, 2021

@littledan
Copy link
Contributor Author

My understanding of the feedback from Chrome, delivered by @syg in the May 2021 TC39 meeting, was that for Realms to be acceptable for Stage 3, the necessary change in this patch is that we move towards the model where there is one module map per document, rather than per Realm, and this module map is keyed by both the module specifier and Realm.

The observable impact of this change will be to guarantee that there be only one fetch per module specifier per document (without relying on unspecified network caching behavior which probably exists reliably cross-browser in practice). I don't think this change has any effect on whether a future "compartments" proposal can define a different module map within a document--it's just that we don't need such a mechanism for Realms.

My understanding of Chrome's position is that the WebIDL work to expose the right set of globals needs to be done before landing this PR and before TC39 Stage 4, but is not a TC39 Stage 3 prerequisite. @syg please correct me if I'm misunderstanding any of this.

I'm working on a rebase + change to keying, which should be ready some time this week; apologies for my delay here, but I think the agreed-on semantics are well-understood among those active in this conversation.

@Ms2ger
Copy link
Member

Ms2ger commented Nov 2, 2022

Huh, that's odd. db370e7...a40b80a seems to show a reasonable diff, at least.

@domenic
Copy link
Member

domenic commented Nov 2, 2022

Yeah, that does look more promising... unfortunately you can't commit on commit ranges like that, from what I can see :-/.

@Ms2ger Ms2ger force-pushed the realms branch 3 times, most recently from 587e049 to 53908c1 Compare November 7, 2022 12:32
This patch gives the TC39 JavaScript ShadowRealm proposal semantics in HTML.
ShadowRealms are given an environment settings object; in general,
ShadowRealms inherit their settings from the outer environment.

When modules are used in ShadowRealms, module specifiers are interpreted with
respect to the base URL of the surrounding environment, but when a module is
imported within a ShadowRealm, it is a separate copy from what may be loaded
in the surrounding environment or other ShadowRealms.

This patch implements the plan described earlier at
<tc39/proposal-shadowrealm#225 (comment)>.

The ShadowRealm proposal is currently at Stage 3.
@Ms2ger
Copy link
Member

Ms2ger commented Nov 7, 2022

This should be reviewable now. (Fwiw, some work seems to be ongoing at https://bugs.chromium.org/p/chromium/issues/detail?id=1281880.)

@caridy
Copy link

caridy commented Nov 7, 2022

@Ms2ger thanks for the help. @domenic let us know if anything.

@littledan
Copy link
Contributor Author

ShadowRealms will be under discussion at TC39 this week; it would be great to have input from HTML folks here.

@smaug----
Copy link

I could remind of some existing open issues, at least whatwg/webidl#1119 and #7591

@caridy
Copy link

caridy commented Jan 26, 2023

I could remind of some existing open issues, at least whatwg/webidl#1119 and #7591

@smaug---- @annevk we have provided answers to both questions:

@andreubotella
Copy link
Member

andreubotella commented Jan 31, 2023

Import maps were recently merged into the HTML spec, and there doesn't seem to have been any discussion about how they interact with shadow realm module maps. From what I can gather, "as written" (or rather, as it would be once the PR is updated to track the main branch), an import map only applies to principal realms, and imports in shadow realms will not prevent an import map from applying to the principal realm. Furthermore, the "caching" going on between the principal and shadow realms only applies to specifiers after the import map resolution. Am I understanding correctly?

cc @domenic

@caridy
Copy link

caridy commented Feb 2, 2023

Import maps were recently merged into the HTML spec, and there doesn't seem to have been any discussion about how they interact with shadow realm module maps. From what I can gather, "as written" (or rather, as it would be once the PR is updated to track the main branch), an import map only applies to principal realms, and imports in shadow realms will not prevent an import map from applying to the principal realm. Furthermore, the "caching" going on between the principal and shadow realms only applies to specifiers after the import map resolution. Am I understanding correctly?

cc @domenic

The forwarding mechanism from the SR to the principal realm for the resolution of the settings object give you sufficient information to resolve modules from within the ShadowRealm using the principal realm's map. Another good example is a ServiceWorker, which will be able to capture requests from a SR just fine. The virtualization of the module graph resolution is not a goal of the ShadowRealm proposal, there are other proposals under TC39 to attempt to solve that.

@annevk annevk added the agenda+ To be discussed at a triage meeting label Feb 7, 2023
@annevk
Copy link
Member

annevk commented Feb 7, 2023

Apologies for the delay here. Nominating this for the HTML triage call to see how to move this forward, though it'll have to wait until @domenic is back so probably Feb 23? (Which unfortunately will be too late for me.)

@littledan @Ms2ger IPR-wise I think all the work here can be attributed to Igalia. Unfortunately our bot is not that smart so @Ms2ger will have to create a fresh PR. We should still acknowledge @littledan by using a suitable Co-authored-by line in the final commit.

@Ms2ger
Copy link
Member

Ms2ger commented Feb 7, 2023

I can do that, but I'll wait until your meeting. If you'd be able to ping me on matrix, that'd be helpful.

@domenic
Copy link
Member

domenic commented Feb 23, 2023

Note that we had some discussion about moving this forward in #8873 (which was not the right place; sorry all). Quoting from myself there:

With regard to ShadowRealm: honestly, it is a very large PR and it is hard for me to prioritize, as I don't work on that area of the web platform currently. I can still do my best to review as part of my general HTML editor duties, but there is no need to block on me, and if another editor is more invested in getting that landed, they should feel free to take over.

It does seem that there have been recent decisions made which haven't made their way into the PR (and/or into Web IDL), namely the idea of adding a new ShadowRealmGlobalScope object and using that: #7591 (comment) . So maybe that's the next step?

@past past removed the agenda+ To be discussed at a triage meeting label Feb 23, 2023
@Ms2ger
Copy link
Member

Ms2ger commented Oct 30, 2023

Rebased and partially updated at #9893

@Ms2ger Ms2ger closed this Oct 30, 2023
@Jamthea

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge yet Pull request must not be merged per rationale in comment
Development

Successfully merging this pull request may close these issues.