Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Created June 20, 2020 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/8d716f8f9bd8458f229965069e7f668f to your computer and use it in GitHub Desktop.
Save dmi3coder/8d716f8f9bd8458f229965069e7f668f to your computer and use it in GitHub Desktop.
Example of C language execution from Quarkus
package net.quarkify.cpp;
import io.quarkus.runtime.StartupEvent;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import javax.enterprise.event.Observes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.IOException;
import java.net.URL;
@Path("/hello")
public class ExampleResource {
private Value helloMethod;
public void onStart(@Observes StartupEvent se) {
new Thread() {{
final URL drawImage = getClass().getClassLoader().getResource("example");
Context polyglot = Context.newBuilder().allowAllAccess(true).build();
Source source = null;
try {
source = Source.newBuilder("llvm", drawImage).build();
} catch (IOException e) {
e.printStackTrace();
}
helloMethod = polyglot.eval(source);
}}.start();
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String executeHello() throws IOException {
helloMethod.execute();
return "See terminal output :)";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment