Skip to content

Commit

Permalink
Merge pull request #14 from extism/chicory-1.0.0-M1
Browse files Browse the repository at this point in the history
Upgrade to Chicory 1.0.0-M1
  • Loading branch information
bhelx authored Nov 19, 2024
2 parents a8a2a4b + b699218 commit 0ff3ba0
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 323 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<java.version>11</java.version>

<chicory.version>0.0.12</chicory.version>
<chicory.version>1.0.0-M1</chicory.version>
<junit.version>3.8.1</junit.version>

<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
Expand All @@ -86,7 +86,7 @@
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>aot</artifactId>
<artifactId>aot-experimental</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/org/extism/chicory/sdk/ChicoryModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.extism.chicory.sdk;

import com.dylibso.chicory.aot.AotMachine;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.experimental.aot.AotMachine;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.Parser;

import java.nio.file.Path;

Expand All @@ -12,22 +14,22 @@ class ChicoryModule {
static Module fromWasm(ManifestWasm m) {
if (m instanceof ManifestWasmBytes) {
ManifestWasmBytes mwb = (ManifestWasmBytes) m;
return Module.builder(mwb.bytes).build();
return Parser.parse(mwb.bytes);
} else if (m instanceof ManifestWasmPath) {
ManifestWasmPath mwp = (ManifestWasmPath) m;
return Module.builder(Path.of(mwp.path)).build();
return Parser.parse(Path.of(mwp.path));
} else if (m instanceof ManifestWasmFile) {
ManifestWasmFile mwf = (ManifestWasmFile) m;
return Module.builder(mwf.filePath).build();
return Parser.parse(mwf.filePath);
} else if (m instanceof ManifestWasmUrl) {
ManifestWasmUrl mwu = (ManifestWasmUrl) m;
return Module.builder(mwu.getUrlAsStream()).build();
return Parser.parse(mwu.getUrlAsStream());
} else {
throw new IllegalArgumentException("Unknown ManifestWasm type " + m.getClass());
}
}

static Module.Builder instanceWithOptions(Module.Builder m, Manifest.Options opts) {
static Instance.Builder instanceWithOptions(Instance.Builder m, Manifest.Options opts) {
if (opts == null) {
return m;
}
Expand Down
67 changes: 35 additions & 32 deletions src/main/java/org/extism/chicory/sdk/DependencyGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.ImportFunction;
import com.dylibso.chicory.runtime.ImportValue;
import com.dylibso.chicory.runtime.ImportValues;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Store;
import com.dylibso.chicory.runtime.WasmFunctionHandle;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.types.Export;
import com.dylibso.chicory.wasm.types.ExportSection;
import com.dylibso.chicory.wasm.types.ExternalType;
Expand Down Expand Up @@ -93,7 +96,7 @@ private void checkCollision(String moduleName, String symbol) {
public void registerModule(String name, Module m) {
checkCollision(name, null);

ExportSection exportSection = m.wasmModule().exportSection();
ExportSection exportSection = m.exportSection();
for (int i = 0; i < exportSection.exportCount(); i++) {
Export export = exportSection.getExport(i);
String exportName = export.name();
Expand All @@ -112,10 +115,10 @@ public boolean validate() {
for (var kv : modules.entrySet()) {
Module m = kv.getValue();

ImportSection imports = m.wasmModule().importSection();
ImportSection imports = m.importSection();
for (int i = 0; i < imports.importCount(); i++) {
Import imp = imports.getImport(i);
String moduleName = imp.moduleName();
String moduleName = imp.module();
String symbolName = imp.name();
if (!registeredSymbols.containsKey(moduleName) || !registeredSymbols.get(moduleName).contains(symbolName)) {
logger.warnf("Cannot find symbol: %s.%s\n", moduleName, symbolName);
Expand Down Expand Up @@ -156,13 +159,13 @@ public Instance instantiate() {
Module m = this.modules.get(moduleId);
boolean satisfied = true;
List<HostFunction> trampolines = new ArrayList<>();
ImportSection imports = m.wasmModule().importSection();
ImportSection imports = m.importSection();
// We assume that each unique `name` in an import of the form `name.symbol`
// is registered as a module with that name
//
// FIXME: this is actually a strong assumption, because we could
// define "overrides" by overwriting individual `name.symbol` in our table.
var requiredModules = imports.stream().collect(groupingBy(Import::moduleName));
var requiredModules = imports.stream().collect(groupingBy(Import::module));

if (!requiredModules.isEmpty()) {
// We need to check whether the given import is available
Expand Down Expand Up @@ -217,47 +220,47 @@ private Instance instantiate(String moduleId, List<HostFunction> moreHostFunctio
Module m = this.modules.get(moduleId);
Objects.requireNonNull(m);

HostImports extendedHostImports =
mergeHostImports(store.toHostImports(), moreHostFunctions);
ImportValues importValues =
mergeImportValues(store.toImportValues(), moreHostFunctions);

Instance instance =
ChicoryModule.instanceWithOptions(
Module.builder(m.wasmModule()), this.options)
.withHostImports(extendedHostImports)
Instance.builder(m), this.options)
.withImportValues(importValues)
.withStart(false)
.build().instantiate();
.build();
this.store.register(moduleId, instance);
this.instances.put(moduleId, instance);
return instance;
}

private HostImports mergeHostImports(HostImports hostImports, List<HostFunction> trampolines) {
HostFunction[] hostFunctions = hostImports.functions();
List<HostFunction> mergedList = new ArrayList<>(trampolines);
for (HostFunction fn : hostFunctions) {
private ImportValues mergeImportValues(ImportValues hostImports, List<HostFunction> trampolines) {
ImportFunction[] hostFunctions = hostImports.functions();
List<ImportFunction> mergedList = new ArrayList<>(trampolines);
for (ImportFunction fn : hostFunctions) {
for (HostFunction t : trampolines) {
if (t.moduleName().equals(fn.fieldName()) && t.fieldName().equals(fn.fieldName())) {
if (t.module().equals(fn.name()) && t.name().equals(fn.name())) {
// If one such case exists, the "proper" function takes precedence over the trampoline.
mergedList.remove(t);
}
}
mergedList.add(fn);
}
return new HostImports(
mergedList.toArray(new HostFunction[mergedList.size()]),
hostImports.globals(),
hostImports.memories(),
hostImports.tables());
return ImportValues.builder().addFunction(
mergedList.toArray(new ImportFunction[mergedList.size()]))
.addGlobal(hostImports.globals())
.addMemory(hostImports.memories())
.addTable(hostImports.tables()).build();
}

private HostFunction registerTrampoline(FunctionImport f, Module m) {
// Trampolines are singletons for each <moduleName, name> pair.
// Trampolines are not registered into the store, as they are not "real" functions.
// They are instead kept separately and passed explicitly to the instance.
Trampoline trampoline = this.trampolines.computeIfAbsent(
new QualifiedName(f.moduleName(), f.name()), k -> new Trampoline());
var functionType = m.wasmModule().typeSection().getType(f.typeIndex());
return trampoline.asHostFunction(f.moduleName(), f.name(), functionType);
new QualifiedName(f.module(), f.name()), k -> new Trampoline());
var functionType = m.typeSection().getType(f.typeIndex());
return trampoline.asHostFunction(f.module(), f.name(), functionType);
}

/**
Expand All @@ -268,8 +271,8 @@ private HostFunction registerTrampoline(FunctionImport f, Module m) {
public void registerFunctions(HostFunction... functions) {
store.addFunction(functions);
for (HostFunction f : functions) {
this.hostModules.add(f.moduleName());
registerSymbol(f.moduleName(), f.fieldName());
this.hostModules.add(f.module());
registerSymbol(f.module(), f.name());
}
}

Expand All @@ -294,7 +297,7 @@ private Instance getMainInstance() {

static final class Trampoline implements WasmFunctionHandle {
WasmFunctionHandle f =
(Instance instance, Value... args) -> {
(Instance instance, long... args) -> {
throw new ExtismException("Unresolved trampoline");
};

Expand All @@ -303,17 +306,17 @@ public void resolveFunction(HostFunction hf) {
}

public void resolveFunction(ExportFunction ef) {
this.f = (Instance instance, Value... args) -> ef.apply(args);
this.f = (Instance instance, long... args) -> ef.apply(args);
}

@Override
public Value[] apply(Instance instance, Value... args) {
public long[] apply(Instance instance, long... args) {
return f.apply(instance, args);
}

public HostFunction asHostFunction(String moduleName, String name, FunctionType functionType) {
return new HostFunction(this, moduleName, name,
functionType.params(), functionType.returns());
return new HostFunction(moduleName, name,
functionType.params(), functionType.returns(), this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/extism/chicory/sdk/ExtismException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.extism.chicory.sdk;

public class ExtismException extends RuntimeException{
public class ExtismException extends RuntimeException {

public ExtismException(String message) {
super(message);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/extism/chicory/sdk/ExtismFunctionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.extism.chicory.sdk;

public class ExtismFunctionException extends ExtismException {

private final String error;

public ExtismFunctionException(String function, String message) {
super(String.format("function %s returned an error: %s", function, message));
this.error = message;
}

/**
* Underlying error returned by the plugin call
*/
public String getError() {
return error;
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/extism/chicory/sdk/ExtismHostFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public void bind(CurrentPlugin p) {

final HostFunction asHostFunction() {
return new HostFunction(
(Instance inst, Value... args) -> handle.apply(this.currentPlugin, args),
module, name, paramTypes, returnTypes);
module, name, paramTypes, returnTypes,
(Instance inst, long... args) -> handle.apply(this.currentPlugin, args));
}

@FunctionalInterface
public interface Handle {
Value[] apply(CurrentPlugin currentPlugin, Value... args);
long[] apply(CurrentPlugin currentPlugin, long... args);
}
}
Loading

0 comments on commit 0ff3ba0

Please sign in to comment.