diff --git a/java/src/main/java/org/extism/sdk/Context.java b/java/src/main/java/org/extism/sdk/Context.java index ae26f8583..56da6f192 100644 --- a/java/src/main/java/org/extism/sdk/Context.java +++ b/java/src/main/java/org/extism/sdk/Context.java @@ -12,38 +12,59 @@ public class Context { // A pointer to the ExtismContext struct private Pointer contextPointer; - // Create a new context + /** + * Create a new Context. A Context is used to manage Plugins + * and make sure they are cleaned up when you are done with them. + */ public Context() { this.contextPointer = LibExtism.INSTANCE.extism_context_new(); } - public Pointer getPointer() { - return this.contextPointer; + /** + * Create a new plugin managed by this context. + * + * @param manifest The manifest for the plugin + * @param withWASI Set to true to enable WASI + * @return + */ + public Plugin newPlugin(Manifest manifest, boolean withWASI) { + return new Plugin(this, manifest, withWASI); } - // Free a context + /** + * Frees the context *and* frees all its Plugins. Use {@link #reset()}, if you just want to + * free the plugins but keep the context. You should ensure this is called when you are done + * with the context. + */ public void free() { LibExtism.INSTANCE.extism_context_free(this.contextPointer); } - // Create a new plugin - public Plugin newPlugin(Manifest manifest, boolean withWASI) { - return new Plugin(this, manifest, withWASI); - } - - // Remove all plugins from the registry + /** + * Resets the context by freeing all its Plugins. Unlike {@link #free()}, it does not + * free the context itself. + */ public void reset() { LibExtism.INSTANCE.extism_context_reset(this.contextPointer); } + /** + * Get the version string of the linked Extism Runtime. + * + * @return + */ + public String getVersion() { + return LibExtism.INSTANCE.extism_version(); + } + // Get the error associated with a context, if plugin is null then the context // error will be returned - public String error(Plugin plugin) { + protected String error(Plugin plugin) { return LibExtism.INSTANCE.extism_error(this.contextPointer, plugin == null ? -1 : plugin.getIndex()); } - // Get the Extism version string - public String version() { - return LibExtism.INSTANCE.extism_version(); + protected Pointer getPointer() { + return this.contextPointer; } + } diff --git a/java/src/main/java/org/extism/sdk/Plugin.java b/java/src/main/java/org/extism/sdk/Plugin.java index dea694f94..af2d538b9 100644 --- a/java/src/main/java/org/extism/sdk/Plugin.java +++ b/java/src/main/java/org/extism/sdk/Plugin.java @@ -7,15 +7,21 @@ // Represents a plugin public class Plugin { - // The ExtismContext that the plugin belongs to private Context context; // The index of the plugin private int index; - // Create a new plugin - public Plugin(Context context, Manifest manifest, boolean withWASI) { + /** + * Constructor for a Plugin. Only expose internally. Plugins should be created and + * managed from {@link org.extism.sdk.Context}. + * + * @param context The context to manage the plugin + * @param manifest The manifest for the plugin + * @param withWASI Set to true to enable WASI + */ + protected Plugin(Context context, Manifest manifest, boolean withWASI) { byte[] manifestJson = manifest.toJson().getBytes(); this.context = context; IntByReference pluginIndex = new IntByReference(); @@ -23,11 +29,21 @@ public Plugin(Context context, Manifest manifest, boolean withWASI) { this.index = pluginIndex.getValue(); } - public int getIndex() { + /** + * Getter for the internal index pointer to this plugin + * @return + */ + protected int getIndex() { return this.index; } - // Call a plugin + /** + * Invoke a function with the given name and input + * + * @param function_name The name of the exported function to invoke + * @param inputData The raw bytes representing any input data + * @return A byte array representing the raw output data + */ public byte[] call(String function_name, byte[] inputData) { int _result = LibExtism.INSTANCE.extism_plugin_call(this.context.getPointer(), this.index, function_name, inputData, inputData.length); int length = LibExtism.INSTANCE.extism_plugin_output_length(this.context.getPointer(), this.index); @@ -35,18 +51,33 @@ public byte[] call(String function_name, byte[] inputData) { return output.getByteArray(0, length); } - // Update a plugin, keeping the existing ID - public boolean update(byte[] wasm, boolean withWASI) { - return LibExtism.INSTANCE.extism_plugin_update(this.context.getPointer(), this.index, wasm, wasm.length, + /** + * Update the plugin code given manifest changes + * + * @param manifest The manifest for the plugin + * @param withWASI Set to true to enable WASI + * @return Returns true if update was successful + */ + public boolean update(Manifest manifest, boolean withWASI) { + byte[] manifestJson = manifest.toJson().getBytes(); + return LibExtism.INSTANCE.extism_plugin_update(this.context.getPointer(), this.index, manifestJson, manifestJson.length, withWASI); } - // Remove a plugin from the registry and free associated memory + /** + * Frees a plugin from memory. Plugins will be automatically cleaned up + * if you free their parent Context using {@link org.extism.sdk.Context#free() free()} or {@link org.extism.sdk.Context#reset() reset()} + */ public void free() { LibExtism.INSTANCE.extism_plugin_free(this.context.getPointer(), this.index); } - // Update plugin config values, this will merge with the existing values + /** + * Update the config for the plugin + * + * @param json + * @return + */ public boolean config(byte[] json) { return LibExtism.INSTANCE.extism_plugin_config(this.context.getPointer(), this.index, json, json.length); }