diff --git a/java/src/main/java/org/extism/sdk/LibExtism.java b/java/src/main/java/org/extism/sdk/LibExtism.java index a4dff9b0..73306ed8 100644 --- a/java/src/main/java/org/extism/sdk/LibExtism.java +++ b/java/src/main/java/org/extism/sdk/LibExtism.java @@ -135,6 +135,8 @@ Pointer extism_function_new(String name, */ String extism_plugin_error(Pointer pluginPointer); + String extension_plugin_error(Pointer pluginPointer); + /** * * @param pluginPointer diff --git a/java/src/main/java/org/extism/sdk/Plugin.java b/java/src/main/java/org/extism/sdk/Plugin.java index 946bb131..1786b43c 100644 --- a/java/src/main/java/org/extism/sdk/Plugin.java +++ b/java/src/main/java/org/extism/sdk/Plugin.java @@ -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) { diff --git a/java/src/test/java/org/extism/sdk/PluginTests.java b/java/src/test/java/org/extism/sdk/PluginTests.java index da087340..fe5ba4a9 100644 --- a/java/src/test/java/org/extism/sdk/PluginTests.java +++ b/java/src/test/java/org/extism/sdk/PluginTests.java @@ -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"); }; @@ -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}, diff --git a/java/src/test/java/org/extism/sdk/TestWasmSources.java b/java/src/test/java/org/extism/sdk/TestWasmSources.java index 839bc337..b5f68f3f 100644 --- a/java/src/test/java/org/extism/sdk/TestWasmSources.java +++ b/java/src/test/java/org/extism/sdk/TestWasmSources.java @@ -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"); } }; diff --git a/runtime/extism.h b/runtime/extism.h index 9b594190..4d3439ff 100644 --- a/runtime/extism.h +++ b/runtime/extism.h @@ -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, diff --git a/runtime/src/current_plugin.rs b/runtime/src/current_plugin.rs index 84f772ed..10443635 100644 --- a/runtime/src/current_plugin.rs +++ b/runtime/src/current_plugin.rs @@ -19,6 +19,7 @@ pub struct CurrentPlugin { pub(crate) id: uuid::Uuid, pub(crate) memory_export: *mut PluginMemory, + pub(crate) extension_error: Option } unsafe impl Send for CurrentPlugin {} @@ -336,6 +337,7 @@ impl CurrentPlugin { available_pages, memory_limiter, id, + extension_error: None }) } diff --git a/runtime/src/extension/sdk.rs b/runtime/src/extension/sdk.rs index 1b28b2bd..b672859c 100644 --- a/runtime/src/extension/sdk.rs +++ b/runtime/src/extension/sdk.rs @@ -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, }; @@ -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]; @@ -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 @@ -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, store: &mut Store,