Skip to content

Commit 40d876e

Browse files
committed
Move foreign function state to the handler.
Add withForeignFunctions and withLogger functions to the WamPlugin Builder. Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent c00a9a6 commit 40d876e

File tree

13 files changed

+159
-42
lines changed

13 files changed

+159
-42
lines changed

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ABI.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,12 @@ int proxyCallForeignFunction(
18681868
try {
18691869
var name = string(readMemory(nameDataPtr, nameSize));
18701870
var argument = readMemory(argumentDataPtr, argumentSize);
1871-
var result = handler.callForeignFunction(name, argument);
1871+
1872+
var func = handler.getForeignFunction(name);
1873+
if (func == null) {
1874+
return WasmResult.NOT_FOUND.getValue();
1875+
}
1876+
var result = func.apply(argument);
18721877

18731878
// Allocate memory in the WebAssembly instance
18741879
int addr = malloc(result.length);

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ public int dispatchHttpCall(
227227
}
228228

229229
@Override
230-
public byte[] callForeignFunction(String name, byte[] bytes) throws WasmException {
231-
return next().callForeignFunction(name, bytes);
230+
public ForeignFunction getForeignFunction(String name) {
231+
return next().getForeignFunction(name);
232232
}
233233

234234
@Override

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/Handler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ default int dispatchHttpCall(
322322
throw new WasmException(WasmResult.UNIMPLEMENTED);
323323
}
324324

325-
default byte[] callForeignFunction(String name, byte[] bytes) throws WasmException {
326-
throw new WasmException(WasmResult.NOT_FOUND);
325+
default ForeignFunction getForeignFunction(String name) {
326+
return null;
327327
}
328328

329329
default int defineMetric(MetricType metricType, String name) throws WasmException {

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public final class ProxyWasm implements Closeable {
3636
private ProxyMap httpCallResponseHeaders;
3737
private ProxyMap httpCallResponseTrailers;
3838
private byte[] httpCallResponseBody;
39-
private HashMap<String, ForeignFunction> foreignFunctions = new HashMap<>();
4039

4140
private ProxyWasm(Builder other) throws StartException {
4241
this.vmConfig = other.vmConfig;
@@ -53,7 +52,16 @@ private ProxyWasm(Builder other) throws StartException {
5352
this.abi.start();
5453
}
5554

56-
// start the vm with the vmHandler, it will receive stuff like log messages.
55+
if (other.start) {
56+
start();
57+
}
58+
}
59+
60+
public void start() throws StartException {
61+
if (pluginContext != null) {
62+
throw new IllegalStateException("already started");
63+
}
64+
5765
this.pluginContext = new PluginContext(this, pluginHandler);
5866
registerContext(pluginContext, 0);
5967
if (!this.abi.proxyOnVmStart(pluginContext.id(), vmConfig.length)) {
@@ -118,15 +126,6 @@ public ProxyMap getHttpCallResponseTrailers() {
118126
public byte[] getHttpCallResponseBody() {
119127
return httpCallResponseBody;
120128
}
121-
122-
@Override
123-
public byte[] callForeignFunction(String name, byte[] bytes) throws WasmException {
124-
ForeignFunction func = foreignFunctions.get(name);
125-
if (func == null) {
126-
throw new WasmException(WasmResult.NOT_FOUND);
127-
}
128-
return func.apply(bytes);
129-
}
130129
};
131130
}
132131

@@ -175,6 +174,9 @@ public void tick() {
175174

176175
@Override
177176
public void close() {
177+
if (this.pluginContext == null) {
178+
return;
179+
}
178180
this.pluginContext.close();
179181
if (wasi != null) {
180182
wasi.close();
@@ -214,10 +216,6 @@ public int contextId() {
214216
return pluginContext.id();
215217
}
216218

217-
public void registerForeignFunction(String name, ForeignFunction func) {
218-
foreignFunctions.put(name, func);
219-
}
220-
221219
ABI abi() {
222220
return abi;
223221
}
@@ -232,6 +230,7 @@ public static class Builder implements Cloneable {
232230
private Handler pluginHandler;
233231
private ImportMemory memory;
234232
private WasiOptions wasiOptions;
233+
private boolean start = true;
235234

236235
@Override
237236
@SuppressWarnings("NoClone")
@@ -247,6 +246,11 @@ public HostFunction[] toHostFunctions() {
247246
return ABI_ModuleFactory.toHostFunctions(abi);
248247
}
249248

249+
public Builder withStart(boolean start) {
250+
this.start = start;
251+
return this;
252+
}
253+
250254
public ProxyWasm.Builder withVmConfig(byte[] vmConfig) {
251255
this.vmConfig = vmConfig;
252256
return this;

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/ForeignCallOnTickTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testOnTick() throws StartException {
2626
try (var host = builder.build(module)) {
2727
assertEquals(tickMilliseconds, handler.getTickPeriodMilliseconds());
2828

29-
host.registerForeignFunction("compress", data -> data);
29+
handler.registerForeignFunction("compress", data -> data);
3030

3131
for (int i = 1; i <= 10; i++) {
3232
host.tick(); // call OnTick

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.roastedroot.proxywasm.Action;
88
import io.roastedroot.proxywasm.ArrayProxyMap;
99
import io.roastedroot.proxywasm.ChainedHandler;
10+
import io.roastedroot.proxywasm.ForeignFunction;
1011
import io.roastedroot.proxywasm.Handler;
1112
import io.roastedroot.proxywasm.Helpers;
1213
import io.roastedroot.proxywasm.LogLevel;
@@ -468,4 +469,15 @@ public WasmResult setProperty(List<String> path, byte[] value) {
468469
properties.put(path, value);
469470
return WasmResult.OK;
470471
}
472+
473+
private final HashMap<String, ForeignFunction> foreignFunctions = new HashMap<>();
474+
475+
@Override
476+
public ForeignFunction getForeignFunction(String name) {
477+
return foreignFunctions.get(name);
478+
}
479+
480+
public void registerForeignFunction(String name, ForeignFunction function) {
481+
foreignFunctions.put(name, function);
482+
}
471483
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.roastedroot.proxywasm.jaxrs;
2+
3+
import io.roastedroot.proxywasm.LogLevel;
4+
5+
public interface Logger {
6+
7+
void log(LogLevel level, String message);
8+
9+
LogLevel getLogLevel();
10+
}

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static io.roastedroot.proxywasm.WellKnownProperties.PLUGIN_VM_ID;
66

77
import io.roastedroot.proxywasm.ChainedHandler;
8+
import io.roastedroot.proxywasm.ForeignFunction;
89
import io.roastedroot.proxywasm.Handler;
910
import io.roastedroot.proxywasm.LogLevel;
1011
import io.roastedroot.proxywasm.MetricType;
@@ -75,20 +76,27 @@ public WasmResult setProperty(List<String> path, byte[] value) {
7576
// Logging
7677
// //////////////////////////////////////////////////////////////////////
7778

79+
public Logger logger;
80+
7881
static final boolean DEBUG = "true".equals(System.getenv("DEBUG"));
7982

8083
@Override
8184
public void log(LogLevel level, String message) throws WasmException {
82-
// TODO: improve
83-
if (DEBUG) {
84-
System.out.println(level + ": " + message);
85+
Logger l = logger;
86+
if (l == null) {
87+
super.log(level, message);
88+
return;
8589
}
90+
l.log(level, message);
8691
}
8792

8893
@Override
8994
public LogLevel getLogLevel() throws WasmException {
90-
// TODO: improve
91-
return super.getLogLevel();
95+
Logger l = logger;
96+
if (l == null) {
97+
return super.getLogLevel();
98+
}
99+
return l.getLogLevel();
92100
}
93101

94102
// //////////////////////////////////////////////////////////////////////
@@ -280,4 +288,14 @@ public WasmResult removeMetric(int metricId) {
280288
metricsByName.remove(metric.name);
281289
return WasmResult.OK;
282290
}
291+
292+
// //////////////////////////////////////////////////////////////////////
293+
// FFI
294+
// //////////////////////////////////////////////////////////////////////
295+
HashMap<String, ForeignFunction> foreignFunctions;
296+
297+
@Override
298+
public ForeignFunction getForeignFunction(String name) {
299+
return super.getForeignFunction(name);
300+
}
283301
}

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.dylibso.chicory.runtime.ImportMemory;
44
import com.dylibso.chicory.runtime.Instance;
55
import com.dylibso.chicory.wasm.WasmModule;
6+
import io.roastedroot.proxywasm.ForeignFunction;
67
import io.roastedroot.proxywasm.ProxyWasm;
78
import io.roastedroot.proxywasm.StartException;
9+
import java.util.HashMap;
10+
import java.util.Map;
811
import java.util.Objects;
912
import java.util.concurrent.locks.ReentrantLock;
1013

@@ -67,6 +70,16 @@ public WasmPlugin.Builder withName(String name) {
6770
return this;
6871
}
6972

73+
public Builder withForeignFunctions(Map<String, ForeignFunction> functions) {
74+
this.handler.foreignFunctions = new HashMap<>(functions);
75+
return this;
76+
}
77+
78+
public Builder withLogger(Logger logger) {
79+
this.handler.logger = logger;
80+
return this;
81+
}
82+
7083
public WasmPlugin.Builder withShared(boolean shared) {
7184
this.shared = shared;
7285
return this;

proxy-wasm-jaxrs/src/test/java/io/roastedroot/proxywasm/jaxrs/HttpHeadersNotSharedTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package io.roastedroot.proxywasm.jaxrs;
22

33
import static io.restassured.RestAssured.given;
4-
import static io.roastedroot.proxywasm.jaxrs.TestHelpers.EXAMPLES_DIR;
4+
import static io.roastedroot.proxywasm.jaxrs.TestHelpers.parseTestModule;
55

6-
import com.dylibso.chicory.wasm.Parser;
76
import io.quarkus.test.junit.QuarkusTest;
87
import io.roastedroot.proxywasm.StartException;
98
import jakarta.enterprise.inject.Produces;
10-
import java.nio.file.Path;
119
import org.junit.jupiter.api.Test;
1210

1311
@QuarkusTest
@@ -20,11 +18,7 @@ public WasmPluginFactory create() throws StartException {
2018
.withName("notSharedHttpHeaders")
2119
.withShared(false)
2220
.withPluginConfig("{\"header\": \"x-wasm-header\", \"value\": \"foo\"}")
23-
.build(
24-
Parser.parse(
25-
Path.of(
26-
EXAMPLES_DIR
27-
+ "/go-examples/http_headers/main.wasm")));
21+
.build(parseTestModule("/go-examples/http_headers/main.wasm"));
2822
}
2923

3024
@Test

0 commit comments

Comments
 (0)