Repository navigation
Expand file tree
/
Copy pathspec.emu
More file actions
281 lines (261 loc) · 15.9 KB
/
Copy pathspec.emu
File metadata and controls
281 lines (261 loc) · 15.9 KB
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
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="./spec.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="./spec.js"></script>
<pre class="metadata">
title: JSON.parse Options
stage: 2.7
contributors: Robin Ricard, Rick Button, Ashley Claymore, Nicolò Ribaudo, Peter Klecha
</pre>
<emu-biblio href="./biblio.json"></emu-biblio>
<emu-clause id="sec-ecmascript-language-scripts-and-modules" number="16">
<h1>ECMAScript Language: Scripts and Modules</h1>
<emu-clause id="sec-modules" number="2">
<h1>Modules</h1>
<emu-clause id="sec-modules-semantics" number="1">
<h1>Module Semantics</h1>
<emu-clause id="sec-synthetic-module-records" number="8">
<h1>Synthetic Module Records</h1>
<emu-clause id="sec-parse-json-module" type="abstract operation">
<h1>
ParseJSONModule (
_source_: a String,
): either a normal completion containing a Synthetic Module Record, or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd></dd>
</dl>
<emu-alg>
1. <ins>Let _options_ be ! CreateJSONParseOptions().</ins>
1. Let _parseResult_ be ? ParseJSON(_source_<ins>, _options_</ins>).
1. Return CreateDefaultExportSyntheticModule(_parseResult_.[[Value]]).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-structured-data" number="25">
<h1>Structured Data</h1>
<emu-note type="editor">
<p>This proposal builds on top of <a href="https://github.com/tc39/ecma262/pull/3933">ecma262 editorial PR #3933</a>, which introduces the following SDOs:</p>
<ul>
<li>JSONEvaluation</li>
<li>JSONArrayAccumulation</li>
<li>JSONMemberListEvaluation</li>
</ul>
</emu-note>
<emu-clause id="sec-json-object" number="5">
<h1>The JSON Object</h1>
<emu-clause id="sec-json.parse" type="built-in function">
<h1>JSON.parse ( _text_ [ , _reviverOrOptions_ ] )</h1>
<p>This function parses a JSON text (a JSON-formatted String) and produces an ECMAScript language value. The JSON format represents literals, arrays, and objects with a syntax similar to the syntax for ECMAScript literals, Array Initializers, and Object Initializers. After parsing, JSON objects are realized as ECMAScript objects. JSON arrays are realized as ECMAScript Array instances. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and *null*.</p>
<p>The optional <del>_reviver_</del><ins>_reviverOrOptions_</ins> parameter is <ins>either</ins> a function that can filter and transform the results<ins>, or an object whose entries specify built-in transforms to be applied</ins>. <del>F</del><ins>When _reviverOrOptions_ is a function, f</ins>or each value produced by the parse, <del>_reviver_</del><ins>_reviverOrOptions_</ins> is called with three arguments (the associated property key, the value, and a context object). If the property is unmodified and its value is primitive, the provided context object has a *"source"* property containing source text of the corresponding Parse Node. If the call returns *undefined*, the property is deleted. Otherwise, the property is redefined to use the return value.</p>
<emu-alg>
1. Let _jsonString_ be ? ToString(_text_).
1. <ins>Let _options_ be ? CreateJSONParseOptions(_reviverOrOptions_)</ins>.
1. Let _parseResult_ be ? ParseJSON(_jsonString_, <ins>_options_</ins>).
1. If IsCallable(<del>_reviver_</del><ins>_reviverOrOptions_</ins>) is *false*, return _parseResult_.[[Value]].
1. Let _root_ be OrdinaryObjectCreate(%Object.prototype%).
1. Assert: _parseResult_.[[Key]] is the empty String.
1. Perform ! CreateDataPropertyOrThrow(_root_, _parseResult_.[[Key]], _parseResult_.[[Value]]).
1. Return ? InternalizeJSONProperty(_root_, _parseResult_.[[Key]], <del>_reviver_</del><ins>_reviverOrOptions_</ins>, _parseResult_).
</emu-alg>
<p>The *"length"* property of this function is *2*<sub>𝔽</sub>.</p>
<emu-clause id="sec-ParseJSON" type="abstract operation">
<h1>
ParseJSON (
_text_: a String,
<ins>_options_: a JSON Parse Options Record,</ins>
): either a normal completion containing a JSON Parse Record, or a throw completion
</h1>
<dl class="header">
</dl>
<emu-note>
<p>This algorithm references the |JSONValue| nonterminal from ECMA-404.</p>
</emu-note>
<emu-alg>
1. If StringToCodePoints(_text_) is not a valid JSON text as specified in ECMA-404, throw a *SyntaxError* exception.
1. NOTE: JSON whitespace is a strict subset of |WhiteSpace|, and JSON syntax does not support comments.
1. Let _jsonParseNode_ be ParseText(_text_, |JSONValue|).
1. If _jsonParseNode_ is not a Parse Node, throw a *SyntaxError* exception.
1. Let _key_ be the empty String.
1. Let _result_ be the JSONEvaluation of _jsonParseNode_<del> with argument _key_</del><ins> with arguments _key_ and _options_</ins>.
1. Return _result_.
</emu-alg>
<p>It is not permitted for a conforming implementation of `JSON.parse` to extend the JSON grammars. If an implementation wishes to support a modified or extended JSON interchange format it must do so by defining a different parse function.</p>
<emu-note>
<p>Valid JSON text is a syntactic subset of the ECMAScript |PrimaryExpression| syntax. However, because JSONEvaluation behaves differently than Evaluation, the same source text can produce different results when evaluated as a |PrimaryExpression| rather than as JSON. Furthermore, the early error for duplicate *"__proto__"* properties in object literals (<emu-xref href="#sec-object-initializer-static-semantics-early-errors"></emu-xref>), which has no analogue in JSON, means that some texts accepted by ParseJSON are not valid as a |PrimaryExpression|, (e.g., <code>{"__proto__":null,"__proto__":null}</code>).</p>
</emu-note>
<emu-note>
<p>In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-json-parse-options-records">
<h1><ins>JSON Parse Options Records</ins></h1>
<p>A <dfn variants="JSON Parse Options Records">JSON Parse Options Record</dfn> is a Record value used to specify JSON deserialization options.</p>
<p>JSON Parse Options Records have the fields listed in <emu-xref href="#table-json-parse-options-records"></emu-xref>.</p>
<emu-table id="table-json-parse-options-records" caption="JSON Parse Options Record Fields">
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Value</th>
<th>Meaning</th>
</tr>
</thead>
<tr>
<td>[[Freeze]]</td>
<td>a Boolean</td>
<td>Whether objects (including arrays) should be frozen upon creation.</td>
</tr>
<tr>
<td>[[PreferNullPrototype]]</td>
<td>a Boolean</td>
<td>Whether non-array objects should be created with a null prototype.</td>
</tr>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-createjsonparseoptions" type="abstract operation">
<h1>
<ins>
CreateJSONParseOptions (
optional _options_: an ECMAScript language value
): either a normal completion containing a JSON Parse Options Record or a throw completion
</ins>
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _freeze_ be *false*.
1. Let _preferNullPrototype_ be *false*.
1. If _options_ is an Object and IsCallable(_options_) is *false*, then
1. Set _freeze_ to ? GetBooleanOption(_options_, *"freeze"*, ~reject~, *false*).
1. Set _preferNullPrototype_ to ? GetBooleanOption(_options_, *"preferNullPrototype"*, ~reject~, _freeze_).
1. Return the JSON Parse Options Record { [[Freeze]]: _freeze_, [[PreferNullPrototype]]: _preferNullPrototype_ }.
</emu-alg>
</emu-clause>
<emu-clause id="sec-jsonevaluation" type="sdo" oldids="sec-createjsonparserecord,sec-shallowestcontainedjsonvalue">
<h1>
Runtime Semantics: JSONEvaluation (
_key_: a property key,
<ins>_options_: a JSON Parse Options Record,</ins>
): a JSON Parse Record
</h1>
<dl class="header">
<dt>description</dt>
<dd>
<p>It generally produces the same values as Evaluation given the same inputs, but is defined over a much narrower set of productions, namely ECMA-404's |JSONValue|. Unlike Evaluation, it does not raise an early error when *"__proto__"* appears twice as a key (see <emu-xref href="#sec-object-initializer-static-semantics-early-errors"></emu-xref>). It also does not perform [[SetPrototypeOf]] when encountering a *"__proto__"* key, and instead creates an ordinary data property (see <emu-xref href="#sec-runtime-semantics-propertydefinitionevaluation"></emu-xref>).</p>
</dd>
</dl>
<emu-grammar>
JSONArray :
`[` `]`
`[` JSONElementList `]`
</emu-grammar>
<emu-alg>
1. Let _array_ be ! ArrayCreate(0).
1. Let _elements_ be a new empty List.
1. If |JSONElementList| is present, perform JSONArrayAccumulation of |JSONElementList| with arguments _array_, <ins>_options_</ins>, _elements_, and 0.
1. <ins>If _options_.[[Freeze]] is *true*, perform ! SetIntegrityLevel(_array_, ~frozen~)</ins>.
1. Return the JSON Parse Record { [[ParseNode]]: |JSONArray|, [[Key]]: _key_, [[Value]]: _array_, [[Elements]]: _elements_, [[Entries]]: « » }.
</emu-alg>
<emu-grammar>
JSONObject :
`{` `}`
`{` JSONMemberList `}`
</emu-grammar>
<emu-alg>
1. <del>Let _obj_ be OrdinaryObjectCreate(%Object.prototype%)</del>.
1. <ins>If _options_.[[PreferNullPrototype]] is *true*, then</ins>
1. <ins>Let _obj_ be OrdinaryObjectCreate(*null*)</ins>.
1. <ins>Else,</ins>
1. <ins>Let _obj_ be OrdinaryObjectCreate(%Object.prototype%)</ins>.
1. Let _entries_ be a new empty List.
1. If |JSONMemberList| is present, perform JSONMemberListEvaluation of |JSONMemberList| with argument _obj_, <ins>_options_</ins>, and _entries_.
1. <ins>If _options_.[[Freeze]] is *true*, perform ! SetIntegrityLevel(_obj_, ~frozen~)</ins>.
1. Return the JSON Parse Record { [[ParseNode]]: |JSONObject|, [[Key]]: _key_, [[Value]]: _obj_, [[Elements]]: « », [[Entries]]: _entries_ }.
</emu-alg>
</emu-clause>
<emu-clause id="sec-jsonarrayaccumulation" type="sdo">
<h1>
Runtime Semantics: JSONArrayAccumulation (
_array_: an Array,
<ins>_options_: a JSON Parse Options Record,</ins>
_elements_: a List of JSON Parse Records,
_nextIndex_: a non-negative integer,
): a non-negative integer
</h1>
<dl class="header">
<dt>description</dt>
<dd>It is analagous to ArrayAccumulation, except defined over a much narrower grammar (namely ECMA-404), and calls JSONEvaluation on its elements, rather than Evaluation.</dd>
</dl>
<emu-grammar>
JSONElementList :
JSONValue
JSONElementList `,` JSONValue
</emu-grammar>
<emu-alg>
1. If the derived |JSONElementList| is present, set _nextIndex_ to the JSONArrayAccumulation of the derived |JSONElementList| with arguments _array_, <ins>_options_,</ins> _elements_, and _nextIndex_.
1. Let _key_ be ! ToString(𝔽(_nextIndex_)).
1. Let _parseRecord_ be the JSONEvaluation of |JSONValue|<del> with argument _key_</del><ins> with arguments _key_ and _options_</ins>.
1. Perform ! CreateDataPropertyOrThrow(_array_, _key_, _parseRecord_.[[Value]]).
1. Append _parseRecord_ to _elements_.
1. Return _nextIndex_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-jsonmemberlistevaluation" type="sdo">
<h1>
Runtime Semantics: JSONMemberListEvaluation (
_obj_: an Object,
<ins>_options_: a JSON Parse Options Record,</ins>
_entries_: a List of JSON Parse Records,
): ~unused~
</h1>
<dl class="header">
<dt>description</dt>
<dd>It is analagous to PropertyDefinitionEvaluation, except 1) it is defined over a much narrower grammar, 2) it does not include the special handling of *"__proto__"* described in <emu-xref href="#sec-jsonevaluation"></emu-xref>, and 3) it calls JSONEvaluation instead of Evaluation.</dd>
</dl>
<emu-grammar>JSONMemberList : JSONMemberList `,` JSONMember</emu-grammar>
<emu-alg>
1. Perform JSONMemberListEvaluation of the derived |JSONMemberList|<del> with arguments _obj_ and _entries_</del><ins> with arguments _obj_, _options_, and _entries_</ins>.
1. Perform JSONMemberListEvaluation of |JSONMember|<del> with arguments _obj_ and _entries_</del><ins> with arguments _obj_, _options_, and _entries_</ins>.
1. Return ~unused~.
</emu-alg>
<emu-grammar>JSONMember : JSONString `:` JSONValue</emu-grammar>
<emu-alg>
1. Let _dummyKey_ be the empty String.
1. Let _propertyKeyRecord_ be the JSONEvaluation of |JSONString|<del> with argument _dummyKey_</del><ins> with arguments _dummyKey_ and _options_</ins>.
1. Let _key_ be _propertyKeyRecord_.[[Value]].
1. Assert: _key_ is a property name.
1. Let _propertyValueRecord_ be the JSONEvaluation of |JSONValue|<del> with argument _key_</del><ins> with arguments _key_ and _options_</ins>.
1. Assert: _obj_ is an ordinary, extensible object with no non-configurable properties.
1. Perform ! CreateDataPropertyOrThrow(_obj_, _key_, _propertyValueRecord_.[[Value]]).
1. Append _propertyValueRecord_ to _entries_.
1. Return ~unused~.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-json.rawjson" type="built-in function">
<h1>JSON.rawJSON ( _text_ )</h1>
<p>This function returns an object representing raw JSON text of a string, number, boolean, or null value.</p>
<emu-alg>
1. Let _jsonString_ be ? ToString(_text_).
1. If _jsonString_ is the empty String, throw a *SyntaxError* exception.
1. If the first code unit of _jsonString_ is not either an ASCII lowercase letter code unit (0x0061 through 0x007A, inclusive), an ASCII digit code unit (0x0030 through 0x0039, inclusive), 0x0022 (QUOTATION MARK), or 0x002D (HYPHEN-MINUS), throw a *SyntaxError* exception.
1. If the last code unit of _jsonString_ is not either an ASCII lowercase letter code unit (0x0061 through 0x007A, inclusive), an ASCII digit code unit (0x0030 through 0x0039, inclusive), or 0x0022 (QUOTATION MARK), throw a *SyntaxError* exception.
1. <ins>Let _options_ be ! CreateJSONParseOptions().</ins>
1. Let _parseResult_ be ? ParseJSON(_jsonString_<ins>, _options_</ins>).
1. Assert: _parseResult_.[[Value]] is either a String, a Number, a Boolean, or *null*.
1. Let _internalSlotsList_ be « [[IsRawJSON]] ».
1. Let _obj_ be OrdinaryObjectCreate(*null*, _internalSlotsList_).
1. Perform ! CreateDataPropertyOrThrow(_obj_, *"rawJSON"*, _jsonString_).
1. Perform ! SetIntegrityLevel(_obj_, ~frozen~).
1. Return _obj_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>