Skip to content

Editorial: Add GetBooleanOption and GetStringOption - #3982

Merged
ljharb merged 1 commit into
tc39:mainfrom
linusg:options
Nov 19, 2026
Merged

ljharb merged 1 commit into
tc39:mainfrom
linusg:options

Conversation

@linusg

@linusg linusg commented Sep 21, 2026

Copy link
Copy Markdown
Member

Option bags have historically been rare in ECMA-262, which is why so far only ECMA-402 provides a dedicated GetOption AO. Now that GetOptionsObject has been pulled into 262 (#3655) and option bags are increasingly used to customize behavior of existing built-ins it seems like a good time to introduce helper AOs. Since normative conventions dictate different coercing behavior from the traditional status quo GetOption cannot be reused directly and we must instead provide an option for behavior on type mismatch, additionally I think splitting into multiple typed variants increases readability.

I decided against adding GetObjectOption as there is no coercion behavior and the name has an unfortunate similarity to GetOptionsObject; for now that Get and throw logic remains inlined in Iterator.zip for its padding option.

Use cases outside of what's been changed here:

  • ECMA-402 can drop GetOption in favor of these new AOs

  • Temporal heavily uses string options

  • GetBooleanOption was written with throw instead of coercing proposal-json-parseimmutable#21 in mind:

    --- a/spec.emu
    +++ b/spec.emu
    @@ -113,12 +113,8 @@ contributors: Robin Ricard, Rick Button, Ashley Claymore, Nicolò Ribaudo, Peter
               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 ? Get(_options_, *"freeze"*).
    -            1. If _freeze_ is *undefined*, set _freeze_ to *false*.
    -            1. If _freeze_ is not a Boolean, throw a *TypeError* exception.
    -            1. Set _preferNullPrototype_ to ? Get(_options_, *"preferNullPrototype"*).
    -            1. If _preferNullPrototype_ is *undefined*, set _preferNullPrototype_ to _freeze_.
    -            1. If _preferNullPrototype_ is not a Boolean, throw a *TypeError* exception.
    +            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>
    

@github-actions

Copy link
Copy Markdown

The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/3982 and as multiple pages at https://tc39.es/ecma262/pr/3982/multipage .

@linusg

linusg commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

Small complication is that 402 GetOption throws RangeError for invalid string values, so we'd either have to see if a normative change to do the same in 262 is web compatible (probably?) or make the option coerce/reject-typeerror/reject-rangeerror.

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm annoyed by the repetition across Get${Type}Option operations as compared to a single GetOption operation, but not so much that I object to them.

Small complication is that 402 GetOption throws RangeError for invalid string values, so we'd either have to see if a normative change to do the same in 262 is web compatible (probably?) or make the option coerce/reject-typeerror/reject-rangeerror.

ECMA-262 functions throwing a TypeError for an invalid string option value where analogous ECMA-402 functions throw a RangeError seems like strictly worse behavior that I would love to correct via normative PR (I think I even commented on one of the former proposals, although likely too late to have made a difference), but failing that—or in advance of it—generalizing the parameter to support both specs seems good to me.

@linusg

linusg commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

I'm not strictly against folding it back into a single GetOption, let's see what the others think.

@ljharb

ljharb commented Sep 22, 2026

Copy link
Copy Markdown
Member

A single one seems simpler to me, personally.

@linusg linusg added the editor call to be discussed in the next editor call label Sep 22, 2026
@linusg

linusg commented Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

Given tc39/proposal-joint-iteration#38 the normative change would have to target ECMA-402, it seems that the recent cases of this (Uint8Array.fromBase64(), Uint8Array.prototype.setFromBase64(), Uint8Array.prototype.toBase64(), Iterator.zip(), Iterator.zipKeyed()) deliberately chose TypeError to match Web Platform precedent.

And yet Temporal uses RangeError for these cases, so it's all a bit of a mess.

@linusg

linusg commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

Added two commits to support the ECMA-402 use cases (RangeError exception and undefined default values).

Comment thread spec.html Resolved
Comment thread spec.html Resolved
Comment thread spec.html Resolved
@linusg
linusg force-pushed the options branch 2 times, most recently from 5e6d8cf to a82d5fd Compare September 22, 2026 21:07

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that Temporal GetRoundingIncrementOption is also a case for reading in integers, e.g.

1. Let _int_ be ? GetIntegerOption(_options_, *"roundingIncrement"*, ~coerce~, 1).
1. If _int_ is not in the inclusive interval from 1 to 10<sup>9</sup>, throw a *RangeError* exception.
1. Return _int_.

I'd like to say the same for ECMA-402 DefaultNumberOption, but that's further out because it checks MV bounds before snapping to integer (tc39/ecma402#691) and therefore in its current form would need e.g.

1. Let _mv_ be ? ValidateMVOption(_value_, ~coerce~, _fallback_).
1. If _mv_ is not in the inclusive interval from _minimum_ to _maximum_, throw a *RangeError* exception.
1. Return _mv_.

There's also GetTemporalFractionalSecondDigitsOption, but its type mixture probably precludes its use of these single-type patterns.


Whether generalization to integer and MV types supports pursuing the polymorphic {GetOption,ValidateOption} approach over a monomorphic {Get,Validate}${Type}Option is still debatable, but the latter is definitely friendlier to ESMeta and more supportive of type-specific parameters like allowedValues for strings and (minimum, maximum) for numerics, even if it does come with lots of repetitive boilerplate. But all in all, I wouldn't object to landing this PR as-is, or with something like ValidateStringOption to sweep in String.prototype.normalize as well.

Comment thread spec.html Resolved
Comment thread spec.html Resolved
Comment thread spec.html Resolved
@linusg

linusg commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

or with something like ValidateStringOption to sweep in String.prototype.normalize as well.

I've done that in a second commit, works out nicely IMO.

@linusg linusg removed the editor call to be discussed in the next editor call label Sep 28, 2026
gibson042 added a commit to gibson042/how-we-work that referenced this pull request Sep 30, 2026
gibson042 added a commit to gibson042/how-we-work that referenced this pull request Oct 1, 2026
… and RangeError

Ref tc39/ecma262#3982 (comment)
An narrower alternative to tc39#176: SyntaxError for large sets defined by
grammar/pattern/etc., RangeError only for numeric intervals.
gibson042 added a commit to gibson042/how-we-work that referenced this pull request Oct 1, 2026
… and RangeError

Ref tc39/ecma262#3982 (comment)

A narrower alternative to tc39#176: SyntaxError for large sets defined by
grammar/pattern/etc., RangeError only for numeric intervals.
Comment thread spec.html Resolved
Comment thread spec.html Resolved
@linusg linusg added editor call to be discussed in the next editor call and removed editor call to be discussed in the next editor call labels Oct 22, 2026
@nicolo-ribaudo nicolo-ribaudo removed the editor call to be discussed in the next editor call label Oct 22, 2026

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As discussed in today's editor call, the defaultValue parameters should not be optional.

Comment thread spec.html Resolved
Comment thread spec.html Resolved
Comment thread spec.html Resolved
@nicolo-ribaudo nicolo-ribaudo added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Nov 19, 2026
@ljharb
ljharb merged commit fbfdb76 into tc39:main Nov 19, 2026
10 checks passed
@linusg
linusg deleted the options branch November 19, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants