Skip to content

Commit c26b904

Browse files
committed
wip
1 parent ec397aa commit c26b904

File tree

6 files changed

+2011
-1790
lines changed

6 files changed

+2011
-1790
lines changed

Jint.Tests.Test262/Test262Harness.settings.json

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"Atomics",
1111
"decorators",
1212
"import-assertions",
13-
"iterator-helpers",
1413
"Math.sumPrecise",
1514
"regexp-lookbehind",
1615
"regexp-modifiers",
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Jint.Collections;
2+
using Jint.Native.Function;
3+
using Jint.Native.Object;
4+
using Jint.Native.Symbol;
5+
using Jint.Runtime;
6+
using Jint.Runtime.Descriptors;
7+
using Jint.Runtime.Interop;
8+
9+
namespace Jint.Native.Iterator;
10+
11+
internal sealed class IteratorConstructor : Constructor
12+
{
13+
private static readonly JsString _functionName = new("Iterator");
14+
15+
internal IteratorConstructor(
16+
Engine engine,
17+
Realm realm,
18+
FunctionPrototype functionPrototype,
19+
ObjectPrototype objectPrototype)
20+
: base(engine, realm, _functionName)
21+
{
22+
_prototype = functionPrototype;
23+
PrototypeObject = new IteratorPrototype(engine, realm, this);
24+
_length = new PropertyDescriptor(0, PropertyFlag.Configurable);
25+
_prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
26+
}
27+
28+
private IteratorPrototype PrototypeObject { get; }
29+
30+
protected override void Initialize()
31+
{
32+
const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
33+
const PropertyFlag LengthFlags = PropertyFlag.Configurable;
34+
var properties = new PropertyDictionary(1, checkExistingKeys: false)
35+
{
36+
["from"] = new(new PropertyDescriptor(new ClrFunction(Engine, "from", From, 1, LengthFlags), PropertyFlags)),
37+
};
38+
SetProperties(properties);
39+
}
40+
41+
public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
42+
{
43+
if (newTarget.IsUndefined() || ReferenceEquals(this, newTarget))
44+
{
45+
ExceptionHelper.ThrowTypeError(_realm);
46+
}
47+
48+
return OrdinaryCreateFromConstructor(
49+
newTarget,
50+
static intrinsics => intrinsics.Iterator.PrototypeObject,
51+
static (Engine engine, Realm realm, object? _) => new JsObject(engine));
52+
}
53+
54+
private JsValue From(JsValue thisObject, JsValue[] arguments)
55+
{
56+
var iterator = GetIteratorFlattenable(thisObject, StringHandlingType.IterateStrings);
57+
var hasInstance = _engine.Intrinsics.Iterator.OrdinaryHasInstance(iterator);
58+
if (hasInstance)
59+
{
60+
return iterator;
61+
}
62+
63+
//var wrapper = OrdinaryObjectCreate(WrapForValidIteratorPrototype);
64+
//wrapper.Iterated = iterator;
65+
//return wrapper;
66+
67+
return Undefined;
68+
}
69+
70+
private IteratorInstance.ObjectIterator GetIteratorFlattenable(JsValue obj, StringHandlingType stringHandling)
71+
{
72+
if (obj is not ObjectInstance)
73+
{
74+
if (stringHandling == StringHandlingType.RejectStrings || obj.IsString())
75+
{
76+
ExceptionHelper.ThrowTypeError(_realm);
77+
}
78+
}
79+
80+
JsValue iterator;
81+
var method = JsValue.GetMethod(_realm, obj, GlobalSymbolRegistry.Iterator);
82+
if (method is null)
83+
{
84+
iterator = obj;
85+
}
86+
else
87+
{
88+
iterator = method.Call(obj);
89+
}
90+
91+
if (iterator is not ObjectInstance objectInstance)
92+
{
93+
ExceptionHelper.ThrowTypeError(_realm);
94+
return null;
95+
}
96+
97+
return new IteratorInstance.ObjectIterator(objectInstance);
98+
}
99+
100+
private enum StringHandlingType
101+
{
102+
IterateStrings,
103+
RejectStrings
104+
}
105+
}

Jint/Native/Iterator/IteratorPrototype.cs

+119-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Jint.Collections;
2+
using Jint.Native.Object;
23
using Jint.Native.Symbol;
34
using Jint.Runtime;
45
using Jint.Runtime.Descriptors;
@@ -14,20 +15,135 @@ internal class IteratorPrototype : Prototype
1415
internal IteratorPrototype(
1516
Engine engine,
1617
Realm realm,
17-
Prototype objectPrototype) : base(engine, realm)
18+
ObjectInstance objectPrototype) : base(engine, realm)
1819
{
1920
_prototype = objectPrototype;
2021
}
2122

2223
protected override void Initialize()
2324
{
24-
var symbols = new SymbolDictionary(1)
25+
var properties = new PropertyDictionary(12, checkExistingKeys: false)
2526
{
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),
2746
};
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), };
2851
SetSymbols(symbols);
2952
}
3053

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+
31147
private static JsValue ToIterator(JsValue thisObject, JsValue[] arguments)
32148
{
33149
return thisObject;

0 commit comments

Comments
 (0)