Agent to change retention of @JavaScriptBody and @JavaScriptResource#54
Agent to change retention of @JavaScriptBody and @JavaScriptResource#54jtulach merged 13 commits intoapache:masterfrom
@JavaScriptBody and @JavaScriptResource#54Conversation
|
This PR properly implements the ideas discussed at oracle/graal#8177 (comment) |
…doesn't change the retention.
|
net.java.html.boot-2.0-SNAPSHOT.jar.gz JAR (after /graalvm-21/bin/java \
-javaagent:net.java.html.boot-2.0-SNAPSHOT.jar \
-cp net.java.html.boot-2.0-SNAPSHOT.jar \
App.javacan be used to change the retention of the import net.java.html.js.JavaScriptBody;
class App {
@JavaScriptBody(args={}, body="")
public static native void hello();
public static void main(String... args) throws Exception {
var hello = App.class.getMethod("hello");
System.err.println("method: " + hello);
System.err.println("ann: " + hello.getAnnotation(JavaScriptBody.class));
}
}Executing the program should print: |
7ff9ccb to
2e49b76
Compare
2e49b76 to
1c2d653
Compare
Testing with Native ImageThe primary goal of this change is to allow integration with GraalVM. Let's do a small test. Take GraalVM repository at release/graalvm-25.0 branch, in particular commit bb84380ba769081088ed5a95c2a4f3df9f1ac652: graal/web-image$ mx build
graal/web-image$ graal/web-image$ mx web-image
Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)With the #!/bin/bash
set -e
mkdir -p hi
V=2.0-SNAPSHOT
CP=$1
if ! [ -e "$CP" ]; then
echo Usage $0 '<path_to_net.java.html.boot.jar>'
echo - typical path may be: $HOME/.m2/repository/org/netbeans/html/net.java.html.boot/$V/net.java.html.boot-$V.jar
exit 1
fi
cat >hi/Hi.java <<KONEC
class Hi {
@net.java.html.js.JavaScriptBody(args="txt", body="""
console.log(txt);
return "...returning back from JavaScript...";
""")
private static native String hello(String txt);
public static void main(String... args) {
System.out.println("In JVM");
var hi = hello("Hi from JavaScript");
System.out.println(hi);
System.out.println("Back in JVM");
}
}
KONEC
javac -cp $CP hi/Hi.java -d hi
mx web-image -H:+UnlockExperimentalVMOptions -o hi -H:Path=hi \
-H:+SILENT_COMPILE -H:-ClosureCompiler -H:+DumpPreClosure \
-H:+VerificationPhases -H:JSRuntime=Generic -H:Class=Hi \
-cp hi \
-J-javaagent:$CP
node hi/hi.jsand then just launch it with graal/web-image$ ./netbeans-html4j-test.sh net.java.html.boot-2.0-SNAPSHOT.jar
======================
GraalVM Native Image: Generating 'hi' (executable)...
======================
...
In JVM
Hi from JavaScript
...returning back from JavaScript...
Back in JVMHeuréka. CCing @Pziegler, @toni-epple, @fniephaus - everything seems to be working. The next version of NetBeans/HTML API will easily generate the right code with |
To support use of HTML/Java API in GraalVM's
native-imagetool:@JavaScriptBodyand@JavaScriptResourceannotation need to be available in runtimeRetentionType.CLASSIt is necessary to change the bytecode of:
runtimeVisibleAnnotationclass file attributeRetentionType.RUNTIMEWith such changes we should be able to use the JVM agent of HTML/Java to make these annotation available at GraalVM's native image build time.
-agentliboption