-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpolyfill.js
324 lines (284 loc) · 9.42 KB
/
polyfill.js
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
// polyfill promises using bluebird
// @ts-ignore
// Promise = require("bluebird");
const debug = false;
// Names are argument order are critical.
function augur_getResolveFor(p, augur_v) {
if (debug) console.log('augur_getResolveFor');
}
// Names and argument order are critical.
function augur_getRejectFor(p, augur_v) {
if (debug) console.log('augur_getRejectFor');
}
function augur_executingReaction(count, augur_v) {
if (debug) console.log('augur_executingReaction');
return augur_v;
}
function augur_executingRejection(count, augur_v) {
if (debug) console.log('augur_executingRejection');
return augur_v;
}
function augur_executingFinally(count, augur_v) {
if (debug) console.log('augur_executingFinally');
return augur_v;
}
const RealPromise = Promise;
let promiseCount = 0;
/*
Promise Redefinitions
*/
Promise = function(fun) {
const thisPromiseId = promiseCount++;
let wrappedFun = function(resolve, reject){
let wrappedResolve = function(augur_v){
if (debug) console.log('[Promise wrappedResolve]');
augur_getResolveFor(thisPromiseId, augur_v);
resolve(augur_v);
}
let wrappedReject = function(err){
if (debug) console.log('[Promise wrappedReject]');
augur_getRejectFor(thisPromiseId, augur_v);
reject(err);
}
fun(wrappedResolve, wrappedReject);
}
let p = new RealPromise(wrappedFun);
let returnMe = PromiseWrapper(p, thisPromiseId);
Object.defineProperty(returnMe, "augur_pid", {
enumerable: false,
writable: true
});
returnMe.augur_pid = thisPromiseId;
return returnMe;
}
/*
Static Promise Methods
*/
Promise.resolve = function(val) {
if (debug) console.log('[Promise.resolve]');
return new Promise(res => res(val));
}
Promise.reject = function(val) {
if (debug) console.log('[Promise.reject]');
return new Promise((res, rej) => rej(val));
}
Promise.all = function promiseAllIterative(values) {
if (debug) console.log('[Promise.all]');
let responses = [];
let errorResp = [];
return new Promise((resolve, reject) => {
/** Loop over promises array **/
if (promises.length > 0) {
promises.forEach(async (singlePromise, i) => {
try {
/** wait for resolving 1 promise **/
let res = await singlePromise;
responses.push(res);
if (i === promises.length - 1) {
if (errorResp.length > 0) {
reject(errorResp);
} else {
resolve(responses);
}
}
} catch (err) {
errorResp.push(err);
reject(err);
}
});
} else {
resolve([]);
}
});
}
Promise.allSettled = function(iterable) {
if (debug) console.log('[Promise.allSettled]');
return Promise.all(iterable.map(p => p
.then(value => ({ status: 'fulfilled', value})).
catch(reason => ({ status: 'rejected', reason }))));
}
Promise.any = function(iterable) {
if (debug) console.log('[Promise.any]');
return new Promise((resolve, reject) => {
let hasResolved = false;
let promiseLikes = [];
let iterableCount = 0;
let rejectionReasons = [];
function resolveOnce(value) {
if (!hasResolved) {
hasResolved = true;
resolve(value);
}
}
function rejectionCheck(reason) {
rejectionReasons.push(reason);
if (rejectionReasons.length >= iterableCount) reject(rejectionReasons);
}
for (let value of iterable) {
iterableCount++;
promiseLikes.push(value);
}
for (let promiseLike of promiseLikes) {
if (promiseLike.then !== undefined ||
promiseLike.catch !== undefined) {
promiseLike.then((result) => resolveOnce(result)).catch((error) => undefined);
promiseLike.catch((reason) => rejectionCheck(reason));
} else resolveOnce(promiseLike);
}
});
}
Promise.race = function(iterable) {
if (debug) console.log('[Promise.race]');
return new Promise((resolve, reject) => {
let hasResolved = false;
let promiseLikes = [];
let iterableCount = 0;
let rejectionReasons = [];
function resolveOnce(value) {
if (!hasResolved) {
hasResolved = true;
resolve(value);
}
}
for (let value of iterable) {
iterableCount++;
promiseLikes.push(value);
}
for (let promiseLike of promiseLikes) {
if (promiseLike.then !== undefined ||
promiseLike.catch !== undefined) {
promiseLike.then((result) => resolveOnce(result)).catch((error) => undefined);
promiseLike.catch((reason) => reject(reason));
} else resolveOnce(promiseLike);
}
});
}
function PromiseWrapper(p, currPromiseId){
let realThen = p.then;
let realCatch = p.catch;
let realFinally = p.finally;
// The return of this function must have all of the methods that promises
// should have.
return {
then: (f) => {
if (debug) console.log('[Promise.then]');
return new Promise((resolve, reject) => {
realThen.call(p, (augur_v) => {
resolve(f(augur_executingReaction(currPromiseId, augur_v)));
});
});
},
catch: (f) => {
if (debug) console.log('[Promise.catch]');
promiseCount++
let nextPromiseId = promiseCount;
return PromiseWrapper(realCatch.call(p, (augur_v) => {
augur_v = augur_executingRejection(currPromiseId, augur_v);
let result = f(augur_v);
// TODO change to getRejectFor
augur_getResolveFor(nextPromiseId, result)
return result;
}), nextPromiseId);
},
finally: (f) => {
if (debug) console.log('[Promise.finally]');
promiseCount++
let nextPromiseId = promiseCount;
return PromiseWrapper(realFinally.call(p, (augur_v) => {
augur_v = augur_executingFinally(currPromiseId, augur_v);
let result = f(augur_v);
// TODO change to getFinallyFor ...?
augur_getResolveFor(nextPromiseId, result)
return result;
}), nextPromiseId);
}
};
}
// Names are argument order are critical.
// function augur_getTaintFor(count, augur_v) {
// return augur_v;
// }
Array.prototype.forEach = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = this;
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t) {
fun.call(thisp, t[i], i, t);
}
}
};
/*
Old code, for posterity.
*/
// function PromiseWrapper(p: any, count: any){
// let realThen = p.then;
// return {
// then: (f: any) => {
// console.log("entering reaction for promise: " + count);
// let thenResult = realThen.call(p, (augur_v: any) => {
// augur_v = augur_getTaintFor(p, count, augur_v);
// return f(augur_v);
// });
// // Should invoke our patch.
// return new Promise((res, rej) => {
// res(thenResult);
// });
// }
// };
// }
/*
(<any> Promise) = function(fun: any) : any {
console.log("Promise " + promiseCount + " Created");
let wrappedFun = function(resolve: any,reject: any){
let wrappedResolve = function(augur_v: any){
console.log("entering wrapped resolve for promise: " + promiseCount);
augur_getResolveFor(promiseCount, augur_v);
resolve(augur_v);
}
let wrappedReject = function(err: any){
reject(err);
}
fun(wrappedResolve, wrappedReject);
}
let p = new RealPromise(wrappedFun);
return PromiseWrapper(p, promiseCount++);
}
(<any> Promise).resolve = function<T>(val : T) : Promise<T> {
console.log('Here!');
return new Promise((res, rej) => res(val));
}
*/
// Example shit.
// const p = new Promise((res, rej) => {
// res(2);
// });
// Array.slice
Array.prototype.slice = function(start, end) {
// If args weren't given, use defaults
if (!start) {
start = 0;
}
if (!end) {
end = this.length;
}
// If given indices are negative, convert them to positive equivalences
if (start < 0) {
start = this.length + start;
}
if (end < 0) {
end = this.length + end;
}
// Is it a valid slice?
if (start > end) {
throw `Array.prototype.slice: start (${start}) > end (${end})!`;
}
// Slice and dice
let result = [];
for (let i = start; i < end; i++) {
result.push(this[i]);
}
return result;
}