Skip to content

Commit

Permalink
SCLang: fix performArgs, wrong type was assumed as default. (supercol…
Browse files Browse the repository at this point in the history
…lider#6388)

Use slotCopy rather than SetObject as the default for kwargs is nil, not an empty array.
authored-by: JordanHendersonMusic <j.henderson.music@outloook.com>
  • Loading branch information
JordanHendersonMusic authored Jul 10, 2024
1 parent a404575 commit d52b052
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lang/LangSource/PyrMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,10 @@ void prepareArgsForExecute(VMGlobals* g, PyrBlock* block, PyrFrame* callFrame, l

// Put var arg array or default on stack.
if (methHasVarArg) {
SetObject(outCallFrameStack + methNumNormArgs,
variableArgumentsArray ? variableArgumentsArray : slotRawObject(&proto->slots[methNumNormArgs]));
if (variableArgumentsArray)
SetObject(outCallFrameStack + methNumNormArgs, variableArgumentsArray);
else
slotCopy(outCallFrameStack + methNumNormArgs, proto->slots + methNumNormArgs);
}

// Put var kwarg array or default on stack.
Expand All @@ -483,7 +485,7 @@ void prepareArgsForExecute(VMGlobals* g, PyrBlock* block, PyrFrame* callFrame, l
keywordArgumentsArray->size = keywordArgumentSize;
SetObject(outCallFrameStack + methNumNormArgs + 1, keywordArgumentsArray);
} else {
SetObject(outCallFrameStack + methNumNormArgs + 1, slotRawObject(&proto->slots[methNumNormArgs + 1]));
slotCopy(outCallFrameStack + methNumNormArgs + 1, proto->slots + methNumNormArgs + 1);
}
}

Expand Down
41 changes: 41 additions & 0 deletions testsuite/classlibrary/TestKwargs.sc
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,45 @@ TestKwargs : UnitTest {
(a: 1, b: 2, c: 3, args: [4], kwargs: [\foo, 10])
);
}

test_wrappers {
var inlined = { |func|
{ |...args, kwargs|
func.performArgs(\value, args, kwargs)
}
};
var asVar = { |func|
{ |...args, kwargs|
var val = func.performArgs(\value, args, kwargs);
val
}
};

this.assertEquals(
inlined.({ |a, b=100, c| [a, b, c] }).([1, 2]),
[[1, 2], 100, nil]
);
this.assertEquals(
asVar.({ |a, b=100, c| [a, b, c] }).([1, 2]),
[[1, 2], 100, nil]
);

this.assertEquals(
inlined.({ |a, b=100, c| [a, b, c] }).([1, 2], 42),
[[1, 2], 42, nil]
);
this.assertEquals(
asVar.({ |a, b=100, c| [a, b, c] }).([1, 2], 42),
[[1, 2], 42, nil]
);

this.assertEquals(
inlined.({ |a, b=100, c| [a, b, c] }).([1, 2], 42, c: 23),
[[1, 2], 42, 23]
);
this.assertEquals(
asVar.({ |a, b=100, c| [a, b, c] }).([1, 2], 42, c: 23),
[[1, 2], 42, 23]
);
}
}

0 comments on commit d52b052

Please sign in to comment.