Merge pull request #99 from tmickel/refactor/branches

Project wide rename substack -> branch
This commit is contained in:
Tim Mickel 2016-08-10 13:16:22 -04:00 committed by GitHub
commit caab305b52
10 changed files with 2403 additions and 2916 deletions

View file

@ -30,15 +30,15 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
util.stackFrame.loopCounter = parseInt(args.TIMES); util.stackFrame.loopCounter = parseInt(args.TIMES);
} }
// Only execute once per frame. // Only execute once per frame.
// When the substack finishes, `repeat` will be executed again and // When the branch finishes, `repeat` will be executed again and
// the second branch will be taken, yielding for the rest of the frame. // the second branch will be taken, yielding for the rest of the frame.
if (!util.stackFrame.executedInFrame) { if (!util.stackFrame.executedInFrame) {
util.stackFrame.executedInFrame = true; util.stackFrame.executedInFrame = true;
// Decrease counter // Decrease counter
util.stackFrame.loopCounter--; util.stackFrame.loopCounter--;
// If we still have some left, start the substack // If we still have some left, start the branch.
if (util.stackFrame.loopCounter >= 0) { if (util.stackFrame.loopCounter >= 0) {
util.startSubstack(); util.startBranch();
} }
} else { } else {
util.stackFrame.executedInFrame = false; util.stackFrame.executedInFrame = false;
@ -48,13 +48,13 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) { Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) {
// Only execute once per frame. // Only execute once per frame.
// When the substack finishes, `repeat` will be executed again and // When the branch finishes, `repeat` will be executed again and
// the second branch will be taken, yielding for the rest of the frame. // the second branch will be taken, yielding for the rest of the frame.
if (!util.stackFrame.executedInFrame) { if (!util.stackFrame.executedInFrame) {
util.stackFrame.executedInFrame = true; util.stackFrame.executedInFrame = true;
// If the condition is true, start the substack. // If the condition is true, start the branch.
if (!args.CONDITION) { if (!args.CONDITION) {
util.startSubstack(); util.startBranch();
} }
} else { } else {
util.stackFrame.executedInFrame = false; util.stackFrame.executedInFrame = false;
@ -64,11 +64,11 @@ Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) {
Scratch3ControlBlocks.prototype.forever = function(args, util) { Scratch3ControlBlocks.prototype.forever = function(args, util) {
// Only execute once per frame. // Only execute once per frame.
// When the substack finishes, `forever` will be executed again and // When the branch finishes, `forever` will be executed again and
// the second branch will be taken, yielding for the rest of the frame. // the second branch will be taken, yielding for the rest of the frame.
if (!util.stackFrame.executedInFrame) { if (!util.stackFrame.executedInFrame) {
util.stackFrame.executedInFrame = true; util.stackFrame.executedInFrame = true;
util.startSubstack(); util.startBranch();
} else { } else {
util.stackFrame.executedInFrame = false; util.stackFrame.executedInFrame = false;
util.yieldFrame(); util.yieldFrame();
@ -85,24 +85,24 @@ Scratch3ControlBlocks.prototype.wait = function(args) {
Scratch3ControlBlocks.prototype.if = function(args, util) { Scratch3ControlBlocks.prototype.if = function(args, util) {
// Only execute one time. `if` will be returned to // Only execute one time. `if` will be returned to
// when the substack finishes, but it shouldn't execute again. // when the branch finishes, but it shouldn't execute again.
if (util.stackFrame.executedInFrame === undefined) { if (util.stackFrame.executedInFrame === undefined) {
util.stackFrame.executedInFrame = true; util.stackFrame.executedInFrame = true;
if (args.CONDITION) { if (args.CONDITION) {
util.startSubstack(); util.startBranch();
} }
} }
}; };
Scratch3ControlBlocks.prototype.ifElse = function(args, util) { Scratch3ControlBlocks.prototype.ifElse = function(args, util) {
// Only execute one time. `ifElse` will be returned to // Only execute one time. `ifElse` will be returned to
// when the substack finishes, but it shouldn't execute again. // when the branch finishes, but it shouldn't execute again.
if (util.stackFrame.executedInFrame === undefined) { if (util.stackFrame.executedInFrame === undefined) {
util.stackFrame.executedInFrame = true; util.stackFrame.executedInFrame = true;
if (args.CONDITION) { if (args.CONDITION) {
util.startSubstack(1); util.startBranch(1);
} else { } else {
util.startSubstack(2); util.startBranch(2);
} }
} }
}; };

View file

@ -23,11 +23,11 @@ function Blocks () {
} }
/** /**
* Blockly inputs that represent statements/substacks * Blockly inputs that represent statements/branch.
* are prefixed with this string. * are prefixed with this string.
* @const{string} * @const{string}
*/ */
Blocks.SUBSTACK_INPUT_PREFIX = 'SUBSTACK'; Blocks.BRANCH_INPUT_PREFIX = 'SUBSTACK';
/** /**
* Provide an object with metadata for the requested block ID. * Provide an object with metadata for the requested block ID.
@ -57,19 +57,19 @@ Blocks.prototype.getNextBlock = function (id) {
}; };
/** /**
* Get the substack for a particular C-shaped block * Get the branch for a particular C-shaped block.
* @param {?string} id ID for block to get the substack for * @param {?string} id ID for block to get the branch for.
* @param {?number} substackNum Which substack to select (e.g. for if-else) * @param {?number} branchNum Which branch to select (e.g. for if-else).
* @return {?string} ID of block in the substack * @return {?string} ID of block in the branch.
*/ */
Blocks.prototype.getSubstack = function (id, substackNum) { Blocks.prototype.getBranch = function (id, branchNum) {
var block = this._blocks[id]; var block = this._blocks[id];
if (typeof block === 'undefined') return null; if (typeof block === 'undefined') return null;
if (!substackNum) substackNum = 1; if (!branchNum) branchNum = 1;
var inputName = Blocks.SUBSTACK_INPUT_PREFIX; var inputName = Blocks.BRANCH_INPUT_PREFIX;
if (substackNum > 1) { if (branchNum > 1) {
inputName += substackNum; inputName += branchNum;
} }
// Empty C-block? // Empty C-block?
@ -98,17 +98,17 @@ Blocks.prototype.getFields = function (id) {
}; };
/** /**
* Get all non-substack inputs for a block. * Get all non-branch inputs for a block.
* @param {?string} id ID of block to query. * @param {?string} id ID of block to query.
* @return {!Object} All non-substack inputs and their associated blocks. * @return {!Object} All non-branch inputs and their associated blocks.
*/ */
Blocks.prototype.getInputs = function (id) { Blocks.prototype.getInputs = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; if (typeof this._blocks[id] === 'undefined') return null;
var inputs = {}; var inputs = {};
for (var input in this._blocks[id].inputs) { for (var input in this._blocks[id].inputs) {
// Ignore blocks prefixed with substack prefix. // Ignore blocks prefixed with branch prefix.
if (input.substring(0, Blocks.SUBSTACK_INPUT_PREFIX.length) if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length)
!= Blocks.SUBSTACK_INPUT_PREFIX) { != Blocks.BRANCH_INPUT_PREFIX) {
inputs[input] = this._blocks[id].inputs[input]; inputs[input] = this._blocks[id].inputs[input];
} }
} }
@ -266,7 +266,7 @@ Blocks.prototype.deleteBlock = function (e) {
this.deleteBlock({id: block.next}); this.deleteBlock({id: block.next});
} }
// Delete inputs (including substacks) // Delete inputs (including branches)
for (var input in block.inputs) { for (var input in block.inputs) {
// If it's null, the block in this input moved away. // If it's null, the block in this input moved away.
if (block.inputs[input].block !== null) { if (block.inputs[input].block !== null) {

View file

@ -58,7 +58,7 @@ var execute = function (sequencer, thread) {
// If we've gotten this far, all of the input blocks are evaluated, // If we've gotten this far, all of the input blocks are evaluated,
// and `argValues` is fully populated. So, execute the block primitive. // and `argValues` is fully populated. So, execute the block primitive.
// First, clear `currentStackFrame.reported`, so any subsequent execution // First, clear `currentStackFrame.reported`, so any subsequent execution
// (e.g., on return from a substack) gets fresh inputs. // (e.g., on return from a branch) gets fresh inputs.
currentStackFrame.reported = {}; currentStackFrame.reported = {};
var primitiveReportedValue = null; var primitiveReportedValue = null;
@ -74,8 +74,8 @@ var execute = function (sequencer, thread) {
sequencer.proceedThread(thread); sequencer.proceedThread(thread);
}, },
stackFrame: currentStackFrame.executionContext, stackFrame: currentStackFrame.executionContext,
startSubstack: function (substackNum) { startBranch: function (branchNum) {
sequencer.stepToSubstack(thread, substackNum); sequencer.stepToBranch(thread, branchNum);
}, },
target: target target: target
}); });

View file

@ -85,7 +85,7 @@ Sequencer.prototype.stepThreads = function (threads) {
Sequencer.prototype.startThread = function (thread) { Sequencer.prototype.startThread = function (thread) {
var currentBlockId = thread.peekStack(); var currentBlockId = thread.peekStack();
if (!currentBlockId) { if (!currentBlockId) {
// A "null block" - empty substack. // A "null block" - empty branch.
// Yield for the frame. // Yield for the frame.
thread.popStack(); thread.popStack();
thread.setStatus(Thread.STATUS_YIELD_FRAME); thread.setStatus(Thread.STATUS_YIELD_FRAME);
@ -102,22 +102,22 @@ Sequencer.prototype.startThread = function (thread) {
}; };
/** /**
* Step a thread into a block's substack. * Step a thread into a block's branch.
* @param {!Thread} thread Thread object to step to substack. * @param {!Thread} thread Thread object to step to branch.
* @param {Number} substackNum Which substack to step to (i.e., 1, 2). * @param {Number} branchNum Which branch to step to (i.e., 1, 2).
*/ */
Sequencer.prototype.stepToSubstack = function (thread, substackNum) { Sequencer.prototype.stepToBranch = function (thread, branchNum) {
if (!substackNum) { if (!branchNum) {
substackNum = 1; branchNum = 1;
} }
var currentBlockId = thread.peekStack(); var currentBlockId = thread.peekStack();
var substackId = this.runtime.targetForThread(thread).blocks.getSubstack( var branchId = this.runtime.targetForThread(thread).blocks.getBranch(
currentBlockId, currentBlockId,
substackNum branchNum
); );
if (substackId) { if (branchId) {
// Push substack ID to the thread's stack. // Push branch ID to the thread's stack.
thread.pushStack(substackId); thread.pushStack(branchId);
} else { } else {
// Push null, so we come back to the current block. // Push null, so we come back to the current block.
thread.pushStack(null); thread.pushStack(null);

View file

@ -12,13 +12,13 @@
"!6Ahqg4f}Ljl}X5Hws?Z" "!6Ahqg4f}Ljl}X5Hws?Z"
] ]
}, },
"createsubstack": { "createbranch": {
"name": "block", "name": "block",
"xml": { "xml": {
"outerHTML": "<block type=\"control_forever\" id=\"r9`RpL74T6*SXPKv7}Dq\" x=\"61\" y=\"90\"><statement name=\"SUBSTACK\"><block type=\"control_wait\" id=\"{Rwt[LFtD1-JPAi-qf:.\"><value name=\"DURATION\"><shadow type=\"math_number\" id=\"VMDxt_9SYe5{*eNRe5dZ\"><field name=\"NUM\">1</field></shadow></value></block></statement></block>" "outerHTML": "<block type=\"control_forever\" id=\"r9`RpL74T6*SXPKv7}Dq\" x=\"61\" y=\"90\"><statement name=\"SUBSTACK\"><block type=\"control_wait\" id=\"{Rwt[LFtD1-JPAi-qf:.\"><value name=\"DURATION\"><shadow type=\"math_number\" id=\"VMDxt_9SYe5{*eNRe5dZ\"><field name=\"NUM\">1</field></shadow></value></block></statement></block>"
} }
}, },
"createtwosubstacks": { "createtwobranches": {
"name": "block", "name": "block",
"xml": { "xml": {
"outerHTML": "<block type=\"control_if_else\" id=\"8W?lmIY!Tgnh)~0!G#9-\" x=\"87\" y=\"159\"><statement name=\"SUBSTACK\"><block type=\"event_broadcast\" id=\"lgU2GGtwlREuasCB02Vr\"></block></statement><statement name=\"SUBSTACK2\"><block type=\"event_broadcast\" id=\"Gb]N,2P;|J%F?pxSwz(2\"></block></statement></block>" "outerHTML": "<block type=\"control_if_else\" id=\"8W?lmIY!Tgnh)~0!G#9-\" x=\"87\" y=\"159\"><statement name=\"SUBSTACK\"><block type=\"event_broadcast\" id=\"lgU2GGtwlREuasCB02Vr\"></block></statement><statement name=\"SUBSTACK2\"><block type=\"event_broadcast\" id=\"Gb]N,2P;|J%F?pxSwz(2\"></block></statement></block>"

View file

@ -43,8 +43,8 @@ test('create event', function (t) {
t.end(); t.end();
}); });
test('create with substack', function (t) { test('create with branch', function (t) {
var result = adapter(events.createsubstack); var result = adapter(events.createbranch);
// Outer block // Outer block
t.type(result[0].id, 'string'); t.type(result[0].id, 'string');
t.type(result[0].opcode, 'string'); t.type(result[0].opcode, 'string');
@ -53,22 +53,22 @@ test('create with substack', function (t) {
t.type(result[0].inputs['SUBSTACK'], 'object'); t.type(result[0].inputs['SUBSTACK'], 'object');
t.type(result[0].topLevel, 'boolean'); t.type(result[0].topLevel, 'boolean');
t.equal(result[0].topLevel, true); t.equal(result[0].topLevel, true);
// In substack // In branch
var substackBlockId = result[0].inputs['SUBSTACK']['block']; var branchBlockId = result[0].inputs['SUBSTACK']['block'];
t.type(substackBlockId, 'string'); t.type(branchBlockId, 'string');
// Find actual substack block // Find actual branch block
var substackBlock = null; var branchBlock = null;
for (var i = 0; i < result.length; i++) { for (var i = 0; i < result.length; i++) {
if (result[i].id == substackBlockId) { if (result[i].id == branchBlockId) {
substackBlock = result[i]; branchBlock = result[i];
} }
} }
t.type(substackBlock, 'object'); t.type(branchBlock, 'object');
t.end(); t.end();
}); });
test('create with two substacks', function (t) { test('create with two branches', function (t) {
var result = adapter(events.createtwosubstacks); var result = adapter(events.createtwobranches);
// Outer block // Outer block
t.type(result[0].id, 'string'); t.type(result[0].id, 'string');
t.type(result[0].opcode, 'string'); t.type(result[0].opcode, 'string');
@ -78,24 +78,24 @@ test('create with two substacks', function (t) {
t.type(result[0].inputs['SUBSTACK2'], 'object'); t.type(result[0].inputs['SUBSTACK2'], 'object');
t.type(result[0].topLevel, 'boolean'); t.type(result[0].topLevel, 'boolean');
t.equal(result[0].topLevel, true); t.equal(result[0].topLevel, true);
// In substacks // In branchs
var firstSubstackBlockId = result[0].inputs['SUBSTACK']['block']; var firstBranchBlockId = result[0].inputs['SUBSTACK']['block'];
var secondSubstackBlockId = result[0].inputs['SUBSTACK2']['block']; var secondBranchBlockId = result[0].inputs['SUBSTACK2']['block'];
t.type(firstSubstackBlockId, 'string'); t.type(firstBranchBlockId, 'string');
t.type(secondSubstackBlockId, 'string'); t.type(secondBranchBlockId, 'string');
// Find actual substack blocks // Find actual branch blocks
var firstSubstackBlock = null; var firstBranchBlock = null;
var secondSubstackBlock = null; var secondBranchBlock = null;
for (var i = 0; i < result.length; i++) { for (var i = 0; i < result.length; i++) {
if (result[i].id == firstSubstackBlockId) { if (result[i].id == firstBranchBlockId) {
firstSubstackBlock = result[i]; firstBranchBlock = result[i];
} }
if (result[i].id == secondSubstackBlockId) { if (result[i].id == secondBranchBlockId) {
secondSubstackBlock = result[i]; secondBranchBlock = result[i];
} }
} }
t.type(firstSubstackBlock, 'object'); t.type(firstBranchBlock, 'object');
t.type(secondSubstackBlock, 'object'); t.type(secondBranchBlock, 'object');
t.end(); t.end();
}); });

View file

@ -19,7 +19,7 @@ test('spec', function (t) {
t.type(b.getBlock, 'function'); t.type(b.getBlock, 'function');
t.type(b.getStacks, 'function'); t.type(b.getStacks, 'function');
t.type(b.getNextBlock, 'function'); t.type(b.getNextBlock, 'function');
t.type(b.getSubstack, 'function'); t.type(b.getBranch, 'function');
t.type(b.getOpcode, 'function'); t.type(b.getOpcode, 'function');
@ -119,9 +119,9 @@ test('getNextBlock', function (t) {
t.end(); t.end();
}); });
test('getSubstack', function (t) { test('getBranch', function (t) {
var b = new Blocks(); var b = new Blocks();
// Single substack // Single branch
b.createBlock({ b.createBlock({
id: 'foo', id: 'foo',
opcode: 'TEST_BLOCK', opcode: 'TEST_BLOCK',
@ -144,18 +144,18 @@ test('getSubstack', function (t) {
topLevel: false topLevel: false
}); });
var substack = b.getSubstack('foo'); var branch = b.getBranch('foo');
t.equals(substack, 'foo2'); t.equals(branch, 'foo2');
var notSubstack = b.getSubstack('?'); var notBranch = b.getBranch('?');
t.equals(notSubstack, null); t.equals(notBranch, null);
t.end(); t.end();
}); });
test('getSubstack2', function (t) { test('getBranch2', function (t) {
var b = new Blocks(); var b = new Blocks();
// Second substack // Second branch
b.createBlock({ b.createBlock({
id: 'foo', id: 'foo',
opcode: 'TEST_BLOCK', opcode: 'TEST_BLOCK',
@ -190,15 +190,15 @@ test('getSubstack2', function (t) {
topLevel: false topLevel: false
}); });
var substack1 = b.getSubstack('foo', 1); var branch1 = b.getBranch('foo', 1);
var substack2 = b.getSubstack('foo', 2); var branch2 = b.getBranch('foo', 2);
t.equals(substack1, 'foo2'); t.equals(branch1, 'foo2');
t.equals(substack2, 'foo3'); t.equals(branch2, 'foo3');
t.end(); t.end();
}); });
test('getSubstack with none', function (t) { test('getBranch with none', function (t) {
var b = new Blocks(); var b = new Blocks();
b.createBlock({ b.createBlock({
id: 'foo', id: 'foo',
@ -208,8 +208,8 @@ test('getSubstack with none', function (t) {
inputs: {}, inputs: {},
topLevel: true topLevel: true
}); });
var noSubstack = b.getSubstack('foo'); var noBranch = b.getBranch('foo');
t.equals(noSubstack, null); t.equals(noBranch, null);
t.end(); t.end();
}); });

5109
vm.js

File diff suppressed because it is too large Load diff

11
vm.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -109,6 +109,10 @@
this.vmWorker.postMessage({method: 'stopAll'}); this.vmWorker.postMessage({method: 'stopAll'});
}; };
VirtualMachine.prototype.animationFrame = function () {
this.vmWorker.postMessage({method: 'animationFrame'});
};
/** /**
* Export and bind to `window` * Export and bind to `window`
*/ */
@ -1026,6 +1030,9 @@
var queueIndex = -1; var queueIndex = -1;
function cleanUpNextTick() { function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false; draining = false;
if (currentQueue.length) { if (currentQueue.length) {
queue = currentQueue.concat(queue); queue = currentQueue.concat(queue);