summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/ReadableStream/readable-stream-globals.js
blob: 37e73d5cbf7b4d89e82f809b1fb835707f791dca (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// |reftest| skip-if(!this.hasOwnProperty("ReadableStream"))

if ("ignoreUnhandledRejections" in this) {
  ignoreUnhandledRejections();
}

async function test() {
    if (typeof newGlobal !== 'undefined') {
        otherGlobal = newGlobal();
    }

    OtherReadableStream = otherGlobal.ReadableStream;

    ReadableStreamReader = new ReadableStream().getReader().constructor;
    OtherReadableStreamReader = new otherGlobal.ReadableStream().getReader().constructor;

    let byteStreamsSupported = false;
    try {
        let controller;
        let reader = new ReadableStream({
            start(c) {
                ByteStreamController = c.constructor;
                controller = c;
            },
            type: "bytes"
        }).getReader({ mode: "byob" })
        ReadableStreamBYOBReader = reader.constructor;
        reader.read(new Uint8Array(10));
        BYOBRequest = controller.byobRequest.constructor;
        reader = new otherGlobal.ReadableStream({
            start(c) {
                OtherByteStreamController = c.constructor;
                controller = c;
            },
            type: "bytes"
        }).getReader({ mode: "byob" });
        OtherReadableStreamBYOBReader = reader.constructor;
        reader.read(new Uint8Array(10));
        OtherBYOBRequest = controller.byobRequest.constructor;

        BYOBRequestGetter = Object.getOwnPropertyDescriptor(ByteStreamController.prototype,
                                                            "byobRequest").get;
        OtherBYOBRequestGetter = Object.getOwnPropertyDescriptor(OtherByteStreamController.prototype,
                                                                 "byobRequest").get;

        byteStreamsSupported = true;
    } catch (e) {
    }

    let chunk = { name: "chunk" };
    let enqueuedError = { name: "enqueuedError" };

    let controller;
    let stream;
    let otherStream;
    let otherController;
    let reader;
    let otherReader;

    function getFreshInstances(type, otherType = type) {
        stream = new ReadableStream({ start(c) { controller = c; }, type });

        otherStream = new OtherReadableStream({ start(c) { otherController = c; }, type: otherType });
    }

    getFreshInstances();

    Controller = controller.constructor;
    OtherController = otherController.constructor;


    otherReader = OtherReadableStream.prototype.getReader.call(stream);
    assertEq(otherReader instanceof ReadableStreamReader, false);
    assertEq(otherReader instanceof OtherReadableStreamReader, true);
    assertEq(otherController instanceof Controller, false);

    assertEq(stream.locked, true);
    Object.defineProperty(stream, "locked",
        Object.getOwnPropertyDescriptor(OtherReadableStream.prototype, "locked"));
    assertEq(stream.locked, true);


    request = otherReader.read();
    assertEq(request instanceof otherGlobal.Promise, true);
    controller.close();
    assertEq(await request instanceof Object, true);

    getFreshInstances();
    otherReader = new OtherReadableStreamReader(stream);

    getFreshInstances();
    otherReader = new OtherReadableStreamReader(stream);
    let cancelSucceeded = false;
    let cancelPromise = ReadableStreamReader.prototype.cancel.call(otherReader);
    assertEq(cancelPromise instanceof Promise, true);
    assertEq(await cancelPromise, undefined);

    getFreshInstances();
    otherReader = new OtherReadableStreamReader(stream);
    let closeSucceeded = false;
    Object.defineProperty(otherReader, "closed",
        Object.getOwnPropertyDescriptor(ReadableStreamReader.prototype, "closed"));
    let closedPromise = otherReader.closed;
    assertEq(closedPromise instanceof otherGlobal.Promise, true);
    controller.close();
    assertEq(await closedPromise, undefined);

    getFreshInstances();

    otherReader = OtherReadableStream.prototype.getReader.call(stream);
    request = otherReader.read();
    assertEq(request instanceof otherGlobal.Promise, true);
    otherController.close.call(controller);
    assertEq(await request instanceof otherGlobal.Object, true);

    getFreshInstances();

    assertEq(controller.desiredSize, 1);
    Object.defineProperty(controller, "desiredSize",
        Object.getOwnPropertyDescriptor(OtherController.prototype, "desiredSize"));
    assertEq(controller.desiredSize, 1);


    request = otherReader.read();

    controller.error(enqueuedError);

    expectException(() => controller.close(), TypeError);
    expectException(() => otherController.close.call(controller), otherGlobal.TypeError);

    otherReader.releaseLock();

    reader = stream.getReader();
    assertEq(await expectAsyncException(async () => reader.read(), enqueuedError.constructor),
             enqueuedError);

    otherReader.releaseLock.call(reader);
    assertEq(reader.closed instanceof otherGlobal.Promise, true);

    // getFreshInstances();

    // reader = stream.getReader();
    // request = otherReader.read.call(reader);
    // assertEq(request instanceof otherGlobal.Promise, true);
    // controller.enqueue(chunk);
    // assertEq((await request).value, chunk);

    // reader.releaseLock();

    // getFreshInstances();

    // reader = stream.getReader();
    // request = otherReader.read.call(reader);
    // otherController.enqueue.call(controller, chunk);
    // otherController.enqueue.call(controller, new otherGlobal.Uint8Array(10));
    // controller.enqueue(new otherGlobal.Uint8Array(10));
    // request = otherReader.read.call(reader);

    getFreshInstances();

    stream = new ReadableStream({ start(c) { controller = c; } }, { size() {return 1} });
    otherController.enqueue.call(controller, chunk);
    otherController.enqueue.call(controller, new otherGlobal.Uint8Array(10));
    controller.enqueue(new otherGlobal.Uint8Array(10));

    getFreshInstances();

    controller.close();
    expectException(() => controller.enqueue(new otherGlobal.Uint8Array(10)), TypeError);
    expectException(() => otherController.enqueue.call(controller, chunk), otherGlobal.TypeError);
    expectException(() => otherController.enqueue.call(controller, new otherGlobal.Uint8Array(10)),
                    otherGlobal.TypeError);

    getFreshInstances();

    let [branch1, branch2] = otherGlobal.ReadableStream.prototype.tee.call(stream);
    assertEq(branch1 instanceof otherGlobal.ReadableStream, true);
    assertEq(branch2 instanceof otherGlobal.ReadableStream, true);

    controller.enqueue(chunk);
    reader = branch1.getReader();
    result = await reader.read();
    reader.releaseLock();
    let subPromiseCreated = false;
    let speciesInvoked = false;
    class SubPromise extends Promise {
        constructor(executor) {
            super(executor);
            subPromiseCreated = true;
        }
    }
    Object.defineProperty(Promise, Symbol.species, {get: function() {
        speciesInvoked = true;
        return SubPromise;
    }
    });

    otherGlobal.eval(`
    subPromiseCreated = false;
    speciesInvoked = false;
    class OtherSubPromise extends Promise {
        constructor(executor) {
            super(executor);
            subPromiseCreated = true;
        }
    }
    Object.defineProperty(Promise, Symbol.species, {get: function() {
        speciesInvoked = true;
        return OtherSubPromise;
    }
    });`);

    controller.error(enqueuedError);
    subPromiseCreated = false;
    speciesInvoked = false;
    otherGlobal.subPromiseCreated = false;
    otherGlobal.speciesInvoked = false;
    let cancelPromise1 = branch1.cancel({ name: "cancel 1" });
    assertEq(cancelPromise1 instanceof otherGlobal.Promise, true);
    assertEq(subPromiseCreated, false);
    assertEq(speciesInvoked, false);
    assertEq(otherGlobal.subPromiseCreated, false);
    assertEq(otherGlobal.speciesInvoked, false);
    subPromiseCreated = false;
    speciesInvoked = false;
    otherGlobal.subPromiseCreated = false;
    otherGlobal.speciesInvoked = false;
    let cancelPromise2 = branch2.cancel({ name: "cancel 2" });
    assertEq(cancelPromise2 instanceof otherGlobal.Promise, true);
    assertEq(subPromiseCreated, false);
    assertEq(speciesInvoked, false);
    assertEq(otherGlobal.subPromiseCreated, false);
    assertEq(otherGlobal.speciesInvoked, false);
    await 1;


    getFreshInstances();

    [branch1, branch2] = otherGlobal.ReadableStream.prototype.tee.call(stream);
    assertEq(branch1 instanceof otherGlobal.ReadableStream, true);
    assertEq(branch2 instanceof otherGlobal.ReadableStream, true);

    controller.enqueue(chunk);
    reader = branch1.getReader();
    result = await reader.read();
    reader.releaseLock();


    assertEq(result.value, chunk);

    controller.error(enqueuedError);
    subPromiseCreated = false;
    speciesInvoked = false;
    otherGlobal.subPromiseCreated = false;
    otherGlobal.speciesInvoked = false;
    cancelPromise1 = ReadableStream.prototype.cancel.call(branch1, { name: "cancel 1" });
    assertEq(cancelPromise1 instanceof Promise, true);
    assertEq(subPromiseCreated, false);
    assertEq(speciesInvoked, false);
    assertEq(otherGlobal.subPromiseCreated, false);
    assertEq(otherGlobal.speciesInvoked, false);
    subPromiseCreated = false;
    speciesInvoked = false;
    otherGlobal.subPromiseCreated = false;
    otherGlobal.speciesInvoked = false;
    cancelPromise2 = ReadableStream.prototype.cancel.call(branch2, { name: "cancel 2" });
    assertEq(cancelPromise2 instanceof Promise, true);
    assertEq(subPromiseCreated, false);
    assertEq(speciesInvoked, false);
    assertEq(otherGlobal.subPromiseCreated, false);
    assertEq(otherGlobal.speciesInvoked, false);

    if (!byteStreamsSupported) {
        return;
    }

    if (typeof nukeCCW === 'function') {
        getFreshInstances("bytes");
        assertEq(otherController instanceof OtherByteStreamController, true);
        reader = ReadableStream.prototype.getReader.call(otherStream);
        otherGlobal.reader = reader;
        otherGlobal.nukeCCW(otherGlobal.reader);
        let chunk = new Uint8Array(10);
        expectException(() => otherController.enqueue(chunk), otherGlobal.TypeError);
        // otherController.error();
        expectException(() => reader.read(), TypeError);
    }

    function testBYOBRequest(controller, view) {
        const request = new BYOBRequest(controller, view);
        let storedView = request.view;
        assertEq(storedView, view);
        storedView = Object.getOwnPropertyDescriptor(OtherBYOBRequest.prototype, "view").get.call(request);
        assertEq(storedView, view);
        request.respond(10);
        OtherBYOBRequest.prototype.respond.call(request, 10);
        request.respondWithNewView(new view.constructor(10));
        OtherBYOBRequest.prototype.respondWithNewView.call(request, new view.constructor(10));
    }

    expectException(() => new BYOBRequest(), TypeError);
    getFreshInstances("bytes");
    expectException(() => new BYOBRequest(controller, new Uint8Array(10)), TypeError);
    expectException(() => new BYOBRequest(otherController, new Uint8Array(10)), TypeError);
    expectException(() => new BYOBRequest(otherController, new Uint8Array(10)), TypeError);
    expectException(() => new BYOBRequest(otherController, new otherGlobal.Uint8Array(10)), TypeError);

    getFreshInstances("bytes");

    reader = stream.getReader({ mode: "byob" });
    request = OtherReadableStreamBYOBReader.prototype.read.call(reader, new Uint8Array(10));
    assertEq(request instanceof otherGlobal.Promise, true);
    controller.enqueue(new Uint8Array([1, 2, 3, 4]));
    result = await request;

    getFreshInstances("bytes");

    reader = stream.getReader({ mode: "byob" });
    request = OtherReadableStreamBYOBReader.prototype.read.call(reader, new Uint8Array(10));
    assertEq(request instanceof otherGlobal.Promise, true);
    try {
        let byobRequest = OtherBYOBRequestGetter.call(controller);
    } catch (e) {
        print(e, '\n', e.stack);
    }
    controller.enqueue(new Uint8Array([1, 2, 3, 4]));
    result = await request;

    await 1;
}

function expectException(closure, errorType) {
    let error;
    try {
        closure();
    } catch (e) {
        error = e;
    }
    assertEq(error !== undefined, true);
    assertEq(error.constructor, errorType);
    return error;
}

async function expectAsyncException(closure, errorType) {
    let error;
    try {
        await closure();
    } catch (e) {
        error = e;
    }
    assertEq(error !== undefined, true);
    assertEq(error.constructor, errorType);
    return error;
}

async function runTest() {
    try {
        await test();
    } catch (e) {
        assertEq(false, true, `Unexpected exception ${e}\n${e.stack}`);
    }
    console.log("done");
    if (typeof reportCompare === "function")
        reportCompare(true, true);
}

runTest();