2 <script src='../resources/testharness.js'
></script>
3 <script src='../resources/testharnessreport.js'
></script>
6 const PromiseBackup
= Promise
;
9 Promise = function() { assert_unreached("streams should not use this Promise object"); };
14 Promise
= PromiseBackup
;
16 }, 'Streams and promises: replace Promise constructor');
19 const PromiseResolveBackup
= Promise
.resolve
;
22 Promise
.resolve = function() { assert_unreached("streams should not use this Promise.resolve method"); };
27 Promise
.resolve
= PromiseResolveBackup
;
29 }, 'Streams and promises: replace Promise.resolve');
32 const PromiseRejectBackup
= Promise
.reject
;
37 Promise
.reject = function() { assert_unreached("streams should not use this Promise.reject method"); };
39 ReadableStream
.prototype.cancel
.call({}, "reason").then(handle
, handle
);
40 WritableStream
.prototype.abort
.call({}, "reason").then(handle
, handle
);
42 Promise
.reject
= PromiseRejectBackup
;
44 }, 'Streams and promises: replace Promise.reject');
47 function createMangledPromise() {
48 const promise
= Promise
.resolve();
49 Object
.setPrototypeOf(promise
, { constructor: Promise
, then: function() { assert_unreached("streams should not use this promise then method"); } });
52 new ReadableStream({ start: function() { return createMangledPromise(); } })
53 new WritableStream({ start: function() { return createMangledPromise(); } })
54 }, 'Streams and promises: replace prototype of a promise object');
57 const PromiseThenBackup
= Promise
.prototype.then
;
60 Promise
.prototype.then = function() { assert_unreached("streams should not use this Promise.prototype.then method"); };
65 Promise
.prototype.then
= PromiseThenBackup
;
67 }, 'Streams and promises: replace then method in Promise prototype');
70 const PromiseCatchBackup
= Promise
.prototype.catch;
71 const PromiseThenBackup
= Promise
.prototype.then
;
74 Promise
.prototype.catch = function() { assert_unreached("streams should not use this Promise.prototype.catch method"); };
75 Promise
.prototype.then = function() { assert_unreached("streams should not use this Promise.prototype.catch method"); };
77 const rs
= new ReadableStream();
80 Promise
.prototype.catch = PromiseCatchBackup
;
81 Promise
.prototype.then
= PromiseThenBackup
;
83 }, 'Streams and promises: replace catch method in Promise prototype');
86 function createMangledPromise() {
87 const promise
= Promise
.resolve();
88 promise
.then = function() { assert_unreached("streams should not use this promise then method"); };
91 new ReadableStream({ start: function() { return createMangledPromise(); } })
92 new WritableStream({ start: function() { return createMangledPromise(); } })
93 }, 'Streams and promises: replace then method in promise object');
96 const NumberBackup
= Number
;
97 const NumberIsNaNBackup
= Number
.isNaN
;
98 const NumberIsFiniteBackup
= Number
.isFinite
;
101 Number
.isNaN = function() { assert_unreached("streams should not use this Number.isNaN method"); };
102 Number
.isFinite = function() { assert_unreached("streams should not use this Number.isFinite method"); };
106 start: function(controller
) {
107 controller
.enqueue("small potato");
110 size: function(chunk
) { return 2; },
115 Number
= NumberBackup
;
116 Number
.isNaN
= NumberIsNaNBackup
;
117 Number
.isFinite
= NumberIsFiniteBackup
;
119 }, 'Streams should not directly use Number and related methods');
121 const ReadableStreamGetReaderBackup
= ReadableStream
.prototype.getReader
;
124 ReadableStream
.prototype.getReader = function() { assert_unreached("streams should not use this ReadableStream.getReader method"); };
125 new ReadableStream().tee();
127 ReadableStream
.prototype.getReader
= ReadableStreamGetReaderBackup
;
129 }, 'Streams should not directly use ReadableStream public APIs');
131 promise_test(function() {
132 const ReadableStreamDefaultReader
= new ReadableStream().getReader().constructor;
133 const ReadableStreamDefaultReaderReadBackup
= ReadableStreamDefaultReader
.prototype.read
;
135 function cleanTest() {
136 ReadableStreamDefaultReader
.prototype.read
= ReadableStreamDefaultReaderReadBackup
;
140 ReadableStreamDefaultReader
.prototype.read = function() { assert_unreached("streams should not use this ReadableStreamDefaultReader.read method"); };
142 [s1
, s2
] = new ReadableStream({
143 start: function(controller
) {
147 return s1
.getReader().closed
.then(cleanTest
, cleanTest
);
151 assert_unreached("test should not throw");
153 }, 'Streams should not directly use ReadableStreamDefaultReader read public API');