-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Edoardo Vacchi <[email protected]>
- Loading branch information
Showing
8 changed files
with
431 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
wasi/src/main/java/com/dylibso/chicory/wasi/FunctionSignature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
wasi/src/main/java/com/dylibso/chicory/wasi/FunctionSignatureBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
wasi/src/main/java/com/dylibso/chicory/wasi/HostFunctionBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
wasi/src/main/java/com/dylibso/chicory/wasi/HostModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
73 changes: 73 additions & 0 deletions
73
wasi/src/main/java/com/dylibso/chicory/wasi/HostModuleInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.