Skip to content

Commit

Permalink
Registry functions: add :date and :time (#51)
Browse files Browse the repository at this point in the history
* fix error and in the dateTime function length
* refactor parts of dateTime and add date function

Co-authored-by: Eemeli Aro <[email protected]>
  • Loading branch information
ryzokuken and eemeli authored Apr 30, 2024
1 parent 23454d3 commit cdad441
Showing 1 changed file with 146 additions and 41 deletions.
187 changes: 146 additions & 41 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,77 @@ contributors: Eemeli Aro
</dl>
</emu-clause>

<emu-clause id="sec-handledatetimeinput" type="abtract operation">
<h1>
HandleDateTimeInput (
_input_: an ECMAScript value,
_opts_: an ECMAScript value,
): a Date object
</h1>
<dl class="header">
<dt>description</dt>
<dd>It accepts an input that could be a Date object, a number or a string and converts them into Date objects.</dd>
</dl>

<emu-alg>
1. If InstanceOfOperator(_input_, �te%) is *true*, then
1. If HasProperty(_input_, *"options"*) is *true*, then
1. Perform ? Call(Object.assign, *undefined*, « _opts_, Get(_input_, *"options"*) »).
1. Return _input_.
1. If Type(_input_) is Object, then
1. Let _valueOf_ be ? Get(_input_, *"valueOf"*).
1. If IsCallable(_valueOf_) is *true*, let _value_ be Call(_valueOf_, _input_).
1. Else throw a *TypeError* exception.
1. If HasProperty(_input_, *"options"*) is *true*, then
1. Perform ? Call(Object.assign, *undefined*, « _opts_, Get(_input_, *"options"*) »).
1. Else,
1. Let _value_ be _input_.
1. If Type(_value_) is Number or String, then
1. Return ? Construct(�te%, « _value_ »).
1. If InstanceOfOperator(_value_, �te%) is *false*, throw a *TypeError* exception.
1. Return _value_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-datetimemessagevalue" type="abstract operation">
<h1>
DateTimeMessageValue (
_value_: a Date object,
_opts_: an Object,
_funcCtx_: an Object,
): an ECMAScript value
</h1>
<emu-alg>
1. Let _locale_ be ! Get(_funcCtx_, *"locale"*).
1. Let _source_ be ! Get(_funcCtx_, *"source"*).
1. Let _dateTimeFormat_ be ? Construct(%Intl.DateTimeFormat%, « _locale_, _opts_ »).
1. Let _toPartsClosure_ be a new Abstract Closure with no parameters that captures _value_, _source_, and _dateTimeFormat_ and performs the following steps when called:
1. Let _parts_ be ? Call(Intl.DateTimeFormat.prototype.formatToParts, _dateTimeFormat_, « _value_ »).
1. Let _result_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"type"*, *"datetime"*).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"source"*, _source_).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"locale"*, _dateTimeFormat_.[[Locale]]).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"parts"*, _parts_).
1. Return CreateArrayFromList(« _result_ »).
1. Let _toParts_ be CreateBuiltinFunction(_toPartsClosure_, *0*, *"toParts"*, « »).
1. Let _toStringClosure_ be a new Abstract Closure with no parameters that captures _value_ and _dateTimeFormat_ and performs the following steps when called:
1. Return ? Call(Intl.DateTimeFormat.prototype.format, _dateTimeFormat_, « _value_ »).
1. Let _toString_ be CreateBuiltinFunction(_toStringClosure_, *0*, *"toString"*, « »).
1. Let _valueOfClosure_ be a new Abstract Closure with no parameters that captures _value_ and performs the following steps when called:
1. Return _value_.
1. Let _valueOf_ be CreateBuiltinFunction(_valueOfClosure_, *0*, *"valueOf"*, « »).
1. Let _mv_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"type"*, *"datetime"*).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"source"*, _source_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"locale"*, _dateTimeFormat_.[[Locale]]).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"options"*, _opts_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"toParts"*, _toParts_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"toString"*, _toString_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"valueOf"*, _valueOf_).
1. Return _mv_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-getmessagefunctions" type="abstract operation">
<h1>
GetMessageFunctions (
Expand All @@ -326,12 +397,18 @@ contributors: Eemeli Aro
1. Let _number_ be CreateBuiltinFunction(_numberSteps_, *3*, *"number"*, « »).
1. Let _stringSteps_ be the algorithm steps defined in <emu-xref href="#sec-messageformat-stringfunctions"></emu-xref>.
1. Let _string_ be CreateBuiltinFunction(_stringSteps_, *3*, *"string"*, « »).
1. Let _dateTimeSteps_ be the algorithm steps defined in <emu-xref href="#sec-messageformat-stringfunctions"></emu-xref>.
1. Let _dateTime_ be CreateBuiltinFunction(_dateTimeSteps_, *3*, *"string"*, « »).
1. Let _dateTimeSteps_ be the algorithm steps defined in <emu-xref href="#sec-messageformat-datetimefunctions"></emu-xref>.
1. Let _dateTime_ be CreateBuiltinFunction(_dateTimeSteps_, *3*, *"datetime"*, « »).
1. Let _dateSteps_ be the algorithm steps defined in <emu-xref href="#sec-messageformat-datefunctions"></emu-xref>.
1. Let _date_ be CreateBuiltinFunction(_dateSteps_, *3*, *"date"*, « »).
1. Let _timeSteps_ be the algorithm steps defined in <emu-xref href="#sec-messageformat-timefunctions"></emu-xref>.
1. Let _time_ be CreateBuiltinFunction(_timeSteps_, *3*, *"time"*, « »).
1. Let _functions_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_functions_, *"number"*, _number_).
1. Perform ! CreateDataPropertyOrThrow(_functions_, *"string"*, _string_).
1. Perform ! CreateDataPropertyOrThrow(_functions_, *"datetime"*, _dateTime_).
1. Perform ! CreateDataPropertyOrThrow(_functions_, *"date"*, _date_).
1. Perform ! CreateDataPropertyOrThrow(_functions_, *"time"*, _time_).
1. For each String _name_ of ? EnumerableOwnProperties(_userFunctions_, ~key~),
1. Let _func_ be ? Get(_userFunctions_, _name_).
1. If IsCallable(_func_) is *true*, then
Expand Down Expand Up @@ -496,20 +573,10 @@ contributors: Eemeli Aro
</p>

<emu-alg>
1. Let _locale_ be ! Get(_funcCtx_, *"locale"*).
1. Let _localeMatcher_ be ! Get(_funcCtx_, *"localeMatcher"*).
1. Let _source_ be ! Get(_funcCtx_, *"source"*).
1. Let _opts_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_opt_, *"localeMatcher"*, _localeMatcher_).
1. If Type(_input_) is Object, then
1. If HasProperty(_input_, *"options"*) is *true*, then
1. Let _inputOptions_ be ? Get(_input_, *"options"*).
1. Perform ? Call(Object.assign, *undefined*, « _opts_, _inputOptions_ »).
1. Let _valueOf_ be ? Get(_input_, *"valueOf"*).
1. If InstanceOfOperator(_input_, �te%) is *false* and IsCallable(_valueOf_) is *true*, set _input_ to Call(_valueOf_, _input_).
1. If Type(_input_) is Number or String, let _value_ be ? Construct(�te%, « _input_ »).
1. Else if Type(_input_) is Object, let _value_ be _input_.
1. If InstanceOfOperator(_value_, �te%) is *false*, throw a *TypeError* exception.
1. Let _localeMatcher_ be ! Get(_funcCtx_, *"localeMatcher"*).
1. Perform ! CreateDataPropertyOrThrow(_opts_, *"localeMatcher"*, _localeMatcher_).
1. Let _value_ be ? HandleDateTimeInput(_input_, _opts_).
1. For each String _optName_ of ? EnumerableOwnProperties(_options_, ~key~),
1. If _optName_ is not *"locale"*, then
1. Let _optValue_ be ? Get(_options_, _optName_).
Expand All @@ -525,35 +592,73 @@ contributors: Eemeli Aro
1. Set _optValue_ to *false*.
1. Else, set _optValue_ to ToString(_optValue_).
1. Perform ? Set(_opts_, _optName_, _optValue_, *"true"*).
1. Let _dateTimeFormat_ be ? Construct(%Intl.DateTimeFormat%, « _locale_, _opts_ »).
1. Let _toPartsClosure_ be a new Abstract Closure with no parameters that captures _value_, _source_, and _dateTimeFormat_ and performs the following steps when called:
1. Let _parts_ be ? Call(Intl.DateTimeFormat.prototype.formatToParts, _dateTimeFormat_, « _value_ »).
1. Let _result_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"type"*, *"datetime"*).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"source"*, _source_).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"locale"*, _dateTimeFormat_.[[Locale]]).
1. Perform ! CreateDataPropertyOrThrow(_result_, *"parts"*, _parts_).
1. Return CreateArrayFromList(« _result_ »).
1. Let _toParts_ be CreateBuiltinFunction(_toPartsClosure_, *0*, *"toParts"*, « »).
1. Let _toStringClosure_ be a new Abstract Closure with no parameters that captures _value_ and _dateTimeFormat_ and performs the following steps when called:
1. Return ? Call(Intl.DateTimeFormat.prototype.format, _dateTimeFormat_, « _value_ »).
1. Let _toString_ be CreateBuiltinFunction(_toStringClosure_, *0*, *"toString"*, « »).
1. Let _valueOfClosure_ be a new Abstract Closure with no parameters that captures _value_ and performs the following steps when called:
1. Return _value_.
1. Let _valueOf_ be CreateBuiltinFunction(_valueOfClosure_, *0*, *"valueOf"*, « »).
1. Let _mv_ be OrdinaryObjectCreate(%Object.prototype%).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"type"*, *"datetime"*).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"source"*, _source_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"locale"*, _dateTimeFormat_.[[Locale]]).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"options"*, _opts_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"toParts"*, _toParts_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"toString"*, _toString_).
1. Perform ! CreateDataPropertyOrThrow(_mv_, *"valueOf"*, _valueOf_).
1. Return _mv_.
1. Return ? DateTimeMessageValue(_value_, _opts_, _funcCtx_).
</emu-alg>

<p>
The *"length"* property of a MessageFormat number function is *3*.
The *"length"* property of a MessageFormat dateTime function is *3*.
</p>
</emu-clause>

<emu-clause id="sec-messageformat-datefunctions">
<h1>MessageFormat Date Functions</h1>
<p>
A MessageFormat date function is an anonymous built-in function.
</p>
<p>
When a MessageFormat date function is called with arguments _funcCtx_ (an Object), _options_ (an Object), and _input_ (an ECMAScript language value), the following steps are taken:
</p>

<emu-alg>
1. Let _opts_ be OrdinaryObjectCreate(%Object.prototype%).
1. Let _localeMatcher_ be ! Get(_funcCtx_, *"localeMatcher"*).
1. Perform ! CreateDataPropertyOrThrow(_opts_, *"localeMatcher"*, _localeMatcher_).
1. Let _value_ be ? HandleDateTimeInput(_input_, _opts_).
1. Let _dateStyle_ be ! Get(_options_, *"style"*).
1. If _dateStyle_ is *null* or *undefined*, then
1. Set _dateStyle_ to ! Get(_opts_, *"dateStyle"*).
1. If _dateStyle_ is *null* or *undefined*, then
1. Set _dateStyle_ to *"short"*.
1. For each String _optName_ of ? EnumerableOwnProperties(_opts_, ~key~),
1. If _optName_ is not *"calendar"*, *"localeMatcher"*, *"hour12"*, *"hourCycle"*, *"numberingSystem"*, or *"timeZone"*, then
1. Perform ? DeletePropertyOrThrow(_opts_, _optName_).
1. Perform ? Set(_opts_, *"dateStyle"*, ToString(_dateStyle_)).
1. Return ? DateTimeMessageValue(_value_, _opts_, _funcCtx_).
</emu-alg>

<p>
The *"length"* property of a MessageFormat date function is *3*.
</p>
</emu-clause>

<emu-clause id="sec-messageformat-timefunctions">
<h1>MessageFormat Time Functions</h1>
<p>
A MessageFormat time function is an anonymous built-in function.
</p>
<p>
When a MessageFormat time function is called with arguments _funcCtx_ (an Object), _options_ (an Object), and _input_ (an ECMAScript language value), the following steps are taken:
</p>

<emu-alg>
1. Let _opts_ be OrdinaryObjectCreate(%Object.prototype%).
1. Let _localeMatcher_ be ! Get(_funcCtx_, *"localeMatcher"*).
1. Perform ! CreateDataPropertyOrThrow(_opts_, *"localeMatcher"*, _localeMatcher_).
1. Let _value_ be ? HandleDateTimeInput(_input_, _opts_).
1. Let _timeStyle_ be ! Get(_options_, *"style"*).
1. If _timeStyle_ is *null* or *undefined*, then
1. Set _timeStyle_ to ! Get(_opts_, *"timeStyle"*).
1. If _timeStyle_ is *null* or *undefined*, then
1. Set _timeStyle_ to *"short"*.
1. For each String _optName_ of ? EnumerableOwnProperties(_opts_, ~key~),
1. If _optName_ is not *"calendar"*, *"localeMatcher"*, *"hour12"*, *"hourCycle"*, *"numberingSystem"*, or *"timeZone"*, then
1. Perform ? DeletePropertyOrThrow(_opts_, _optName_).
1. Perform ? Set(_opts_, *"timeStyle"*, ToString(_timeStyle_)).
1. Return ? DateTimeMessageValue(_value_, _opts_, _funcCtx_).
</emu-alg>

<p>
The *"length"* property of a MessageFormat time function is *3*.
</p>
</emu-clause>
</emu-clause>
Expand Down

0 comments on commit cdad441

Please sign in to comment.