Skip to content

Commit f82c7de

Browse files
authored
Merge pull request #18926 from jsfan3/BAEL-9118
BAEL-9118
2 parents ed2cf92 + fdc01ea commit f82c7de

File tree

9 files changed

+169
-0
lines changed

9 files changed

+169
-0
lines changed

libraries-bytecode/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<artifactId>asm-util</artifactId>
4141
<version>${asm.version}</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>com.dylibso.chicory</groupId>
45+
<artifactId>runtime</artifactId>
46+
<version>${chicory.version}</version>
47+
</dependency>
4348
<!--
4449
Importing the libraries as jars using system path. These are two versions of the same library, which is generated
4550
from the code inside /incompatibleclasschange/libraries/modified and /incompatibleclasschange/libraries/original
@@ -118,6 +123,7 @@
118123
<asm.version>5.2</asm.version>
119124
<teavm.version>0.10.1</teavm.version>
120125
<objenesis.version>3.4</objenesis.version>
126+
<chicory.version>1.5.3</chicory.version>
121127
</properties>
122128

123129
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.chicory;
2+
3+
public class ChicoryAddExample {
4+
5+
}
6+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.chicory;
2+
3+
import com.dylibso.chicory.runtime.ExportFunction;
4+
import com.dylibso.chicory.runtime.HostFunction;
5+
import com.dylibso.chicory.runtime.Instance;
6+
import com.dylibso.chicory.runtime.Store;
7+
import com.dylibso.chicory.wasm.Parser;
8+
import com.dylibso.chicory.wasm.WasmModule;
9+
import com.dylibso.chicory.wasm.types.FunctionType;
10+
import com.dylibso.chicory.wasm.types.ValType;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.io.InputStream;
14+
import java.util.List;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
20+
class ChicoryUnitTest {
21+
22+
@Test
23+
void givenAddModule_whenCallingAddWithTwoAndForty_thenResultIsFortyTwo() {
24+
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
25+
assertNotNull(wasm);
26+
27+
WasmModule module = Parser.parse(wasm);
28+
Instance instance = Instance.builder(module).build();
29+
ExportFunction add = instance.export("add");
30+
31+
long[] result = add.apply(2, 40);
32+
33+
assertEquals(42, (int) result[0]);
34+
}
35+
36+
@Test
37+
void givenImportDouble_whenCallingUseDouble_thenResultIsDoubled() {
38+
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
39+
assertNotNull(wasm);
40+
41+
HostFunction doubleFn = new HostFunction(
42+
"host",
43+
"double",
44+
FunctionType.of(List.of(ValType.I32), List.of(ValType.I32)),
45+
(Instance instance, long... args) -> new long[] { args[0] * 2 }
46+
);
47+
48+
Store store = new Store();
49+
store.addFunction(doubleFn);
50+
51+
WasmModule module = Parser.parse(wasm);
52+
Instance instance = store.instantiate("imports", module);
53+
ExportFunction useDouble = instance.export("useDouble");
54+
55+
long[] result = useDouble.apply(21);
56+
57+
assertEquals(42L, result[0]);
58+
}
59+
60+
@Test
61+
void whenInstantiatingModuleWithoutRequiredImport_thenErrorIsThrown() {
62+
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
63+
assertNotNull(wasm);
64+
65+
WasmModule module = Parser.parse(wasm);
66+
67+
assertThrows(RuntimeException.class, () -> {
68+
Instance.builder(module).build();
69+
});
70+
}
71+
72+
@Test
73+
void whenRequestingMissingExport_thenErrorIsThrown() {
74+
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
75+
assertNotNull(wasm);
76+
77+
WasmModule module = Parser.parse(wasm);
78+
Instance instance = Instance.builder(module).build();
79+
80+
assertThrows(RuntimeException.class, () -> instance.export("sum"));
81+
}
82+
83+
}
53 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(func (export "add") (param i32 i32) (result i32)
3+
local.get 0
4+
local.get 1
5+
i32.add))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Render with Graphviz, https://dreampuf.github.io/GraphvizOnline/?engine=dot
2+
digraph WasmOnJVM {
3+
rankdir=LR;
4+
node [shape=box, style=rounded];
5+
6+
subgraph cluster_host {
7+
label="Host (JVM)";
8+
style=rounded;
9+
host_app [label="JUnit test / application"];
10+
chicory [label="Chicory runtime\n(Instance)"];
11+
}
12+
13+
subgraph cluster_module {
14+
label="Wasm module";
15+
style=rounded;
16+
module [label=".wasm binary"];
17+
export_add [label="Exported functions"];
18+
imports [label="Imported functions"];
19+
}
20+
21+
host_app -> chicory [label="Java calls"];
22+
chicory -> module [label="load & instantiate"];
23+
chicory -> export_add [label="invoke exports"];
24+
export_add -> chicory [label="results"];
25+
chicory -> host_app [label="map to JVM types"];
26+
27+
chicory -> imports [label="provide imports", style=dashed];
28+
}
29+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Render with Graphviz, https://dreampuf.github.io/GraphvizOnline/?engine=dot
2+
digraph ImportsFlow {
3+
rankdir=LR;
4+
node [shape=box, style=rounded];
5+
6+
subgraph cluster_host {
7+
label="Host (JVM)";
8+
style=rounded;
9+
10+
junit [label="JUnit test"];
11+
store [label="Store\n(registers imports)"];
12+
doublef [label="HostFunction\nhost.double : (i32) -> i32"];
13+
inst [label="Chicory Instance"];
14+
}
15+
16+
subgraph cluster_module {
17+
label="Wasm module: imports.wasm";
18+
style=rounded;
19+
20+
importD [label="Import required:\nhost.double : (i32) -> i32"];
21+
useD [label="Exported function:\nuseDouble(i32) -> i32"];
22+
}
23+
24+
// Linking at instantiation
25+
store -> inst [label="instantiate with Store", style=dashed];
26+
importD -> doublef [label="resolved to HostFunction", style=dashed];
27+
28+
// Call flow at runtime
29+
junit -> inst [label="invoke useDouble(21)"];
30+
inst -> useD [label="call export"];
31+
useD -> importD[label="import call"];
32+
importD -> doublef [label="dispatch"];
33+
doublef -> inst [label="return i32 42"];
34+
inst -> junit [label="result as long[]{42}"];
35+
}
87 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(import "host" "double" (func $double (param i32) (result i32)))
3+
(func (export "useDouble") (param i32) (result i32)
4+
local.get 0
5+
call $double))

0 commit comments

Comments
 (0)