ECMAScript Realms
Proposal at Stage 2�
TC39 June, 2020� https://github.com/tc39/proposal-realms/
Changes
Changes
API Changes
A lot of simplifications, closing all remaining questions about the API:
Realm API
Few Clarifications
Important clarification about the terminology
In the Web Platform, both Realm and Global Object are usually associated to Window, Worker, and Worklets semantics. They are also associated to their detachable nature.
This proposal is limited to the semantics specified by ECMA-262 with no extra requirements from the web counterparts.
Important clarification about the global object
Important clarification about "detachable" realms
Window or�Worker
Realm A
Realm B
Realm C
Important clarification about evaluation
Important clarification about the module graph
const realm = new Realm();
// imports code that executes within its own environment.
const { doSomething } = await realm.import('./file.js');
doSomething();
Important clarification about compartments
const compartment = new Compartment(options);
const VirtualizedRealm = compartment.globalThis.Realm;
const realm = new VirtualizedRealm();
const { doSomething } = await realm.import('./file.js');
Use Cases
Third Party Scripts
Third Party Scripts: Plugins
import { api } from 'plugingFramework';
const realm = new Realm();
realm.globalThis.api = api;
await realm.import('./plugin1.js');
Code Testing
Code Testing: Running tests in a realm
import { test } from 'testFramework';
const realm = new Realm();
realm.globalThis.test = test;
await realm.import('./main-spec.js');
test.report();
Code Testing: Running test FW and tests in a realm
const realm = new Realm();
const [ framework, { tap } ] = await Promise.all([
realm.import('testFramework'),
realm.import('reporters')
]);
framework.use(tap);
await realm.import('./main-spec.js');
Codebase segmentation
Template Library
var compiled = _.template(
'<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>'
);��compiled({ users: ['user1', 'user2'] });
DOM Virtualization
DOM Virtualization: AMP WorkerDOM Challenge
google.com
AMP
Worker
(Virtual DOM)
???.cdn.ampproject.org
Runs AMP Framework
Runs NYT Scripts
async comm
Problem: Element.getBoundingClientRect() doesn't work over async comm channels (i.e. worker-dom)
Preloaded�
cross-domain iframe
DOM Virtualization
import virtualDocument from 'virtual-document';
const realm = new Realm();
realm.globalThis.document = virtualDocument;
await realm.import('./publisher-amin.js');
Where are we right now?
Open Question 1
Open Question 2
Questions?