[graalvm-users] Javascript Questions

Steve Brenneis sbrenneis930 at gmail.com
Sun Oct 3 20:48:34 PDT 2021


Hello.

I have a project to analyze how scripts on web pages interact with the
browser window and document objects. I can only analyze them. I can't
modify them. Most things are fairly obvious, but I have come up against a
couple of items that I can't find answers for anywhere. If there is
documentation that I have missed, I would be eternally grateful if someone
could point me in the right direction. The project will be written in Java.

Given this evaluation:

Value value = context.eval("js", "var w = 5;");

value will be of type undefined and value.hasMembers will return false;
Is there any way to bring the value of w back to Java?

This leads into the second question.

In a browser, the window object is actually a pseudonym for the global
scope. Therefore

var w = 5;

can be functionally equivalent to

window.w = 5;

How does the javascript polyglot line things up between a weakly typed
language like javascript and a strongly typed one like Java?

I can define a Window class in Java and give it a member variable w

class Window {
    public int w;
}

But then, because this is perfectly valid javascipt

window.w = 'hello';

w obviously can't be typed as an integer.

And how would I handle this case?

window.x = {
     number: 4,
    letter: '6',
    string: "hello",
    method: (() => a = b)
}

I could define x of type Object, but I would have no idea how to access the
members. And obviously if I don't know what variables will be created
beforehand, I can't declare them.

I have also run into nightmares like this

if (window.z === undefined) {
    window.z = [];
    window.z.a = { variableOne: "something", variableTwo: "something else"};
    window.z.push({variableThree: "another thing", variableFour: "yet
another thing"});
}

There may be (I hope) a simple solution and I just haven't been able to
find it. Any help is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20211003/c36841dc/attachment.html 


More information about the GraalVM-Users mailing list