Skip to content

Commit

Permalink
Add support for foreign function callbacks. (#290)
Browse files Browse the repository at this point in the history
Signed-off-by: Caio Ramos Casimiro <[email protected]>
casimiro authored Jan 15, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8604c64 commit ae38c0f
Showing 3 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -554,6 +554,22 @@ 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 +738,13 @@ 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))
}
2 changes: 2 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -197,6 +197,8 @@ 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,
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@ pub enum BufferType {
GrpcReceiveBuffer = 5,
VmConfiguration = 6,
PluginConfiguration = 7,
CallData = 8,
}

#[repr(u32)]

0 comments on commit ae38c0f

Please sign in to comment.