-
Notifications
You must be signed in to change notification settings - Fork 847
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
add Object.hasOwn (#1052) #1157
Merged
Merged
Changes from 2 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
112 changes: 112 additions & 0 deletions
112
testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.mozilla.javascript.tests.es2022; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ScriptableObject; | ||
|
||
public class NativeObjectTest { | ||
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.
|
||
|
||
private Context cx; | ||
private ScriptableObject scope; | ||
|
||
@Before | ||
public void setUp() { | ||
cx = Context.enter(); | ||
cx.setLanguageVersion(Context.VERSION_ES6); | ||
scope = cx.initStandardObjects(); | ||
} | ||
|
||
@After | ||
public void tearDown() { Context.exit(); } | ||
|
||
@Test | ||
public void testHasStringOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: '123' }, 'test');\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasUndefinedOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: undefined }, 'test');\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasNullOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: null }, 'test');\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasArrayPropertyOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"let dessert = [\"cake\", \"coffee\", \"chocolate\"];\n" + | ||
"let result = Object.hasOwn(dessert, 2);\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasNoOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ cake: 123 }, 'test');\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = false", result); | ||
} | ||
|
||
@Test | ||
public void testCreateHasOwn() { | ||
Object result = cx.evaluateString( | ||
scope, | ||
"var foo = Object.create(null);\n" + | ||
"foo.prop = 'test';\n" + | ||
"var result = Object.hasOwn(foo, 'prop')\n" + | ||
"'result = ' + result;", | ||
"test", | ||
1, | ||
null | ||
); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
} |
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.
I read the spec.
https://tc39.es/proposal-accessible-object-hasownproperty/
Object.hasOwn
calls theHasOwnProperty
operation in the same way asObject.prototype.hasOwnProperty
. Could you make this common? I think it is better to write it inScriptRuntime
orScriptableObject
.rhino/src/org/mozilla/javascript/NativeObject.java
Lines 207 to 219 in a0a9fb9
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.
Good point!
The correct location to put it however is org.mozilla.javascript.AbstractEcmaObjectOperations, as the spec defines the HasOwnProperty operation as one of the Abstract Operations on Objects (section 7.3.13).
Do check the notes at the top of the file as to how to add stuff: https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/AbstractEcmaObjectOperations.java#L8
Also possibly see where else the new function ought to be used and see if you can refactor that existing code to use the new function as well
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 changed 'hasOwnProperty' and 'hasOwn' to call the function 'hasOwnPropertyInObject' with the ec9b150 commit. I'll have to find more codes to handle the same process here.