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

ADD: delete method and set length property to NativeJavaList #1031

Merged
merged 2 commits into from
Oct 14, 2021

Conversation

rPraml
Copy link
Contributor

@rPraml rPraml commented Sep 17, 2021

This change is extracted from #830 and adds support for delete and setting the length

  • javaList.length = x I think this is clear. It will grow or truncate the underlying java list to given size.
  • delete javaList[x] which effectively set the value in the wrapped javaList to null. (It will not remove elements, so the size of the list will stay the same)

Note: on the java side, we can not distinguish between null and undefined, So increasing the size will add null values in java vs. empty slots in JS.
The same is also true for delete javaList[x], it will be equivalent with javaList[x] = null

@tuchida you always have good objections:
I know the current implemetation is not exactly what the spec describes:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
Especially in strict mode, delete javaList['length'] etc. should throw TypeError and should return true if element is deleted.

What I want to achive is, that delete on a javaList "works". Currently it is completely ignored. So I think, this implementation is an improvement.

(Btw: The same is for NativeJavaObject. Deleting properties should throw a TypeError)

Cheers
Roland

Copy link
Contributor

@tuchida tuchida left a comment

Choose a reason for hiding this comment

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

Sounds good.

@tuchida
Copy link
Contributor

tuchida commented Sep 17, 2021

By the way, what do you think Object.freeze and property descriptor should be?

var a = Object.freeze([1, 2, 3]);
a[1] = 123;
a;  // 1,2,3

var al = java.util.ArrayList([1, 2, 3]);
Object.freeze(al); // `-version 200` is required.
al[1] = 123;
al; // [1.0, 123.0, 3]

if (longVal < list.size()) {
list.subList((int) longVal, list.size()).clear();
} else {
ensureCapacity((int) longVal);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems dangerous since typical java Lists don't support sparse layouts like javascript arrays do. If you set length to a very large value on a javascript array it's no big deal, but do the same on an ArrayList, and you're probably going to run out of memory.

Should we at least add a javadoc description for the class that describes the additional behaviors which NativeJavaList adds to NativeJavaObject and potential pitfalls when using these features as if Lists were arrays? Is this stuff documented anywhere else other than in PRs and commit messages?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Improving the Javadoc sounds great to me.

In general, the "embedding guide" is very very old and hasn't it been archived by Mozilla? Do we have any volunteers to resurrect it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should we document this? JavaDoc, MD, wiki-page?

I would prefer a markdown documentation like this one: https://github.com/oracle/graaljs/blob/master/docs/user/JavaInteroperability.md

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I think the javadoc should contain basic important information for using the class. Wiki/user guide type documentation is good for going into more detail if someone is willing to write that. As we know, external documentation is not always easily accessible and developers should have some idea how a class works without having to closely study the code or pull up external docs.

@rPraml
Copy link
Contributor Author

rPraml commented Sep 21, 2021

I've added some documentation and using add instead of grow/set if last element is added to list

@p-bakker p-bakker added the Java Interop Issues related to the interaction between Java and JavaScript label Sep 29, 2021
@rPraml
Copy link
Contributor Author

rPraml commented Oct 5, 2021

@gbrail @tuchida @tonygermano
I wanted to ask if the PR can be merged or if there are still objections?

@tonygermano
Copy link
Contributor

I'm ok with the added documentation.

@gbrail
Copy link
Collaborator

gbrail commented Oct 14, 2021

No, I think that this looks good now. Let me run another test to make sure.

@gbrail gbrail merged commit 43793a4 into mozilla:master Oct 14, 2021
@p-bakker p-bakker added this to the Release 1.7.14 milestone Oct 15, 2021
@p-bakker p-bakker added the docs Issues containing stuff that ought to be documented label Oct 15, 2021
@tuchida tuchida mentioned this pull request Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Issues containing stuff that ought to be documented Java Interop Issues related to the interaction between Java and JavaScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants