Skip to content

Commit f35f772

Browse files
committed
don't fill empty slots inside JavaScript function block+support empty slot MultiArgMorph
1 parent 46ad6fb commit f35f772

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/threads.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8274,6 +8274,7 @@ VariableFrame.prototype.allNames = function (upTo, includeHidden) {
82748274

82758275
function JSCompiler(outerScope) {
82768276
this.implicitParamCount = 0;
8277+
this.implicitMultiArgCount = 0;
82778278
this.params = 0;
82788279
this.gensymArgIndexes = new Map();
82798280
this.scope = new Map();
@@ -8286,17 +8287,19 @@ function JSCompiler(outerScope) {
82868287
this.scope.outerScope = outerScope;
82878288
}
82888289

8289-
JSCompiler.prototype.C = Object.freeze({
8290+
JSCompiler.prototype.C = {
82908291
// functions used in compiled rings
8291-
'__proto__': null,
82928292
'ring': (func, ...inputs) => (func.inputs = inputs, func),
82938293
'invoke': (proc, func, argsList) =>
82948294
func ? (
82958295
typeof func === 'function' ?
8296-
func.apply(null, argsList.itemsArray().concat(proc)) :
8296+
func.apply(
8297+
proc.blockReceiver(),
8298+
argsList.itemsArray().concat(proc)
8299+
) :
82978300
invoke(func, argsList)
82988301
) : null
8299-
});
8302+
};
83008303

83018304
JSCompiler.prototype.toString = () => 'a JSCompiler';
83028305

@@ -8335,8 +8338,15 @@ JSCompiler.prototype.functionHead = function () {
83358338
str1 += 'proc=params.pop();\n';
83368339
switch (params) {
83378340
case -1:
8341+
params = this.implicitParamCount;
83388342
// implicit parameters mode
8339-
switch (params = this.implicitParamCount) {
8343+
if (this.implicitMultiArgCount) {
8344+
if (params === this.implicitMultiArgCount) {
8345+
return str1 + 'var plist=new List(params);\n';
8346+
}
8347+
str1 += 'var plist=new List(params.slice());\n';
8348+
}
8349+
switch (params) {
83408350
case 0:
83418351
return str1;
83428352
case 1:
@@ -8392,6 +8402,9 @@ JSCompiler.prototype.findEmptySlot = function findEmptySlot(m) {
83928402
// don't look in rings (they are not current scope)
83938403
return false;
83948404
}
8405+
if (m.selector === 'reportJSFunction') {
8406+
return false;
8407+
}
83958408
m = m.children;
83968409
var i = m.length;
83978410
while (i) {
@@ -8407,8 +8420,8 @@ JSCompiler.prototype.compileFunctionBody = function (aContext) {
84078420
parameters = aContext.inputs,
84088421
code;
84098422

8410-
if (block instanceof Array) {
8411-
throw new Error('can\'t compile empty ring');
8423+
if (!(block instanceof BlockMorph)) {
8424+
throw new Error('compiling does not yet support\nempty rings');
84128425
}
84138426

84148427
if (parameters.length) {
@@ -8554,6 +8567,24 @@ JSCompiler.prototype.compileExpression = function (block) {
85548567
')';
85558568
case 'reportNot':
85568569
return '!' + this.compileInput(inputs[0]);
8570+
case 'reifyScript':
8571+
case 'reifyReporter':
8572+
case 'reifyPredicate':
8573+
return new JSCompiler(this.scope).compileFunctionBody({
8574+
'expression': inputs[0].children[0],
8575+
'inputs': inputs[1].inputs().map(x => x.children[0].blockSpec),
8576+
});
8577+
case 'reportJSFunction':
8578+
//don't fill empty slots inside JavaScript function block
8579+
target = this.params;
8580+
this.params = -2;
8581+
selector = 'proc.reportJSFunction(' +
8582+
this.compileInput(inputs[0]) +
8583+
',' +
8584+
this.compileInput(inputs[1]) +
8585+
')';
8586+
this.params = target;
8587+
return selector;
85578588
default:
85588589
if (Process.prototype[selector]) {
85598590
if (selector.substring(0, 6) === 'report') {
@@ -8599,17 +8630,18 @@ JSCompiler.prototype.compileInput = function (inp) {
85998630
if (inp.isEmptySlot != null && inp.isEmptySlot()) {
86008631
if (this.params === -1) {
86018632
// implicit parameter
8633+
if (inp instanceof MultiArgMorph) {
8634+
this.implicitParamCount += 1;
8635+
this.implicitMultiArgCount += 1;
8636+
return 'plist';
8637+
}
86028638
return 'params[' + this.implicitParamCount++ + ']';
86038639
}
8640+
if (inp instanceof MultiArgMorph) {
8641+
return 'new List()';
8642+
}
86048643
return '""';
86058644
}
8606-
if (inp instanceof RingMorph) {
8607-
inp = inp.children;
8608-
return new JSCompiler(this.scope).compileFunctionBody({
8609-
'expression': inp[0].children[0],
8610-
'inputs': inp[1].inputs().map(x => x.children[0].blockSpec),
8611-
});
8612-
}
86138645
if (inp instanceof MultiArgMorph) {
86148646
return 'new List([' + this.compileInputs(inp.inputs()) + '])';
86158647
}

0 commit comments

Comments
 (0)