-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
n-api: add napi_has_own_property() #14063
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,40 @@ assert.strictEqual(newObject.test_string, 'test string'); | |
Object.prototype.toString); | ||
} | ||
|
||
{ | ||
// Verify that napi_has_own_property() fails if property is not a name. | ||
[true, false, null, undefined, {}, [], 0, 1, () => {}].forEach((value) => { | ||
assert.throws(() => { | ||
test_object.HasOwn({}, value); | ||
}, /^Error: A string or symbol was expected$/); | ||
}); | ||
} | ||
|
||
{ | ||
// Verify that napi_has_own_property() does not walk the prototype chain. | ||
const symbol1 = Symbol(); | ||
const symbol2 = Symbol(); | ||
|
||
function MyObject() { | ||
this.foo = 42; | ||
this.bar = 43; | ||
this[symbol1] = 44; | ||
} | ||
|
||
MyObject.prototype.bar = 45; | ||
MyObject.prototype.baz = 46; | ||
MyObject.prototype[symbol2] = 47; | ||
|
||
const obj = new MyObject(); | ||
|
||
assert.strictEqual(test_object.HasOwn(obj, 'foo'), true); | ||
assert.strictEqual(test_object.HasOwn(obj, 'bar'), true); | ||
assert.strictEqual(test_object.HasOwn(obj, symbol1), true); | ||
assert.strictEqual(test_object.HasOwn(obj, 'baz'), false); | ||
assert.strictEqual(test_object.HasOwn(obj, 'toString'), false); | ||
assert.strictEqual(test_object.HasOwn(obj, symbol2), false); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test case where the key is not a name? |
||
|
||
{ | ||
// test_object.Inflate increases all properties by 1 | ||
const cube = { | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than returning false, shouldn't we call the ToPrimitive method on the key? I'm thinking of an example like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhinkel what
ToPrimitive()
method? I'm not seeing anything exposed by the V8 API except for retrieving the symbol itself, but maybe I'm just missing it.Also cc @nodejs/n-api as this case isn't handled anywhere else in n-api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we expose it by itself. According to the spec, the key is converted to a name, which calls
toPrimitive
, toPropertyKey.So, I think we should delete the
return false if not IsName()
, and let V8 handle it in a spec compliant way. V8 only needs avalue
, not a string/symbol here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm all for letting V8 handle this, but I get a compiler error passing in a
Value
:-/It looks like V8 expects a
Name
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I was looking at a comment for has(). Sorry for the confusion. I think we should make sure though that it's obvious that the n-api function is not identical to JS behavior, in particular, that we'll never use the
ToPrimitive
symbol if the key is an object. (We should probably point that out in the V8 API, but there it might be less of an issue because we only allownames
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhinkel I added a note to the docs that no type conversions will be performed. That should cover
ToPrimitive
and any other implicit conversions.