From d620329ceaf3f91a9ffc68e2ec61757be0445306 Mon Sep 17 00:00:00 2001 From: Caio Ramos Casimiro Date: Thu, 14 Nov 2024 16:37:48 +0000 Subject: [PATCH 1/2] 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..30c87672 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -554,6 +554,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) + } + } } #[no_mangle] @@ -722,3 +743,14 @@ pub extern "C" fn proxy_on_grpc_receive_trailing_metadata( pub extern "C" fn proxy_on_grpc_close(_context_id: u32, token_id: u32, status_code: u32) { DISPATCHER.with(|dispatcher| dispatcher.on_grpc_close(token_id, status_code)) } + +#[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) + }) +} diff --git a/src/traits.rs b/src/traits.rs index bd54bcbe..79453405 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -197,6 +197,13 @@ pub trait Context { hostcalls::get_grpc_status().unwrap() } + fn on_foreign_function( + &mut self, + _function_id: u32, + _arguments_size: usize, + ) { + } + fn call_foreign_function( &self, function_name: &str, diff --git a/src/types.rs b/src/types.rs index 7407d3ca..4272ba90 100644 --- a/src/types.rs +++ b/src/types.rs @@ -81,6 +81,7 @@ pub enum BufferType { GrpcReceiveBuffer = 5, VmConfiguration = 6, PluginConfiguration = 7, + CallData = 8, } #[repr(u32)] From 78ba1db1f416d679d9944932303ba02debe5a07a Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 15 Jan 2025 08:28:29 -0500 Subject: [PATCH 2/2] review: fix format. Signed-off-by: Piotr Sikora --- src/dispatcher.rs | 12 +++--------- src/traits.rs | 7 +------ 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/dispatcher.rs b/src/dispatcher.rs index 30c87672..19742565 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -555,12 +555,7 @@ impl Dispatcher { } } - fn on_foreign_function( - &self, - context_id: u32, - function_id: u32, - arugments_size: usize, - ) { + 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(); @@ -750,7 +745,6 @@ pub extern "C" fn proxy_on_foreign_function( function_id: u32, arguments_size: usize, ) { - DISPATCHER.with(|dispatcher| { - dispatcher.on_foreign_function(context_id, function_id, arguments_size) - }) + DISPATCHER + .with(|dispatcher| dispatcher.on_foreign_function(context_id, function_id, arguments_size)) } diff --git a/src/traits.rs b/src/traits.rs index 79453405..15fe72df 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -197,12 +197,7 @@ pub trait Context { hostcalls::get_grpc_status().unwrap() } - fn on_foreign_function( - &mut self, - _function_id: u32, - _arguments_size: usize, - ) { - } + fn on_foreign_function(&mut self, _function_id: u32, _arguments_size: usize) {} fn call_foreign_function( &self,