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

Editorial: Extract JSON parsing into its own AO #3540

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -18545,7 +18545,7 @@ <h1>Static Semantics: Early Errors</h1>
</emu-grammar>
<ul>
<li>
It is a Syntax Error if the PropertyNameList of |PropertyDefinitionList| contains any duplicate entries for *"__proto__"* and at least two of those entries were obtained from productions of the form <emu-grammar>PropertyDefinition : PropertyName `:` AssignmentExpression</emu-grammar>. This rule is not applied if this |ObjectLiteral| is contained within a |Script| that is being parsed for JSON.parse (see step <emu-xref href="#step-json-parse-parse"></emu-xref> of <emu-xref href="#sec-json.parse">JSON.parse</emu-xref>).
It is a Syntax Error if the PropertyNameList of |PropertyDefinitionList| contains any duplicate entries for *"__proto__"* and at least two of those entries were obtained from productions of the form <emu-grammar>PropertyDefinition : PropertyName `:` AssignmentExpression</emu-grammar>. This rule is not applied if this |ObjectLiteral| is contained within a |Script| that is being parsed for ParseJSON (see step <emu-xref href="#step-json-parse-parse"></emu-xref> of ParseJSON).
</li>
</ul>
<emu-note>
Expand Down Expand Up @@ -18657,7 +18657,7 @@ <h1>
<emu-grammar>PropertyDefinition : PropertyName `:` AssignmentExpression</emu-grammar>
<emu-alg>
1. Let _propKey_ be ? Evaluation of |PropertyName|.
1. If this |PropertyDefinition| is contained within a |Script| that is being evaluated for JSON.parse (see step <emu-xref href="#step-json-parse-eval"></emu-xref> of <emu-xref href="#sec-json.parse">JSON.parse</emu-xref>), then
1. If this |PropertyDefinition| is contained within a |Script| that is being evaluated for ParseJSON (see step <emu-xref href="#step-json-parse-eval"></emu-xref> of ParseJSON), then
1. Let _isProtoSetter_ be *false*.
1. Else if _propKey_ is *"__proto__"* and IsComputedPropertyKey of |PropertyName| is *false*, then
1. Let _isProtoSetter_ be *true*.
Expand Down Expand Up @@ -28589,7 +28589,7 @@ <h1>
</dl>

<emu-alg>
1. Let _json_ be ? Call(%JSON.parse%, *undefined*, « _source_ »).
1. Let _json_ be ? ParseJSON(_source_).
1. Return CreateDefaultExportSyntheticModule(_json_).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -46165,14 +46165,7 @@ <h1>JSON.parse ( _text_ [ , _reviver_ ] )</h1>
<p>The optional _reviver_ parameter is a function that takes two parameters, _key_ and _value_. It can filter and transform the results. It is called with each of the _key_/_value_ pairs produced by the parse, and its return value is used instead of the original value. If it returns what it received, the structure is not modified. If it returns *undefined* then the property is deleted from the result.</p>
<emu-alg>
1. Let _jsonString_ be ? ToString(_text_).
1. [id="step-json-parse-validate"] Parse StringToCodePoints(_jsonString_) as a JSON text as specified in ECMA-404. Throw a *SyntaxError* exception if it is not a valid JSON text as defined in that specification.
1. Let _scriptString_ be the string-concatenation of *"("*, _jsonString_, and *");"*.
1. [id="step-json-parse-parse"] Let _script_ be ParseText(_scriptString_, |Script|).
1. NOTE: The early error rules defined in <emu-xref href="#sec-object-initializer-static-semantics-early-errors"></emu-xref> have special handling for the above invocation of ParseText.
1. Assert: _script_ is a Parse Node.
1. [id="step-json-parse-eval"] Let _unfiltered_ be ! <emu-meta suppress-effects="user-code">Evaluation of _script_</emu-meta>.
1. NOTE: The PropertyDefinitionEvaluation semantics defined in <emu-xref href="#sec-runtime-semantics-propertydefinitionevaluation"></emu-xref> have special handling for the above evaluation.
1. [id="step-json-parse-assert-type"] Assert: _unfiltered_ is either a String, a Number, a Boolean, an Object that is defined by either an |ArrayLiteral| or an |ObjectLiteral|, or *null*.
1. Let _unfiltered_ be ? ParseJSON(_jsonString_).
1. If IsCallable(_reviver_) is *true*, then
1. Let _root_ be OrdinaryObjectCreate(%Object.prototype%).
1. Let _rootName_ be the empty String.
Expand All @@ -46182,10 +46175,35 @@ <h1>JSON.parse ( _text_ [ , _reviver_ ] )</h1>
1. Return _unfiltered_.
</emu-alg>
<p>The *"length"* property of this function is *2*<sub>𝔽</sub>.</p>
<emu-note>
<p>Valid JSON text is a subset of the ECMAScript |PrimaryExpression| syntax. Step <emu-xref href="#step-json-parse-validate"></emu-xref> verifies that _jsonString_ conforms to that subset, and step <emu-xref href="#step-json-parse-assert-type"></emu-xref> asserts that that parsing and evaluation returns a value of an appropriate type.</p>
<p>However, because <emu-xref href="#sec-runtime-semantics-propertydefinitionevaluation"></emu-xref> behaves differently during `JSON.parse`, 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, which likewise does not apply during `JSON.parse`, means that not all texts accepted by `JSON.parse` are valid as a |PrimaryExpression|, despite matching the grammar.</p>
</emu-note>

<emu-clause id="sec-ParseJSON" type="abstract operation">
<h1>
ParseJSON (
_text_: a String,
): either a normal completion containing an ECMAScript language value or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. [id="step-json-parse-validate"] If StringToCodePoints(_text_) is not a valid JSON text as specified in ECMA-404, throw a *SyntaxError* exception.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to reword this step otherwise ecmarkup was complaining that this AO can never return an abrupt completion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's arguably a bug in ecmarkup, but I like this wording better anyway.

1. Let _scriptString_ be the string-concatenation of *"("*, _text_, and *");"*.
1. [id="step-json-parse-parse"] Let _script_ be ParseText(_scriptString_, |Script|).
1. NOTE: The early error rules defined in <emu-xref href="#sec-object-initializer-static-semantics-early-errors"></emu-xref> have special handling for the above invocation of ParseText.
1. Assert: _script_ is a Parse Node.
1. [id="step-json-parse-eval"] Let _result_ be ! <emu-meta suppress-effects="user-code">Evaluation of _script_</emu-meta>.
1. NOTE: The PropertyDefinitionEvaluation semantics defined in <emu-xref href="#sec-runtime-semantics-propertydefinitionevaluation"></emu-xref> have special handling for the above evaluation.
1. [id="step-json-parse-assert-type"] Assert: _result_ is either a String, a Number, a Boolean, an Object that is defined by either an |ArrayLiteral| or an |ObjectLiteral|, or *null*.
1. Return _result_.
</emu-alg>
<emu-note>
<p>Valid JSON text is a subset of the ECMAScript |PrimaryExpression| syntax. Step <emu-xref href="#step-json-parse-validate"></emu-xref> verifies that _jsonString_ conforms to that subset, and step <emu-xref href="#step-json-parse-assert-type"></emu-xref> asserts that that parsing and evaluation returns a value of an appropriate type.</p>
<p>However, because <emu-xref href="#sec-runtime-semantics-propertydefinitionevaluation"></emu-xref> behaves differently during ParseJSON, 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, which likewise does not apply during ParseJSON, means that not all texts accepted by ParseJSON are valid as a |PrimaryExpression|, despite matching the grammar.</p>
</emu-note>
<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>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>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these two sentences from InternalizeJSONProperty to here, since they didn't really make sense there.

</emu-clause>

<emu-clause id="sec-internalizejsonproperty" type="abstract operation">
<h1>
Expand Down Expand Up @@ -46226,10 +46244,6 @@ <h1>
1. Perform ? CreateDataProperty(_val_, _P_, _newElement_).
1. Return ? Call(_reviver_, _holder_, « _name_, _val_ »).
</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>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>

Expand Down