1
- package prelude
2
-
3
- const goroutines = `
4
1
var $stackDepthOffset = 0 ;
5
2
var $getStackDepth = function ( ) {
6
3
var err = new Error ( ) ;
@@ -10,7 +7,8 @@ var $getStackDepth = function() {
10
7
return $stackDepthOffset + err . stack . split ( "\n" ) . length ;
11
8
} ;
12
9
13
- var $panicStackDepth = null, $panicValue;
10
+ var $panicStackDepth = null ,
11
+ $panicValue ;
14
12
var $callDeferred = function ( deferred , jsErr , fromPanic ) {
15
13
if ( ! fromPanic && deferred !== null && deferred . index >= $curGoroutine . deferStack . length ) {
16
14
throw jsErr ;
@@ -109,10 +107,20 @@ var $recover = function() {
109
107
$panicStackDepth = null ;
110
108
return $panicValue ;
111
109
} ;
112
- var $throw = function(err) { throw err; };
110
+ var $throw = function ( err ) {
111
+ throw err ;
112
+ } ;
113
113
114
- var $noGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
115
- var $curGoroutine = $noGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0, $checkForDeadlock = true;
114
+ var $noGoroutine = {
115
+ asleep : false ,
116
+ exit : false ,
117
+ deferStack : [ ] ,
118
+ panicStack : [ ] ,
119
+ } ;
120
+ var $curGoroutine = $noGoroutine ,
121
+ $totalGoroutines = 0 ,
122
+ $awakeGoroutines = 0 ,
123
+ $checkForDeadlock = true ;
116
124
var $mainFinished = false ;
117
125
var $go = function ( fun , args ) {
118
126
$totalGoroutines ++ ;
@@ -122,7 +130,9 @@ var $go = function(fun, args) {
122
130
$curGoroutine = $goroutine ;
123
131
var r = fun . apply ( undefined , args ) ;
124
132
if ( r && r . $blk !== undefined ) {
125
- fun = function() { return r.$blk(); };
133
+ fun = function ( ) {
134
+ return r . $blk ( ) ;
135
+ } ;
126
136
args = [ ] ;
127
137
return ;
128
138
}
@@ -133,7 +143,8 @@ var $go = function(fun, args) {
133
143
}
134
144
} finally {
135
145
$curGoroutine = $noGoroutine ;
136
- if ($goroutine.exit) { /* also set by runtime.Goexit() */
146
+ if ( $goroutine . exit ) {
147
+ /* also set by runtime.Goexit() */
137
148
$totalGoroutines -- ;
138
149
$goroutine . asleep = true ;
139
150
}
@@ -222,7 +233,7 @@ var $send = function(chan, value) {
222
233
if ( closedDuringSend ) {
223
234
$throwRuntimeError ( "send on closed channel" ) ;
224
235
}
225
- }
236
+ } ,
226
237
} ;
227
238
} ;
228
239
var $recv = function ( chan ) {
@@ -239,7 +250,11 @@ var $recv = function(chan) {
239
250
}
240
251
241
252
var thisGoroutine = $curGoroutine ;
242
- var f = { $blk: function() { return this.value; } };
253
+ var f = {
254
+ $blk : function ( ) {
255
+ return this . value ;
256
+ } ,
257
+ } ;
243
258
var queueEntry = function ( v ) {
244
259
f . value = v ;
245
260
$schedule ( thisGoroutine ) ;
@@ -275,22 +290,22 @@ var $select = function(comms) {
275
290
var comm = comms [ i ] ;
276
291
var chan = comm [ 0 ] ;
277
292
switch ( comm . length ) {
278
- case 0: /* default */
279
- selection = i;
280
- break;
281
- case 1: /* recv */
282
- if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
283
- ready.push(i);
284
- }
285
- break;
286
- case 2: /* send */
287
- if (chan.$closed) {
288
- $throwRuntimeError("send on closed channel");
289
- }
290
- if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
291
- ready.push(i);
292
- }
293
- break;
293
+ case 0 /* default */ :
294
+ selection = i ;
295
+ break ;
296
+ case 1 /* recv */ :
297
+ if ( chan . $sendQueue . length !== 0 || chan . $buffer . length !== 0 || chan . $closed ) {
298
+ ready . push ( i ) ;
299
+ }
300
+ break ;
301
+ case 2 /* send */ :
302
+ if ( chan . $closed ) {
303
+ $throwRuntimeError ( "send on closed channel" ) ;
304
+ }
305
+ if ( chan . $recvQueue . length !== 0 || chan . $buffer . length < chan . $capacity ) {
306
+ ready . push ( i ) ;
307
+ }
308
+ break ;
294
309
}
295
310
}
296
311
@@ -300,19 +315,23 @@ var $select = function(comms) {
300
315
if ( selection !== - 1 ) {
301
316
var comm = comms [ selection ] ;
302
317
switch ( comm . length ) {
303
- case 0: /* default */
304
- return [selection];
305
- case 1: /* recv */
306
- return [selection, $recv(comm[0])];
307
- case 2: /* send */
308
- $send(comm[0], comm[1]);
309
- return [selection];
318
+ case 0 /* default */ :
319
+ return [ selection ] ;
320
+ case 1 /* recv */ :
321
+ return [ selection , $recv ( comm [ 0 ] ) ] ;
322
+ case 2 /* send */ :
323
+ $send ( comm [ 0 ] , comm [ 1 ] ) ;
324
+ return [ selection ] ;
310
325
}
311
326
}
312
327
313
328
var entries = [ ] ;
314
329
var thisGoroutine = $curGoroutine ;
315
- var f = { $blk: function() { return this.selection; } };
330
+ var f = {
331
+ $blk : function ( ) {
332
+ return this . selection ;
333
+ } ,
334
+ } ;
316
335
var removeFromQueues = function ( ) {
317
336
for ( var i = 0 ; i < entries . length ; i ++ ) {
318
337
var entry = entries [ i ] ;
@@ -327,32 +346,31 @@ var $select = function(comms) {
327
346
( function ( i ) {
328
347
var comm = comms [ i ] ;
329
348
switch ( comm . length ) {
330
- case 1: /* recv */
331
- var queueEntry = function(value) {
332
- f.selection = [i, value];
333
- removeFromQueues();
334
- $schedule(thisGoroutine);
335
- };
336
- entries.push([comm[0].$recvQueue, queueEntry]);
337
- comm[0].$recvQueue.push(queueEntry);
338
- break;
339
- case 2: /* send */
340
- var queueEntry = function() {
341
- if (comm[0].$closed) {
342
- $throwRuntimeError("send on closed channel");
343
- }
344
- f.selection = [i];
345
- removeFromQueues();
346
- $schedule(thisGoroutine);
347
- return comm[1];
348
- };
349
- entries.push([comm[0].$sendQueue, queueEntry]);
350
- comm[0].$sendQueue.push(queueEntry);
351
- break;
349
+ case 1 /* recv */ :
350
+ var queueEntry = function ( value ) {
351
+ f . selection = [ i , value ] ;
352
+ removeFromQueues ( ) ;
353
+ $schedule ( thisGoroutine ) ;
354
+ } ;
355
+ entries . push ( [ comm [ 0 ] . $recvQueue , queueEntry ] ) ;
356
+ comm [ 0 ] . $recvQueue . push ( queueEntry ) ;
357
+ break ;
358
+ case 2 /* send */ :
359
+ var queueEntry = function ( ) {
360
+ if ( comm [ 0 ] . $closed ) {
361
+ $throwRuntimeError ( "send on closed channel" ) ;
362
+ }
363
+ f . selection = [ i ] ;
364
+ removeFromQueues ( ) ;
365
+ $schedule ( thisGoroutine ) ;
366
+ return comm [ 1 ] ;
367
+ } ;
368
+ entries . push ( [ comm [ 0 ] . $sendQueue , queueEntry ] ) ;
369
+ comm [ 0 ] . $sendQueue . push ( queueEntry ) ;
370
+ break ;
352
371
}
353
372
} ) ( i ) ;
354
373
}
355
374
$block ( ) ;
356
375
return f ;
357
376
} ;
358
- `
0 commit comments