forked from tc39/proposal-shadowrealm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.emu
390 lines (333 loc) · 19.2 KB
/
index.emu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="https://bterlson.github.io/ecmarkup/ecmarkup.js"></script>
<link rel="stylesheet" href="https://bterlson.github.io/ecmarkup/elements.css">
<pre class=metadata>
title: ECMAScript Realms Spec Proposal
stage: 0
contributors: Dave Herman, Caridy Patiño
status: draft
copyright: false
location: https://rawgit.com/caridy/proposal-realms/master/index.html
</pre>
<emu-table id="table-1" caption="Well-known Intrinsic Objects">
<table>
<tbody>
<tr>
<th>Intrinsic Name</th>
<th>Global Name</th>
<th>ECMAScript Language Association</th>
</tr>
<tr>
<td>%Realm%</td>
<td><code>Realm</code></td>
<td>…</td>
</tr>
<tr>
<td>%RealmPrototype%</td>
<td><code>Realm.prototype</code></td>
<td>…</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-table id="table-2" caption="Well-known Symbols">
<table>
<tbody>
<tr>
<th>Specification Name</th>
<th>[[Description]]</th>
<th>Value and Purpose</th>
</tr>
<tr>
<td>@@indirectEval</td>
<td><code>Realm.indirectEval</code></td>
<td>A function valued property that is the indirectEval hook function of Realm’s instances.</td>
</tr>
<tr>
<td>@@directEval</td>
<td><code>Realm.directEval</code></td>
<td>A function valued property that is the directEval hook function of Realm’s instances.</td>
</tr>
</tbody>
</table>
</emu-table>
<emu-table id="table-2" caption="Realm Record Fields">
<table>
<tbody>
<tr>
<th>Field Name</th>
<th>Value</th>
<th>Meaning</th>
</tr>
<tr>
<td>[[HostDefined]]</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>[[IndirectEval]]</td>
<td>Object | Undefined</td>
<td>If the value is an Object it must be a function object. The function is call prior to perform any indirect evaluation in the realm.</td>
</tr>
<tr>
<td>[[DirectEval]]</td>
<td>Object | Undefined</td>
<td>If the value is an Object it must be a function object. The function is call prior to perform any direct evaluation in the realm.</td>
</tr>
</tbody>
</table>
</emu-table>
<!-- es6num="18.2.1.1" -->
<emu-clause id="sec-performeval" aoid="PerformEval">
<h1>Runtime Semantics: PerformEval( _x_, _evalRealm_, _strictCaller_, _direct_ )</h1>
<p>The abstract operation PerformEval with arguments _x_, _evalRealm_, _strictCaller_, and _direct_ performs the following steps:</p>
<emu-alg>
1. Assert: If _direct_ is *false*, then _strictCaller_ is also *false*.
1. <ins>If _direct_ is *true*, then</ins>
1. <ins>Let _xValue_ be ? PrepareForDirectEval(_x_, _evalRealm_).</ins>
1. <ins>Else,</ins>
1. <ins>Let _xValue_ be ? PrepareForIndirectEval(_x_, _evalRealm_).</ins>
1. If Type(_xValue_) is not String, return _xValue_.
1. Let _script_ be the ECMAScript code that is the result of parsing _xValue_, interpreted as UTF-16 encoded Unicode text as described in <emu-xref href="#sec-ecmascript-language-types-string-type"></emu-xref>, for the goal symbol |Script|. If the parse fails, throw a *SyntaxError* exception. If any early errors are detected, throw a *SyntaxError* or a *ReferenceError* exception, depending on the type of the error (but see also clause <emu-xref href="#sec-error-handling-and-language-extensions"></emu-xref>). Parsing and early error detection may be interweaved in an implementation dependent manner.
1. If _script_ Contains |ScriptBody| is *false*, return *undefined*.
1. Let _body_ be the |ScriptBody| of _script_.
1. If _strictCaller_ is *true*, let _strictEval_ be *true*.
1. Else, let _strictEval_ be IsStrict of _script_.
1. Let _ctx_ be the running execution context. If _direct_ is *true*, _ctx_ will be the execution context that performed the direct eval. If _direct_ is *false*, _ctx_ will be the execution context for the invocation of the `eval` function.
1. If _direct_ is *true*, then
1. Let _lexEnv_ be NewDeclarativeEnvironment(_ctx_'s LexicalEnvironment).
1. Let _varEnv_ be _ctx_'s VariableEnvironment.
1. Else,
1. Let _lexEnv_ be NewDeclarativeEnvironment(_evalRealm_.[[GlobalEnv]]).
1. Let _varEnv_ be _evalRealm_.[[GlobalEnv]].
1. If _strictEval_ is *true*, let _varEnv_ be _lexEnv_.
1. If _ctx_ is not already suspended, suspend _ctx_.
1. Let _evalCxt_ be a new ECMAScript code execution context.
1. Set the _evalCxt_'s Function to *null*.
1. Set the _evalCxt_'s Realm to _evalRealm_.
1. Set the _evalCxt_'s ScriptOrModule to _ctx_'s ScriptOrModule.
1. Set the _evalCxt_'s VariableEnvironment to _varEnv_.
1. Set the _evalCxt_'s LexicalEnvironment to _lexEnv_.
1. Push _evalCxt_ on to the execution context stack; _evalCxt_ is now the running execution context.
1. Let _result_ be EvalDeclarationInstantiation(_body_, _varEnv_, _lexEnv_, _strictEval_).
1. If _result_.[[Type]] is ~normal~, then
1. Let _result_ be the result of evaluating _body_.
1. If _result_.[[Type]] is ~normal~ and _result_.[[Value]] is ~empty~, then
1. Let _result_ be NormalCompletion(*undefined*).
1. Suspend _evalCxt_ and remove it from the execution context stack.
1. Resume the context that is now on the top of the execution context stack as the running execution context.
1. Return Completion(_result_).
</emu-alg>
<emu-note>
<p>The eval code cannot instantiate variable or function bindings in the variable environment of the calling context that invoked the eval if the calling context is evaluating formal parameter initializers or if either the code of the calling context or the eval code is strict mode code. Instead such bindings are instantiated in a new VariableEnvironment that is only accessible to the eval code. Bindings introduced by `let`, `const`, or `class` declarations are always instantiated in a new LexicalEnvironment.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-realm-objects">
<h1>Realm Objects</h1>
<emu-clause id="sec-realm-abstract-operations">
<h1>Realm Abstract Operations</h1>
<emu-clause id="sec-preparefordirecteval" aoid="PrepareForDirectEval">
<h1>PrepareForDirectEval ( _x_, _realmRec_ )</h1>
<p>The abstract operation PrepareForDirectEval with arguments _x_ and _realmRec_ performs the following steps:</p>
<emu-alg>
1. Assert: realm is a Realm Record.
1. Let _fn_ be _realmRec_.[[DirectEval]].
1. If _fn_ is *undefined*, return _x_.
1. Assert: IsCallable(_fn_) is *true*.
1. Return ? Call(_fn_, *undefined*, « _x_ »).
</emu-alg>
</emu-clause>
<emu-clause id="sec-prepareforindirecteval" aoid="PrepareForIndirectEval">
<h1>PrepareForIndirectEval ( _x_, _realmRec_ )</h1>
<p>The abstract operation PrepareForIndirectEval with arguments _x_ and _realmRec_ performs the following steps:</p>
<emu-alg>
1. Assert: realm is a Realm Record.
1. Let _fn_ be _realmRec_.[[IndirectEval]].
1. If _fn_ is *undefined*, return _x_.
1. Assert: IsCallable(_fn_) is *true*.
1. Return ? Call(_fn_, *undefined*, « _x_ »).
</emu-alg>
</emu-clause>
<emu-clause id="sec-extract-realm-methods" aoid="ExtractRealmMethods">
<h1>ExtractRealmMethods ( _realmObj_ )</h1>
When ExtractRealmMethods is called with argument _realmObj_ performs the following steps:
<emu-alg>
1. Assert: Type(_realmObj_) is not Object, throw a *TypeError* exception.
1. Assert: _realmObj_ does have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Let _realmRec_ be _realmObj_.[[Realm]].
1. Let _directEvalFn_ be ? GetMethod(_realmObj_, @@directEval).
1. If IsCallable(_directEvalFn_) is not *true*, throw a *TypeError* exception.
1. Let _boundDirectEvalFn_ be BoundFunctionCreate(_directEvalFn_, _realmObj_, « »).
1. Perform ! DefinePropertyOrThrow(_boundDirectEvalFn_, "length", PropertyDescriptor {[[Value]]: 2, [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true*}).
1. Set _realmRec_'s [[DirectEval]] internal slot to _boundDirectEvalFn_.
1. Let _indirectEvalFn_ be ? GetMethod(_realmObj_, @@indirectEval).
1. If IsCallable(_indirectEvalFn_) is not *true*, throw a *TypeError* exception.
1. Let _boundIndirectEvalFn_ be BoundFunctionCreate(_indirectEvalFn_, _realmObj_, « »).
1. Perform ! DefinePropertyOrThrow(_boundIndirectEvalFn_, "length", PropertyDescriptor {[[Value]]: 1, [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true*}).
1. Set _realmRec_'s [[IndirectEval]] internal slot to _boundIndirectEvalFn_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-the-realm-constructor">
<h1>The Realm Constructor</h1>
<p>
The Ream constructor is the %Realm% intrinsic object and the initial value of the *Realm* property of the global object. When called as a constructor it creates and initializes a new Realm object. Realm is not intended to be called as a function and will throw an exception when called in that manner.
</p>
<p>
The Realm constructor is designed to be subclassable. It may be used as the value in an extends clause of a class definition. Subclass constructors that intend to inherit the specified Realm behaviour must include a super call to the Realm constructor to create and initialize the subclass instance with the internal state necessary to support the Realm.prototype built-in methods.
</p>
<emu-clause id="sec-realm" aoid="Realm">
<h1>Realm ([ _target_, _handler_ ])</h1>
When Realm is called with arguments _target_ and _handler_ performs the following steps:
<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. Let _O_ be ? OrdinaryCreateFromConstructor(NewTarget, "%RealmPrototype%", « [[Realm]] »).
1. If _handler_ is provided, then:
1. If Type(_target_) is not Object, throw a *TypeError* exception.
1. If Type(_handler_) is not Object, throw a *TypeError* exception.
1. Let _globalEnv_ be ? ProxyCreate(_target_, _handler_).
1. Else,
1. Let _globalEnv_ be *undefined*.
1. Let _realmRec_ be CreateRealm().
1. Set _O_'s [[Realm]] internal slot to _realmRec_.
1. Perform ? ExtractRealmMethods(_O_).
1. Perform ? SetRealmGlobalObject(_realmRec_, _globalEnv_, *undefined*).
1. Let _init_ be ? GetMethod(_O_, *"init"*).
1. If IsCallable(_init_) is not *true*, throw a *TypeError* exception.
1. Perform ? Call(_init_, _O_).
1. Return _O_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-realm-constructor">
<h1>Properties of the Realm Constructor</h1>
The value of the [[Prototype]] internal slot of the *Realm* constructor is the intrinsic object %FunctionPrototype%.
</emu-clause>
<emu-clause id="sec-properties-of-the-realm-prototype-object">
<h1>Properties of the Realm Prototype Object</h1>
<emu-clause id="sec-realm.prototype.init">
<h1>Realm.prototype.init ()</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Perform ? SetDefaultGlobalBindings(_O_.[[Realm]]).
</emu-alg>
<emu-note>
Extensible web: This is the dynamic way to define globals in a new realm.
</emu-note>
</emu-clause>
<emu-clause id="sec-realm.prototype.eval">
<h1>Realm.prototype.eval ( _x_ )</h1>
Synchronously execute a top-level script. The _x_ is interpreted as a Script and evaluated with this bound to the realm's global object.
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Let _evalRealm_ be _O_.[[Realm]].
1. Perform ? HostEnsureCanCompileStrings(the current Realm Record, _evalRealm_).
1. Return ? PerformEval(_x_, _evalRealm_, *false*, *false*).
</emu-alg>
<emu-note>
Extensible web: This is the dynamic equivalent of a <script> in HTML.
</emu-note>
</emu-clause>
<emu-clause id="sec-realm.prototype-@@directEval">
<h1>Realm.prototype [ @@directEval ] ( _x_ )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Return _x_.
</emu-alg>
<emu-note>
Extensible web: This is the hook to pre-process _x_ as the code to be evaluated by calling <code>eval()</code>.
</emu-note>
</emu-clause>
<emu-clause id="sec-realm.prototype-@@indirectEval">
<h1>Realm.prototype [ @@indirectEval ] ( _x_ )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Return _x_.
</emu-alg>
<emu-note>
Extensible web: This is the hook to pre-process _x_ as the code to be evaluated by calling a reference to the <code>eval</code> function.
</emu-note>
</emu-clause>
<emu-clause id="sec-realm.prototype.global">
<h1>get Realm.prototype.global</h1>
Realm.prototype.global is an accessor property whose set accessor function is *undefined*. Its get accessor function performs the following steps:
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Return _O_.[[Realm]].[[GlobalObject]].
</emu-alg>
</emu-clause>
<emu-clause id="sec-realm.prototype.stdlib">
<h1>get Realm.prototype.stdlib</h1>
Realm.prototype.stdlib is an accessor property whose set accessor function is *undefined*. Its get accessor function performs the following steps:
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Let _realmRec_ be _O_.[[Realm]].
1. Let global be realmRec.[[GlobalObject]].
1. Let _stdlib_ be ObjectCreate(%ObjectPrototype%).
1. For each property of the Global Object specified in clause <emu-xref href="#sec-global-object"></emu-xref> that correspond to an intrinsic object, do:
1. Let _name_ be the String value of the property name.
1. Let _value_ be the corresponding value of the intrinsic object from record _realmRec_.[[Intrinsics]].
1. Let _descObj_ be ObjectCreate(%ObjectPrototype%).
1. Perform ? DefinePropertyOrThrow(_descObj_, *"value"*, PropertyDescriptor{[[Value]]: _value_, [[Writable]]: *true*, [[Enumerable]]: *true*, [[Configurable]]: *true*}).
1. Perform ? DefinePropertyOrThrow(_stdlib_, _name_, PropertyDescriptor{[[Value]]: _descObj_, [[Writable]]: *true*, [[Enumerable]]: *true*, [[Configurable]]: *true*}).
1. Return _stdlib_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-realm.prototype.intrinsics">
<h1>get Realm.prototype.intrinsics</h1>
Realm.prototype.intrinsics is an accessor property whose set accessor function is *undefined*. Its get accessor function performs the following steps:
<emu-alg>
1. Let _O_ be *this* value.
1. If Type(_O_) is not Object, throw a *TypeError* exception.
1. If _O_ does not have an [[Realm]] internal slot, throw a *TypeError* exception.
1. Let _realmRec_ be _O_.[[Realm]].
1. Let _intrinsics_ be ObjectCreate(%ObjectPrototype%).
1. For each intrinsic name listed in <emu-xref href="#table-7"></emu-xref>, do:
1. Let _intrinsicName_ be the String value in column one of the table.
1. Let _name_ be the String value of _intrinsicName_ without the *%* symbols.
1. Let _value_ be the value of the field name _intrisicName_ from record _realmRec_.[[Intrinsics]].
1. Perform ? DefinePropertyOrThrow(_intrinsics_, _name_, PropertyDescriptor{[[Value]]: _value_, [[Writable]]: *true*, [[Enumerable]]: *true*, [[Configurable]]: *true*}).
1. Return _intrinsics_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-realm.prototype-@@tostringtag">
<h1>Realm.prototype [ @@toStringTag ]</h1>
The initial value of the @@toStringTag property is the String value "Realm".
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-realm-instances">
<h1>Properties of Realm Instances</h1>
Realm instances are ordinary objects that inherit properties from the Realm prototype object (the intrinsic, %RealmPrototype%). Realm instances are initially created with the internal slots described in <emu-xref href="#table-3"></emu-xref>.
<emu-table id="table-3" caption="Internal Slots of Realm Instances">
<table>
<tbody>
<tr>
<th>Internal Slot</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>[[Realm]]</td>
<td>Realm Record</td>
<td>The Realm Record for the initial execution context.</td>
</tr>
</tbody>
</table>
</emu-table>
</emu-clause>
</emu-clause>