Skip to content

Commit

Permalink
Some more javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelx committed Dec 5, 2022
1 parent fb3cd5c commit 58e5a1b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
49 changes: 35 additions & 14 deletions java/src/main/java/org/extism/sdk/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
51 changes: 41 additions & 10 deletions java/src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,77 @@

// 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();
LibExtism.INSTANCE.extism_plugin_new(context.getPointer(), manifestJson, manifestJson.length, withWASI, pluginIndex);
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);
Pointer output = LibExtism.INSTANCE.extism_plugin_output_data(this.context.getPointer(), this.index);
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);
}
Expand Down

0 comments on commit 58e5a1b

Please sign in to comment.