1
1
using Jint . Collections ;
2
+ using Jint . Native . Object ;
2
3
using Jint . Native . Symbol ;
3
4
using Jint . Runtime ;
4
5
using Jint . Runtime . Descriptors ;
@@ -14,20 +15,135 @@ internal class IteratorPrototype : Prototype
14
15
internal IteratorPrototype (
15
16
Engine engine ,
16
17
Realm realm ,
17
- Prototype objectPrototype ) : base ( engine , realm )
18
+ ObjectInstance objectPrototype ) : base ( engine , realm )
18
19
{
19
20
_prototype = objectPrototype ;
20
21
}
21
22
22
23
protected override void Initialize ( )
23
24
{
24
- var symbols = new SymbolDictionary ( 1 )
25
+ var properties = new PropertyDictionary ( 12 , checkExistingKeys : false )
25
26
{
26
- [ GlobalSymbolRegistry . Iterator ] = new ( new ClrFunction ( Engine , "[Symbol.iterator]" , ToIterator , 0 , PropertyFlag . Configurable ) , true , false , true ) ,
27
+ [ KnownKeys . Constructor ] = new GetSetPropertyDescriptor (
28
+ new ClrFunction ( _engine , "Iterator.prototype.constructor" , ( _ , _ ) => _engine . Intrinsics . Iterator ) ,
29
+ new ClrFunction ( _engine , "Iterator.prototype.constructor" , ( _ , _ ) =>
30
+ {
31
+ ExceptionHelper . ThrowTypeError ( _realm ) ;
32
+ return Undefined ;
33
+ } ) ,
34
+ PropertyFlag . Configurable ) ,
35
+ [ "map" ] = new ( new ClrFunction ( _engine , "map" , Map , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
36
+ [ "filter" ] = new ( new ClrFunction ( _engine , "filter" , Filter , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
37
+ [ "take" ] = new ( new ClrFunction ( _engine , "take" , Take , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
38
+ [ "drop" ] = new ( new ClrFunction ( _engine , "drop" , Drop , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
39
+ [ "flatMap" ] = new ( new ClrFunction ( _engine , "flatMap" , FlatMap , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
40
+ [ "reduce" ] = new ( new ClrFunction ( _engine , "reduce" , Reduce , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
41
+ [ "toArray" ] = new ( new ClrFunction ( _engine , "toArray" , ToArray , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
42
+ [ "forEach" ] = new ( new ClrFunction ( _engine , "forEach" , ForEach , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
43
+ [ "some" ] = new ( new ClrFunction ( _engine , "some" , Some , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
44
+ [ "evey" ] = new ( new ClrFunction ( _engine , "every" , Every , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
45
+ [ "find" ] = new ( new ClrFunction ( _engine , "find" , Find , 1 , PropertyFlag . Configurable ) , PropertyFlag . Writable | PropertyFlag . Configurable ) ,
27
46
} ;
47
+
48
+ SetProperties ( properties ) ;
49
+
50
+ var symbols = new SymbolDictionary ( 1 ) { [ GlobalSymbolRegistry . Iterator ] = new ( new ClrFunction ( Engine , "[Symbol.iterator]" , ToIterator , 0 , PropertyFlag . Configurable ) , true , false , true ) , } ;
28
51
SetSymbols ( symbols ) ;
29
52
}
30
53
54
+ private JsValue Map ( JsValue thisObject , JsValue [ ] arguments )
55
+ {
56
+ if ( thisObject is not ObjectInstance o )
57
+ {
58
+ ExceptionHelper . ThrowTypeError ( _realm , "object must be an Object" ) ;
59
+ return Undefined ;
60
+ }
61
+
62
+ var callable = GetCallable ( arguments . At ( 0 ) ) ;
63
+ var iterated = GetIteratorDirect ( o ) ;
64
+ //var iterator = new iterao
65
+
66
+ var closure = ( ) =>
67
+ {
68
+ //a. Let counter be 0.
69
+ // b. Repeat,
70
+ //i. Let value be ? IteratorStepValue(iterated).
71
+ // ii. If value is done, return undefined.
72
+ // iii. Let mapped be Completion(Call(mapper, undefined, « value, 𝔽(counter) »)).
73
+ //iv. IfAbruptCloseIterator(mapped, iterated).
74
+ // v. Let completion be Completion(Yield(mapped)).
75
+ // vi. IfAbruptCloseIterator(completion, iterated).
76
+ // vii. Set counter to counter + 1.
77
+ } ;
78
+
79
+ var result = new SuperFoo ( _engine , closure , iterated ) ;
80
+ return result ;
81
+ }
82
+
83
+ private static IteratorInstance . ObjectIterator GetIteratorDirect ( ObjectInstance objectInstance ) => new ( objectInstance ) ;
84
+
85
+ private sealed class SuperFoo : IteratorInstance
86
+ {
87
+ public SuperFoo ( Engine engine , Action closure , IteratorInstance iterated ) : base ( engine )
88
+ {
89
+ }
90
+
91
+ public override bool TryIteratorStep ( out ObjectInstance nextItem )
92
+ {
93
+ throw new NotImplementedException ( ) ;
94
+ }
95
+ }
96
+
97
+ private JsValue Filter ( JsValue thisObject , JsValue [ ] arguments )
98
+ {
99
+ return Undefined ;
100
+ }
101
+
102
+ private JsValue Take ( JsValue thisObject , JsValue [ ] arguments )
103
+ {
104
+ return Undefined ;
105
+ }
106
+
107
+ private JsValue Drop ( JsValue thisObject , JsValue [ ] arguments )
108
+ {
109
+ return Undefined ;
110
+ }
111
+
112
+ private JsValue FlatMap ( JsValue thisObject , JsValue [ ] arguments )
113
+ {
114
+ return Undefined ;
115
+ }
116
+
117
+ private JsValue Reduce ( JsValue thisObject , JsValue [ ] arguments )
118
+ {
119
+ return Undefined ;
120
+ }
121
+
122
+ private JsValue ToArray ( JsValue thisObject , JsValue [ ] arguments )
123
+ {
124
+ return Undefined ;
125
+ }
126
+
127
+ private JsValue ForEach ( JsValue thisObject , JsValue [ ] arguments )
128
+ {
129
+ return Undefined ;
130
+ }
131
+
132
+ private JsValue Some ( JsValue thisObject , JsValue [ ] arguments )
133
+ {
134
+ return Undefined ;
135
+ }
136
+
137
+ private JsValue Every ( JsValue thisObject , JsValue [ ] arguments )
138
+ {
139
+ return Undefined ;
140
+ }
141
+
142
+ private JsValue Find ( JsValue thisObject , JsValue [ ] arguments )
143
+ {
144
+ return Undefined ;
145
+ }
146
+
31
147
private static JsValue ToIterator ( JsValue thisObject , JsValue [ ] arguments )
32
148
{
33
149
return thisObject ;
0 commit comments