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 implementation of delete operation in foreign arrays #787

Closed
lucas-mpc-brant opened this issue Dec 21, 2023 · 1 comment
Closed

Allow implementation of delete operation in foreign arrays #787

lucas-mpc-brant opened this issue Dec 21, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@lucas-mpc-brant
Copy link

As discussed here currently it is not possible to delete elements in non-javascript arrays. I'd like to ask for the ability to include our own implementation of what a delete operation should do.

Code sample that throws the related error:

final List<Object> myList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
    myList.add(i);
}

final ProxyArray arr = ProxyArray.fromList(myList);
final Context context = Context.newBuilder("js").option("js.strict", "true").build();

context.getBindings("js").putMember("myArr", arr);
context.eval("js", "delete myArr[2]");
context.eval("js", "myArr[3] = 10");

final Value aux = context.eval("js", "myArr");
@iamstolis iamstolis self-assigned this Dec 31, 2023
@iamstolis iamstolis added the enhancement New feature or request label Dec 31, 2023
@iamstolis
Copy link
Member

JavaScript has a bit unusual semantics when it comes to the deletion of array elements: it removes the element and leaves a hole there. Other languages typically do not support deletion at all or shrink the array (i.e. move the elements to fill the hole and decrease the size of the array/list). That's why we hesitate to implement the general delegation of JavaScript delete operation to a custom/non-javascript remove[ArrayElement]().

A possible workaround is to wrap the non-javascript array in JavaScript proxy, i.e., create JS Proxy whose target is the array and provide a custom deleteProperty trap in the handler of this proxy. Unfortunately, it turned out that there are several bugs that are blocking this approach. I have fixed these bugs and created a simple example of the mentioned approach.

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

No branches or pull requests

2 participants