Merge branch 'master' of github.com:MathWizz/scratch-html5 into test
Conflicts: js/Interpreter.js js/primitives/LooksPrims.js js/primitives/MotionAndPenPrims.js js/primitives/Primitives.js js/primitives/SoundPrims.js js/primitives/VarListPrims.js Tested with: http://scratch.mit.edu/projects/13841687/ http://scratch.mit.edu/projects/2192048/
This commit is contained in:
commit
07415badbe
6 changed files with 179 additions and 155 deletions
|
@ -139,10 +139,11 @@ Interpreter.prototype.stepActiveThread = function() {
|
|||
// Advance the "program counter" to the next block before running the primitive.
|
||||
// Control flow primitives (e.g. if) may change activeThread.nextBlock.
|
||||
this.activeThread.nextBlock = b.nextBlock;
|
||||
if(this.debugOps && this.debugFunc) {
|
||||
var finalArgs = new Array(b.args.length);
|
||||
for(var i=0; i<b.args.length; ++i)
|
||||
finalArgs[i] = this.arg(b, i);
|
||||
if (this.debugOps && this.debugFunc) {
|
||||
var finalArgs = [];
|
||||
for (var i = 0; i < b.args.length; ++i) {
|
||||
finalArgs.push(this.arg(b, i));
|
||||
}
|
||||
|
||||
this.debugFunc(this.opCount2, b.op, finalArgs);
|
||||
++this.opCount2;
|
||||
|
@ -150,13 +151,22 @@ Interpreter.prototype.stepActiveThread = function() {
|
|||
b.primFcn(b);
|
||||
if (this.yield) { this.activeThread.nextBlock = b; return; }
|
||||
b = this.activeThread.nextBlock; // refresh local variable b in case primitive did some control flow
|
||||
while (b == null) {
|
||||
while (!b) {
|
||||
// end of a substack; pop the owning control flow block from stack
|
||||
// Note: This is a loop to handle nested control flow blocks.
|
||||
b = this.activeThread.stack.pop();
|
||||
if ((b == null) || (b.isLoop)) {
|
||||
this.activeThread.nextBlock = b;
|
||||
return; // yield at the end of a loop or when stack is empty
|
||||
|
||||
// yield at the end of a loop or when stack is empty
|
||||
if (this.activeThread.stack.length === 0) {
|
||||
this.activeThread.nextBlock = null;
|
||||
return;
|
||||
} else {
|
||||
b = this.activeThread.stack.pop();
|
||||
if (b.isLoop) {
|
||||
this.activeThread.nextBlock = b; // preserve where it left off
|
||||
return;
|
||||
} else {
|
||||
b = b.nextBlock; // skip and continue for non looping blocks
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -206,6 +216,24 @@ Interpreter.prototype.arg = function(block, index) {
|
|||
return arg;
|
||||
};
|
||||
|
||||
Interpreter.prototype.numarg = function(block, index) {
|
||||
var arg = Number(this.arg(block, index));
|
||||
if (arg !== arg) {
|
||||
return 0;
|
||||
}
|
||||
return arg;
|
||||
};
|
||||
|
||||
Interpreter.prototype.boolarg = function(block, index) {
|
||||
var arg = this.arg(block, index);
|
||||
if (typeof arg === 'boolean') {
|
||||
return arg;
|
||||
} else if (typeof arg === 'string') {
|
||||
return !(arg === '' || arg === '0' || arg.toLowerCase() === 'false');
|
||||
}
|
||||
return Boolean(arg);
|
||||
};
|
||||
|
||||
Interpreter.prototype.targetSprite = function() {
|
||||
return this.activeThread.target;
|
||||
};
|
||||
|
@ -242,14 +270,14 @@ Interpreter.prototype.initPrims = function() {
|
|||
this.primitiveTable['whenGreenFlag'] = this.primNoop;
|
||||
this.primitiveTable['whenKeyPressed'] = this.primNoop;
|
||||
this.primitiveTable['whenClicked'] = this.primNoop;
|
||||
this.primitiveTable['if'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b); };
|
||||
this.primitiveTable['if'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); };
|
||||
this.primitiveTable['doForever'] = function(b) { interp.startSubstack(b, true); };
|
||||
this.primitiveTable['doForeverIf'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b, true); else interp.yield = true; };
|
||||
this.primitiveTable['doIf'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b); };
|
||||
this.primitiveTable['doForeverIf'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b, true); else interp.yield = true; };
|
||||
this.primitiveTable['doIf'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); };
|
||||
this.primitiveTable['doRepeat'] = this.primRepeat;
|
||||
this.primitiveTable['doIfElse'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b); else interp.startSubstack(b, false, true); };
|
||||
this.primitiveTable['doWaitUntil'] = function(b) { if (!interp.arg(b, 0)) interp.yield = true; };
|
||||
this.primitiveTable['doUntil'] = function(b) { if (!interp.arg(b, 0)) interp.startSubstack(b, true); };
|
||||
this.primitiveTable['doIfElse'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); else interp.startSubstack(b, false, true); };
|
||||
this.primitiveTable['doWaitUntil'] = function(b) { if (!interp.boolarg(b, 0)) interp.yield = true; };
|
||||
this.primitiveTable['doUntil'] = function(b) { if (!interp.boolarg(b, 0)) interp.startSubstack(b, true); };
|
||||
this.primitiveTable['doReturn'] = function(b) { interp.activeThread = new Thread(null); };
|
||||
this.primitiveTable['stopAll'] = function(b) { interp.activeThread = new Thread(null); interp.threads = []; }
|
||||
this.primitiveTable['whenIReceive'] = this.primNoop;
|
||||
|
@ -275,13 +303,13 @@ Interpreter.prototype.lookupPrim = function(op) {
|
|||
Interpreter.prototype.primNoop = function(b) { console.log(b.op); };
|
||||
|
||||
Interpreter.prototype.primWait = function(b) {
|
||||
if (interp.activeThread.firstTime) interp.startTimer(interp.arg(b, 0));
|
||||
if (interp.activeThread.firstTime) interp.startTimer(interp.numarg(b, 0));
|
||||
else interp.checkTimer();
|
||||
};
|
||||
|
||||
Interpreter.prototype.primRepeat = function(b) {
|
||||
if (b.tmp == -1) {
|
||||
b.tmp = Math.max(interp.arg(b, 0), 0); // Initialize repeat count on this block
|
||||
b.tmp = Math.max(interp.numarg(b, 0), 0); // Initialize repeat count on this block
|
||||
}
|
||||
if (b.tmp > 0) {
|
||||
b.tmp -= 1; // decrement count
|
||||
|
@ -299,7 +327,7 @@ Interpreter.prototype.broadcast = function(b, waitFlag) {
|
|||
var receivers = [];
|
||||
var msg = String(interp.arg(b, 0)).toLowerCase();
|
||||
var findReceivers = function(stack, target) {
|
||||
if ((stack.op == "whenIReceive") && (stack.args[0].toLowerCase() == msg)) {
|
||||
if ((stack.op == 'whenIReceive') && (stack.args[0].toLowerCase() == msg)) {
|
||||
receivers.push([stack, target]);
|
||||
}
|
||||
}
|
||||
|
@ -334,10 +362,8 @@ Interpreter.prototype.isRunning = function(b) {
|
|||
|
||||
Interpreter.prototype.startSubstack = function(b, isLoop, secondSubstack) {
|
||||
// Start the substack of a control structure command such as if or forever.
|
||||
if (isLoop) {
|
||||
b.isLoop = true;
|
||||
this.activeThread.stack.push(b); // remember the block that started the substack
|
||||
}
|
||||
b.isLoop = !!isLoop;
|
||||
this.activeThread.stack.push(b); // remember the block that started the substack
|
||||
if (!secondSubstack) {
|
||||
this.activeThread.nextBlock = b.substack;
|
||||
} else {
|
||||
|
|
Reference in a new issue