From 2b6b110c44085fd59a1a2c94fd5689cf598f8307 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Sun, 10 Dec 2023 18:00:48 -0800 Subject: [PATCH] fix(exo)!: reject extra args --- packages/exo/src/exo-tools.js | 15 +++++++-------- packages/exo/test/test-heap-classes.js | 7 +++++-- yarn.lock | 14 +++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/exo/src/exo-tools.js b/packages/exo/src/exo-tools.js index 7ccb235be3..0cdb21fe3d 100644 --- a/packages/exo/src/exo-tools.js +++ b/packages/exo/src/exo-tools.js @@ -57,8 +57,8 @@ const PassableMethodGuard = M.call().rest(M.any()).returns(M.any()); * @param {import('@endo/pass-style').Passable[]} syncArgs * @param {MatchConfig} matchConfig * @param {string} [label] - * @returns {import('@endo/pass-style').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 { @@ -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; @@ -96,11 +97,9 @@ 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`${label} accepts at most ${declaredLen} arguments, not ${syncArgs.length}`; + return syncArgs; }; /** diff --git a/packages/exo/test/test-heap-classes.js b/packages/exo/test/test-heap-classes.js index c6c3525cf7..675150187f 100644 --- a/packages/exo/test/test-heap-classes.js +++ b/packages/exo/test/test-heap-classes.js @@ -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', + }); }); const OptionalArrayI = M.interface('OptionalArray', { diff --git a/yarn.lock b/yarn.lock index d4c77a56b8..e903667c66 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5099,7 +5099,7 @@ error-ex@^1.3.1: dependencies: is-arrayish "^0.2.1" -es-abstract@^1.17.2, es-abstract@^1.19.0, es-abstract@^1.19.2, es-abstract@^1.20.1, es-abstract@^1.20.4, es-abstract@^1.22.1: +es-abstract@^1.17.2, es-abstract@^1.19.2, es-abstract@^1.20.1, es-abstract@^1.20.4, es-abstract@^1.22.1: version "1.22.3" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.22.3.tgz#48e79f5573198de6dee3589195727f4f74bc4f32" integrity sha512-eiiY8HQeYfYH2Con2berK+To6GrK2RxbPawDkGq4UiCQQfZHb6wX9qQqkbpPqaxQFcl8d9QzZqo0tGE0VcrdwA== @@ -6016,7 +6016,7 @@ function-bind@^1.1.1, function-bind@^1.1.2: resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.2.tgz#2c02d864d97f3ea6c8830c464cbd11ab6eab7a1c" integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA== -function.prototype.name@^1.1.5, function.prototype.name@^1.1.6: +function.prototype.name@^1.1.6: version "1.1.6" resolved "https://registry.yarnpkg.com/function.prototype.name/-/function.prototype.name-1.1.6.tgz#cdf315b7d90ee77a4c6ee216c3c3362da07533fd" integrity sha512-Z5kx79swU5P27WEayXM1tBi5Ze/lbIyiNgU3qyXUOf9b2rgXYyF9Dy9Cx+IQv/Lc8WCG6L82zwUPpSS9hGehIg== @@ -6026,7 +6026,7 @@ function.prototype.name@^1.1.5, function.prototype.name@^1.1.6: es-abstract "^1.22.1" functions-have-names "^1.2.3" -functions-have-names@^1.2.2, functions-have-names@^1.2.3: +functions-have-names@^1.2.3: version "1.2.3" resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834" integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ== @@ -8935,7 +8935,7 @@ object-copy@^0.1.0: define-property "^0.2.5" kind-of "^3.0.3" -object-inspect@^1.12.2, object-inspect@^1.12.3, object-inspect@^1.13.1, object-inspect@^1.9.0: +object-inspect@^1.12.3, object-inspect@^1.13.1, object-inspect@^1.9.0: version "1.13.1" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.13.1.tgz#b96c6109324ccfef6b12216a956ca4dc2ff94bc2" integrity sha512-5qoj1RUiKOMsCCNLV1CBiPYE10sziTsnmNxkAI/rZhiD63CF7IqdFGC/XzjWjpSgLf0LxXX3bDFIh0E18f6UhQ== @@ -10498,7 +10498,7 @@ regex-not@^1.0.0, regex-not@^1.0.2: extend-shallow "^3.0.2" safe-regex "^1.1.0" -regexp.prototype.flags@^1.4.3, regexp.prototype.flags@^1.5.0, regexp.prototype.flags@^1.5.1: +regexp.prototype.flags@^1.5.0, regexp.prototype.flags@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.5.1.tgz#90ce989138db209f81492edd734183ce99f9677e" integrity sha512-sy6TXMN+hnP/wMy+ISxg3krXx7BAtWVO4UouuCN/ziM9UEne0euamVNafDfvC83bRNr95y0V5iijeDQFUNpvrg== @@ -11511,7 +11511,7 @@ string.prototype.trim@^1.2.7, string.prototype.trim@^1.2.8: define-properties "^1.2.0" es-abstract "^1.22.1" -string.prototype.trimend@^1.0.6, string.prototype.trimend@^1.0.7: +string.prototype.trimend@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.7.tgz#1bb3afc5008661d73e2dc015cd4853732d6c471e" integrity sha512-Ni79DqeB72ZFq1uH/L6zJ+DKZTkOtPIHovb3YZHQViE+HDouuU4mBrLOLDn5Dde3RF8qw5qVETEjhu9locMLvA== @@ -11520,7 +11520,7 @@ string.prototype.trimend@^1.0.6, string.prototype.trimend@^1.0.7: define-properties "^1.2.0" es-abstract "^1.22.1" -string.prototype.trimstart@^1.0.6, string.prototype.trimstart@^1.0.7: +string.prototype.trimstart@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/string.prototype.trimstart/-/string.prototype.trimstart-1.0.7.tgz#d4cdb44b83a4737ffbac2d406e405d43d0184298" integrity sha512-NGhtDFu3jCEm7B4Fy0DpLewdJQOZcQ0rGbwQ/+stjnrp2i+rlKeCvos9hOIeCmqwratM47OBxY7uFZzjxHXmrg==