Hacked implementation of broadcasts

This commit is contained in:
Tim Mickel 2016-05-03 14:57:10 -04:00
parent 326e03b5d2
commit 9363432456
4 changed files with 86 additions and 8 deletions

View file

@ -57,14 +57,26 @@ Scratch3Blocks.prototype.stop = function() {
Scratch3Blocks.prototype.whenFlagClicked = function() {
console.log('Running: event_whenflagclicked');
// No-op
};
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
console.log('Running: event_whenbroadcastreceived');
// No-op
};
Scratch3Blocks.prototype.broadcast = function() {
Scratch3Blocks.prototype.broadcast = function(argValues, util) {
console.log('Running: event_broadcast');
util.startHats(function(hat) {
if (hat.opcode === 'event_whenbroadcastreceived') {
var shadows = hat.fields.CHOICE.blocks;
for (var sb in shadows) {
var shadowblock = shadows[sb];
return shadowblock.fields.CHOICE.value === argValues[0];
}
}
return false;
});
};
module.exports = Scratch3Blocks;

View file

@ -137,6 +137,32 @@ Sequencer.prototype.stepThread = function (thread) {
thread.stackFrames.pop();
};
/**
* A callback for the primitive to start hats.
* @todo very hacked...
*/
var startHats = function(callback) {
for (var i = 0; i < instance.runtime.stacks.length; i++) {
var stack = instance.runtime.stacks[i];
var stackBlock = instance.runtime.blocks[stack];
var result = callback(stackBlock);
if (result) {
// Check if the stack is already running
var stackRunning = false;
for (var j = 0; j < instance.runtime.threads.length; j++) {
if (instance.runtime.threads[j].topBlock == stack) {
stackRunning = true;
break;
}
}
if (!stackRunning) {
instance.runtime._pushThread(stack);
}
}
}
};
/**
* Record whether we have switched stack,
* to avoid proceeding the thread automatically.
@ -191,7 +217,8 @@ Sequencer.prototype.stepThread = function (thread) {
done: threadDoneCallback,
timeout: YieldTimers.timeout,
stackFrame: currentStackFrame,
startSubstack: threadStartSubstack
startSubstack: threadStartSubstack,
startHats: startHats
});
}
catch(e) {

43
vm.js
View file

@ -1702,6 +1702,32 @@
thread.stackFrames.pop();
};
/**
* A callback for the primitive to start hats.
* @todo very hacked...
*/
var startHats = function(callback) {
for (var i = 0; i < instance.runtime.stacks.length; i++) {
var stack = instance.runtime.stacks[i];
var stackBlock = instance.runtime.blocks[stack];
var result = callback(stackBlock);
if (result) {
// Check if the stack is already running
var stackRunning = false;
for (var j = 0; j < instance.runtime.threads.length; j++) {
if (instance.runtime.threads[j].topBlock == stack) {
stackRunning = true;
break;
}
}
if (!stackRunning) {
instance.runtime._pushThread(stack);
}
}
}
};
/**
* Record whether we have switched stack,
* to avoid proceeding the thread automatically.
@ -1756,7 +1782,8 @@
done: threadDoneCallback,
timeout: YieldTimers.timeout,
stackFrame: currentStackFrame,
startSubstack: threadStartSubstack
startSubstack: threadStartSubstack,
startHats: startHats
});
}
catch(e) {
@ -2038,14 +2065,26 @@
Scratch3Blocks.prototype.whenFlagClicked = function() {
console.log('Running: event_whenflagclicked');
// No-op
};
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
console.log('Running: event_whenbroadcastreceived');
// No-op
};
Scratch3Blocks.prototype.broadcast = function() {
Scratch3Blocks.prototype.broadcast = function(argValues, util) {
console.log('Running: event_broadcast');
util.startHats(function(hat) {
if (hat.opcode === 'event_whenbroadcastreceived') {
var shadows = hat.fields.CHOICE.blocks;
for (var sb in shadows) {
var shadowblock = shadows[sb];
return shadowblock.fields.CHOICE.value === argValues[0];
}
}
return false;
});
};
module.exports = Scratch3Blocks;

8
vm.min.js vendored

File diff suppressed because one or more lines are too long