fix: preserve arguments.length for non-arrow wrapped functions#69
Closed
crysmags wants to merge 1 commit intonodejs:mainfrom
Closed
fix: preserve arguments.length for non-arrow wrapped functions#69crysmags wants to merge 1 commit intonodejs:mainfrom
crysmags wants to merge 1 commit intonodejs:mainfrom
Conversation
PR nodejs#60 replaced every wrapped function's params with numbered placeholders and rebuilt `__apm$arguments` from them. That rebuild pads unfilled named slots with `undefined`, so the array handed to `.apply` always has the declared arity rather than the caller's real arity. Any wrapped function that dispatches on `arguments.length` — e.g. graphql's `execute(argsOrSchema, document, ...)` overload — silently takes the wrong branch on calls with fewer args than params. Arrow wrappers still need PR nodejs#60's numbered-placeholder rebuild (arrows have no own `arguments`, and ESM top-level arrows have no enclosing one), so the fix is scoped to non-arrow outers: keep the author's original params on the outer wrapper (preserves `.length`) and materialize `arguments` via `Array.prototype.slice.call(arguments)` in the preamble (preserves real caller arity going into the inner body). Adds a regression test mirroring graphql's `arguments.length === 1` dispatch pattern.
Contributor
|
I came up with a simpler alternative solution (#70) that also fixes generators when used with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PR #60 replaced every wrapped function's params with numbered placeholders (
__apm$arg0…N, ...__apm$args) and rebuilt__apm$argumentsfrom them. That rebuild pads unfilled named slots withundefined, so the array handed to.applyalways has the declared arity rather than the caller's real arity. Wrapped functions that dispatch onarguments.lengthsilently take the wrong branch on calls with fewer args than params.Concrete breakage: graphql's
execute(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver)uses the classic dual-mode patternAfter the PR #60 rewrite,
arguments.lengthinside the moved-out body is always 8 regardless of what the caller passed.graphql.execute({ schema, source })now takes the positional branch, handsundefinedtoexecuteImplasdocument, and throws"Must provide document."before any user code runs. Any function with a similar overload (lodash-style options-vs-positional dispatch, optional-trailing-arg detection, variadics that distinguish zero args from one explicitundefined) breaks the same way.Fix
Two templates, split on whether the outer wrapper is an arrow:
arguments(and ESM top-level arrows have no enclosing one), so they can't read real arity fromarguments. The numbered-placeholder rebuild is the only approach that works uniformly for arrows in every context — nested in a function, at ESM top level, or at CJS top level. This is exactly the correctness guarantee PR convert ArrowFunctionExpression to FunctionExpression in traceFunction #60 was built to deliver, and it's preserved here..length) and materializeargumentsviaArray.prototype.slice.call(arguments)in the preamble (preserves real caller arity). Non-arrow outer wrappers always have their ownargumentsobject regardless of surrounding scope or module type, so reading it is safe in every case where this path is taken.The
Array.prototype.X.call(args-like)idiom matches existing usage elsewhere in the wrapper code (Array.prototype.at.call,Array.prototype.splice.call).Why not unify the two templates
This re-introduces two templates, which a previous iteration of PR #60 was moved away from. Two things have changed:
.lengthto 0 on the arrow side because it used a bare...restparam. This PR keeps PR convert ArrowFunctionExpression to FunctionExpression in traceFunction #60's numbered-placeholder approach for arrows, so.lengthstays correct on both paths.arguments.length-dispatching functions that cannot be fixed without either (a) readingargumentsin the outer wrapper, which re-opens PR convert ArrowFunctionExpression to FunctionExpression in traceFunction #60's arrow and ESM bugs, or (b) detecting unfilled params at runtime, which is not possible in JS (f(undefined)is indistinguishable fromf()once a param binding is established). Splitting on wrapper shape is the only way both correctness properties hold simultaneously.Test plan
tests/arguments_length_dispatch_cjs/— regular function that dispatches onarguments.length, called with 1 arg (expects object-form branch) and with 2 args (expects positional branch), and assertsfn.lengthstill equals the declared arity.arrow_expr_args_cjs,arrow_this_binding_cjs,arrow_this_property_args_cjs,object_property_this_cjs,object_property_named_cjs) confirm the PR convert ArrowFunctionExpression to FunctionExpression in traceFunction #60 fast path is unchanged.npm run lintclean.🤖 Generated with Claude Code