forked from wasmerio/wasmer-java
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModule.java
More file actions
110 lines (96 loc) · 3.24 KB
/
Module.java
File metadata and controls
110 lines (96 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.wasmer;
/**
* `Module` is a Java class that represents a WebAssembly module.
* <p>
* Example:
* <pre>{@code
* boolean isValid = Module.validate(wasmBytes);
*
* Module module = new Module(wasmBytes);
* Instance instance = module.instantiate();
* }</pre>
*/
// Used in Rust
@SuppressWarnings("unused")
public class Module {
static {
if (!Native.LOADED_EMBEDDED_LIBRARY) {
System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT);
}
}
private native long nativeModuleInstantiate(Module self, byte[] moduleBytes) throws RuntimeException;
private native void nativeDrop(long modulePointer);
private native long nativeInstantiate(long modulePointer, Instance instance, long importsPointer);
private static native boolean nativeValidate(byte[] moduleBytes);
private native byte[] nativeSerialize(long modulePointer);
private static native long nativeDeserialize(Module module, byte[] serializedBytes);
protected long modulePointer;
/**
* Check that given bytes represent a valid WebAssembly module.
*
* @param moduleBytes WebAssembly bytes.
* @return true if, and only if, given bytes are valid as a WebAssembly module.
*/
public static boolean validate(byte[] moduleBytes) {
return Module.nativeValidate(moduleBytes);
}
/**
* The constructor instantiates a new WebAssembly module based on
* WebAssembly bytes.
*
* @param moduleBytes WebAssembly bytes.
*/
public Module(byte[] moduleBytes) throws RuntimeException {
this.modulePointer = this.nativeModuleInstantiate(this, moduleBytes);
}
private Module() {}
/**
* Delete a module object pointer.
*/
public void close() {
// To avoid duplicate native dropping
if (this.modulePointer != 0L) {
this.nativeDrop(this.modulePointer);
this.modulePointer = 0L;
}
}
/**
* Delete a module object pointer, which is called by the garbage collector
* before an object is removed from the memory.
*/
public void finalize() {
this.close();
}
/**
* Create an instance object based on a module object.
*
* @return Instance object.
*/
public Instance instantiate(Imports imports) {
Instance instance = new Instance();
long instancePointer = this.nativeInstantiate(this.modulePointer, instance, imports.importsPointer);
instance.instancePointer = instancePointer;
Instance.nativeInitializeExportedFunctions(instancePointer);
Instance.nativeInitializeExportedMemories(instancePointer);
Instance.nativeInitializeExportedGlobals(instancePointer);
return instance;
}
/**
* Create a serialized byte array from a WebAssembly module.
*
* @return Serialized bytes.
*/
public byte[] serialize() {
return this.nativeSerialize(this.modulePointer);
}
/**
* Create an original Module object from a byte array.
*
* @return Module object.
*/
public static Module deserialize(byte[] serializedBytes) {
Module module = new Module();
module.modulePointer = Module.nativeDeserialize(module, serializedBytes);
return module;
}
}