From 501e78c45c5b3b203d51ae8bb8ebf03509fb55e1 Mon Sep 17 00:00:00 2001 From: Caio Ramos Casimiro Date: Thu, 14 Nov 2024 16:37:48 +0000 Subject: [PATCH] Add support for foreign function callback. Signed-off-by: Caio Ramos Casimiro --- src/dispatcher.rs | 32 ++++++++++++++++++++++++++++++++ src/traits.rs | 7 +++++++ src/types.rs | 1 + 3 files changed, 40 insertions(+) diff --git a/src/dispatcher.rs b/src/dispatcher.rs index 671ef0f3..65afd362 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -427,6 +427,27 @@ impl Dispatcher { } } + fn on_foreign_function( + &self, + context_id: u32, + function_id: u32, + arugments_size: usize, + ) { + if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) { + self.active_id.set(context_id); + hostcalls::set_effective_context(context_id).unwrap(); + http_stream.on_foreign_function(function_id, arugments_size) + } else if let Some(stream) = self.streams.borrow_mut().get_mut(&context_id) { + self.active_id.set(context_id); + hostcalls::set_effective_context(context_id).unwrap(); + stream.on_foreign_function(function_id, arugments_size) + } else if let Some(root) = self.roots.borrow_mut().get_mut(&context_id) { + self.active_id.set(context_id); + hostcalls::set_effective_context(context_id).unwrap(); + root.on_foreign_function(function_id, arugments_size) + } + } + fn on_grpc_receive_initial_metadata(&self, token_id: u32, headers: u32) { let context_id = match self.grpc_streams.borrow_mut().get(&token_id) { Some(id) => *id, @@ -695,6 +716,17 @@ pub extern "C" fn proxy_on_http_call_response( }) } +#[no_mangle] +pub extern "C" fn proxy_on_foreign_function( + context_id: u32, + function_id: u32, + arguments_size: usize, +) { + DISPATCHER.with(|dispatcher| { + dispatcher.on_foreign_function(context_id, function_id, arguments_size) + }) +} + #[no_mangle] pub extern "C" fn proxy_on_grpc_receive_initial_metadata( _context_id: u32, diff --git a/src/traits.rs b/src/traits.rs index bd54bcbe..7c503114 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -78,6 +78,13 @@ pub trait Context { ) { } + fn on_foreign_function( + &mut self, + _function_id: u32, + _arguments_size: usize, + ) { + } + fn get_http_call_response_headers(&self) -> Vec<(String, String)> { hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap() } diff --git a/src/types.rs b/src/types.rs index 7407d3ca..07006f5b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -81,6 +81,7 @@ pub enum BufferType { GrpcReceiveBuffer = 5, VmConfiguration = 6, PluginConfiguration = 7, + ForeignFunctionArguments = 8, } #[repr(u32)]