@@ -115,10 +115,11 @@ Object.defineProperty( Array.prototype, 'join', {
115
115
} ( Array . prototype . join ) )
116
116
} ) ;
117
117
118
+
118
119
/* we define it outside so it'll not be in strict mode */
119
120
var exec = function ( code ) {
120
121
return eval ( 'undefined;\n' + code ) ;
121
- }
122
+ } ;
122
123
var console = {
123
124
_items : [ ] ,
124
125
log : function ( ) {
@@ -127,30 +128,51 @@ var console = {
127
128
} ;
128
129
console . error = console . info = console . debug = console . log ;
129
130
130
- ( function ( ) {
131
+ ( function ( ) {
131
132
"use strict" ;
132
133
133
134
global . onmessage = function ( event ) {
134
- postMessage ( {
135
+ global . postMessage ( {
135
136
event : 'start'
136
137
} ) ;
137
138
138
139
var jsonStringify = JSON . stringify , /*backup*/
139
- result ;
140
+ result ,
141
+
142
+ originalSetTimeout = setTimeout ,
143
+ timeoutCounter = 0 ;
144
+
145
+ var sendResult = function ( result ) {
146
+ global . postMessage ( {
147
+ answer : jsonStringify ( result , reviver ) ,
148
+ log : jsonStringify ( console . _items , reviver ) . slice ( 1 , - 1 )
149
+ } ) ;
150
+ } ;
151
+ var done = function ( result ) {
152
+ if ( timeoutCounter < 1 ) {
153
+ sendResult ( result ) ;
154
+ }
155
+ } ;
140
156
141
- try {
142
- result = exec ( event . data ) ;
143
- }
144
- catch ( e ) {
145
- result = e . toString ( ) ;
146
- }
157
+ var reviver = function ( key , value ) {
158
+ var output ;
147
159
148
- /*JSON does not like any of the following*/
160
+ if ( shouldString ( value ) ) {
161
+ output = '' + value ;
162
+ }
163
+ else {
164
+ output = value ;
165
+ }
166
+
167
+ return output ;
168
+ } ;
169
+
170
+ /*JSON does not like any of the following*/
149
171
var strung = {
150
172
Function : true , Error : true ,
151
173
Undefined : true , RegExp : true
152
174
} ;
153
- var should_string = function ( value ) {
175
+ var shouldString = function ( value ) {
154
176
var type = ( { } ) . toString . call ( value ) . slice ( 8 , - 1 ) ;
155
177
156
178
if ( type in strung ) {
@@ -160,22 +182,34 @@ console.error = console.info = console.debug = console.log;
160
182
return value !== value || value === Infinity ;
161
183
} ;
162
184
163
- var reviver = function ( key , value ) {
164
- var output ;
185
+ self . setTimeout = function ( cb ) {
186
+ var args = [ ] . slice . call ( arguments ) ;
187
+ args [ 0 ] = wrapper ;
188
+ timeoutCounter += 1 ;
165
189
166
- if ( should_string ( value ) ) {
167
- output = '' + value ;
168
- }
169
- else {
170
- output = value ;
171
- }
190
+ originalSetTimeout . apply ( self , args ) ;
172
191
173
- return output ;
174
- } ;
192
+ function wrapper ( ) {
193
+ timeoutCounter -= 1 ;
194
+ cb . apply ( self , arguments ) ;
175
195
176
- postMessage ( {
177
- answer : jsonStringify ( result , reviver ) ,
178
- log : jsonStringify ( console . _items , reviver ) . slice ( 1 , - 1 )
179
- } ) ;
196
+ done ( ) ;
197
+ }
198
+ } ;
199
+
200
+ try {
201
+ result = exec ( event . data ) ;
202
+ }
203
+ catch ( e ) {
204
+ result = e . toString ( ) ;
205
+ }
206
+
207
+ /*handle promises appropriately*/
208
+ if ( result . then && result . catch ) {
209
+ result . then ( done ) . catch ( done ) ;
210
+ }
211
+ else {
212
+ done ( result ) ;
213
+ }
180
214
} ;
181
215
} ) ( ) ;
0 commit comments