mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Merge pull request #99 from tmickel/refactor/branches
Project wide rename substack -> branch
This commit is contained in:
commit
caab305b52
10 changed files with 2403 additions and 2916 deletions
|
@ -30,15 +30,15 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
|
|||
util.stackFrame.loopCounter = parseInt(args.TIMES);
|
||||
}
|
||||
// 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.
|
||||
if (!util.stackFrame.executedInFrame) {
|
||||
util.stackFrame.executedInFrame = true;
|
||||
// Decrease counter
|
||||
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) {
|
||||
util.startSubstack();
|
||||
util.startBranch();
|
||||
}
|
||||
} else {
|
||||
util.stackFrame.executedInFrame = false;
|
||||
|
@ -48,13 +48,13 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
|
|||
|
||||
Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) {
|
||||
// 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.
|
||||
if (!util.stackFrame.executedInFrame) {
|
||||
util.stackFrame.executedInFrame = true;
|
||||
// If the condition is true, start the substack.
|
||||
// If the condition is true, start the branch.
|
||||
if (!args.CONDITION) {
|
||||
util.startSubstack();
|
||||
util.startBranch();
|
||||
}
|
||||
} else {
|
||||
util.stackFrame.executedInFrame = false;
|
||||
|
@ -64,11 +64,11 @@ Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) {
|
|||
|
||||
Scratch3ControlBlocks.prototype.forever = function(args, util) {
|
||||
// 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.
|
||||
if (!util.stackFrame.executedInFrame) {
|
||||
util.stackFrame.executedInFrame = true;
|
||||
util.startSubstack();
|
||||
util.startBranch();
|
||||
} else {
|
||||
util.stackFrame.executedInFrame = false;
|
||||
util.yieldFrame();
|
||||
|
@ -85,24 +85,24 @@ Scratch3ControlBlocks.prototype.wait = function(args) {
|
|||
|
||||
Scratch3ControlBlocks.prototype.if = function(args, util) {
|
||||
// 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) {
|
||||
util.stackFrame.executedInFrame = true;
|
||||
if (args.CONDITION) {
|
||||
util.startSubstack();
|
||||
util.startBranch();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Scratch3ControlBlocks.prototype.ifElse = function(args, util) {
|
||||
// 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) {
|
||||
util.stackFrame.executedInFrame = true;
|
||||
if (args.CONDITION) {
|
||||
util.startSubstack(1);
|
||||
util.startBranch(1);
|
||||
} else {
|
||||
util.startSubstack(2);
|
||||
util.startBranch(2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -23,11 +23,11 @@ function Blocks () {
|
|||
}
|
||||
|
||||
/**
|
||||
* Blockly inputs that represent statements/substacks
|
||||
* Blockly inputs that represent statements/branch.
|
||||
* are prefixed with this string.
|
||||
* @const{string}
|
||||
*/
|
||||
Blocks.SUBSTACK_INPUT_PREFIX = 'SUBSTACK';
|
||||
Blocks.BRANCH_INPUT_PREFIX = 'SUBSTACK';
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param {?string} id ID for block to get the substack for
|
||||
* @param {?number} substackNum Which substack to select (e.g. for if-else)
|
||||
* @return {?string} ID of block in the substack
|
||||
* Get the branch for a particular C-shaped block.
|
||||
* @param {?string} id ID for block to get the branch for.
|
||||
* @param {?number} branchNum Which branch to select (e.g. for if-else).
|
||||
* @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];
|
||||
if (typeof block === 'undefined') return null;
|
||||
if (!substackNum) substackNum = 1;
|
||||
if (!branchNum) branchNum = 1;
|
||||
|
||||
var inputName = Blocks.SUBSTACK_INPUT_PREFIX;
|
||||
if (substackNum > 1) {
|
||||
inputName += substackNum;
|
||||
var inputName = Blocks.BRANCH_INPUT_PREFIX;
|
||||
if (branchNum > 1) {
|
||||
inputName += branchNum;
|
||||
}
|
||||
|
||||
// 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.
|
||||
* @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) {
|
||||
if (typeof this._blocks[id] === 'undefined') return null;
|
||||
var inputs = {};
|
||||
for (var input in this._blocks[id].inputs) {
|
||||
// Ignore blocks prefixed with substack prefix.
|
||||
if (input.substring(0, Blocks.SUBSTACK_INPUT_PREFIX.length)
|
||||
!= Blocks.SUBSTACK_INPUT_PREFIX) {
|
||||
// Ignore blocks prefixed with branch prefix.
|
||||
if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length)
|
||||
!= Blocks.BRANCH_INPUT_PREFIX) {
|
||||
inputs[input] = this._blocks[id].inputs[input];
|
||||
}
|
||||
}
|
||||
|
@ -266,7 +266,7 @@ Blocks.prototype.deleteBlock = function (e) {
|
|||
this.deleteBlock({id: block.next});
|
||||
}
|
||||
|
||||
// Delete inputs (including substacks)
|
||||
// Delete inputs (including branches)
|
||||
for (var input in block.inputs) {
|
||||
// If it's null, the block in this input moved away.
|
||||
if (block.inputs[input].block !== null) {
|
||||
|
|
|
@ -58,7 +58,7 @@ var execute = function (sequencer, thread) {
|
|||
// If we've gotten this far, all of the input blocks are evaluated,
|
||||
// and `argValues` is fully populated. So, execute the block primitive.
|
||||
// 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 = {};
|
||||
|
||||
var primitiveReportedValue = null;
|
||||
|
@ -74,8 +74,8 @@ var execute = function (sequencer, thread) {
|
|||
sequencer.proceedThread(thread);
|
||||
},
|
||||
stackFrame: currentStackFrame.executionContext,
|
||||
startSubstack: function (substackNum) {
|
||||
sequencer.stepToSubstack(thread, substackNum);
|
||||
startBranch: function (branchNum) {
|
||||
sequencer.stepToBranch(thread, branchNum);
|
||||
},
|
||||
target: target
|
||||
});
|
||||
|
|
|
@ -85,7 +85,7 @@ Sequencer.prototype.stepThreads = function (threads) {
|
|||
Sequencer.prototype.startThread = function (thread) {
|
||||
var currentBlockId = thread.peekStack();
|
||||
if (!currentBlockId) {
|
||||
// A "null block" - empty substack.
|
||||
// A "null block" - empty branch.
|
||||
// Yield for the frame.
|
||||
thread.popStack();
|
||||
thread.setStatus(Thread.STATUS_YIELD_FRAME);
|
||||
|
@ -102,22 +102,22 @@ Sequencer.prototype.startThread = function (thread) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Step a thread into a block's substack.
|
||||
* @param {!Thread} thread Thread object to step to substack.
|
||||
* @param {Number} substackNum Which substack to step to (i.e., 1, 2).
|
||||
* Step a thread into a block's branch.
|
||||
* @param {!Thread} thread Thread object to step to branch.
|
||||
* @param {Number} branchNum Which branch to step to (i.e., 1, 2).
|
||||
*/
|
||||
Sequencer.prototype.stepToSubstack = function (thread, substackNum) {
|
||||
if (!substackNum) {
|
||||
substackNum = 1;
|
||||
Sequencer.prototype.stepToBranch = function (thread, branchNum) {
|
||||
if (!branchNum) {
|
||||
branchNum = 1;
|
||||
}
|
||||
var currentBlockId = thread.peekStack();
|
||||
var substackId = this.runtime.targetForThread(thread).blocks.getSubstack(
|
||||
var branchId = this.runtime.targetForThread(thread).blocks.getBranch(
|
||||
currentBlockId,
|
||||
substackNum
|
||||
branchNum
|
||||
);
|
||||
if (substackId) {
|
||||
// Push substack ID to the thread's stack.
|
||||
thread.pushStack(substackId);
|
||||
if (branchId) {
|
||||
// Push branch ID to the thread's stack.
|
||||
thread.pushStack(branchId);
|
||||
} else {
|
||||
// Push null, so we come back to the current block.
|
||||
thread.pushStack(null);
|
||||
|
|
4
test/fixtures/events.json
vendored
4
test/fixtures/events.json
vendored
|
@ -12,13 +12,13 @@
|
|||
"!6Ahqg4f}Ljl}X5Hws?Z"
|
||||
]
|
||||
},
|
||||
"createsubstack": {
|
||||
"createbranch": {
|
||||
"name": "block",
|
||||
"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>"
|
||||
}
|
||||
},
|
||||
"createtwosubstacks": {
|
||||
"createtwobranches": {
|
||||
"name": "block",
|
||||
"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>"
|
||||
|
|
|
@ -43,8 +43,8 @@ test('create event', function (t) {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('create with substack', function (t) {
|
||||
var result = adapter(events.createsubstack);
|
||||
test('create with branch', function (t) {
|
||||
var result = adapter(events.createbranch);
|
||||
// Outer block
|
||||
t.type(result[0].id, '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].topLevel, 'boolean');
|
||||
t.equal(result[0].topLevel, true);
|
||||
// In substack
|
||||
var substackBlockId = result[0].inputs['SUBSTACK']['block'];
|
||||
t.type(substackBlockId, 'string');
|
||||
// Find actual substack block
|
||||
var substackBlock = null;
|
||||
// In branch
|
||||
var branchBlockId = result[0].inputs['SUBSTACK']['block'];
|
||||
t.type(branchBlockId, 'string');
|
||||
// Find actual branch block
|
||||
var branchBlock = null;
|
||||
for (var i = 0; i < result.length; i++) {
|
||||
if (result[i].id == substackBlockId) {
|
||||
substackBlock = result[i];
|
||||
if (result[i].id == branchBlockId) {
|
||||
branchBlock = result[i];
|
||||
}
|
||||
}
|
||||
t.type(substackBlock, 'object');
|
||||
t.type(branchBlock, 'object');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('create with two substacks', function (t) {
|
||||
var result = adapter(events.createtwosubstacks);
|
||||
test('create with two branches', function (t) {
|
||||
var result = adapter(events.createtwobranches);
|
||||
// Outer block
|
||||
t.type(result[0].id, '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].topLevel, 'boolean');
|
||||
t.equal(result[0].topLevel, true);
|
||||
// In substacks
|
||||
var firstSubstackBlockId = result[0].inputs['SUBSTACK']['block'];
|
||||
var secondSubstackBlockId = result[0].inputs['SUBSTACK2']['block'];
|
||||
t.type(firstSubstackBlockId, 'string');
|
||||
t.type(secondSubstackBlockId, 'string');
|
||||
// Find actual substack blocks
|
||||
var firstSubstackBlock = null;
|
||||
var secondSubstackBlock = null;
|
||||
// In branchs
|
||||
var firstBranchBlockId = result[0].inputs['SUBSTACK']['block'];
|
||||
var secondBranchBlockId = result[0].inputs['SUBSTACK2']['block'];
|
||||
t.type(firstBranchBlockId, 'string');
|
||||
t.type(secondBranchBlockId, 'string');
|
||||
// Find actual branch blocks
|
||||
var firstBranchBlock = null;
|
||||
var secondBranchBlock = null;
|
||||
for (var i = 0; i < result.length; i++) {
|
||||
if (result[i].id == firstSubstackBlockId) {
|
||||
firstSubstackBlock = result[i];
|
||||
if (result[i].id == firstBranchBlockId) {
|
||||
firstBranchBlock = result[i];
|
||||
}
|
||||
if (result[i].id == secondSubstackBlockId) {
|
||||
secondSubstackBlock = result[i];
|
||||
if (result[i].id == secondBranchBlockId) {
|
||||
secondBranchBlock = result[i];
|
||||
}
|
||||
}
|
||||
t.type(firstSubstackBlock, 'object');
|
||||
t.type(secondSubstackBlock, 'object');
|
||||
t.type(firstBranchBlock, 'object');
|
||||
t.type(secondBranchBlock, 'object');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ test('spec', function (t) {
|
|||
t.type(b.getBlock, 'function');
|
||||
t.type(b.getStacks, 'function');
|
||||
t.type(b.getNextBlock, 'function');
|
||||
t.type(b.getSubstack, 'function');
|
||||
t.type(b.getBranch, 'function');
|
||||
t.type(b.getOpcode, 'function');
|
||||
|
||||
|
||||
|
@ -119,9 +119,9 @@ test('getNextBlock', function (t) {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('getSubstack', function (t) {
|
||||
test('getBranch', function (t) {
|
||||
var b = new Blocks();
|
||||
// Single substack
|
||||
// Single branch
|
||||
b.createBlock({
|
||||
id: 'foo',
|
||||
opcode: 'TEST_BLOCK',
|
||||
|
@ -144,18 +144,18 @@ test('getSubstack', function (t) {
|
|||
topLevel: false
|
||||
});
|
||||
|
||||
var substack = b.getSubstack('foo');
|
||||
t.equals(substack, 'foo2');
|
||||
var branch = b.getBranch('foo');
|
||||
t.equals(branch, 'foo2');
|
||||
|
||||
var notSubstack = b.getSubstack('?');
|
||||
t.equals(notSubstack, null);
|
||||
var notBranch = b.getBranch('?');
|
||||
t.equals(notBranch, null);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('getSubstack2', function (t) {
|
||||
test('getBranch2', function (t) {
|
||||
var b = new Blocks();
|
||||
// Second substack
|
||||
// Second branch
|
||||
b.createBlock({
|
||||
id: 'foo',
|
||||
opcode: 'TEST_BLOCK',
|
||||
|
@ -190,15 +190,15 @@ test('getSubstack2', function (t) {
|
|||
topLevel: false
|
||||
});
|
||||
|
||||
var substack1 = b.getSubstack('foo', 1);
|
||||
var substack2 = b.getSubstack('foo', 2);
|
||||
t.equals(substack1, 'foo2');
|
||||
t.equals(substack2, 'foo3');
|
||||
var branch1 = b.getBranch('foo', 1);
|
||||
var branch2 = b.getBranch('foo', 2);
|
||||
t.equals(branch1, 'foo2');
|
||||
t.equals(branch2, 'foo3');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('getSubstack with none', function (t) {
|
||||
test('getBranch with none', function (t) {
|
||||
var b = new Blocks();
|
||||
b.createBlock({
|
||||
id: 'foo',
|
||||
|
@ -208,8 +208,8 @@ test('getSubstack with none', function (t) {
|
|||
inputs: {},
|
||||
topLevel: true
|
||||
});
|
||||
var noSubstack = b.getSubstack('foo');
|
||||
t.equals(noSubstack, null);
|
||||
var noBranch = b.getBranch('foo');
|
||||
t.equals(noBranch, null);
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
|
11
vm.min.js
vendored
11
vm.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -109,6 +109,10 @@
|
|||
this.vmWorker.postMessage({method: 'stopAll'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.animationFrame = function () {
|
||||
this.vmWorker.postMessage({method: 'animationFrame'});
|
||||
};
|
||||
|
||||
/**
|
||||
* Export and bind to `window`
|
||||
*/
|
||||
|
@ -1026,6 +1030,9 @@
|
|||
var queueIndex = -1;
|
||||
|
||||
function cleanUpNextTick() {
|
||||
if (!draining || !currentQueue) {
|
||||
return;
|
||||
}
|
||||
draining = false;
|
||||
if (currentQueue.length) {
|
||||
queue = currentQueue.concat(queue);
|
||||
|
|
Loading…
Reference in a new issue