Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nfbind bound function to handle missing args #753

Open
wants to merge 2 commits into
base: v1
Choose a base branch
from
Open

Allow nfbind bound function to handle missing args #753

wants to merge 2 commits into from

Conversation

denised
Copy link

@denised denised commented Dec 31, 2015

A common coding habit in javascript is to omit trailing arguments whose value is null. Currently a function created via nfbind will not behave properly if used this way, This patch allows the function to behave properly, by padding out any missing arguments with null values.

The function bound by nfbind will behave properly when invoked with
fewer args than expected, because we pad out the missing args before
appending the callback.
// Pad an array value with nulls to the specified length. Longer arrays are left alone.
function padArrayTo(arry,len) {
if ( arry.length < len ) {
arry = array_concat(arry, new Array(len - arry.length));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be accomplished without allocations array.length = Math.min(array.length, length)

@kriskowal
Copy link
Owner

First glance, this looks like a legitimate improvement to me. cc @domenic for sanity check.

Please consider making some concessions for the prevailing style. Spaces around operators, fully-spelled variable names (array over arry, length over len).

@denised
Copy link
Author

denised commented Jan 3, 2016

I will change the variable names and look for any place I have squished operators.
I didn't know you could assign to array lengths, I will make that change as well.

remove one array allocation and clean up styling inconsistencies
@@ -345,6 +346,14 @@ function isObject(value) {
return value === Object(value);
}

// Pad an array value with nulls to the specified length. Longer arrays are left alone.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do what it says. It pads the array with holes, not nulls.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you are right; I will change the comment.

@domenic
Copy link
Collaborator

domenic commented Jan 3, 2016

This is a pretty major change in semantics and as such should only go into 2.0. I am neutral on the functionality; I can see how it might make sense to test fn.length to find the last argument, but in general I think that's fragile and am not sure relying on it is a good idea.

@denised
Copy link
Author

denised commented Jan 4, 2016

Just to be clear, what currently happens is that if you omit a trailing argument, the call breaks with a TypeError, because the handler gets moved to an earlier argument position and is thus not available when called. Is it truly the case that this is intentional semantics ?
I think this is adding functionality in an edge case, rather than backwords incompatibility (it is hard for me to imagine anyone relying on a TypeError for anything). The value, I think, is the principle of "least surprise" for callers that may not be aware that a function was defined via nfbind.
I can't comment as to the reliability of fn.length, though. If you think it would really be likely to be incompatible between different implementations, then I would agree that it would be even more surprising to have it fail sometimes but not others.

@domenic
Copy link
Collaborator

domenic commented Jan 4, 2016

Right, those are intentional semantics currently, because they are the simplest thing that can be done without any magic guessing that might e.g. hide bugs. Magic guessing is OK to add, but it's a major semantic change and deserves 2.0 only.

fn.length is not incompatible between implementations, but it's just not always useful. For example, many core functions in the Node.js standard library have fn.lengths that mismatch their actual call signature, since they switch on arguments[n] where n > fn.length.

@denised
Copy link
Author

denised commented Jan 4, 2016

Ah, yes I can see how that might cause problems.   I leave it in your
hands to decide whether on balance it is an improvement or not.

  Denise Draper
  [email protected]

On Sun, Jan 3, 2016, at 04:39 PM, Domenic Denicola wrote:

Right, those are intentional semantics currently, because they are the simplest thing that can be done without any magic guessing that might e.g. hide bugs. Magic guessing is OK to add, but it's a major semantic change and deserves 2.0 only.

fn.length is not incompatible between implementations, but it's just not always useful. For example, many core functions in the Node.js standard library have fn.lengths that mismatch their actual call signature, since they switch on arguments[n] where n > fn.length.


Reply to this email directly or view it on GitHub[1].

Links:

  1. Allow nfbind bound function to handle missing args #753 (comment)

@kriskowal
Copy link
Owner

I’m compelled to leave the library as it stands. Undesirable as the behavior is in some cases, it is less surprising in the majority. Explicitly filling missing arguments at the call site seems safer than implicitly filling them in the library.

Regardless, thank you for contributing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants