Merge pull request #100 from tmickel/refactor/stacks-to-scripts

Rename `stacks` to `scripts`
This commit is contained in:
Tim Mickel 2016-08-11 15:45:18 -04:00 committed by GitHub
commit 4c67fc3bae
5 changed files with 114 additions and 114 deletions

View file

@ -15,11 +15,11 @@ function Blocks () {
this._blocks = {}; this._blocks = {};
/** /**
* All stacks in the workspace. * All top-level scripts in the workspace.
* A list of block IDs that represent stacks (first block in stack). * A list of block IDs that represent scripts (i.e., first block in script).
* @type {Array.<String>} * @type {Array.<String>}
*/ */
this._stacks = []; this._scripts = [];
} }
/** /**
@ -39,11 +39,11 @@ Blocks.prototype.getBlock = function (blockId) {
}; };
/** /**
* Get all known top-level blocks that start stacks. * Get all known top-level blocks that start scripts.
* @return {Array.<string>} List of block IDs. * @return {Array.<string>} List of block IDs.
*/ */
Blocks.prototype.getStacks = function () { Blocks.prototype.getScripts = function () {
return this._stacks; return this._scripts;
}; };
/** /**
@ -136,10 +136,10 @@ Blocks.prototype.generateBlockListener = function (isFlyout, opt_runtime) {
if (typeof e !== 'object') return; if (typeof e !== 'object') return;
if (typeof e.blockId !== 'string') return; if (typeof e.blockId !== 'string') return;
// UI event: clicked stacks toggle in the runtime. // UI event: clicked scripts toggle in the runtime.
if (e.element === 'stackclick') { if (e.element === 'stackclick') {
if (opt_runtime) { if (opt_runtime) {
opt_runtime.toggleStack(e.blockId); opt_runtime.toggleScript(e.blockId);
} }
return; return;
} }
@ -182,7 +182,7 @@ Blocks.prototype.generateBlockListener = function (isFlyout, opt_runtime) {
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Block management: create blocks and stacks from a `create` event * Block management: create blocks and scripts from a `create` event
* @param {!Object} block Blockly create event to be processed * @param {!Object} block Blockly create event to be processed
* @param {boolean} opt_isFlyoutBlock Whether the block is in the flyout. * @param {boolean} opt_isFlyoutBlock Whether the block is in the flyout.
*/ */
@ -190,12 +190,12 @@ Blocks.prototype.createBlock = function (block, opt_isFlyoutBlock) {
// Create new block // Create new block
this._blocks[block.id] = block; this._blocks[block.id] = block;
// Push block id to stacks array. // Push block id to scripts array.
// Blocks are added as a top-level stack if they are marked as a top-block // Blocks are added as a top-level stack if they are marked as a top-block
// (if they were top-level XML in the event) and if they are not // (if they were top-level XML in the event) and if they are not
// flyout blocks. // flyout blocks.
if (!opt_isFlyoutBlock && block.topLevel) { if (!opt_isFlyoutBlock && block.topLevel) {
this._addStack(block.id); this._addScript(block.id);
} }
}; };
@ -233,10 +233,10 @@ Blocks.prototype.moveBlock = function (e) {
// Has the block become a top-level block? // Has the block become a top-level block?
if (e.newParent === undefined) { if (e.newParent === undefined) {
this._addStack(e.id); this._addScript(e.id);
} else { } else {
// Remove stack, if one exists. // Remove script, if one exists.
this._deleteStack(e.id); this._deleteScript(e.id);
// Otherwise, try to connect it in its new place. // Otherwise, try to connect it in its new place.
if (e.newInput !== undefined) { if (e.newInput !== undefined) {
// Moved to the new parent's input. // Moved to the new parent's input.
@ -252,11 +252,11 @@ Blocks.prototype.moveBlock = function (e) {
}; };
/** /**
* Block management: delete blocks and their associated stacks * Block management: delete blocks and their associated scripts.
* @param {!Object} e Blockly delete event to be processed * @param {!Object} e Blockly delete event to be processed.
*/ */
Blocks.prototype.deleteBlock = function (e) { Blocks.prototype.deleteBlock = function (e) {
// @todo In runtime, stop threads running on this stack // @todo In runtime, stop threads running on this script.
// Get block // Get block
var block = this._blocks[e.id]; var block = this._blocks[e.id];
@ -274,36 +274,36 @@ Blocks.prototype.deleteBlock = function (e) {
} }
} }
// Delete stack // Delete any script starting with this block.
this._deleteStack(e.id); this._deleteScript(e.id);
// Delete block // Delete block itself.
delete this._blocks[e.id]; delete this._blocks[e.id];
}; };
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Helper to add a stack to `this._stacks` * Helper to add a stack to `this._scripts`.
* @param {?string} id ID of block that starts the stack * @param {?string} topBlockId ID of block that starts the script.
*/ */
Blocks.prototype._addStack = function (id) { Blocks.prototype._addScript = function (topBlockId) {
var i = this._stacks.indexOf(id); var i = this._scripts.indexOf(topBlockId);
if (i > -1) return; // Already in stacks. if (i > -1) return; // Already in scripts.
this._stacks.push(id); this._scripts.push(topBlockId);
// Update `topLevel` property on the top block. // Update `topLevel` property on the top block.
this._blocks[id].topLevel = true; this._blocks[topBlockId].topLevel = true;
}; };
/** /**
* Helper to remove a stack from `this._stacks` * Helper to remove a script from `this._scripts`.
* @param {?string} id ID of block that starts the stack * @param {?string} topBlockId ID of block that starts the script.
*/ */
Blocks.prototype._deleteStack = function (id) { Blocks.prototype._deleteScript = function (topBlockId) {
var i = this._stacks.indexOf(id); var i = this._scripts.indexOf(topBlockId);
if (i > -1) this._stacks.splice(i, 1); if (i > -1) this._scripts.splice(i, 1);
// Update `topLevel` property on the top block. // Update `topLevel` property on the top block.
if (this._blocks[id]) this._blocks[id].topLevel = false; if (this._blocks[topBlockId]) this._blocks[topBlockId].topLevel = false;
}; };
module.exports = Blocks; module.exports = Blocks;

View file

@ -12,7 +12,7 @@ var defaultBlockPackages = {
}; };
/** /**
* Manages targets, stacks, and the sequencer. * Manages targets, scripts, and the sequencer.
* @param {!Array.<Target>} targets List of targets for this runtime. * @param {!Array.<Target>} targets List of targets for this runtime.
*/ */
function Runtime (targets) { function Runtime (targets) {
@ -146,39 +146,39 @@ Runtime.prototype._removeThread = function (thread) {
}; };
/** /**
* Toggle a stack * Toggle a script.
* @param {!string} stackId ID of block that starts the stack * @param {!string} topBlockId ID of block that starts the script.
*/ */
Runtime.prototype.toggleStack = function (stackId) { Runtime.prototype.toggleScript = function (topBlockId) {
// Remove any existing thread // Remove any existing thread.
for (var i = 0; i < this.threads.length; i++) { for (var i = 0; i < this.threads.length; i++) {
if (this.threads[i].topBlock == stackId) { if (this.threads[i].topBlock == topBlockId) {
this._removeThread(this.threads[i]); this._removeThread(this.threads[i]);
return; return;
} }
} }
// Otherwise add it // Otherwise add it.
this._pushThread(stackId); this._pushThread(topBlockId);
}; };
/** /**
* Green flag, which stops currently running threads * Green flag, which stops currently running threads
* and adds all top-level stacks that start with the green flag * and adds all top-level scripts that start with the green flag
*/ */
Runtime.prototype.greenFlag = function () { Runtime.prototype.greenFlag = function () {
// Remove all existing threads // Remove all existing threads
for (var i = 0; i < this.threads.length; i++) { for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]); this._removeThread(this.threads[i]);
} }
// Add all top stacks with green flag // Add all top scripts with green flag
for (var t = 0; t < this.targets.length; t++) { for (var t = 0; t < this.targets.length; t++) {
var target = this.targets[t]; var target = this.targets[t];
var stacks = target.blocks.getStacks(); var scripts = target.blocks.getScripts();
for (var j = 0; j < stacks.length; j++) { for (var j = 0; j < scripts.length; j++) {
var topBlock = stacks[j]; var topBlock = scripts[j];
if (target.blocks.getBlock(topBlock).opcode === if (target.blocks.getBlock(topBlock).opcode ===
'event_whenflagclicked') { 'event_whenflagclicked') {
this._pushThread(stacks[j]); this._pushThread(scripts[j]);
} }
} }
} }

View file

@ -9,15 +9,15 @@ test('spec', function (t) {
t.ok(b instanceof Blocks); t.ok(b instanceof Blocks);
t.type(b._blocks, 'object'); t.type(b._blocks, 'object');
t.type(b._stacks, 'object'); t.type(b._scripts, 'object');
t.ok(Array.isArray(b._stacks)); t.ok(Array.isArray(b._scripts));
t.type(b.createBlock, 'function'); t.type(b.createBlock, 'function');
t.type(b.moveBlock, 'function'); t.type(b.moveBlock, 'function');
t.type(b.changeBlock, 'function'); t.type(b.changeBlock, 'function');
t.type(b.deleteBlock, 'function'); t.type(b.deleteBlock, 'function');
t.type(b.getBlock, 'function'); t.type(b.getBlock, 'function');
t.type(b.getStacks, 'function'); t.type(b.getScripts, 'function');
t.type(b.getNextBlock, 'function'); t.type(b.getNextBlock, 'function');
t.type(b.getBranch, 'function'); t.type(b.getBranch, 'function');
t.type(b.getOpcode, 'function'); t.type(b.getOpcode, 'function');
@ -44,11 +44,11 @@ test('getBlock', function (t) {
t.end(); t.end();
}); });
test('getStacks', function (t) { test('getScripts', function (t) {
var b = new Blocks(); var b = new Blocks();
var stacks = b.getStacks(); var scripts = b.getScripts();
t.type(stacks, 'object'); t.type(scripts, 'object');
t.equals(stacks.length, 0); t.equals(scripts.length, 0);
// Create two top-level blocks and one not. // Create two top-level blocks and one not.
b.createBlock({ b.createBlock({
id: 'foo', id: 'foo',
@ -75,12 +75,12 @@ test('getStacks', function (t) {
topLevel: false topLevel: false
}); });
stacks = b.getStacks(); scripts = b.getScripts();
t.type(stacks, 'object'); t.type(scripts, 'object');
t.equals(stacks.length, 2); t.equals(scripts.length, 2);
t.ok(stacks.indexOf('foo') > -1); t.ok(scripts.indexOf('foo') > -1);
t.ok(stacks.indexOf('foo2') > -1); t.ok(scripts.indexOf('foo2') > -1);
t.equals(stacks.indexOf('foo3'), -1); t.equals(scripts.indexOf('foo3'), -1);
t.end(); t.end();
}); });
@ -244,7 +244,7 @@ test('create', function (t) {
t.type(b._blocks['foo'], 'object'); t.type(b._blocks['foo'], 'object');
t.equal(b._blocks['foo'].opcode, 'TEST_BLOCK'); t.equal(b._blocks['foo'].opcode, 'TEST_BLOCK');
t.notEqual(b._stacks.indexOf('foo'), -1); t.notEqual(b._scripts.indexOf('foo'), -1);
t.end(); t.end();
}); });
@ -272,7 +272,7 @@ test('move', function (t) {
id: 'bar', id: 'bar',
newParent: 'foo' newParent: 'foo'
}); });
t.equal(b._stacks.length, 1); t.equal(b._scripts.length, 1);
t.equal(Object.keys(b._blocks).length, 2); t.equal(Object.keys(b._blocks).length, 2);
t.equal(b._blocks['foo'].next, 'bar'); t.equal(b._blocks['foo'].next, 'bar');
@ -281,7 +281,7 @@ test('move', function (t) {
id: 'bar', id: 'bar',
oldParent: 'foo' oldParent: 'foo'
}); });
t.equal(b._stacks.length, 2); t.equal(b._scripts.length, 2);
t.equal(Object.keys(b._blocks).length, 2); t.equal(Object.keys(b._blocks).length, 2);
t.equal(b._blocks['foo'].next, null); t.equal(b._blocks['foo'].next, null);
@ -360,7 +360,7 @@ test('delete', function (t) {
}); });
t.type(b._blocks['foo'], 'undefined'); t.type(b._blocks['foo'], 'undefined');
t.equal(b._stacks.indexOf('foo'), -1); t.equal(b._scripts.indexOf('foo'), -1);
t.end(); t.end();
}); });
@ -398,9 +398,9 @@ test('delete chain', function (t) {
t.type(b._blocks['foo'], 'undefined'); t.type(b._blocks['foo'], 'undefined');
t.type(b._blocks['foo2'], 'undefined'); t.type(b._blocks['foo2'], 'undefined');
t.type(b._blocks['foo3'], 'undefined'); t.type(b._blocks['foo3'], 'undefined');
t.equal(b._stacks.indexOf('foo'), -1); t.equal(b._scripts.indexOf('foo'), -1);
t.equal(Object.keys(b._blocks).length, 0); t.equal(Object.keys(b._blocks).length, 0);
t.equal(b._stacks.length, 0); t.equal(b._scripts.length, 0);
t.end(); t.end();
}); });
@ -461,8 +461,8 @@ test('delete inputs', function (t) {
t.type(b._blocks['foo2'], 'undefined'); t.type(b._blocks['foo2'], 'undefined');
t.type(b._blocks['foo3'], 'undefined'); t.type(b._blocks['foo3'], 'undefined');
t.type(b._blocks['foo4'], 'undefined'); t.type(b._blocks['foo4'], 'undefined');
t.equal(b._stacks.indexOf('foo'), -1); t.equal(b._scripts.indexOf('foo'), -1);
t.equal(Object.keys(b._blocks).length, 0); t.equal(Object.keys(b._blocks).length, 0);
t.equal(b._stacks.length, 0); t.equal(b._scripts.length, 0);
t.end(); t.end();
}); });

88
vm.js
View file

@ -1585,11 +1585,11 @@
this._blocks = {}; this._blocks = {};
/** /**
* All stacks in the workspace. * All top-level scripts in the workspace.
* A list of block IDs that represent stacks (first block in stack). * A list of block IDs that represent scripts (i.e., first block in script).
* @type {Array.<String>} * @type {Array.<String>}
*/ */
this._stacks = []; this._scripts = [];
} }
/** /**
@ -1609,11 +1609,11 @@
}; };
/** /**
* Get all known top-level blocks that start stacks. * Get all known top-level blocks that start scripts.
* @return {Array.<string>} List of block IDs. * @return {Array.<string>} List of block IDs.
*/ */
Blocks.prototype.getStacks = function () { Blocks.prototype.getScripts = function () {
return this._stacks; return this._scripts;
}; };
/** /**
@ -1706,10 +1706,10 @@
if (typeof e !== 'object') return; if (typeof e !== 'object') return;
if (typeof e.blockId !== 'string') return; if (typeof e.blockId !== 'string') return;
// UI event: clicked stacks toggle in the runtime. // UI event: clicked scripts toggle in the runtime.
if (e.element === 'stackclick') { if (e.element === 'stackclick') {
if (opt_runtime) { if (opt_runtime) {
opt_runtime.toggleStack(e.blockId); opt_runtime.toggleScript(e.blockId);
} }
return; return;
} }
@ -1752,7 +1752,7 @@
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Block management: create blocks and stacks from a `create` event * Block management: create blocks and scripts from a `create` event
* @param {!Object} block Blockly create event to be processed * @param {!Object} block Blockly create event to be processed
* @param {boolean} opt_isFlyoutBlock Whether the block is in the flyout. * @param {boolean} opt_isFlyoutBlock Whether the block is in the flyout.
*/ */
@ -1760,12 +1760,12 @@
// Create new block // Create new block
this._blocks[block.id] = block; this._blocks[block.id] = block;
// Push block id to stacks array. // Push block id to scripts array.
// Blocks are added as a top-level stack if they are marked as a top-block // Blocks are added as a top-level stack if they are marked as a top-block
// (if they were top-level XML in the event) and if they are not // (if they were top-level XML in the event) and if they are not
// flyout blocks. // flyout blocks.
if (!opt_isFlyoutBlock && block.topLevel) { if (!opt_isFlyoutBlock && block.topLevel) {
this._addStack(block.id); this._addScript(block.id);
} }
}; };
@ -1803,10 +1803,10 @@
// Has the block become a top-level block? // Has the block become a top-level block?
if (e.newParent === undefined) { if (e.newParent === undefined) {
this._addStack(e.id); this._addScript(e.id);
} else { } else {
// Remove stack, if one exists. // Remove script, if one exists.
this._deleteStack(e.id); this._deleteScript(e.id);
// Otherwise, try to connect it in its new place. // Otherwise, try to connect it in its new place.
if (e.newInput !== undefined) { if (e.newInput !== undefined) {
// Moved to the new parent's input. // Moved to the new parent's input.
@ -1822,11 +1822,11 @@
}; };
/** /**
* Block management: delete blocks and their associated stacks * Block management: delete blocks and their associated scripts.
* @param {!Object} e Blockly delete event to be processed * @param {!Object} e Blockly delete event to be processed.
*/ */
Blocks.prototype.deleteBlock = function (e) { Blocks.prototype.deleteBlock = function (e) {
// @todo In runtime, stop threads running on this stack // @todo In runtime, stop threads running on this script.
// Get block // Get block
var block = this._blocks[e.id]; var block = this._blocks[e.id];
@ -1844,34 +1844,34 @@
} }
} }
// Delete stack // Delete any script starting with this block.
this._deleteStack(e.id); this._deleteScript(e.id);
// Delete block // Delete block itself.
delete this._blocks[e.id]; delete this._blocks[e.id];
}; };
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Helper to add a stack to `this._stacks` * Helper to add a stack to `this._scripts`.
* @param {?string} id ID of block that starts the stack * @param {?string} id ID of block that starts the script.
*/ */
Blocks.prototype._addStack = function (id) { Blocks.prototype._addScript = function (id) {
var i = this._stacks.indexOf(id); var i = this._scripts.indexOf(id);
if (i > -1) return; // Already in stacks. if (i > -1) return; // Already in scripts.
this._stacks.push(id); this._scripts.push(id);
// Update `topLevel` property on the top block. // Update `topLevel` property on the top block.
this._blocks[id].topLevel = true; this._blocks[id].topLevel = true;
}; };
/** /**
* Helper to remove a stack from `this._stacks` * Helper to remove a stack from `this._scripts`.
* @param {?string} id ID of block that starts the stack * @param {?string} id ID of block that starts the script.
*/ */
Blocks.prototype._deleteStack = function (id) { Blocks.prototype._deleteStack = function (id) {
var i = this._stacks.indexOf(id); var i = this._scripts.indexOf(id);
if (i > -1) this._stacks.splice(i, 1); if (i > -1) this._scripts.splice(i, 1);
// Update `topLevel` property on the top block. // Update `topLevel` property on the top block.
if (this._blocks[id]) this._blocks[id].topLevel = false; if (this._blocks[id]) this._blocks[id].topLevel = false;
}; };
@ -11279,7 +11279,7 @@
}; };
/** /**
* Manages targets, stacks, and the sequencer. * Manages targets, scripts, and the sequencer.
* @param {!Array.<Target>} targets List of targets for this runtime. * @param {!Array.<Target>} targets List of targets for this runtime.
*/ */
function Runtime (targets) { function Runtime (targets) {
@ -11413,39 +11413,39 @@
}; };
/** /**
* Toggle a stack * Toggle a script.
* @param {!string} stackId ID of block that starts the stack * @param {!string} topBlockId ID of block that starts the script.
*/ */
Runtime.prototype.toggleStack = function (stackId) { Runtime.prototype.toggleScript = function (topBlockId) {
// Remove any existing thread // Remove any existing thread.
for (var i = 0; i < this.threads.length; i++) { for (var i = 0; i < this.threads.length; i++) {
if (this.threads[i].topBlock == stackId) { if (this.threads[i].topBlock == topBlockId) {
this._removeThread(this.threads[i]); this._removeThread(this.threads[i]);
return; return;
} }
} }
// Otherwise add it // Otherwise add it.
this._pushThread(stackId); this._pushThread(topBlockId);
}; };
/** /**
* Green flag, which stops currently running threads * Green flag, which stops currently running threads
* and adds all top-level stacks that start with the green flag * and adds all top-level scripts that start with the green flag
*/ */
Runtime.prototype.greenFlag = function () { Runtime.prototype.greenFlag = function () {
// Remove all existing threads // Remove all existing threads
for (var i = 0; i < this.threads.length; i++) { for (var i = 0; i < this.threads.length; i++) {
this._removeThread(this.threads[i]); this._removeThread(this.threads[i]);
} }
// Add all top stacks with green flag // Add all top scripts with green flag
for (var t = 0; t < this.targets.length; t++) { for (var t = 0; t < this.targets.length; t++) {
var target = this.targets[t]; var target = this.targets[t];
var stacks = target.blocks.getStacks(); var scripts = target.blocks.getScripts();
for (var j = 0; j < stacks.length; j++) { for (var j = 0; j < scripts.length; j++) {
var topBlock = stacks[j]; var topBlock = scripts[j];
if (target.blocks.getBlock(topBlock).opcode === if (target.blocks.getBlock(topBlock).opcode ===
'event_whenflagclicked') { 'event_whenflagclicked') {
this._pushThread(stacks[j]); this._pushThread(scripts[j]);
} }
} }
} }

4
vm.min.js vendored

File diff suppressed because one or more lines are too long