Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@HostModule("env")
class ABI {

private Handler handler;
private final Handler handler;
private Memory memory;
ExportFunction initializeFn;
ExportFunction mainFn;
Expand Down Expand Up @@ -58,12 +59,8 @@ class ABI {
ExportFunction proxyOnQueueReadyFn;
ExportFunction proxyOnForeignFunctionFn;

Handler getHandler() {
return handler;
}

void setHandler(Handler handler) {
this.handler = handler;
ABI(Handler handler) {
this.handler = Objects.requireNonNull(handler);
}

void setInstance(Instance instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.roastedroot.proxywasm.LogLevel;
import io.roastedroot.proxywasm.WasmException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
* Implements a restricted subset of the WASI ABI, specifically so that a WASM module can
Expand All @@ -29,7 +30,7 @@ public class ABI_WASI {
private final Handler handler;

public ABI_WASI(Handler handler) {
this.handler = handler;
this.handler = Objects.requireNonNull(handler);
}

////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ public final class ProxyWasm implements Closeable {

private ProxyWasm(Builder other) throws StartException {
this.pluginHandler = Objects.requireNonNullElse(other.pluginHandler, new Handler() {});
this.abi = other.abi;
this.abi.setHandler(createImportsHandler());

// initialize/start the vm
if (this.abi.initialize()) {
this.abi.main(0, 0);
} else {
this.abi.start();
}

if (other.start) {
start();
}
this.abi = new ABI(createImportsHandler());
}

public Handler getPluginHandler() {
Expand Down Expand Up @@ -251,13 +239,13 @@ ABI abi() {

public static class Builder implements Cloneable {

private final ABI abi = new ABI();

private Handler pluginHandler;
private ImportMemory memory;
private boolean start = true;
private Function<Instance, Machine> machineFactory;

private Builder() {}

@Override
@SuppressWarnings("NoClone")
protected Builder clone() {
Expand Down Expand Up @@ -288,24 +276,16 @@ public ProxyWasm.Builder withMachineFactory(Function<Instance, Machine> machineF
return this;
}

Builder() {}

public ProxyWasm build(Instance instance) throws StartException {
abi.setInstance(instance);
return new ProxyWasm(this.clone());
}

public ProxyWasm build(WasmModule module) throws StartException {
return this.build(Instance.builder(module));
}

public ProxyWasm build(Instance.Builder instanceBuilder) throws StartException {
var imports = ImportValues.builder();

var instanceBuilder = Instance.builder(module);
if (this.machineFactory != null) {
instanceBuilder.withMachineFactory(this.machineFactory);
}

var proxyWasm = new ProxyWasm(this.clone());
ABI abi = proxyWasm.abi;

var imports = ImportValues.builder();
imports.addMemory(Objects.requireNonNullElseGet(memory, this::defaultImportMemory));
imports.addFunction(abi.toHostFunctions());

Expand All @@ -315,7 +295,18 @@ public ProxyWasm build(Instance.Builder instanceBuilder) throws StartException {
.withImportValues(imports.build())
.build();

return build(instance);
abi.setInstance(instance);

if (abi.initialize()) {
abi.main(0, 0);
} else {
abi.start();
}
if (start) {
proxyWasm.start();
}

return proxyWasm;
}

ImportMemory defaultImportMemory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.experimental.aot.AotMachine;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.compiler.MachineFactoryCompiler;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import io.roastedroot.proxywasm.StartException;
Expand All @@ -24,9 +23,11 @@ public class DispatchCallOnTickTest {
@Test
public void testOnTick() throws StartException {
var handler = new MockHandler();
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
var instanceBuilder = Instance.builder(module).withMachineFactory(AotMachine::new);
try (var host = builder.build(instanceBuilder)) {
ProxyWasm.Builder builder =
ProxyWasm.builder()
.withPluginHandler(handler)
.withMachineFactory(MachineFactoryCompiler::compile);
try (var host = builder.build(module)) {
assertEquals(tickMilliseconds, handler.getTickPeriodMilliseconds());

for (int i = 1; i <= 10; i++) {
Expand Down
Loading