Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libraries-bytecode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>asm-util</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>${chicory.version}</version>
</dependency>
<!--
Importing the libraries as jars using system path. These are two versions of the same library, which is generated
from the code inside /incompatibleclasschange/libraries/modified and /incompatibleclasschange/libraries/original
Expand Down Expand Up @@ -118,6 +123,7 @@
<asm.version>5.2</asm.version>
<teavm.version>0.10.1</teavm.version>
<objenesis.version>3.4</objenesis.version>
<chicory.version>1.5.3</chicory.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.chicory;

public class ChicoryAddExample {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.baeldung.chicory;

import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Store;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import com.dylibso.chicory.wasm.types.FunctionType;
import com.dylibso.chicory.wasm.types.ValType;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ChicoryUnitTest {

@Test
void givenAddModule_whenCallingAddWithTwoAndForty_thenResultIsFortyTwo() {
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
assertNotNull(wasm);

WasmModule module = Parser.parse(wasm);
Instance instance = Instance.builder(module).build();
ExportFunction add = instance.export("add");

long[] result = add.apply(2, 40);

assertEquals(42, (int) result[0]);
}

@Test
void givenImportDouble_whenCallingUseDouble_thenResultIsDoubled() {
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
assertNotNull(wasm);

HostFunction doubleFn = new HostFunction(
"host",
"double",
FunctionType.of(List.of(ValType.I32), List.of(ValType.I32)),
(Instance instance, long... args) -> new long[] { args[0] * 2 }
);

Store store = new Store();
store.addFunction(doubleFn);

WasmModule module = Parser.parse(wasm);
Instance instance = store.instantiate("imports", module);
ExportFunction useDouble = instance.export("useDouble");

long[] result = useDouble.apply(21);

assertEquals(42L, result[0]);
}

@Test
void whenInstantiatingModuleWithoutRequiredImport_thenErrorIsThrown() {
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
assertNotNull(wasm);

WasmModule module = Parser.parse(wasm);

assertThrows(RuntimeException.class, () -> {
Instance.builder(module).build();
});
}

@Test
void whenRequestingMissingExport_thenErrorIsThrown() {
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
assertNotNull(wasm);

WasmModule module = Parser.parse(wasm);
Instance instance = Instance.builder(module).build();

assertThrows(RuntimeException.class, () -> instance.export("sum"));
}

}
Binary file not shown.
5 changes: 5 additions & 0 deletions libraries-bytecode/src/test/resources/chicory/add.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
29 changes: 29 additions & 0 deletions libraries-bytecode/src/test/resources/chicory/graph.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Render with Graphviz, https://dreampuf.github.io/GraphvizOnline/?engine=dot
digraph WasmOnJVM {
rankdir=LR;
node [shape=box, style=rounded];

subgraph cluster_host {
label="Host (JVM)";
style=rounded;
host_app [label="JUnit test / application"];
chicory [label="Chicory runtime\n(Instance)"];
}

subgraph cluster_module {
label="Wasm module";
style=rounded;
module [label=".wasm binary"];
export_add [label="Exported functions"];
imports [label="Imported functions"];
}

host_app -> chicory [label="Java calls"];
chicory -> module [label="load & instantiate"];
chicory -> export_add [label="invoke exports"];
export_add -> chicory [label="results"];
chicory -> host_app [label="map to JVM types"];

chicory -> imports [label="provide imports", style=dashed];
}

35 changes: 35 additions & 0 deletions libraries-bytecode/src/test/resources/chicory/imports-flow.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Render with Graphviz, https://dreampuf.github.io/GraphvizOnline/?engine=dot
digraph ImportsFlow {
rankdir=LR;
node [shape=box, style=rounded];

subgraph cluster_host {
label="Host (JVM)";
style=rounded;

junit [label="JUnit test"];
store [label="Store\n(registers imports)"];
doublef [label="HostFunction\nhost.double : (i32) -> i32"];
inst [label="Chicory Instance"];
}

subgraph cluster_module {
label="Wasm module: imports.wasm";
style=rounded;

importD [label="Import required:\nhost.double : (i32) -> i32"];
useD [label="Exported function:\nuseDouble(i32) -> i32"];
}

// Linking at instantiation
store -> inst [label="instantiate with Store", style=dashed];
importD -> doublef [label="resolved to HostFunction", style=dashed];

// Call flow at runtime
junit -> inst [label="invoke useDouble(21)"];
inst -> useD [label="call export"];
useD -> importD[label="import call"];
importD -> doublef [label="dispatch"];
doublef -> inst [label="return i32 42"];
inst -> junit [label="result as long[]{42}"];
}
Binary file not shown.
5 changes: 5 additions & 0 deletions libraries-bytecode/src/test/resources/chicory/imports.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(import "host" "double" (func $double (param i32) (result i32)))
(func (export "useDouble") (param i32) (result i32)
local.get 0
call $double))