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

Using ArrayProxy as function parameter do not allow some operations. #732

Closed
GabrielFilocre opened this issue May 19, 2023 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@GabrielFilocre
Copy link

Hello, guys. I'm migrating an application from Rhino to Graal and I'm stuck trying to reproduce some behaviors of JS Arrays that was possible on Rhino. Here's a simple test example that emulates my problem:

final Context context = Context.newBuilder("js")
                                       .option("js.strict", Boolean.TRUE.toString())
                                       .option("js.foreign-object-prototype", Boolean.TRUE.toString())
                                 .allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT).allowPublicAccess(false).allowArrayAccess(true).allowListAccess(true).allowIterableAccess(true).build())
                                       .allowExperimentalOptions(true)
                                       .build();
        final String script = """
                (function(object){
                    object.length = 2;
                    return object;
                })
                """;
        final Source source = Source.newBuilder("js", script, "ArrayExample").build();
        final Value value = context.eval(source);

        final Value returnValue = value.execute(ProxyArray.fromList(Arrays.asList(1, 2, 3, 4, 5)));
        System.out.println(returnValue);

This code throws the Exception: TypeError: writeMember (length) ..... Message not supported. This exception throws only by passing the proxy as the function parameter. Some JS Array methods, like .push() and .pop(), do not work in this scenario too. Any idea that what could be done to allow this attribution?

@iamstolis iamstolis self-assigned this May 19, 2023
@iamstolis iamstolis added the bug Something isn't working label May 19, 2023
@iamstolis
Copy link
Member

The support for writing the length property of foreign (= non-JavaScript arrays) has been implemented by e3582e2.

Your test-case will throw TypeError still because ProxyArray.fromList(Arrays.asList(1, 2, 3, 4, 5)) does not support the removal of its members (because Arrays.asList(1, 2, 3, 4, 5) does not support remove() operation). On the other hand, the test-case should work (with the mentioned fix) if you use ProxyArray.fromList() with a list that supports remove() (or if you use a custom implementation of ProxyArray that supports removal/addition of elements).

Some JS Array methods, like .push() and .pop(), do not work in this scenario too.

Array.prototype.push and Array.prototype.pop should work fine with a foreign array if the array supports the removal/addition of an element. Note that this is not the case for ProxyArray.fromList() (which does not support the addition of an element).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants