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

Javascript failing. Multiple engine support? #201

Open
Osiris-Team opened this issue Aug 3, 2020 · 18 comments
Open

Javascript failing. Multiple engine support? #201

Osiris-Team opened this issue Aug 3, 2020 · 18 comments

Comments

@Osiris-Team
Copy link

Osiris-Team commented Aug 3, 2020

The issue:
When trying to download a jar file from https://spigotmc.org we get a "default values inside destructuring assignments are not supported" exception.
The root of this problem lays at the javascript engine used by htmlunit (mozilla/rhino#756).
We have two possible fixes:

  1. Integrating another javascript(graaljs) engine. Javascript failing. Multiple engine support? #201 (comment)
  2. Adding support for default values inside destructuring assignments in the rhino engine Support ES2015 default values inside destructuring assignments mozilla/rhino#756

Status:

  1. A lot of work, due to htmlunits strong dependency on rhino. Seems almost impossible for one single person with little insight to htmlunit (me)
  2. No feedback from the authors. Also, rhino is updated very infrequently(Support ES2015 default values inside destructuring assignments mozilla/rhino#756)

The code: #201 (comment)

@rbri
Copy link
Member

rbri commented Aug 3, 2020

Can you please give the latest snapshot build a try.

@Osiris-Team
Copy link
Author

Can you please give the latest snapshot build a try.

Sure gimme a min...

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 3, 2020

@rbri
Ok I'm running on the latest snapshot now. Btw the maven stuff didnt work, I had to add it as repository, not just in distributionManagement (see code below).

The download is still failing but now the error messages are different: https://pastebin.com/7QrYLxkj

    <repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
    </repositories>

<distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
</distributionManagement>

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 4, 2020

@rbri
Ok it seems like the download link was pointing to the jars thread/html page and not the actual jar. Sry my bad!
So I changed the test download links and added some options to show the pages content before/after waiting 7 secs.
But the issue still exists even when the download link is correct.
Here is the console output: https://pastebin.com/PGGBLbD4 (This was tested on the latest snapshot btw)

@rbri
Copy link
Member

rbri commented Aug 5, 2020

Looks like the js code uses one of the js features not supported by rhino at the moment.

Frome the Rhino sources

            // default values inside destructuring assignments,
            // like 'var [a = 10] = b' or 'var {a: b = 10} = c',
            // are not supported
            reportError("msg.destruct.default.vals");

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 6, 2020

@rbri Ok so the only option I have is to wait, till support is added?
I created an issue there: mozilla/rhino#756

@Osiris-Team
Copy link
Author

@rbri Is there a way I can add support for default values inside destructuring assignments by myself to rhino?
What are the classes responsible for this?
I already forked the repo.

@Osiris-Team
Copy link
Author

@rbri Or is there any other program similar to Htmlunit I could use, where default values inside destructuring assignments are supported?

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 22, 2020

@rbri How about adding support to change the javascript engine?
There are alternatives like:
https://www.graalvm.org/
or
https://michel-kraemer.github.io/citeproc-java/
or
https://github.com/eclipsesource/J2V8
They seem to have also a better performance than rhino.

@rbri
Copy link
Member

rbri commented Aug 22, 2020 via email

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 23, 2020

@rbri Ok I would really like to help. I will do some more research on this topic before, and see which of the engines supports default values inside destructuring assignments.
Oh and there is a great chrome extension to display github notifications: https://chrome.google.com/webstore/detail/notifier-for-github/lmjdlojahmbbcodnpecnjnmlddbkjhnn?hl=de

@Osiris-Team
Copy link
Author

Osiris-Team commented Aug 29, 2020

@rbri
Citeproc (https://michel-kraemer.github.io/citeproc-java/) seems to integrate 3 different javascript engines (graalvm, jre and v8).
After some testing it looks like the graalvm engine is the only one supporting default values inside destructuring assignments.
By integrating this engine it should be possible to fix my issue ;)
I'm slowly getting into htmlunits JavaScriptEngine class and I'll try to implement this.
Checkout my fork for status of implementation: https://github.com/Osiris-Team/htmlunit
Compare with master: master...Osiris-Team:master

Here is the example:

package com.osiris.JavaScriptExecutor.javascript;

import de.undercouch.citeproc.script.ScriptRunner;
import de.undercouch.citeproc.script.ScriptRunnerFactory;

import java.io.*;

public class GitHubExample {

    private static ScriptRunner scriptRunner = null;

    public static void main(String[] args) {

        // Loop 3 times to test the same operation on different engines
        for (int i = 0; i < 3; i++) {
            // Select the engine
            setEngine(i);
            System.out.println("Testing engine "+i+"...");

            // Example to run javascript from a string:
            try{
                // Test JS code to check if default values inside destructuring assignments are supported
                String destruct_test = "const dummy = {" +
                        "  name: undefined" +
                        "};" +
                        "const { name = 'Peter' } = dummy;" +
                        "console.log(name)";

                // Get the input stream of the selected string
                InputStream in = new ByteArrayInputStream(destruct_test.getBytes());
                Reader reader = new InputStreamReader(in);

                // Run the javascript
                scriptRunner.eval(reader);
                System.out.println("Engine "+i+" completed successfully!");
                // Result is that only the graal-engine will finish successfully
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Select the engine:
     * 0 = graal_engine (default);
     * 1 = java_engine;
     * 2 = v8_engine;
     * @param engine
     */
    public static void setEngine(int engine){
        if (engine==0){
            //Select graal_engine
            ScriptRunnerFactory.setRunnerType(ScriptRunnerFactory.RunnerType.GRAALJS);
            scriptRunner = ScriptRunnerFactory.createRunner();
        }
        else if (engine==1){
            //Select java_engine
            ScriptRunnerFactory.setRunnerType(ScriptRunnerFactory.RunnerType.JRE);
            scriptRunner = ScriptRunnerFactory.createRunner();
        }
        else {
            //Select v8_engine
            ScriptRunnerFactory.setRunnerType(ScriptRunnerFactory.RunnerType.V8);
            scriptRunner = ScriptRunnerFactory.createRunner();
        }
    }

}

@Osiris-Team Osiris-Team changed the title Javascript failing. Javascript failing. Multiple engine support? Aug 29, 2020
@Osiris-Team
Copy link
Author

Osiris-Team commented Sep 1, 2020

@rbri
I need to modify the htmlunit-core-js jar to implement a new engine. Is the source code available somewhere?

@rbri
Copy link
Member

rbri commented Sep 1, 2020

@rbri rbri added this to Rhino Related in HtmlUnit - RBRi Sep 25, 2020
@Osiris-Team
Copy link
Author

Osiris-Team commented Sep 7, 2021

@rbri Im back with some news. I think implementing the GraalJS Engine into htmlunit is quite much impossible (at least for me alone) because of its strong dependency and interconnectivity with the rhino engine, like you said. That's why I created this https://github.com/Osiris-Team/JG-Browser which is the same as htmlunit just built with the graal js engine as base. What do you think?

@rbi
Copy link
Contributor

rbi commented Sep 7, 2021

@Osiris-Team you probably mean @rbri, not @rbi

@Osiris-Team
Copy link
Author

Ups!

@kerbymart
Copy link

@Osiris-Team so it seems the RhinoJS is the one that needs to be updated to make HtmlUnit more robust to handle javascript...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
HtmlUnit - RBRi
Rhino Related
Development

No branches or pull requests

4 participants