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

Class WasmEngine

src/main/java/net/bluejekyll/wasmtime/WasmEngine.java:12–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11// TODO implement cloneable and clone the underlying engine between threads
12@NotThreadSafe
13public class WasmEngine extends AbstractOpaquePtr implements Cloneable {
14 WasmEngine(long ptr) {
15 super(ptr, WasmEngine::freeEngine);
16 }
17
18 private static native void freeEngine(long ptr);
19
20 private static native long newStoreNtv(long engine_ptr);
21
22 private static native long newModuleNtv(long engine_ptr, ByteBuffer wasm_bytes) throws WasmtimeException;
23
24 // takes a pointer to an engine
25 private static native long newLinker(long engine_ptr) throws WasmtimeException;
26
27 public WasmStore newStore() {
28 long storePtr = newStoreNtv(super.getPtr());
29
30 System.err.printf("Java Store Pointer: %d%n", storePtr);
31
32 return new WasmStore(storePtr);
33 }
34
35 public WasmModule newModule(ByteBuffer wasm_bytes) throws WasmtimeException {
36 if (!wasm_bytes.isDirect())
37 throw new WasmtimeException("passed in buffer must be direct");
38
39 return new WasmModule(newModuleNtv(super.getPtr(), wasm_bytes.asReadOnlyBuffer()));
40 }
41
42 public WasmModule newModule(byte[] wasm_bytes) throws WasmtimeException {
43 // ByteBuffers must be direct
44 ByteBuffer buf = ByteBuffer.allocateDirect(wasm_bytes.length);
45 buf.put(wasm_bytes);
46 return new WasmModule(newModuleNtv(super.getPtr(), buf));
47 }
48
49 public WasmModule newModule(File wasm_file) throws WasmtimeException, IOException {
50 try (InputStream in = new FileInputStream(wasm_file)) {
51 return this.newModule(in.readAllBytes());
52 }
53 }
54
55 public WasmLinker newLinker() throws WasmtimeException {
56 long ptr = newLinker(this.getPtr());
57 return new WasmLinker(ptr);
58 }
59
60 /** Need to support clone for safe copies being used across threads */
61 @Override
62 protected Object clone() throws CloneNotSupportedException {
63 // TODO Auto-generated method stub
64 return super.clone();
65 }
66}

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…