Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89a286a

Browse files
authored
JNA callback should check inputs and outputs for null. (#30)
This addresses#27 once wemergeextism/extism#760 and release a newversion of libextism.This PR simply check the parameters for null and the counts to be valid,in preparation forextism/extism#760 whichotherwise would cause a NullPointerException when outputs and inputs areempty.Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
1 parent585fa0f commit89a286a

File tree

3 files changed

+119
-75
lines changed

3 files changed

+119
-75
lines changed

‎src/main/java/org/extism/sdk/HostFunction.java‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,12 @@ public class HostFunction<T extends HostUserData> {
2020

2121
publicfinalLibExtism.ExtismValType[]returns;
2222

23-
publicfinalOptional<T>userData;
24-
2523
publicHostFunction(Stringname,LibExtism.ExtismValType[]params,LibExtism.ExtismValType[]returns,ExtismFunctionf,Optional<T>userData) {
2624
this.freed =false;
2725
this.name =name;
2826
this.params =params;
2927
this.returns =returns;
30-
this.userData =userData;
31-
this.callback = (PointercurrentPlugin,
32-
LibExtism.ExtismValinputs,
33-
intnInputs,
34-
LibExtism.ExtismValouts,
35-
intnOutputs,
36-
Pointerdata) -> {
37-
38-
LibExtism.ExtismVal[]outputs = (LibExtism.ExtismVal[])outs.toArray(nOutputs);
39-
40-
f.invoke(
41-
newExtismCurrentPlugin(currentPlugin),
42-
(LibExtism.ExtismVal[])inputs.toArray(nInputs),
43-
outputs,
44-
userData
45-
);
46-
47-
for (LibExtism.ExtismValoutput :outputs) {
48-
convertOutput(output,output);
49-
}
50-
};
28+
this.callback =newCallback(f,userData);
5129

5230
this.pointer =LibExtism.INSTANCE.extism_function_new(
5331
this.name,
@@ -61,7 +39,7 @@ public HostFunction(String name, LibExtism.ExtismValType[] params, LibExtism.Ext
6139
);
6240
}
6341

64-
voidconvertOutput(LibExtism.ExtismValoriginal,LibExtism.ExtismValfromHostFunction) {
42+
staticvoidconvertOutput(LibExtism.ExtismValoriginal,LibExtism.ExtismValfromHostFunction) {
6543
if (fromHostFunction.t !=original.t)
6644
thrownewExtismException(String.format("Output type mismatch, got %d but expected %d",fromHostFunction.t,original.t));
6745

@@ -103,4 +81,45 @@ public void free() {
10381
this.freed =true;
10482
}
10583
}
84+
85+
staticclassCallback<T>implementsLibExtism.InternalExtismFunction {
86+
privatefinalExtismFunctionf;
87+
privatefinalOptional<T>userData;
88+
89+
publicCallback(ExtismFunctionf,Optional<T>userData) {
90+
this.f =f;
91+
this.userData =userData;
92+
}
93+
94+
@Override
95+
publicvoidinvoke(PointercurrentPlugin,LibExtism.ExtismValins,intnInputs,LibExtism.ExtismValouts,intnOutputs,Pointerdata) {
96+
97+
LibExtism.ExtismVal[]inputs;
98+
LibExtism.ExtismVal[]outputs;
99+
100+
if (outs ==null) {
101+
if (nOutputs >0) {
102+
thrownewExtismException("Output array is null but nOutputs is greater than 0");
103+
}
104+
outputs =newLibExtism.ExtismVal[0];
105+
}else {
106+
outputs = (LibExtism.ExtismVal[])outs.toArray(nOutputs);
107+
}
108+
109+
if (ins ==null) {
110+
if (nInputs >0) {
111+
thrownewExtismException("Input array is null but nInputs is greater than 0");
112+
}
113+
inputs =newLibExtism.ExtismVal[0];
114+
}else {
115+
inputs = (LibExtism.ExtismVal[])ins.toArray(nInputs);
116+
}
117+
118+
f.invoke(newExtismCurrentPlugin(currentPlugin),inputs,outputs,userData);
119+
120+
for (LibExtism.ExtismValoutput :outputs) {
121+
convertOutput(output,output);
122+
}
123+
}
124+
}
106125
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packageorg.extism.sdk;
2+
3+
importcom.sun.jna.Pointer;
4+
importorg.junit.jupiter.api.Test;
5+
6+
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
7+
8+
publicclassHostFunctionTests {
9+
@Test
10+
publicvoidcallbackShouldAcceptNullParameters() {
11+
varcallback =newHostFunction.Callback<>(
12+
(plugin,params,returns,userData) -> {/* NOOP */},null);
13+
callback.invoke(Pointer.NULL,null,0,null,0,Pointer.NULL);
14+
}
15+
16+
@Test
17+
publicvoidcallbackShouldThrowOnNullParametersAndNonzeroCounts() {
18+
varcallback =newHostFunction.Callback<>(
19+
(plugin,params,returns,userData) -> {/* NOOP */},null);
20+
assertThrows(ExtismException.class, () ->
21+
callback.invoke(Pointer.NULL,null,1,null,0,Pointer.NULL));
22+
assertThrows(ExtismException.class, () ->
23+
callback.invoke(Pointer.NULL,null,0,null,1,Pointer.NULL));
24+
}
25+
}

‎src/test/java/org/extism/sdk/PluginTests.java‎

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,57 +53,57 @@ public void shouldInvokeFunctionFromUrlWasmSource() {
5353
assertThat(output).isEqualTo("{\"count\":4,\"total\":4,\"vowels\":\"aeiouyAEIOUY\"}");
5454
}
5555

56-
// @Test
57-
// public void shouldInvokeFunctionFromUrlWasmSourceHostFuncs() {
58-
// var url = "https://github.com/extism/plugins/releases/latest/download/count_vowels_kvstore.wasm";
59-
// var manifest = new Manifest(List.of(UrlWasmSource.fromUrl(url)));
60-
//
61-
// // Our application KV store
62-
// // Pretend this is redis or a database :)
63-
// var kvStore = new HashMap<String, byte[]>();
64-
//
65-
// ExtismFunction kvWrite = (plugin, params, returns, data) -> {
66-
// System.out.println("Hello from Java Host Function!");
67-
// var key = plugin.inputString(params[0]);
68-
// var value = plugin.inputBytes(params[1]);
69-
// System.out.println("Writing to key " + key);
70-
// kvStore.put(key, value);
71-
// };
72-
//
73-
// ExtismFunction kvRead = (plugin, params, returns, data) -> {
74-
// System.out.println("Hello from Java Host Function!");
75-
// var key = plugin.inputString(params[0]);
76-
// System.out.println("Reading from key " + key);
77-
// var value = kvStore.get(key);
78-
// if (value == null) {
79-
// // default to zeroed bytes
80-
// var zero = new byte[]{0,0,0,0};
81-
// plugin.returnBytes(returns[0], zero);
82-
// } else {
83-
// plugin.returnBytes(returns[0], value);
84-
// }
85-
// };
86-
//
87-
// HostFunction kvWriteHostFn = new HostFunction<>(
88-
// "kv_write",
89-
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I64, LibExtism.ExtismValType.I64},
90-
// new LibExtism.ExtismValType[0],
91-
// kvWrite,
92-
// Optional.empty()
93-
// );
94-
//
95-
// HostFunction kvReadHostFn = new HostFunction<>(
96-
// "kv_read",
97-
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I64},
98-
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I64},
99-
// kvRead,
100-
// Optional.empty()
101-
// );
102-
//
103-
// HostFunction[] functions = {kvWriteHostFn, kvReadHostFn};
104-
// var plugin = new Plugin(manifest, false, functions);
105-
// var output = plugin.call("count_vowels", "Hello, World!");
106-
// }
56+
@Test
57+
publicvoidshouldInvokeFunctionFromUrlWasmSourceHostFuncs() {
58+
varurl ="https://github.com/extism/plugins/releases/latest/download/count_vowels_kvstore.wasm";
59+
varmanifest =newManifest(List.of(UrlWasmSource.fromUrl(url)));
60+
61+
// Our application KV store
62+
// Pretend this is redis or a database :)
63+
varkvStore =newHashMap<String,byte[]>();
64+
65+
ExtismFunctionkvWrite = (plugin,params,returns,data) -> {
66+
System.out.println("Hello from Java Host Function!");
67+
varkey =plugin.inputString(params[0]);
68+
varvalue =plugin.inputBytes(params[1]);
69+
System.out.println("Writing to key " +key);
70+
kvStore.put(key,value);
71+
};
72+
73+
ExtismFunctionkvRead = (plugin,params,returns,data) -> {
74+
System.out.println("Hello from Java Host Function!");
75+
varkey =plugin.inputString(params[0]);
76+
System.out.println("Reading from key " +key);
77+
varvalue =kvStore.get(key);
78+
if (value ==null) {
79+
// default to zeroed bytes
80+
varzero =newbyte[]{0,0,0,0};
81+
plugin.returnBytes(returns[0],zero);
82+
}else {
83+
plugin.returnBytes(returns[0],value);
84+
}
85+
};
86+
87+
HostFunctionkvWriteHostFn =newHostFunction<>(
88+
"kv_write",
89+
newLibExtism.ExtismValType[]{LibExtism.ExtismValType.I64,LibExtism.ExtismValType.I64},
90+
newLibExtism.ExtismValType[0],
91+
kvWrite,
92+
Optional.empty()
93+
);
94+
95+
HostFunctionkvReadHostFn =newHostFunction<>(
96+
"kv_read",
97+
newLibExtism.ExtismValType[]{LibExtism.ExtismValType.I64},
98+
newLibExtism.ExtismValType[]{LibExtism.ExtismValType.I64},
99+
kvRead,
100+
Optional.empty()
101+
);
102+
103+
HostFunction[]functions = {kvWriteHostFn,kvReadHostFn};
104+
varplugin =newPlugin(manifest,false,functions);
105+
varoutput =plugin.call("count_vowels","Hello, World!");
106+
}
107107

108108
@Test
109109
publicvoidshouldInvokeFunctionFromByteArrayWasmSource() {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp