Skip to content

Commit

Permalink
Merge pull request #1890 from endojs/markm-1888-reject-extra-args
Browse files Browse the repository at this point in the history
fix(exo)!: reject extra args by default
  • Loading branch information
erights authored Dec 11, 2023
2 parents c64bc4e + 4d100ef commit 281e318
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
17 changes: 9 additions & 8 deletions packages/exo/src/exo-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const PassableMethodGuard = M.call().rest(M.any()).returns(M.any());
* @param {Passable[]} syncArgs
* @param {MatchConfig} matchConfig
* @param {string} [label]
* @returns {Passable[]} Returns the args that should be passed to the
* raw method
* @returns {import('@endo/pass-style').Passable[]}
* Returns the args that should be passed to the raw method.
*/
const defendSyncArgs = (syncArgs, matchConfig, label = undefined) => {
const {
Expand All @@ -69,7 +69,8 @@ const defendSyncArgs = (syncArgs, matchConfig, label = undefined) => {
redactedIndices,
} = matchConfig;

// Use syncArgs if possible, but copy it when necessary to implement redactions.
// Use syncArgs if possible, but copy it when necessary to implement
// redactions.
let matchableArgs = syncArgs;
if (restArgGuardIsRaw && syncArgs.length > declaredLen) {
const restLen = syncArgs.length - declaredLen;
Expand All @@ -96,11 +97,11 @@ const defendSyncArgs = (syncArgs, matchConfig, label = undefined) => {
if (hasRestArgGuard) {
return syncArgs;
}
if (syncArgs.length <= declaredLen) {
return syncArgs;
}
// Ignore extraneous arguments, as a JS function call would do.
return syncArgs.slice(0, declaredLen);
syncArgs.length <= declaredLen ||
Fail`${q(label)} accepts at most ${q(declaredLen)} arguments, not ${q(
syncArgs.length,
)}: ${syncArgs}`;
return syncArgs;
};

/**
Expand Down
7 changes: 5 additions & 2 deletions packages/exo/test/test-heap-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ const NoExtraI = M.interface('NoExtra', {
});

test('what happens with extra arguments', t => {
const exo = makeExo('WithExtra', NoExtraI, {
const exo = makeExo('NoExtraArgs', NoExtraI, {
foo(x) {
t.is(x, undefined);
},
});
exo.foo('an extra arg');
t.throws(() => exo.foo('an extra arg'), {
message:
'"In \\"foo\\" method of (NoExtraArgs)" accepts at most 0 arguments, not 1: ["an extra arg"]',
});
});

const OptionalArrayI = M.interface('OptionalArray', {
Expand Down

0 comments on commit 281e318

Please sign in to comment.