Skip to content

Commit

Permalink
feat: add return value for webf function
Browse files Browse the repository at this point in the history
  • Loading branch information
LeuisKen committed Dec 14, 2024
1 parent fa63644 commit 272526a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 30 deletions.
16 changes: 11 additions & 5 deletions bridge/core/frame/module_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ NativeValue* handleInvokeModuleTransientCallback(void* ptr,
context->HandleException(&result);
}
NativeValue native_result = result.ToNative(ctx, exception_state);
return_value = static_cast<NativeValue*>(malloc(sizeof(NativeValue)));
return_value = static_cast<NativeValue*>(dart_malloc(sizeof(NativeValue)));
memcpy(return_value, &native_result, sizeof(NativeValue));
} else {
ScriptValue arguments[] = {ScriptValue::Empty(ctx), ScriptValue(ctx, *extra_data)};
Expand All @@ -59,7 +59,7 @@ NativeValue* handleInvokeModuleTransientCallback(void* ptr,
context->HandleException(&result);
}
NativeValue native_result = result.ToNative(ctx, exception_state);
return_value = static_cast<NativeValue*>(malloc(sizeof(NativeValue)));
return_value = static_cast<NativeValue*>(dart_malloc(sizeof(NativeValue)));
memcpy(return_value, &native_result, sizeof(NativeValue));
}

Expand All @@ -75,18 +75,24 @@ NativeValue* handleInvokeModuleTransientCallback(void* ptr,
} else if (auto* callback = DynamicTo<WebFNativeFunction>(callback_value.get())) {
context->dartIsolateContext()->profiler()->StartTrackAsyncEvaluation();
context->dartIsolateContext()->profiler()->StartTrackSteps("handleInvokeModuleTransientCallback");

NativeValue* return_value = nullptr;
if (errmsg != nullptr) {
NativeValue error_object = Native_NewCString(errmsg);
callback->Invoke(context, 1, &error_object);
NativeValue native_result = callback->Invoke(context, 1, &error_object);
return_value = static_cast<NativeValue*>(dart_malloc(sizeof(NativeValue)));
memcpy(return_value, &native_result, sizeof(NativeValue));
} else {
auto params = new NativeValue[2];
params[0] = Native_NewNull();
params[1] = *extra_data;
callback->Invoke(context, 2, params);
NativeValue native_result = callback->Invoke(context, 2, params);
return_value = static_cast<NativeValue*>(dart_malloc(sizeof(NativeValue)));
memcpy(return_value, &native_result, sizeof(NativeValue));
}
context->dartIsolateContext()->profiler()->FinishTrackSteps();
context->dartIsolateContext()->profiler()->FinishTrackAsyncEvaluation();
return nullptr;
return return_value;
}
}

Expand Down
6 changes: 3 additions & 3 deletions bridge/core/native/native_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace webf {
class SharedExceptionState;
typedef struct WebFNativeFunctionContext WebFNativeFunctionContext;

using WebFNativeFunctionCallback = void (*)(WebFNativeFunctionContext* callback_context,
using WebFNativeFunctionCallback = NativeValue (*)(WebFNativeFunctionContext* callback_context,
int32_t argc,
NativeValue* argv,
SharedExceptionState* shared_exception_state);
Expand Down Expand Up @@ -44,8 +44,8 @@ class WebFNativeFunction : public Function {

bool IsWebFNativeFunction() const override { return true; }

void Invoke(ExecutingContext* context, int32_t argc, NativeValue* argv) {
callback_context_->callback(callback_context_, argc, argv, shared_exception_state_);
NativeValue Invoke(ExecutingContext* context, int32_t argc, NativeValue* argv) {
return callback_context_->callback(callback_context_, argc, argv, shared_exception_state_);
}

private:
Expand Down
6 changes: 4 additions & 2 deletions bridge/rusty_webf_sys/src/executing_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ impl ExecutingContext {
let general_callback: WebFNativeFunction = Box::new(move |argc, argv| {
if argc != 0 {
println!("Invalid argument count for timeout callback");
return;
return NativeValue::new_null();
}
callback();
NativeValue::new_null()
});

let callback_data = Box::new(WebFNativeFunctionContextData {
Expand Down Expand Up @@ -214,9 +215,10 @@ impl ExecutingContext {
let general_callback: WebFNativeFunction = Box::new(move |argc, argv| {
if argc != 0 {
println!("Invalid argument count for interval callback");
return;
return NativeValue::new_null();
}
callback();
NativeValue::new_null()
});

let callback_data = Box::new(WebFNativeFunctionContextData {
Expand Down
24 changes: 13 additions & 11 deletions bridge/rusty_webf_sys/src/frame/async_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ impl AsyncStorage {
let key_string = NativeValue::new_string(key);
let general_callback: WebFNativeFunction = Box::new(move |argc, argv| {
if argc == 1 {
let error_string = unsafe { *argv };
let error_string = unsafe { (*argv).clone() };
let error_string = error_string.to_string();
callback(Err(error_string));
return;
return NativeValue::new_null();
}
if argc == 2 {
let item_string = unsafe { *argv.wrapping_add(1) };
let item_string = unsafe { (*argv.wrapping_add(1)).clone() };
if item_string.is_null() {
callback(Ok(None));
return;
return NativeValue::new_null();
}
let item_string = item_string.to_string();
callback(Ok(Some(item_string)));
return;
return NativeValue::new_null();
}
println!("Invalid argument count for timeout callback");
println!("Invalid argument count for async storage callback");
NativeValue::new_null()
});
self.context().webf_invoke_module_with_params_and_callback("AsyncStorage", "getItem", &key_string, general_callback, exception_state).unwrap();
}
Expand All @@ -54,18 +55,19 @@ impl AsyncStorage {
let params = NativeValue::new_list(params_vec);
let general_callback: WebFNativeFunction = Box::new(move |argc, argv| {
if argc == 1 {
let error_string = unsafe { *argv };
let error_string = unsafe { (*argv).clone() };
let error_string = error_string.to_string();
callback(Err(error_string));
return;
return NativeValue::new_null();
}
if argc == 2 {
let result = unsafe { *argv.wrapping_add(1) };
let result = unsafe { (*argv.wrapping_add(1)).clone() };
let result = result.to_string();
callback(Ok(Some(result)));
return;
return NativeValue::new_null();
}
println!("Invalid argument count for timeout callback");
println!("Invalid argument count for async storage callback");
NativeValue::new_null()
});
self.context().webf_invoke_module_with_params_and_callback("AsyncStorage", "setItem", &params, general_callback, exception_state).unwrap();
}
Expand Down
26 changes: 23 additions & 3 deletions bridge/rusty_webf_sys/src/native_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ pub enum NativeTag {
}

#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Copy, Clone)]
pub union ValueField {
pub int64: i64,
pub float64: f64,
pub ptr: *mut c_void,
}

#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct NativeValue {
pub u: ValueField,
pub uint32: u32,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl NativeValue {

for (i, val) in values.iter().enumerate() {
unsafe {
array_ptr.add(i).write(*val);
array_ptr.add(i).write(val.clone());
}
}

Expand All @@ -203,3 +203,23 @@ impl NativeValue {
values
}
}

impl Drop for NativeValue {
fn drop(&mut self) {
if self.tag == NativeTag::TagString as i32 {
println!("Drop NativeValue string: {}", self.to_string());
} else if self.tag == NativeTag::TagList as i32 {
println!("Drop NativeValue list");
let ptr = unsafe {
self.u.ptr as *mut NativeValue
};
for i in 0..self.uint32 {
let offset = i.try_into().unwrap();
let val = unsafe { ptr.add(offset).read() };
drop(val);
}
} else {
println!("Drop NativeValue: {:?}", self.tag);
}
}
}
10 changes: 4 additions & 6 deletions bridge/rusty_webf_sys/src/webf_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::ffi::*;
use crate::*;

pub type WebFNativeFunction = Box<dyn Fn(c_int, *const NativeValue)>;
pub type WebFNativeFunction = Box<dyn Fn(c_int, *const NativeValue) -> NativeValue>;

pub struct WebFNativeFunctionContextData {
pub func: WebFNativeFunction,
Expand All @@ -21,7 +21,7 @@ pub struct WebFNativeFunctionContext {
pub callback: extern "C" fn(callback_context: *const OpaquePtr,
argc: c_int,
argv: *const NativeValue,
exception_state: *const OpaquePtr) -> *const c_void,
exception_state: *const OpaquePtr) -> NativeValue,
pub free_ptr: extern "C" fn(callback_context: *const OpaquePtr) -> *const c_void,
pub ptr: *const WebFNativeFunctionContextData,
}
Expand All @@ -31,7 +31,7 @@ pub extern "C" fn invoke_webf_native_function(
argc: c_int,
argv: *const NativeValue,
exception_state: *const OpaquePtr,
) -> *const c_void {
) -> NativeValue {
let callback_context = unsafe {
&(*(callback_context_ptr as *mut WebFNativeFunctionContext))
};
Expand All @@ -41,10 +41,8 @@ pub extern "C" fn invoke_webf_native_function(

unsafe {
let func = &(*callback_context_data).func;
func(argc, argv);
func(argc, argv)
}

std::ptr::null()
}

pub extern "C" fn release_webf_native_function(callback_context_ptr: *const OpaquePtr) -> *const c_void {
Expand Down

0 comments on commit 272526a

Please sign in to comment.