-
Notifications
You must be signed in to change notification settings - Fork 13
/
spec.emu
136 lines (128 loc) · 8.31 KB
/
spec.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
<!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: Range proposal
status: proposal
stage: 1
location: https://tc39.es/proposal-Number.range
copyright: false
contributors: Jack Works
</pre>
<emu-clause id="sec-control-abstraction-objects" number="27">
<h1>Control Abstraction Objects</h1>
<emu-clause id="sec-iteration" number="1">
<h1>Iteration</h1>
<emu-clause id="sec-properties-of-the-iterator-constructor" number="2">
<h1>Properties of the Iterator Constructor</h1>
<emu-clause id="sec-iterator.range">
<h1>Iterator.range ( _start_, _end_, _optionOrStep_ )</h1>
<emu-alg>
1. If _start_ is a Number, return ? CreateNumericRangeIterator(_start_, _end_, _optionOrStep_, ~number-range~).
1. If _start_ is a BigInt, return ? CreateNumericRangeIterator(_start_, _end_, _optionOrStep_, ~bigint-range~).
1. Throw a *TypeError* exception.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-numeric-range-iterator-object">
<h1>The <dfn>NumericRangeIterator</dfn> Object</h1>
<p>A NumericRangeIterator object is an iterator that yields numbers. There is not a named constructor for NumericRangeIterator objects. Instead, NumericRangeIterator objects are created by the CreateNumericRangeIterator abstract operation as needed.</p>
<emu-clause id="sec-create-numeric-range-iterator" type="abstract operation">
<h1>
CreateNumericRangeIterator (
_start_: a Number or a BigInt,
_end_: an ECMAScript language value,
_optionOrStep_: an ECMAScript language value,
_type_: ~number-range~ or ~bigint-range~
) : either a normal completion containing an ECMAScript language value or a throw completion
</h1>
<dl class="header"></dl>
<emu-alg>
1. If _start_ is *NaN*, throw a *RangeError* exception.
1. If _end_ is *NaN*, throw a *RangeError* exception.
1. If _type_ is ~number-range~, then
1. Assert: _start_ is a Number.
1. If _end_ is not a Number, throw a *TypeError* exception.
1. Let _zero_ be *0*<sub>ℤ</sub>.
1. Let _one_ be *1*<sub>ℤ</sub>.
1. Else,
1. Assert: _start_ is a BigInt.
<emu-note type="editor">Allowing all kinds of number (bigint, decimals, ...) to range from a finite number to infinity.</emu-note>
1. If _end_ is not *+∞*<sub>𝔽</sub> or *-∞*<sub>𝔽</sub> and _end_ is not a BigInt, throw a *TypeError* exception.
1. Let _zero_ be *0*<sub>ℤ</sub>.
1. Let _one_ be *1*<sub>ℤ</sub>.
1. If _start_ is *+∞*<sub>𝔽</sub> or *-∞*<sub>𝔽</sub>, throw a *RangeError* exception.
1. Let _inclusiveEnd_ be *false*.
1. If _optionOrStep_ is *undefined* or *null*, then
1. Let _step_ be *undefined*.
1. Else if _optionOrStep_ is an Object, then
1. Let _step_ be ? Get(_optionOrStep_, *"step"*).
1. Set _inclusiveEnd_ to ToBoolean(? Get(_optionOrStep_, *"inclusive"*)).
1. Else if _type_ is ~number-range~ and _optionOrStep_ is a Number, then
1. Let _step_ be _optionOrStep_.
1. Else if _type_ is ~bigint-range~ and _optionOrStep_ is a BigInt, then
1. Let _step_ be _optionOrStep_.
1. Else,
1. Throw a *TypeError* exception.
1. If _step_ is *undefined* or *null*, then
1. If _end_ > _start_, let _step_ be _one_.
1. Else let _step_ be -_one_.
1. If _step_ is *NaN*, throw a *RangeError* exception.
1. If _type_ is ~number-range~ and _step_ is not a Number, throw a *TypeError* exception.
1. Else if _type_ is ~bigint-range~ and _step_ is not a BigInt, throw a *TypeError* exception.
1. If _step_ is *+∞*<sub>𝔽</sub> or *-∞*<sub>𝔽</sub>, throw a *RangeError* exception.
1. If _step_ is _zero_ and _start_ is not _end_, throw a *RangeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _start_, _end_, _step_, _inclusiveEnd_, _zero_, _one_ and performs the following steps when called:
1. If _end_ > _start_, let _ifIncrease_ be *true*.
1. Else let _ifIncrease_ be *false*.
1. If _step_ > _zero_, let _ifStepIncrease_ be *true*.
1. Else let _ifStepIncrease_ be *false*.
1. If _ifIncrease_ is not _ifStepIncrease_, return *undefined*.
1. Let _hitsEnd_ be *false*.
1. Let _currentCount_ be _zero_.
1. NOTE: You can debug these steps at https://tc39.es/proposal-Number.range/playground.html .
1. Repeat, while _hitsEnd_ is *false*,
1. Let _currentYieldingValue_ be _start_ + (_step_ \* _currentCount_).
1. If _currentYieldingValue_ is _end_, Set _hitsEnd_ to *true*.
1. Set _currentCount_ to _currentCount_ + _one_.
1. If _ifIncrease_ is *true*, then
1. If _inclusiveEnd_ is *true*, then
1. If _currentYieldingValue_ > _end_, return *undefined*.
1. Else,
1. If _currentYieldingValue_ ≥ _end_, return *undefined*.
1. Else,
1. If _inclusiveEnd_ is *true*, then
1. If _end_ > _currentYieldingValue_, return *undefined*.
1. Else,
1. If _end_ ≥ _currentYieldingValue_, return *undefined*.
1. Perform ? Yield(_currentYieldingValue_).
1. Return *undefined*.
1. Return CreateIteratorFromClosure(_closure_, *"%NumericRangeIteratorPrototype%"*, %NumericRangeIteratorPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-%numericrangeiteratorprototype%-object">
<h1>The %NumericRangeIteratorPrototype% Object</h1>
<p>The <dfn>%NumericRangeIteratorPrototype%</dfn> object:</p>
<ul>
<li>has properties that are inherited by all NumericRangeIterator Objects.</li>
<li>is an ordinary object.</li>
<li>has a [[Prototype]] internal slot whose value is %IteratorPrototype%.</li>
<li>has the following properties:</li>
</ul>
<emu-clause id="sec-properties-of-the-numericrangeiterator-prototype-object-next">
<h1>%NumericRangeIterator%.next ( )</h1>
<emu-alg>
1. Return ? GeneratorResume(*this* value, ~empty~, *"%NumericRangeIteratorPrototype%"*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-properties-of-the-numericrangeiterator-prototype-object-@@tostringtag">
<h1>%NumericRangeIteratorPrototype%.[@@toStringTag]</h1>
<p>The initial value of the @@toStringTag property is the String value `"NumericRangeIterator"`.</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true* }.</p>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>