Permalink
Browse files

throw error when blocking in synchronous JS callbacks

  • Loading branch information...
neelance committed Apr 2, 2015
1 parent a62e6b4 commit 6e21f510064ca112881fe5ff3542a082a4f9da0f
Showing with 10 additions and 3 deletions.
  1. +3 −2 compiler/prelude/goroutines.go
  2. +7 −1 compiler/prelude/jsmapping.go
@@ -109,7 +109,7 @@ var $recover = function() {
};
var $throw = function(err) { throw err; };
var $dummyGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
var $dummyGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [], canBlock: false };
var $curGoroutine = $dummyGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0, $checkForDeadlock = true;
var $go = function(fun, args, direct) {
$totalGoroutines++;
@@ -147,6 +147,7 @@ var $go = function(fun, args, direct) {
$goroutine.exit = false;
$goroutine.deferStack = [];
$goroutine.panicStack = [];
$goroutine.canBlock = true;
$schedule($goroutine, direct);
};
@@ -179,7 +180,7 @@ var $schedule = function(goroutine, direct) {
};
var $block = function() {
if ($curGoroutine === $dummyGoroutine) {
if (!$curGoroutine.canBlock) {
$throwRuntimeError("cannot block in JavaScript callback, fix by wrapping code in goroutine");
}
$curGoroutine.asleep = true;
@@ -68,7 +68,13 @@ var $externalize = function(v, t) {
}
args.push($internalize(arguments[i], t.params[i]));
}
var result = v.apply(this, args);
var canBlock = $curGoroutine.canBlock;
$curGoroutine.canBlock = false;
try {
var result = v.apply(this, args);
} finally {
$curGoroutine.canBlock = canBlock;
}
switch (t.results.length) {
case 0:
return;

0 comments on commit 6e21f51

Please sign in to comment.