-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (246 loc) · 13.6 KB
/
index.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
const duktape = require('node-cmake')('duktype');
const compiler = require('./dist/TypescriptCompiler');
duktape.TypescriptCompiler = compiler.TypescriptCompiler;
duktape.Context.prototype.enableTimers = function()
{
if (this.timersEnabled)
{
return;
}
this.pendingTimers = {};
this.lastTimerID = 0;
let scope = this.getGlobalObject();
scope.setProperty('setTimeout', (cb, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'timeout',
timeout: setTimeout(() =>
{
delete this.pendingTimers[ourID];
cb.call();
}, parseInt(timeout, 10))
};
return ourID;
});
scope.setProperty('setInterval', (func, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'interval',
timeout: setInterval(() =>
{
func.call();
}, parseInt(timeout, 10))
};
return ourID;
});
scope.setProperty('setImmediate', (func, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'immediate',
timeout: setImmediate(() =>
{
delete this.pendingTimers[ourID];
func.call();
}, parseInt(timeout, 10))
};
return ourID;
});
scope.setProperty('clearImmediate', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'immediate')
{
clearTimeout(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
scope.setProperty('clearTimeout', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'timeout')
{
clearTimeout(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
scope.setProperty('clearInterval', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'interval')
{
clearInterval(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
scope = undefined;
this.timersEnabled = true;
};
duktape.Context.prototype.enablePromises = function()
{
if (this.promisesEnabled)
{
return;
}
if (!this.timersEnabled)
{
this.enableTimers();
}
this.eval(`!function(e){"object"==typeof exports&&"undefined"!=typeof module||"function"!=typeof define||!define.amd?e():define(e)}(function(){"use strict";function e(e){var n=this.constructor;return this.then(function(t){return n.resolve(e()).then(function(){return t})},function(t){return n.resolve(e()).then(function(){return n.reject(t)})})}var n=setTimeout;function t(e){return e&&void 0!==e.length}function o(){}function r(e){if(!(this instanceof r))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],a(e,this)}function i(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,r._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var o;try{o=t(e._value)}catch(s){return void u(n.promise,s)}f(n.promise,o)}else(1===e._state?f:u)(n.promise,e._value)})):e._deferreds.push(n)}function f(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var t=n.then;if(n instanceof r)return e._state=3,e._value=n,void c(e);if("function"==typeof t)return void a(function(e,n){return function(){e.apply(n,arguments)}}(t,n),e)}e._state=1,e._value=n,c(e)}catch(i){u(e,i)}}function u(e,n){e._state=2,e._value=n,c(e)}function c(e){2===e._state&&0===e._deferreds.length&&r._immediateFn(function(){e._handled||r._unhandledRejectionFn(e._value)});for(var n=0,t=e._deferreds.length;n<t;n++)i(e,e._deferreds[n]);e._deferreds=null}function a(e,n){var t=!1;try{e(function(e){t||(t=!0,f(n,e))},function(e){t||(t=!0,u(n,e))})}catch(o){if(t)return;t=!0,u(n,o)}}r.prototype.catch=function(e){return this.then(null,e)},r.prototype.then=function(e,n){var t=new this.constructor(o);return i(this,new function(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}(e,n,t)),t},r.prototype.finally=e,r.all=function(e){return new r(function(n,r){if(!t(e))return r(new TypeError("Promise.all accepts an array"));var i=Array.prototype.slice.call(e);if(0===i.length)return n([]);var f=i.length;function u(e,t){try{if(t&&("object"==typeof t||"function"==typeof t)){var c=t.then;if("function"==typeof c)return void c.call(t,function(n){u(e,n)},r)}i[e]=t,0==--f&&n(i)}catch(o){r(o)}}for(var c=0;c<i.length;c++)u(c,i[c])})},r.resolve=function(e){return e&&"object"==typeof e&&e.constructor===r?e:new r(function(n){n(e)})},r.reject=function(e){return new r(function(n,t){t(e)})},r.race=function(e){return new r(function(n,o){if(!t(e))return o(new TypeError("Promise.race accepts an array"));for(var i=0,f=e.length;i<f;i++)r.resolve(e[i]).then(n,o)})},r._immediateFn="function"==typeof setImmediate?function(e){setImmediate(e)}:function(e){n(e,0)},r._unhandledRejectionFn=function(e){void 0!==console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var s=new Function("return this;")();"function"!=typeof s.Promise?s.Promise=r:s.Promise.prototype.finally||(s.Promise.prototype.finally=e)});`);
this.promisesEnabled = true;
};
duktape.AsyncContext.prototype.enableTimers = async function()
{
if (this.timersEnabled)
{
return;
}
this.pendingTimers = {};
this.lastTimerID = 0;
let scope = await this.getGlobalObject();
await scope.setProperty('setTimeout', (cb, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'timeout',
timeout: setTimeout(() =>
{
delete this.pendingTimers[ourID];
cb.call();
}, parseInt(timeout, 10))
};
return ourID;
});
await scope.setProperty('setInterval', (func, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'interval',
timeout: setInterval(() =>
{
func.call();
}, parseInt(timeout, 10))
};
return ourID;
});
await scope.setProperty('setImmediate', (func, timeout) =>
{
const ourID = this.lastTimerID++;
this.pendingTimers[ourID] = {
type: 'immediate',
timeout: setImmediate(() =>
{
delete this.pendingTimers[ourID];
func.call();
}, parseInt(timeout, 10))
};
return ourID;
});
await scope.setProperty('clearImmediate', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'immediate')
{
clearTimeout(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
await scope.setProperty('clearTimeout', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'timeout')
{
clearTimeout(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
await scope.setProperty('clearInterval', (ref) =>
{
const refSan = parseInt(ref, 10);
if (this.pendingTimers[refSan] !== undefined && this.pendingTimers[refSan].type === 'interval')
{
clearInterval(this.pendingTimers[refSan].timeout);
delete this.pendingTimers[refSan];
}
});
scope = undefined;
this.timersEnabled = true;
};
duktape.AsyncContext.prototype.enablePromises = async function()
{
if (this.promisesEnabled)
{
return;
}
if (!this.timersEnabled)
{
await this.enableTimers();
}
await this.eval(`!function(e){"object"==typeof exports&&"undefined"!=typeof module||"function"!=typeof define||!define.amd?e():define(e)}(function(){"use strict";function e(e){var n=this.constructor;return this.then(function(t){return n.resolve(e()).then(function(){return t})},function(t){return n.resolve(e()).then(function(){return n.reject(t)})})}var n=setTimeout;function t(e){return e&&void 0!==e.length}function o(){}function r(e){if(!(this instanceof r))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],a(e,this)}function i(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,r._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var o;try{o=t(e._value)}catch(s){return void u(n.promise,s)}f(n.promise,o)}else(1===e._state?f:u)(n.promise,e._value)})):e._deferreds.push(n)}function f(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var t=n.then;if(n instanceof r)return e._state=3,e._value=n,void c(e);if("function"==typeof t)return void a(function(e,n){return function(){e.apply(n,arguments)}}(t,n),e)}e._state=1,e._value=n,c(e)}catch(i){u(e,i)}}function u(e,n){e._state=2,e._value=n,c(e)}function c(e){2===e._state&&0===e._deferreds.length&&r._immediateFn(function(){e._handled||r._unhandledRejectionFn(e._value)});for(var n=0,t=e._deferreds.length;n<t;n++)i(e,e._deferreds[n]);e._deferreds=null}function a(e,n){var t=!1;try{e(function(e){t||(t=!0,f(n,e))},function(e){t||(t=!0,u(n,e))})}catch(o){if(t)return;t=!0,u(n,o)}}r.prototype.catch=function(e){return this.then(null,e)},r.prototype.then=function(e,n){var t=new this.constructor(o);return i(this,new function(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}(e,n,t)),t},r.prototype.finally=e,r.all=function(e){return new r(function(n,r){if(!t(e))return r(new TypeError("Promise.all accepts an array"));var i=Array.prototype.slice.call(e);if(0===i.length)return n([]);var f=i.length;function u(e,t){try{if(t&&("object"==typeof t||"function"==typeof t)){var c=t.then;if("function"==typeof c)return void c.call(t,function(n){u(e,n)},r)}i[e]=t,0==--f&&n(i)}catch(o){r(o)}}for(var c=0;c<i.length;c++)u(c,i[c])})},r.resolve=function(e){return e&&"object"==typeof e&&e.constructor===r?e:new r(function(n){n(e)})},r.reject=function(e){return new r(function(n,t){t(e)})},r.race=function(e){return new r(function(n,o){if(!t(e))return o(new TypeError("Promise.race accepts an array"));for(var i=0,f=e.length;i<f;i++)r.resolve(e[i]).then(n,o)})},r._immediateFn="function"==typeof setImmediate?function(e){setImmediate(e)}:function(e){n(e,0)},r._unhandledRejectionFn=function(e){void 0!==console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var s=new Function("return this;")();"function"!=typeof s.Promise?s.Promise=r:s.Promise.prototype.finally||(s.Promise.prototype.finally=e)});`);
this.promisesEnabled = true;
};
duktape.AsyncContext.prototype.disableTimers = async function()
{
const tmrs = Object.keys(this.pendingTimers);
if (tmrs.length > 0)
{
for (const tmrKey of tmrs)
{
const tmr = this.pendingTimers[tmrKey];
switch(tmr.type)
{
case 'immediate':
clearImmediate(tmr.timeout);
break;
case 'timeout':
clearTimeout(tmr.timeout);
break;
case 'interval':
clearInterval(tmr.timeout);
break;
}
delete this.pendingTimers[tmrKey];
}
}
const scope = await this.getGlobalObject();
await scope.deleteProperty('setTimeout');
await scope.deleteProperty('setInterval');
await scope.deleteProperty('setImmediate');
await scope.deleteProperty('clearTimeout');
await scope.deleteProperty('clearInterval');
await scope.deleteProperty('clearImmediate');
}
duktape.Context.prototype.disableTimers = function()
{
const tmrs = Object.keys(this.pendingTimers);
if (tmrs.length > 0)
{
for (const tmrKey of tmrs)
{
const tmr = this.pendingTimers[tmrKey];
switch(tmr.type)
{
case 'immediate':
clearImmediate(tmr.timeout);
break;
case 'timeout':
clearTimeout(tmr.timeout);
break;
case 'interval':
clearInterval(tmr.timeout);
break;
}
delete this.pendingTimers[tmrKey];
}
}
const scope = this.getGlobalObject();
scope.deleteProperty('setTimeout');
scope.deleteProperty('setInterval');
scope.deleteProperty('setImmediate');
scope.deleteProperty('clearTimeout');
scope.deleteProperty('clearInterval');
scope.deleteProperty('clearImmediate');
}
module.exports = duktape;