diff --git a/pom.xml b/pom.xml index 9c364e0c0..c1cd7dcb2 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 - com.dylibso.chickory + com.dylibso.chicory chicory 1.0-SNAPSHOT pom @@ -30,7 +30,7 @@ - com.dylibso.chickory + com.dylibso.chicory wasm ${project.version} diff --git a/runtime/pom.xml b/runtime/pom.xml index 8f7f62c00..1329f025c 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -3,7 +3,7 @@ 4.0.0 - com.dylibso.chickory + com.dylibso.chicory chicory 1.0-SNAPSHOT @@ -12,7 +12,7 @@ - com.dylibso.chickory + com.dylibso.chicory wasm @@ -25,7 +25,7 @@ - com.dylibso.chickory + com.dylibso.chicory test-gen-plugin ${project.version} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java index 1af173f10..49c0983d9 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java @@ -4,6 +4,7 @@ import com.dylibso.chicory.wasm.exceptions.ChicoryException; import com.dylibso.chicory.wasm.exceptions.InvalidException; import com.dylibso.chicory.wasm.types.*; +import java.io.InputStream; import java.util.HashMap; public class Module { @@ -15,6 +16,11 @@ public static Module build(String wasmFile) { return new Module(parser.parseModule()); } + public static Module build(InputStream inputWasmFile) { + var parser = new Parser(inputWasmFile); + return new Module(parser.parseModule()); + } + public static Module build(String wasmFile, ModuleType type) { switch (type) { case TEXT: diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java index 1f2121e17..88c7f914a 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -167,6 +167,7 @@ public void shouldSupportGlobals() { public void shouldCountVowels() { var instance = Module.build("src/test/resources/wasm/count_vowels.rs.wasm").instantiate(); var alloc = instance.getExport("alloc"); + var dealloc = instance.getExport("dealloc"); var countVowels = instance.getExport("count_vowels"); var memory = instance.getMemory(); var message = "Hello, World!"; @@ -174,6 +175,7 @@ public void shouldCountVowels() { var ptr = alloc.apply(Value.i32(len)).asInt(); memory.put(ptr, message); var result = countVowels.apply(Value.i32(ptr), Value.i32(len)); + dealloc.apply(Value.i32(ptr), Value.i32(len)); assertEquals(3, result.asInt()); } diff --git a/runtime/src/test/resources/wasm/count_vowels.rs b/runtime/src/test/resources/wasm/count_vowels.rs index 7114a9672..250f75f41 100644 --- a/runtime/src/test/resources/wasm/count_vowels.rs +++ b/runtime/src/test/resources/wasm/count_vowels.rs @@ -14,8 +14,8 @@ pub extern "C" fn alloc(len: i32) -> *const u8 { } #[no_mangle] -pub unsafe extern "C" fn dealloc(ptr: &mut u8) { - let _ = Vec::from_raw_parts(ptr, 0, 1024); +pub unsafe extern "C" fn dealloc(ptr: &mut u8, len: i32) { + let _ = Vec::from_raw_parts(ptr, 0, len as usize); } #[no_mangle] diff --git a/runtime/src/test/resources/wasm/count_vowels.rs.wasm b/runtime/src/test/resources/wasm/count_vowels.rs.wasm index 4ae0cc008..f9b5cfdf4 100755 Binary files a/runtime/src/test/resources/wasm/count_vowels.rs.wasm and b/runtime/src/test/resources/wasm/count_vowels.rs.wasm differ diff --git a/runtime/src/test/resources/wasm/extism-runtime-d.wasm b/runtime/src/test/resources/wasm/extism-runtime-d.wasm new file mode 100644 index 000000000..52d18e5ae Binary files /dev/null and b/runtime/src/test/resources/wasm/extism-runtime-d.wasm differ diff --git a/test-gen-plugin/pom.xml b/test-gen-plugin/pom.xml index 53d2b5f9a..4fed3d476 100644 --- a/test-gen-plugin/pom.xml +++ b/test-gen-plugin/pom.xml @@ -3,7 +3,7 @@ 4.0.0 - com.dylibso.chickory + com.dylibso.chicory chicory 1.0-SNAPSHOT diff --git a/wasm/pom.xml b/wasm/pom.xml index eaa6dce82..4e1de912e 100644 --- a/wasm/pom.xml +++ b/wasm/pom.xml @@ -3,7 +3,7 @@ 4.0.0 - com.dylibso.chickory + com.dylibso.chicory chicory 1.0-SNAPSHOT diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java b/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java index e048abbf4..e4a6b03b5 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java @@ -4,6 +4,7 @@ import com.dylibso.chicory.wasm.types.*; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; @@ -13,6 +14,7 @@ public class Parser { private String filePath; + private InputStream inputStream; private ParserListener listener; private List includeSections; @@ -24,6 +26,12 @@ public Parser(String filePath) { this.includeSections = null; } + public Parser(InputStream inputStream) { + this.inputStream = inputStream; + this.listener = null; + this.includeSections = null; + } + public void setListener(ParserListener listener) { this.listener = listener; } @@ -36,7 +44,10 @@ public void includeSection(int sectionId) { private ByteBuffer readByteBuffer() { try { // Read the Wasm file into a ByteBuffer - FileInputStream fileInputStream = new FileInputStream(filePath); + var fileInputStream = inputStream; + if (this.inputStream == null) { + fileInputStream = new FileInputStream(filePath); + } byte[] buf = new byte[fileInputStream.available()]; fileInputStream.read(buf); fileInputStream.close();