Skip to content

Commit 2722be8

Browse files
committed
Reorganizing content
1 parent e731c39 commit 2722be8

File tree

3 files changed

+238
-245
lines changed

3 files changed

+238
-245
lines changed

EXAMPLES.md

+1-172
Original file line numberDiff line numberDiff line change
@@ -1,172 +1 @@
1-
# Realm API usage
2-
3-
### Example: simple realm
4-
5-
```js
6-
let g = globalThis; // outer global
7-
let r = new Realm(); // root realm
8-
9-
let f = r.globalThis.Function("return 17");
10-
11-
f() === 17 // true
12-
13-
Reflect.getPrototypeOf(f) === g.Function.prototype // false
14-
Reflect.getPrototypeOf(f) === r.globalThis.Function.prototype // true
15-
```
16-
17-
### Example: Importing Module
18-
19-
```js
20-
let r = new Realm();
21-
const { x } = await r.import('/path/to/foo.js');
22-
```
23-
24-
In this example, the new realm will fetch, and evaluate the module, and extract the `x` named export from that module namespace. `Realm.prototype.import` is equivalent to the dynamic import syntax (e.g.: `const { x } = await import('/path/to/foo.js');` from within the realm. In some cases, evaluation will not be available (e.g.: in browsers, CSP might block unsafe-eval), while importing from module is still possible.
25-
26-
### Example: Virtualized contexts
27-
28-
Importing modules allow us to run asynchronous executions with set boundaries for access to global environment contexts.
29-
30-
#### main js file:
31-
32-
```js
33-
globalThis.DATA = "a global value";
34-
35-
let r = new Realm();
36-
37-
// r.import is equivalent to the dynamic import expression
38-
// It provides asynchronous execution, without creating or relying in a
39-
// different thread or process.
40-
r.import("./sandbox.js").then(({test}) => {
41-
42-
// globals in this root realm are not leaked
43-
test("DATA"); // undefined
44-
45-
let desc = test("Array"); // {writable: true, enumerable: false, configurable: true, value: ƒ}
46-
let Arr = desc.value;
47-
48-
Arr === r.globalThis.Array; // true
49-
Arr === Array; // false
50-
51-
// foo and bar are immediately visible as globals here.
52-
});
53-
```
54-
55-
#### sandbox.js file
56-
57-
```js
58-
// DATA is not available as a global name here
59-
60-
// Names here are not leaked to the root realm
61-
var foo = 42;
62-
globalThis.bar = 39;
63-
64-
export function test(property) {
65-
66-
// Built-ins like `Object` are included.
67-
return Object.getPropertyDescriptor(globalThis, property);
68-
}
69-
```
70-
71-
### Example: simple subclass
72-
73-
```js
74-
class EmptyRealm extends Realm {
75-
constructor(...args) {
76-
super(...args);
77-
let globalThis = this.globalThis;
78-
79-
// delete global descriptors:
80-
delete globalThis.Math;
81-
...
82-
}
83-
}
84-
```
85-
86-
### Example: DOM mocking
87-
88-
```js
89-
class FakeWindow extends Realm {
90-
constructor(...args) {
91-
super(...args);
92-
let globalThis = this.globalThis;
93-
94-
globalThis.document = new FakeDocument(...);
95-
globalThis.alert = new Proxy(fakeAlert, { ... });
96-
...
97-
}
98-
}
99-
```
100-
101-
102-
103-
## iframes vs realms
104-
105-
If you're using anonymous iframes today to "evaluate" javascript code in a different realm, you can replace it with a new Realm, as a more performant option, e.g.:
106-
107-
```js
108-
const globalOne = window;
109-
let iframe = document.createElement('iframe');
110-
document.body.appendChild(iframe);
111-
const globalTwo = iframe.contentWindow;
112-
```
113-
114-
will become:
115-
116-
```js
117-
const globalOne = window;
118-
const globalTwo = new Realm().globalThis;
119-
```
120-
121-
### Indirect evaluation
122-
123-
This operation should be equivalent, in both scenarios:
124-
125-
```js
126-
globalOne.eval('1 + 2'); // yield 3
127-
globalTwo.eval('1 + 2'); // yield 3
128-
```
129-
130-
### Direct evaluation
131-
132-
This operation should be equivalent, in both scenarios:
133-
134-
```js
135-
globalOne.eval('eval("1 + 2")'); // yield 3
136-
globalTwo.eval('eval("1 + 2")'); // yield 3
137-
```
138-
139-
### Identity Discontinuity
140-
141-
Considering that you're creating a brand new realm, with its brand new global variable,
142-
the identity discontinuity is still present, just like in the iframe example:
143-
144-
```js
145-
let a1 = globalOne.eval('[1,2,3]');
146-
let a2 = globalTwo.eval('[1,2,3]');
147-
a1.prototype === a2.prototype; // yield false
148-
a1 instanceof globalTwo.Array; // yield false
149-
a2 instanceof globalOne.Array; // yield false
150-
```
151-
152-
_Note: There are other solutions to this problem by using proxies and membranes, which has some performance implications. It is not a goal of this proposal to solve this._
153-
154-
## node's vm objects vs realms
155-
156-
If you're using node's `vm` module today to "evaluate" javascript code in a different realm, you can replace it with a new Realm, e.g.:
157-
158-
```js
159-
const vm = require('vm');
160-
const script = new vm.Script('this');
161-
const globalOne = globalThis;
162-
const globalTwo = script.runInContext(new vm.createContext());
163-
```
164-
165-
will become:
166-
167-
```js
168-
const globalOne = globalThis;
169-
const globalTwo = new Realm().globalThis;
170-
```
171-
172-
_Note: these two are equivalent in functionality._
1+
_all the examples moved to the [Explainer](explainer.md) file_

README.md

+25-63
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,35 @@
11
# ECMAScript spec proposal for Realms API
22

3-
## Index
4-
5-
- [Status](#status)
6-
- [Spec Text](#spec-text)
7-
- [History](#history)
8-
- [What Are Realms?](#what-are-realms)
9-
- [API](#api-typescript-format)
10-
- [Examples](#examples)
11-
- [Presentations](#presentations)
12-
- [Contributing](#contributing)
13-
14-
### Other documents:
3+
## <a name='Status'></a>Status
154

5+
- [Explainer](explainer.md).
6+
- [HTML Rendered Spec](https://tc39.es/proposal-realms/).
7+
- Currently at [Stage 2](https://tc39.es/process-document/).
168
- [Code of Conduct](https://tc39.es/code-of-conduct/)
17-
- [Explainer](explainer.md)
18-
- [Examples](EXAMPLES.md)
19-
20-
## Status
219

22-
### Current Stage
23-
24-
This proposal is at stage 2 of [the TC39 Process](https://tc39.es/process-document/).
25-
26-
### Champions
10+
### <a name='Champions'></a>Champions
2711

2812
* @dherman
2913
* @caridy
3014
* @erights
3115
* @leobalter
3216

33-
### Spec Text
34-
35-
You can view the spec rendered as [HTML](https://tc39.es/proposal-realms/).
17+
## Index
3618

37-
## History
19+
* [What are Realms?](#WhatareRealms)
20+
* [API (TypeScript Format)](#APITypeScriptFormat)
21+
* [Presentations](#Presentations)
22+
* [History](#History)
23+
* [Contributing](#Contributing)
24+
* [Updating the spec text for this proposal](#Updatingthespectextforthisproposal)
3825

39-
* we worked on this during ES2015 time frame, so never went through stages process ([ES6 Realm Objects proto-spec.pdf](https://github.com/tc39/proposal-realms/files/717415/ES6.Realm.Objects.proto-spec.pdf))
40-
* got punted to later (rightly so!)
41-
* goal of this proposal: resume work on this, reassert committee interest via advancing to stage 2
42-
* original idea from @dherman: [What are Realms?](https://gist.github.com/dherman/7568885)
43-
44-
## What are realms?
26+
## <a name='WhatareRealms'></a>What are Realms?
4527

4628
Realms are a distinct global environment, with its own global object containing its own intrinsics and built-ins (standard objects that are not bound to global variables, like the initial value of Object.prototype).
4729

4830
See more at the [explainer](explainer.md) document.
4931

50-
## API (TypeScript Format)
32+
## <a name='APITypeScriptFormat'></a>API (TypeScript Format)
5133

5234
```ts
5335
declare class Realm {
@@ -57,45 +39,25 @@ declare class Realm {
5739
}
5840
```
5941

60-
## Examples
61-
62-
```js
63-
// this is the root realm
64-
var x = 39;
65-
const realm = new Realm();
66-
67-
// globals from the root/parent realm are not leaked to the nested realms
68-
realm.globalThis.x; // undefined
69-
70-
// evaluation occurs within the nested realm
71-
realm.globalThis.eval("var x = 42");
72-
73-
// global names can be regularly observed in the realm's globalThis
74-
realm.globalThis.x; // 42
75-
76-
// global values are not leaked to the parent realms
77-
x; // 39
78-
79-
// built-ins configurability are not different
80-
delete realm.globalThis.Math;
81-
82-
// realms can dynamic import module that will execute within it's own
83-
// environment. Imports can be observed from the parent realm.
84-
realm.import('./file.js').then(ns => ns.execCustomCode());
85-
```
86-
87-
See some other examples [here](EXAMPLES.md).
42+
See some examples [in the Explainer file](explainer.md).
8843

89-
## Presentations
44+
## <a name='Presentations'></a>Presentations
9045

9146
* [TC39 Meeting, June 4th 2020 - Stage 2 Update](https://docs.google.com/presentation/d/1TfVtfolisUrxAPflzm8wIhBBv_7ij3KLeqkfpdvpFiQ/edit?ts=5ed5d3e7)
9247
* [TC39 Incubator Call May 26th 2020](https://docs.google.com/presentation/d/1FMQB8fu059zSJOtC3uOCbBCYiXAcvHojxzcDjoVQYAo/edit)
9348
* [TC39 Feb 2020 - Stage 2 Update](https://docs.google.com/presentation/d/1umg2Kw18IlQyzrWwaQCAkeZ6xLTGZPPB6MtnI2LFzWE/edit)
9449
* [TC39 May 2018 - Stage 2 Request](https://docs.google.com/presentation/d/1blHLQuB3B2eBpt_FbtLgqhT6Zdwi8YAv6xhxPNA_j0A/edit) (archived)
9550

96-
## Contributing
51+
## <a name='History'></a>History
52+
53+
* we worked on this during ES2015 time frame, so never went through stages process ([ES6 Realm Objects proto-spec.pdf](https://github.com/tc39/proposal-realms/files/717415/ES6.Realm.Objects.proto-spec.pdf))
54+
* got punted to later (rightly so!)
55+
* goal of this proposal: resume work on this, reassert committee interest via advancing to stage 2
56+
* original idea from @dherman: [What are Realms?](https://gist.github.com/dherman/7568885)
57+
58+
## <a name='Contributing'></a>Contributing
9759

98-
### Updating the spec text for this proposal
60+
### <a name='Updatingthespectextforthisproposal'></a>Updating the spec text for this proposal
9961

10062
The source for the spec text is located in [spec.html](spec.html) and it is written in
10163
[ecmarkup](https://github.com/bterlson/ecmarkup) language.

0 commit comments

Comments
 (0)