oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.82k stars 191 forks source link

Extending a Java class in Javascript without using Java.extend #828

Closed RevolvingMadness closed 5 months ago

RevolvingMadness commented 5 months ago

I have a Java class:

public class Foo {
    public void method() {
        System.out.println("Foo in Java");
    }
}

I have a JavaScript class:

class ExtendedFoo extends Foo {
    method() {
        console.log("ExtendedFoo in JavaScript");
    }
}

This code gets ran in GraalJS:

var extendedFoo = ExtendedFoo();
/*
Should print "ExtendedFoo in JavaScript"
Prints "Foo in Java"
*/
extendedFoo.method();

For some reason it doesn't print ExtendedFoo in JavaScript. I don't want to use Java.extend because it is very confusing to me.

I would ask in a Discord but there isn't one so I made an issue.

iamstolis commented 5 months ago

I am sorry, I am not able to reproduce this problem. I don't think that we allow JavaScript class to extend Java class:

$ js
> class JavaScriptObject extends java.lang.Object {}
TypeError: protoParent is neither Object nor Null
    at <js> :program(<shell>:1:1:0-49)
RevolvingMadness commented 5 months ago

Hmm... I know GraalPy allows this but maybe there's a limitation.

iamstolis commented 5 months ago

It is not that important what GraalPy does but what your test-case looks like exactly. It seems to be more complicated/slightly different than what you described because an attempt to define a JavaScript class that extends Java class results in TypeError normally.

RevolvingMadness commented 5 months ago

Ok