diff --git a/src/main/java/org/extism/sdk/LibExtism.java b/src/main/java/org/extism/sdk/LibExtism.java index 78523f1..85dda02 100644 --- a/src/main/java/org/extism/sdk/LibExtism.java +++ b/src/main/java/org/extism/sdk/LibExtism.java @@ -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. @@ -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` diff --git a/src/main/java/org/extism/sdk/Plugin.java b/src/main/java/org/extism/sdk/Plugin.java index df61256..c646f02 100644 --- a/src/main/java/org/extism/sdk/Plugin.java +++ b/src/main/java/org/extism/sdk/Plugin.java @@ -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");