Skip to content

Commit f8213a4

Browse files
committed
Setup SynthInstance class + Host functions support
1 parent 4d6e7b9 commit f8213a4

3 files changed

Lines changed: 105 additions & 7 deletions

File tree

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
package com.synthmodloader.modloader.integration;
22

3-
import com.dylibso.chicory.runtime.ExportFunction; // works fine
4-
import com.dylibso.chicory.wasm.Parser; // breaks
3+
import com.dylibso.chicory.runtime.HostFunction;
54
import com.dylibso.chicory.runtime.Instance;
5+
import com.dylibso.chicory.wasm.types.FunctionType;
6+
import com.dylibso.chicory.wasm.types.ValType;
67
import com.synthmodloader.modloader.SynthModLoader;
8+
import com.synthmodloader.modloader.synth.SynthInstance;
9+
import net.minecraft.resources.ResourceLocation;
710

8-
import java.io.IOException;
11+
import java.util.List;
912

1013
public class ChicoryIntegration {
1114
public static void init() {
1215
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
1316
try {
14-
var module = Parser.parse(classloader.getResourceAsStream("test.wasm"));
15-
Instance instance = Instance.builder(module).build();
16-
ExportFunction testFunction = instance.export("testFunction");
17-
SynthModLoader.LOGGER.info("OUTPUT: " + testFunction.apply(1)[0]);
17+
// var module = Parser.parse(classloader.getResourceAsStream("test.wasm"));
18+
// Instance instance = Instance.builder(module).build();
19+
// ExportFunction testFunction = instance.export("iterFact");
20+
var synth = SynthInstance.Builder.create()
21+
.location(ResourceLocation.fromNamespaceAndPath("sml", "data/logger.wasm"))
22+
.function(new HostFunction(
23+
"console",
24+
"log",
25+
FunctionType.of(
26+
List.of(ValType.I32, ValType.I32),
27+
List.of()
28+
),
29+
ChicoryIntegration::log
30+
))
31+
.build();
32+
var testFunction = synth.export("logIt").orElseThrow();
33+
testFunction.apply();
34+
SynthModLoader.LOGGER.info("OUTPUT");
35+
1836
} catch(Exception e) {
1937
SynthModLoader.LOGGER.error("Error loading test module !", e);
2038
}
2139
}
40+
private static long[] log(Instance instance, long... args) {
41+
var len = (int) args[0];
42+
var offset = (int) args[1];
43+
var message = instance.memory().readString(offset, len);
44+
SynthModLoader.LOGGER.info(message);
45+
return null;
46+
}
2247
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.synthmodloader.modloader.synth;
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.synthmodloader.modloader.SynthModLoader;
9+
import net.minecraft.client.Minecraft;
10+
import net.minecraft.resources.ResourceLocation;
11+
import net.neoforged.fml.ModContainer;
12+
import net.neoforged.fml.ModList;
13+
import net.neoforged.fml.ModLoader;
14+
import net.neoforged.neoforge.server.ServerLifecycleHooks;
15+
16+
import java.io.IOException;
17+
import java.time.chrono.MinguoEra;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.List;
21+
import java.util.Optional;
22+
23+
public class SynthInstance {
24+
public final ResourceLocation location;
25+
private final Instance wasmInstance;
26+
private SynthInstance(ResourceLocation _location, Collection<HostFunction> functions) throws IOException {
27+
location = _location;
28+
var store = new Store();
29+
functions.forEach(store::addFunction);
30+
if (ModList.get().getModFileById(location.getNamespace()) == null)
31+
throw new IllegalArgumentException("Namespace `" + location.getNamespace() + "` does not exist !");
32+
var path = ModList.get().getModFileById(location.getNamespace()).getFile().findResource(location.getPath());
33+
wasmInstance = store.instantiate(location.getPath(), Parser.parse(path));
34+
}
35+
36+
public Optional<ExportFunction> export(String functionName) {
37+
if (wasmInstance != null && wasmInstance.export(functionName) != null)
38+
return Optional.of(wasmInstance.export(functionName));
39+
else
40+
return Optional.empty();
41+
42+
}
43+
44+
public static class Builder {
45+
private List<HostFunction> functions;
46+
private ResourceLocation location;
47+
private Builder() {}
48+
49+
public Builder function(HostFunction function) {
50+
functions.add(function);
51+
return this;
52+
}
53+
public Builder location(ResourceLocation location) {
54+
this.location = location;
55+
return this;
56+
}
57+
public SynthInstance build() {
58+
try {
59+
return new SynthInstance(location, functions);
60+
} catch (Exception e) {
61+
SynthModLoader.LOGGER.error("Couldn't create SynthInstance !", e);
62+
}
63+
return null;
64+
65+
}
66+
67+
public static Builder create() {
68+
var builder = new Builder();
69+
builder.functions = new ArrayList<>();
70+
return builder;
71+
}
72+
}
73+
}
150 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)