Skip to content

Commit

Permalink
allow non extism plugin to catch and return error
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed May 24, 2024
1 parent 5add781 commit 0c32684
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 35 deletions.
2 changes: 2 additions & 0 deletions java/src/main/java/org/extism/sdk/LibExtism.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ Pointer extism_function_new(String name,
*/
String extism_plugin_error(Pointer pluginPointer);

String extension_plugin_error(Pointer pluginPointer);

/**
*
* @param pluginPointer
Expand Down
6 changes: 3 additions & 3 deletions java/src/main/java/org/extism/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public Results call(String functionName, Parameters params, int resultsLength) {
);

if (results == null && resultsLength > 0) {
// String error = error();
// throw new ExtismException(error);
return new Results(0);
String error = LibExtism.INSTANCE.extension_plugin_error(this.pluginPointer);
throw new ExtismException(error);
//return new Results(0);
}

if (results == null) {
Expand Down
50 changes: 27 additions & 23 deletions java/src/test/java/org/extism/sdk/PluginTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,13 @@ public void shouldHttpWasmWorking() {
String foo = "/host";
System.out.println("get_uri");
//
System.out.println("vLen " + foo.length());
var memory = plugin.customMemoryGet();
System.out.println(memory);
memory.write(params[0].v.i32, foo.getBytes(StandardCharsets.UTF_8), 0, foo.length());

returns[0].v.i32 = foo.length();

// this.values[length].t = 0;
// this.values[length].v.setType(java.lang.Integer.TYPE);
// this.values[length].v.i32 = value;

System.out.println("ending get_uri");
};

Expand Down Expand Up @@ -278,33 +276,39 @@ public void shouldHttpWasmWorking() {
//
// }, Optional.empty()
// ).withNamespace("http_handler"),
// new HostFunction<>(
// "get_uri",
// getUriParamaters,
// getUriReturns,
// getUriFunction, Optional.empty()
// ).withNamespace("http_handler"),
new HostFunction<>(
"get_uri",
getUriParamaters,
getUriReturns,
getUriFunction, Optional.empty()
).withNamespace("http_handler"),
new HostFunction<>(
"set_uri",
new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32, LibExtism.ExtismValType.I32},
new LibExtism.ExtismValType[]{},
(plugin, params, returns, data) -> {
System.out.println("set_uri coucou");
System.out.println("set_uri");
var memory = plugin.customMemoryGet();

var offset = params[0].v.i32;
var byteCount = params[1].v.i32;

System.out.println(Arrays.toString(memory.share(offset).getByteArray(0, byteCount)));
System.out.println("Ending set_uri");
}, Optional.empty()
).withNamespace("http_handler"),
// new HostFunction<>(
// "set_header_value",
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32, LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32},
// new LibExtism.ExtismValType[]{},
// (plugin, params, returns, data) -> System.out.println("set_header_value"), Optional.empty()
// ).withNamespace("http_handler"),
// new HostFunction<>(
// "write_body",
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32},
// new LibExtism.ExtismValType[]{},
// (plugin, params, returns, data) -> System.out.println("write_body"), Optional.empty()
// ).withNamespace("http_handler"),
new HostFunction<>(
"set_header_value",
new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32, LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32},
new LibExtism.ExtismValType[]{},
(plugin, params, returns, data) -> System.out.println("set_header_value"), Optional.empty()
).withNamespace("http_handler"),
new HostFunction<>(
"write_body",
new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32},
new LibExtism.ExtismValType[]{},
(plugin, params, returns, data) -> System.out.println("write_body"), Optional.empty()
).withNamespace("http_handler"),
// new HostFunction<>(
// "read_body",
// new LibExtism.ExtismValType[]{LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32,LibExtism.ExtismValType.I32},
Expand Down
2 changes: 1 addition & 1 deletion java/src/test/java/org/extism/sdk/TestWasmSources.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Path getWasmWaf() {
}

public Path getLogPath() {
return Paths.get(WASM_LOCATION, "httpwasm/test.wasm");
return Paths.get(WASM_LOCATION, "httpwasm/router.wasm");
}
};

Expand Down
2 changes: 2 additions & 0 deletions runtime/extism.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ uint8_t *linear_memory_get_from_plugin(ExtismPlugin *plugin,
const char *namespace_,
const char *name);

const char *extension_plugin_error(ExtismPlugin *plugin);

uintptr_t linear_memory_size(ExtismPlugin *instance_ptr, const char *namespace_, const char *name);

void linear_memory_reset_from_plugin(ExtismPlugin *instance_ptr,
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/current_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct CurrentPlugin {
pub(crate) id: uuid::Uuid,

pub(crate) memory_export: *mut PluginMemory,
pub(crate) extension_error: Option<Error>
}

unsafe impl Send for CurrentPlugin {}
Expand Down Expand Up @@ -336,6 +337,7 @@ impl CurrentPlugin {
available_pages,
memory_limiter,
id,
extension_error: None
})
}

Expand Down
40 changes: 32 additions & 8 deletions runtime/src/extension/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::os::raw::c_char;

use crate::{
function,
extension::*,
function,
sdk::{ExtismFunction, ExtismVal, Size},
CurrentPlugin, Internal, Plugin, EXTISM_ENV_MODULE,
};
Expand All @@ -28,9 +28,10 @@ unsafe fn extension_plugin_call_native(
let name = std::ffi::CStr::from_ptr(func_name);
let name = match name.to_str() {
Ok(name) => name,
Err(e) =>
panic!("{}", e)
// return plugin.return_error(&mut acquired_lock, e, None),
Err(e) => {
plugin.current_plugin_mut().extension_error = Some(Error::new(e));
return None;
}
};

let mut results = vec![wasmtime::Val::null(); 0];
Expand All @@ -45,11 +46,12 @@ unsafe fn extension_plugin_call_native(
);

return match res {
Err((e, _rc)) =>
panic!("{}", e),
//plugin.return_error(&mut acquired_lock, e, None),
Err((e, _rc)) => {
plugin.current_plugin_mut().extension_error = Some(e);
None
}
Ok(_x) => Some(results),
}
};
}

None
Expand Down Expand Up @@ -238,6 +240,28 @@ pub unsafe extern "C" fn linear_memory_get_from_plugin(
internal_linear_memory_get(linker, store, namespace, name)
}

#[no_mangle]
pub unsafe extern "C" fn extension_plugin_error(plugin: *mut Plugin) -> *const c_char {
if plugin.is_null() {
return std::ptr::null();
}
let plugin = &mut *plugin;
let _lock = plugin.instance.clone();
let _lock = _lock.lock().unwrap();

// panic!("{:?}", plugin.current_plugin_mut().extension_error);
let err = plugin
.current_plugin_mut()
.extension_error
.as_mut()
.unwrap();
let backtrace_string = format!("{:?}", err);
let c_string = std::ffi::CString::new(backtrace_string).unwrap();
c_string.into_raw()
// .backtrace()
// .as_ptr() as *const _
}

unsafe fn internal_linear_memory_get(
linker: &mut Linker<CurrentPlugin>,
store: &mut Store<CurrentPlugin>,
Expand Down

0 comments on commit 0c32684

Please sign in to comment.