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

Commit5b7d095

Browse files
author
zach
authored
fix: add calls toextism_function_free to cleanup HostFunctions (#14)
It looks like there isn't a good way to define a destructor in Java, sothis PR adds a `HostFunction.free` function that is automatically calledwhen a Plugin is freed, if a `HostFunction` is never registered with aplugin then it should be called manually.Fixes#13
1 parent1e1fd2c commit5b7d095

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class HostFunction<T extends HostUserData> {
1010

1111
privatefinalLibExtism.InternalExtismFunctioncallback;
1212

13+
privatebooleanfreed;
14+
1315
publicfinalPointerpointer;
1416

1517
publicfinalStringname;
@@ -21,7 +23,7 @@ public class HostFunction<T extends HostUserData> {
2123
publicfinalOptional<T>userData;
2224

2325
publicHostFunction(Stringname,LibExtism.ExtismValType[]params,LibExtism.ExtismValType[]returns,ExtismFunctionf,Optional<T>userData) {
24-
26+
this.freed =false;
2527
this.name =name;
2628
this.params =params;
2729
this.returns =returns;
@@ -85,8 +87,15 @@ public void setNamespace(String name) {
8587
}
8688
}
8789

88-
HostFunctionwithNamespace(Stringname) {
90+
publicHostFunctionwithNamespace(Stringname) {
8991
this.setNamespace(name);
9092
returnthis;
9193
}
94+
95+
publicvoidfree() {
96+
if (!this.freed){
97+
LibExtism.INSTANCE.extism_function_free(this.pointer);
98+
this.freed =true;
99+
}
100+
}
92101
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Pointer extism_function_new(String name,
6262
PointeruserData,
6363
PointerfreeUserData);
6464

65+
voidextism_function_free(Pointerfunction);
66+
6567
/**
6668
* Get the length of an allocated block
6769
* NOTE: this should only be called from host functions.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Plugin implements AutoCloseable {
1717
*/
1818
privatefinalPointerpluginPointer;
1919

20+
privatefinalHostFunction[]functions;
21+
2022
/**
2123
* @param manifestBytes The manifest for the plugin
2224
* @param functions The Host functions for th eplugin
@@ -40,13 +42,18 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
4042
withWASI,
4143
errormsg);
4244
if (p ==null) {
45+
if (functions !=null)
46+
for (inti =0;i <functions.length;i++) {
47+
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
48+
}
4349
interrlen =LibExtism.INSTANCE.strlen(errormsg[0]);
4450
byte[]msg =newbyte[errlen];
4551
errormsg[0].read(0,msg,0,errlen);
4652
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
4753
thrownewExtismException(newString(msg));
4854
}
4955

56+
this.functions =functions;
5057
this.pluginPointer =p;
5158
}
5259

@@ -114,6 +121,11 @@ protected String error() {
114121
* Frees a plugin from memory
115122
*/
116123
publicvoidfree() {
124+
if (this.functions !=null){
125+
for (inti =0;i <this.functions.length;i++) {
126+
this.functions[i].free();
127+
}
128+
}
117129
LibExtism.INSTANCE.extism_plugin_free(this.pluginPointer);
118130
}
119131

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp