@@ -389,7 +389,20 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
389
389
390
390
c .builder .SetInsertPointBefore (inst )
391
391
392
- parentHandle := f .LastParam ()
392
+ var parentHandle llvm.Value
393
+ if f .Linkage () == llvm .ExternalLinkage {
394
+ // Exported function.
395
+ // Note that getTaskPromisePtr will panic if it is called with
396
+ // a nil pointer, so blocking exported functions that try to
397
+ // return anything will not work.
398
+ parentHandle = llvm .ConstPointerNull (c .i8ptrType )
399
+ } else {
400
+ parentHandle = f .LastParam ()
401
+ if parentHandle .IsNil () || parentHandle .Name () != "parentHandle" {
402
+ // sanity check
403
+ panic ("trying to make exported function async" )
404
+ }
405
+ }
393
406
394
407
// Store return values.
395
408
switch inst .OperandsCount () {
@@ -417,7 +430,7 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
417
430
// behavior somehow (with the unreachable instruction).
418
431
continuePoint := c .builder .CreateCall (coroSuspendFunc , []llvm.Value {
419
432
llvm .ConstNull (c .ctx .TokenType ()),
420
- llvm .ConstInt (c .ctx .Int1Type (), 1 , false ),
433
+ llvm .ConstInt (c .ctx .Int1Type (), 0 , false ),
421
434
}, "ret" )
422
435
sw := c .builder .CreateSwitch (continuePoint , frame .suspendBlock , 2 )
423
436
sw .AddCase (llvm .ConstInt (c .ctx .Int8Type (), 0 , false ), frame .unreachableBlock )
@@ -488,7 +501,7 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
488
501
c .builder .SetInsertPointBefore (deadlockCall )
489
502
continuePoint := c .builder .CreateCall (coroSuspendFunc , []llvm.Value {
490
503
llvm .ConstNull (c .ctx .TokenType ()),
491
- llvm .ConstInt (c .ctx .Int1Type (), 1 , false ), // final suspend
504
+ llvm .ConstInt (c .ctx .Int1Type (), 0 , false ),
492
505
}, "" )
493
506
c .splitBasicBlock (deadlockCall , llvm .NextBasicBlock (c .builder .GetInsertBlock ()), "task.wakeup.dead" )
494
507
c .builder .SetInsertPointBefore (deadlockCall )
0 commit comments