Created
June 30, 2025 16:55
-
-
Save mukel/157d33d471342fbf518e457f67e921b8 to your computer and use it in GitHub Desktop.
hello-espresso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.hello; | |
import com.oracle.truffle.espresso.polyglot.Polyglot; | |
import org.graalvm.polyglot.Context; | |
import org.graalvm.polyglot.Value; | |
public class Main { | |
public static void main(String[] args) { | |
try (Context context = Context.newBuilder() | |
// Just for fun, use the same class path as the host, never do this!!! | |
.option("java.Classpath", System.getProperty("java.class.path")) | |
.option("java.Polyglot", "true") | |
.allowAllAccess(true) | |
.build()) { | |
// 'java' bindings expose the system/application class loader. | |
Value bindings = context.getBindings("java"); | |
Value main = bindings.getMember("com.example.hello.Main"); | |
// Call Main.sayHello method in the host first. | |
Main.sayHello("Host VM"); | |
// Then call it within the Espresso context. | |
main.invokeMember("sayHello", "Espresso"); | |
// Show Espresso interop with JS. | |
main.invokeMember("espressoInterop"); | |
} | |
} | |
public static void sayHello(String toWhom) { | |
System.out.println("Hello, " + toWhom + "! from " + System.getProperty("java.vm.name")); | |
} | |
public static void espressoInterop() { | |
// language=js | |
String mandelbrot = """ | |
const mandelbrot = function(width = 120, height = 30, maxIterations = 25) { | |
let result = ""; | |
for (let py = 0; py < height; py++) { | |
let line = ""; | |
for (let px = 0; px < width; px++) { | |
// Convert pixel coordinates to complex plane (-2..2) | |
const x0 = (px / width) * 3.5 - 2.5; | |
const y0 = (py / height) * 2 - 1; | |
let x = 0; | |
let y = 0; | |
let iteration = 0; | |
// Mandelbrot formula: z = z² + c | |
while (x*x + y*y <= 4 && iteration < maxIterations) { | |
const xtemp = x*x - y*y + x0; | |
y = 2*x*y + y0; | |
x = xtemp; | |
iteration++; | |
} | |
// Choose character based on iteration count | |
line += iteration === maxIterations ? "■"\s | |
: iteration > maxIterations * 0.8 ? "#"\s | |
: iteration > maxIterations * 0.6 ? "*"\s | |
: iteration > maxIterations * 0.4 ? "+"\s | |
: iteration > maxIterations * 0.2 ? "."\s | |
: " "; | |
} | |
result += line + "\\n"; | |
} | |
return result; | |
} | |
mandelbrot() | |
"""; | |
String result = Polyglot.eval("js", mandelbrot, String.class); | |
System.out.println(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | |
http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>hello</artifactId> | |
<version>0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>Hello Espresso</name> | |
<description>An Espresso embedding using GraalVM</description> | |
<properties> | |
<maven.compiler.source>21</maven.compiler.source> | |
<maven.compiler.target>21</maven.compiler.target> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<graalvm.version>24.2.1</graalvm.version> | |
<truffle.version>24.2.1</truffle.version> | |
</properties> | |
<dependencies> | |
<!-- Polyglot API --> | |
<dependency> | |
<groupId>org.graalvm.polyglot</groupId> | |
<artifactId>polyglot</artifactId> | |
<version>${graalvm.version}</version> | |
</dependency> | |
<!-- Espresso --> | |
<dependency> | |
<groupId>org.graalvm.polyglot</groupId> | |
<artifactId>java</artifactId> | |
<version>${graalvm.version}</version> | |
<type>pom</type> | |
</dependency> | |
<!-- GraalJS --> | |
<dependency> | |
<groupId>org.graalvm.polyglot</groupId> | |
<artifactId>js</artifactId> | |
<version>${graalvm.version}</version> | |
<type>pom</type> | |
</dependency> | |
<!-- Espresso's Polyglot API --> | |
<dependency> | |
<groupId>org.graalvm.espresso</groupId> | |
<artifactId>polyglot</artifactId> | |
<version>${graalvm.version}</version> | |
<scope>compile</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment