diff options
Diffstat (limited to 'threads.js')
| -rw-r--r-- | threads.js | 190 |
1 files changed, 57 insertions, 133 deletions
@@ -40,8 +40,8 @@ ThreadManager Process Context + Variable VariableFrame - UpvarReference credits @@ -83,13 +83,12 @@ ArgLabelMorph, localize, XML_Element, hex_sha512*/ // Global stuff //////////////////////////////////////////////////////// -modules.threads = '2014-July-28'; +modules.threads = '2014-September-29'; var ThreadManager; var Process; var Context; var VariableFrame; -var UpvarReference; function snapEquals(a, b) { if (a instanceof List || (b instanceof List)) { @@ -134,7 +133,11 @@ ThreadManager.prototype.toggleProcess = function (block) { } }; -ThreadManager.prototype.startProcess = function (block, isThreadSafe) { +ThreadManager.prototype.startProcess = function ( + block, + isThreadSafe, + exportResult +) { var active = this.findProcess(block), top = block.topBlock(), newProc; @@ -147,6 +150,7 @@ ThreadManager.prototype.startProcess = function (block, isThreadSafe) { } top.addHighlight(); newProc = new Process(block.topBlock()); + newProc.exportResult = exportResult; this.processes.push(newProc); return newProc; }; @@ -232,11 +236,17 @@ ThreadManager.prototype.removeTerminatedProcesses = function () { if (proc.topBlock instanceof ReporterBlockMorph) { if (proc.homeContext.inputs[0] instanceof List) { - proc.topBlock.showBubble(new ListWatcherMorph( - proc.homeContext.inputs[0] - )); + proc.topBlock.showBubble( + new ListWatcherMorph( + proc.homeContext.inputs[0] + ), + proc.exportResult + ); } else { - proc.topBlock.showBubble(proc.homeContext.inputs[0]); + proc.topBlock.showBubble( + proc.homeContext.inputs[0], + proc.exportResult + ); } } } else { @@ -302,6 +312,8 @@ ThreadManager.prototype.findProcess = function (block) { httpRequest active instance of an HttpRequest or null pauseOffset msecs between the start of an interpolated operation and when the process was paused + exportResult boolean flag indicating whether a picture of the top + block along with the result bubble shoud be exported */ Process.prototype = {}; @@ -325,6 +337,7 @@ function Process(topBlock) { this.isPaused = false; this.pauseOffset = null; this.frameCount = 0; + this.exportResult = false; if (topBlock) { this.homeContext.receiver = topBlock.receiver(); @@ -586,8 +599,7 @@ Process.prototype.evaluateSequence = function (arr) { outer = this.context.outerContext, isLambda = this.context.isLambda, isImplicitLambda = this.context.isImplicitLambda, - isCustomBlock = this.context.isCustomBlock, - upvars = this.context.upvars; + isCustomBlock = this.context.isCustomBlock; if (pc === (arr.length - 1)) { // tail call elimination this.context = new Context( this.context.parentContext, @@ -598,9 +610,6 @@ Process.prototype.evaluateSequence = function (arr) { this.context.isLambda = isLambda; this.context.isImplicitLambda = isImplicitLambda; this.context.isCustomBlock = isCustomBlock; - if (upvars) { - this.context.upvars = new UpvarReference(upvars); - } } else { if (pc >= arr.length) { this.popContext(); @@ -760,7 +769,7 @@ Process.prototype.evaluate = function ( if (!context) {return null; } if (context instanceof Function) { return context.apply( - this.homeContext.receiver, + this.blockReceiver(), args.asArray().concat([this]) ); } @@ -846,9 +855,6 @@ Process.prototype.evaluate = function ( } } } - if (this.context.upvars) { - runnable.upvars = new UpvarReference(this.context.upvars); - } if (runnable.expression instanceof CommandBlockMorph) { runnable.expression = runnable.expression.blockSequence(); @@ -1012,7 +1018,6 @@ Process.prototype.evaluateCustomBlock = function () { extra, i, value, - upvars, outer; if (!context) {return null; } @@ -1047,26 +1052,14 @@ Process.prototype.evaluateCustomBlock = function () { outer.variables.addVar(context.inputs[i], value); // if the parameter is an upvar, - // create an UpvarReference to it + // create a reference to the variable it points to if (declarations[context.inputs[i]][0] === '%upvar') { - if (!upvars) { // lazy initialization - upvars = new UpvarReference(this.context.upvars); - } - upvars.addReference( - value, - context.inputs[i], - outer.variables - ); + this.context.outerContext.variables.vars[value] = + outer.variables.vars[context.inputs[i]]; } } } - if (upvars) { - runnable.upvars = upvars; - } else if (this.context.upvars) { - runnable.upvars = new UpvarReference(this.context.upvars); - } - if (runnable.expression instanceof CommandBlockMorph) { runnable.expression = runnable.expression.blockSequence(); } @@ -1107,11 +1100,8 @@ Process.prototype.doChangeVar = function (varName, value) { Process.prototype.reportGetVar = function () { // assumes a getter block whose blockSpec is a variable name - var varName = this.context.expression.blockSpec; - return this.context.variables.getVar( - varName, - this.context.upvars + this.context.expression.blockSpec ); }; @@ -1318,8 +1308,7 @@ Process.prototype.doIf = function () { outer = this.context.outerContext, // for tail call elimination isLambda = this.context.isLambda, isImplicitLambda = this.context.isImplicitLambda, - isCustomBlock = this.context.isCustomBlock, - upvars = this.context.upvars; + isCustomBlock = this.context.isCustomBlock; this.popContext(); if (args[0]) { @@ -1328,7 +1317,6 @@ Process.prototype.doIf = function () { this.context.isLambda = isLambda; this.context.isImplicitLambda = isImplicitLambda; this.context.isCustomBlock = isCustomBlock; - this.context.upvars = new UpvarReference(upvars); } } this.pushContext(); @@ -1339,8 +1327,7 @@ Process.prototype.doIfElse = function () { outer = this.context.outerContext, // for tail call elimination isLambda = this.context.isLambda, isImplicitLambda = this.context.isImplicitLambda, - isCustomBlock = this.context.isCustomBlock, - upvars = this.context.upvars; + isCustomBlock = this.context.isCustomBlock; this.popContext(); if (args[0]) { @@ -1358,7 +1345,6 @@ Process.prototype.doIfElse = function () { this.context.isLambda = isLambda; this.context.isImplicitLambda = isImplicitLambda; this.context.isCustomBlock = isCustomBlock; - this.context.upvars = new UpvarReference(upvars); } this.pushContext(); @@ -1537,8 +1523,7 @@ Process.prototype.doRepeat = function (counter, body) { outer = this.context.outerContext, // for tail call elimination isLambda = this.context.isLambda, isImplicitLambda = this.context.isImplicitLambda, - isCustomBlock = this.context.isCustomBlock, - upvars = this.context.upvars; + isCustomBlock = this.context.isCustomBlock; if (counter < 1) { // was '=== 0', which caused infinite loops on non-ints return null; @@ -1550,7 +1535,6 @@ Process.prototype.doRepeat = function (counter, body) { this.context.isLambda = isLambda; this.context.isImplicitLambda = isImplicitLambda; this.context.isCustomBlock = isCustomBlock; - this.context.upvars = new UpvarReference(upvars); this.context.addInput(counter - 1); @@ -2728,7 +2712,6 @@ Process.prototype.inputOption = function (dta) { // Process stack Process.prototype.pushContext = function (expression, outerContext) { - var upvars = this.context ? this.context.upvars : null; this.context = new Context( this.context, expression, @@ -2737,9 +2720,6 @@ Process.prototype.pushContext = function (expression, outerContext) { this.context ? // check needed due to tail call elimination this.context.receiver : this.homeContext.receiver ); - if (upvars) { - this.context.upvars = new UpvarReference(upvars); - } }; Process.prototype.popContext = function () { @@ -2785,7 +2765,6 @@ Process.prototype.reportFrameCount = function () { null or a String denoting a selector, e.g. 'doYield' receiver the object to which the expression applies, if any variables the current VariableFrame, if any - upvars the current UpvarReference, if any (default: null) inputs an array of input values computed so far (if expression is a BlockMorph) pc the index of the next block to evaluate @@ -2815,7 +2794,6 @@ function Context( this.variables.parentFrame = this.outerContext.variables; this.receiver = this.outerContext.receiver; } - this.upvars = null; // set to an UpvarReference in custom blocks this.inputs = []; this.pc = 0; this.startTime = null; @@ -2968,6 +2946,20 @@ Context.prototype.stackSize = function () { return 1 + this.parentContext.stackSize(); }; +// Variable ///////////////////////////////////////////////////////////////// + +function Variable(value) { + this.value = value; +} + +Variable.prototype.toString = function () { + return 'a Variable [' + this.value + ']'; +}; + +Variable.prototype.copy = function () { + return new Variable(this.value); +}; + // VariableFrame /////////////////////////////////////////////////////// function VariableFrame(parentFrame, owner) { @@ -2981,8 +2973,11 @@ VariableFrame.prototype.toString = function () { }; VariableFrame.prototype.copy = function () { - var frame = new VariableFrame(this.parentFrame); - frame.vars = copy(this.vars); + var frame = new VariableFrame(this.parentFrame), + myself = this; + this.names().forEach(function (vName) { + frame.addVar(vName, myself.getVar(vName)); + }); return frame; }; @@ -3035,7 +3030,7 @@ VariableFrame.prototype.setVar = function (name, value) { */ var frame = this.find(name); if (frame) { - frame.vars[name] = value; + frame.vars[name].value = value; } }; @@ -3049,21 +3044,20 @@ VariableFrame.prototype.changeVar = function (name, delta) { var frame = this.find(name), value; if (frame) { - value = parseFloat(frame.vars[name]); + value = parseFloat(frame.vars[name].value); if (isNaN(value)) { - frame.vars[name] = delta; + frame.vars[name].value = delta; } else { - frame.vars[name] = value + parseFloat(delta); + frame.vars[name].value = value + parseFloat(delta); } } }; -VariableFrame.prototype.getVar = function (name, upvars) { +VariableFrame.prototype.getVar = function (name) { var frame = this.silentFind(name), - value, - upvarReference; + value; if (frame) { - value = frame.vars[name]; + value = frame.vars[name].value; return (value === 0 ? 0 : value === false ? false : value === '' ? '' @@ -3073,12 +3067,6 @@ VariableFrame.prototype.getVar = function (name, upvars) { // empty input with a Binding-ID called without an argument return ''; } - if (upvars) { - upvarReference = upvars.find(name); - if (upvarReference) { - return upvarReference.getVar(name); - } - } throw new Error( 'a variable of name \'' + name @@ -3087,7 +3075,7 @@ VariableFrame.prototype.getVar = function (name, upvars) { }; VariableFrame.prototype.addVar = function (name, value) { - this.vars[name] = (value === 0 ? 0 + this.vars[name] = new Variable(value === 0 ? 0 : value === false ? false : value === '' ? '' : value || 0); }; @@ -3144,67 +3132,3 @@ VariableFrame.prototype.allNames = function () { } return answer; }; - -// Variable ///////////////////////////////////////////////////////////////// - -function Variable(value) { - this.value = value; -} - -Variable.prototype.toString = function () { - return 'a Variable [' + this.value + ']'; -}; - -Variable.prototype.copy = function () { - return new Variable(this.value); -}; - -// UpvarReference /////////////////////////////////////////////////////////// - -// ... quasi-inherits some features from VariableFrame - -function UpvarReference(parent) { - this.vars = {}; // structure: {upvarName : [varName, varFrame]} - this.parentFrame = parent || null; -} - -UpvarReference.prototype.addReference = function ( - upvarName, - varName, - varFrame -) { - this.vars[upvarName] = [varName, varFrame]; -}; - -UpvarReference.prototype.find = function (name) { -/* - answer the closest upvar reference containing - the specified variable, or answer null. -*/ - if (this.vars[name] !== undefined) { - return this; - } - if (this.parentFrame) { - return this.parentFrame.find(name); - } - return null; -}; - -UpvarReference.prototype.getVar = function (name) { - var varName = this.vars[name][0], - varFrame = this.vars[name][1], - value = varFrame.vars[varName]; - return (value === 0 ? 0 : value || 0); // don't return null -}; - -// UpvarReference tools - -UpvarReference.prototype.toString = function () { - return 'an UpvarReference {' + this.names() + '}'; -}; - -// UpvarReference quasi-inheritance from VariableFrame - -UpvarReference.prototype.names = VariableFrame.prototype.names; -UpvarReference.prototype.allNames = VariableFrame.prototype.allNames; -UpvarReference.prototype.allNamesDict = VariableFrame.prototype.allNamesDict; |
