MCPcopy Create free account
hub / github.com/bluejekyll/wasmtime-java / Wasmtime

Class Wasmtime

src/main/java/net/bluejekyll/wasmtime/Wasmtime.java:14–105  ·  view source on GitHub ↗

Wasmtime A wrapper over the FFI of the Rust wasmtime library, which uses Wasmtime for the WASM runtime

Source from the content-addressed store, hash-verified

12 * the WASM runtime
13 */
14public class Wasmtime {
15 private static final String NATIVE_LIB = "wasmtime_jni";
16 private static volatile boolean libraryLoaded = false;
17
18 private static void loadNative() {
19 if (Wasmtime.libraryLoaded) {
20 return;
21 }
22
23 try {
24 System.loadLibrary(NATIVE_LIB);
25 System.out.printf("loadLibrary succeeded for %s%n", NATIVE_LIB);
26 libraryLoaded = true;
27 return;
28 } catch (UnsatisfiedLinkError e) {
29 System.out.printf("Failed to loadLibrary %s, will try from classpath%n", NATIVE_LIB);
30 }
31
32 String osName = System.getProperty("os.name");
33 String osArch = System.getProperty("os.arch");
34
35 if (osName.contains("Mac OS X")) {
36 osName = "Darwin";
37 }
38 if (osArch.contains("amd64")) {
39 osArch = "x86_64";
40 }
41
42 final String libName = System.mapLibraryName(NATIVE_LIB);
43 final String libPath = String.format("NATIVE/%s/%s/%s", osName, osArch, libName);
44
45 // open a temporary file for the native_lib
46 final String tmpDir = System.getProperty("java.io.tmpdir");
47
48 if (tmpDir == null) {
49 throw new RuntimeException("java.io.tmpdir is null?");
50 }
51
52 final File libFile = new File(tmpDir, libName);
53 final String path = libFile.getAbsolutePath();
54
55 // FIXME: add back...
56 // if (libFile.exists()) {
57 // System.out.printf("Temporary library already exists %s, did not replace%n",
58 // libFile);
59 // loadLibrary(path);
60 // return;
61 // }
62
63 try (OutputStream os = new FileOutputStream(libFile, false);
64 InputStream in = ClassLoader.getSystemResourceAsStream(libPath);) {
65 if (in == null)
66 throw new RuntimeException(String.format("could not find %s in classpath", libPath));
67 long length = in.transferTo(os);
68 System.out.printf("Created temporary library sized %d at %s%n", length, libFile);
69 os.flush();
70 } catch (Exception e) {
71 System.err.printf("Failed to write %s to %s library: %s%n", libPath, libFile, e.getMessage());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…