Skip to content

Commit

Permalink
Introduce host modules.
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Aug 22, 2024
1 parent 0f71166 commit 5989b31
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 628 deletions.
14 changes: 10 additions & 4 deletions cli/src/main/java/com/dylibso/chicory/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ public void run() {
var imports =
wasi
? new HostImports(
new WasiPreview1(
logger,
WasiOptions.builder().inheritSystem().build())
.toHostFunctions())
WasiPreview1.instance(
WasiPreview1.toHostModule(),
WasiPreview1.builder()
.withLogger(logger)
.withOpts(
WasiOptions.builder()
.inheritSystem()
.build())
.build())
.hostFunctions())
: new HostImports();
var instance =
Instance.builder(module)
Expand Down
38 changes: 38 additions & 0 deletions wasi/src/main/java/com/dylibso/chicory/wasi/FunctionSignature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dylibso.chicory.wasi;

import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;

public class FunctionSignature {
private final String moduleName;
private final String name;
private final List<ValueType> paramTypes;
private final List<ValueType> returnTypes;

public FunctionSignature(
String moduleName,
String name,
List<ValueType> paramTypes,
List<ValueType> returnTypes) {
this.moduleName = moduleName;
this.name = name;
this.paramTypes = paramTypes;
this.returnTypes = returnTypes;
}

public String moduleName() {
return moduleName;
}

public String name() {
return name;
}

public List<ValueType> paramTypes() {
return paramTypes;
}

public List<ValueType> returnTypes() {
return returnTypes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dylibso.chicory.wasi;

public class FunctionSignatureBundle implements HostModule {

private final String name;
private final FunctionSignature[] signatures;

public FunctionSignatureBundle(String name, FunctionSignature[] signatures) {
this.name = name;
this.signatures = signatures;
}

@Override
public FunctionSignature[] signatures() {
return signatures;
}

@Override
public String name() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dylibso.chicory.wasi;

import com.dylibso.chicory.runtime.HostFunction;

public class HostFunctionBundle implements HostModuleInstance {
private final HostModule module;
private final HostFunction[] hostFunctions;
private final Runnable onClose;

public HostFunctionBundle(HostModule module, HostFunction[] hostFunctions, Runnable onClose) {
this.module = module;
this.hostFunctions = hostFunctions;
this.onClose = onClose;
}

@Override
public HostModule module() {
return module;
}

@Override
public HostFunction[] hostFunctions() {
return hostFunctions;
}

@Override
public void close() {
onClose.run();
}
}
38 changes: 38 additions & 0 deletions wasi/src/main/java/com/dylibso/chicory/wasi/HostModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dylibso.chicory.wasi;

import com.dylibso.chicory.wasm.types.ValueType;
import java.util.ArrayList;
import java.util.List;

public interface HostModule {

public static class Builder {
private final String moduleName;
private final List<FunctionSignature> signatures;
private AutoCloseable closeable;

public Builder(String moduleName) {
this.moduleName = moduleName;
this.signatures = new ArrayList<>();
}

public Builder withFunctionSignature(
String name, List<ValueType> paramTypes, List<ValueType> returnTypes) {
signatures.add(new FunctionSignature(this.moduleName, name, paramTypes, returnTypes));
return this;
}

public HostModule build() {
return new FunctionSignatureBundle(
moduleName, signatures.toArray(new FunctionSignature[signatures.size()]));
}
}

static Builder builder(String moduleName) {
return new HostModule.Builder(moduleName);
}

FunctionSignature[] signatures();

String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.dylibso.chicory.wasi;

import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.WasmFunctionHandle;
import java.util.HashMap;
import java.util.Map;

public interface HostModuleInstance extends AutoCloseable {

class Builder {
private HostModule hostModule;
private Runnable onClose;
private Map<String, WasmFunctionHandle> bindings;

private Builder(HostModule hostModule) {
this.hostModule = hostModule;
this.bindings = new HashMap<>(hostModule.signatures().length);
}

public Builder onClose(Runnable onClose) {
this.onClose = onClose;
return this;
}

public Builder bind(String fname, WasmFunctionHandle handle) {
this.bindings.put(fname, handle);
return this;
}

public HostModuleInstance build() {
var onClose = this.onClose;
if (onClose == null) {
onClose = () -> {}; // NOP
}

FunctionSignature[] signatures = this.hostModule.signatures();
if (this.bindings.size() != signatures.length) {
throw new IllegalArgumentException(
"Must supply a binding for each function signature");
}

HostFunction[] hostFunctions = new HostFunction[signatures.length];
for (int i = 0; i < signatures.length; i++) {
FunctionSignature signature = signatures[i];
String name = signature.name();
WasmFunctionHandle handle = this.bindings.get(name);
if (handle == null) {
throw new IllegalArgumentException(
String.format("Cannot find a binding for function signature %s", name));
}
hostFunctions[i] =
new HostFunction(
handle,
signature.moduleName(),
name,
signature.paramTypes(),
signature.returnTypes());
}

return new HostFunctionBundle(hostModule, hostFunctions, onClose);
}
}

static Builder builder(HostModule hostModule) {
return new Builder(hostModule);
}

HostModule module();

HostFunction[] hostFunctions();

void close();
}
Loading

0 comments on commit 5989b31

Please sign in to comment.