Skip to content

Commit e56f649

Browse files
authored
Add support for calling foreign functions. (#143)
Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 8a13397 commit e56f649

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/hostcalls.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,49 @@ pub fn set_effective_context(context_id: u32) -> Result<(), Status> {
990990
}
991991
}
992992

993+
extern "C" {
994+
fn proxy_call_foreign_function(
995+
function_name_data: *const u8,
996+
function_name_size: usize,
997+
arguments_data: *const u8,
998+
arguments_size: usize,
999+
results_data: *mut *mut u8,
1000+
results_size: *mut usize,
1001+
) -> Status;
1002+
}
1003+
1004+
pub fn call_foreign_function(
1005+
function_name: &str,
1006+
arguments: Option<&[u8]>,
1007+
) -> Result<Option<Bytes>, Status> {
1008+
let mut return_data: *mut u8 = null_mut();
1009+
let mut return_size: usize = 0;
1010+
unsafe {
1011+
match proxy_call_foreign_function(
1012+
function_name.as_ptr(),
1013+
function_name.len(),
1014+
arguments.map_or(null(), |arguments| arguments.as_ptr()),
1015+
arguments.map_or(0, |arguments| arguments.len()),
1016+
&mut return_data,
1017+
&mut return_size,
1018+
) {
1019+
Status::Ok => {
1020+
if !return_data.is_null() {
1021+
Ok(Some(Vec::from_raw_parts(
1022+
return_data,
1023+
return_size,
1024+
return_size,
1025+
)))
1026+
} else {
1027+
Ok(None)
1028+
}
1029+
}
1030+
Status::NotFound => Err(Status::NotFound),
1031+
status => panic!("unexpected status: {}", status as u32),
1032+
}
1033+
}
1034+
}
1035+
9931036
extern "C" {
9941037
fn proxy_done() -> Status;
9951038
}

src/traits.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ pub trait Context {
197197
hostcalls::get_grpc_status().unwrap()
198198
}
199199

200+
fn call_foreign_function(
201+
&self,
202+
function_name: &str,
203+
arguments: Option<&[u8]>,
204+
) -> Result<Option<Bytes>, Status> {
205+
hostcalls::call_foreign_function(function_name, arguments)
206+
}
207+
200208
fn on_done(&mut self) -> bool {
201209
true
202210
}

0 commit comments

Comments
 (0)