diff --git a/src/threads.js b/src/threads.js index 1b0db94b..1b2fc017 100644 --- a/src/threads.js +++ b/src/threads.js @@ -7506,10 +7506,9 @@ VariableFrame.prototype.allNames = function (upTo, includeHidden) { function JSCompiler(aProcess) { this.process = aProcess; this.source = null; // a context - this.gensyms = new Map(); // temp dictionary for parameter substitutions + this.gensyms = null; // temp dictionary for parameter substitutions this.implicitParams = null; this.paramCount = null; - this.scriptVarCounter = null; } JSCompiler.prototype.toString = function () { @@ -7519,19 +7518,12 @@ JSCompiler.prototype.toString = function () { JSCompiler.prototype.compileFunction = function (aContext, implicitParamCount) { var block = aContext.expression, parameters = aContext.inputs, + parms = [], hasEmptySlots = false, - plength = 0; + i; this.source = aContext; - if (implicitParamCount == null || implicitParamCount === '') { - this.implicitParams = 1; - } else { - this.implicitParams = implicitParamCount >> 0; - if (this.implicitParams < 0) { - // use 1 if implicitParamCount doesn't make sense - this.implicitParams = 1; - } - } + this.implicitParams = implicitParamCount || 1; // scan for empty input slots hasEmptySlots = !isNil(detect( @@ -7540,7 +7532,7 @@ JSCompiler.prototype.compileFunction = function (aContext, implicitParamCount) { )); // translate formal parameters into gensyms - this.gensyms.clear(); + this.gensyms = new Map(); this.paramCount = 0; if (parameters.length) { // test for conflicts @@ -7553,34 +7545,33 @@ JSCompiler.prototype.compileFunction = function (aContext, implicitParamCount) { } // map explicit formal parameters parameters.forEach((pName, idx) => { - this.gensyms.set(pName, 'p[' + idx + ']'); + var pn = 'p' + idx; + parms.push(pn); + this.gensyms.set(pName, pn); }); - plength = parameters.length; } else if (hasEmptySlots) { - plength = this.implicitParams; - } + if (this.implicitParams > 1) { + for (i = 0; i < this.implicitParams; i += 1) { + parms.push('p' + i); + } + } else { + // allow for a single implicit formal parameter + parms = ['p0']; + } + } // compile using gensyms - this.scriptVarCounter = 0; - var code = 'proc=p.pop();\n'; - if (plength) { - // fill missing parameters with empty string - code += 'while(' + plength + '>p.length)p.push("");\n'; - } if (block instanceof CommandBlockMorph) { - code += this.compileSequence(block) + 'return ""'; - } else { - code += 'return ' + this.compileExpression(block); + return Function.apply( + null, + parms.concat([this.compileSequence(block)]) + ); } - block = 'var '; - this.gensyms.forEach(function (value) { - if (value.charAt(0) === 's') { - // declare script variable - block += value + '=0,'; - } - }); - return Function('...p', block + code); + return Function.apply( + null, + parms.concat(['return ' + this.compileExpression(block)]) + ); }; JSCompiler.prototype.compileExpression = function (block) { @@ -7615,49 +7606,21 @@ JSCompiler.prototype.compileExpression = function (block) { case 'evaluate': return 'invoke(' + this.compileInput(inputs[0]) + - ', ' + + ',' + this.compileInput(inputs[1]) + ')'; // special command forms - case 'doDeclareVariables': - block = ''; - inputs[0].inputs().forEach(({children: {0: {blockSpec: name}}}) => { - if (gensym = this.gensyms.get(name)) { - // we already have that script variable, just set it to 0 - block += gensym + '='; - return; - } - var gensym = 's' + this.scriptVarCounter++; - block += gensym + '='; - this.gensyms.set(name, gensym); - }); - return block + '0'; - case 'reportGetVar': - return this.gensyms.get(block.blockSpec) || ('proc.getVarNamed("' + - this.escape(block.blockSpec) + - '")'); - case 'doSetVar': - if (inputs[0] instanceof ArgMorph && (target = this.gensyms.get(inputs[0].evaluate()))) { - // setting gensym (script or argument) variable - return target + ' = ' + this.compileInput(inputs[1]); - } - // redirect var to process - return 'proc.setVarNamed(' + + case 'doSetVar': // redirect var to process + return 'arguments[arguments.length - 1].setVarNamed(' + this.compileInput(inputs[0]) + - ', ' + + ',' + this.compileInput(inputs[1]) + ')'; - case 'doChangeVar': - if (inputs[0] instanceof ArgMorph && (target = this.gensyms.get(inputs[0].evaluate()))) { - return '{const d=' + this.compileInput(inputs[1]) + - ',v=parseFloat(' + target + ');' + - target + '=isNaN(v)?d:v+parseFloat(d)}'; - } - // redirect var to process - return 'proc.incrementVarNamed(' + + case 'doChangeVar': // redirect var to process + return 'arguments[arguments.length - 1].incrementVarNamed(' + this.compileInput(inputs[0]) + - ', ' + + ',' + this.compileInput(inputs[1]) + ')'; case 'doReport': @@ -7676,9 +7639,7 @@ JSCompiler.prototype.compileExpression = function (block) { '} else {\n' + this.compileSequence(inputs[2].evaluate()) + '}'; - case 'reportBoolean': - case 'reportNewList': - return this.compileInput(inputs[0]); + default: target = this.process[selector] ? this.process : (this.source.receiver || this.process.receiver); @@ -7687,11 +7648,13 @@ JSCompiler.prototype.compileExpression = function (block) { if (isSnapObject(target)) { if (rcvr === 'SpriteMorph.prototype') { // fix for blocks like (x position) - rcvr = 'proc.blockReceiver()'; + rcvr = 'arguments[arguments.length - 1].blockReceiver()'; } - return rcvr + '.' + selector + '(' + args + ')'; + return rcvr + '.' + selector + '(' + args +')'; } else { - return 'proc.' + selector + '(' + args + ')'; + return 'arguments[arguments.length - 1].' + + selector + + '(' + args + ')'; } } }; @@ -7706,13 +7669,13 @@ JSCompiler.prototype.compileSequence = function (commandBlock) { JSCompiler.prototype.compileInfix = function (operator, inputs) { return '(' + this.compileInput(inputs[0]) + ' ' + operator + ' ' + - this.compileInput(inputs[1]) + ')'; + this.compileInput(inputs[1]) +')'; }; JSCompiler.prototype.compileInputs = function (array) { var args = ''; array.forEach(inp => { - if (args) { + if (args.length) { args += ', '; } args += this.compileInput(inp); @@ -7728,7 +7691,7 @@ JSCompiler.prototype.compileInput = function (inp) { if (this.implicitParams > 1) { if (this.paramCount < this.implicitParams) { this.paramCount += 1; - return 'p[' + (this.paramCount - 1) + ']'; + return 'p' + (this.paramCount - 1); } throw new Error( localize('expecting') + ' ' + this.implicitParams + ' ' @@ -7736,7 +7699,7 @@ JSCompiler.prototype.compileInput = function (inp) { + this.paramCount ); } - return 'p[0]'; + return 'p0'; } else if (inp instanceof MultiArgMorph) { return 'new List([' + this.compileInputs(inp.inputs()) + '])'; } else if (inp instanceof ArgLabelMorph) { @@ -7765,6 +7728,16 @@ JSCompiler.prototype.compileInput = function (inp) { ); } } else if (inp instanceof BlockMorph) { + if (inp.selector === 'reportGetVar') { + if (this.gensyms.has(inp.blockSpec)) { + // un-quoted gensym: + return this.gensyms.get(inp.blockSpec); + } + // redirect var query to process + return 'arguments[arguments.length - 1].getVarNamed("' + + this.escape(inp.blockSpec) + + '")'; + } return this.compileExpression(inp); } else { throw new Error(