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
3 changes: 3 additions & 0 deletions src/main/java/org/extism/sdk/LibExtism.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Pointer extism_function_new(String name,

void extism_function_free(Pointer function);


/**
* Get the length of an allocated block
* NOTE: this should only be called from host functions.
Expand Down Expand Up @@ -119,6 +120,8 @@ Pointer extism_function_new(String name,
* @return pointer to the plugin, or null in case of error
*/
Pointer extism_plugin_new(byte[] wasm, long wasmSize, Pointer[] functions, int nFunctions, boolean withWASI, Pointer[] errmsg);
Pointer extism_plugin_new_with_fuel_limit(byte[] wasm, long wasmSize, Pointer[] functions, int nFunctions, boolean withWASI, long fuelLimit, Pointer[] errmsg);


/**
* Free error message from `extism_plugin_new`
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,48 @@ public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions)
this.pluginPointer = p;
}


public Plugin(byte[] manifestBytes, boolean withWASI, HostFunction[] functions, long fuelLimit) {

Objects.requireNonNull(manifestBytes, "manifestBytes");

Pointer[] ptrArr = new Pointer[functions == null ? 0 : functions.length];

if (functions != null)
for (int i = 0; i < functions.length; i++) {
ptrArr[i] = functions[i].pointer;
}

Pointer[] errormsg = new Pointer[1];
Pointer p = LibExtism.INSTANCE.extism_plugin_new_with_fuel_limit(manifestBytes, manifestBytes.length,
ptrArr,
functions == null ? 0 : functions.length,
withWASI,
fuelLimit,
errormsg);
if (p == null) {
if (functions != null) {
for (int i = 0; i < functions.length; i++) {
LibExtism.INSTANCE.extism_function_free(functions[i].pointer);
}
}
String msg = errormsg[0].getString(0);
LibExtism.INSTANCE.extism_plugin_new_error_free(errormsg[0]);
throw new ExtismException(msg);
}

this.functions = functions;
this.pluginPointer = p;
}

public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions) {
this(serialize(manifest), withWASI, functions);
}


public Plugin(Manifest manifest, boolean withWASI, HostFunction[] functions, long fuelLimit) {
this(serialize(manifest), withWASI, functions, fuelLimit);
}

private static byte[] serialize(Manifest manifest) {
Objects.requireNonNull(manifest, "manifest");
Expand Down