@@ -22,10 +22,12 @@ pub struct ExecutingContextRustMethods {
2222 pub set_interval : extern "C" fn ( * const OpaquePtr , * const WebFNativeFunctionContext , c_int , * const OpaquePtr ) -> c_int ,
2323 pub clear_timeout : extern "C" fn ( * const OpaquePtr , c_int , * const OpaquePtr ) ,
2424 pub clear_interval : extern "C" fn ( * const OpaquePtr , c_int , * const OpaquePtr ) ,
25+ pub set_run_rust_future_tasks : extern "C" fn ( * const OpaquePtr , * const WebFNativeFunctionContext , * const OpaquePtr ) -> c_void ,
2526}
2627
2728pub type TimeoutCallback = Box < dyn Fn ( ) > ;
2829pub type IntervalCallback = Box < dyn Fn ( ) > ;
30+ pub type RunRustFutureTasksCallback = Box < dyn Fn ( ) > ;
2931
3032/// An environment contains all the necessary running states of a web page.
3133///
@@ -259,6 +261,43 @@ impl ExecutingContext {
259261 }
260262 }
261263
264+ pub fn set_run_rust_future_tasks ( & self , callback : RunRustFutureTasksCallback , exception_state : & ExceptionState ) -> Result < ( ) , String > {
265+ let general_callback: WebFNativeFunction = Box :: new ( move |argc, argv| {
266+ if argc != 0 {
267+ println ! ( "Invalid argument count for run rust future tasks callback" ) ;
268+ return NativeValue :: new_null ( ) ;
269+ }
270+ callback ( ) ;
271+ NativeValue :: new_null ( )
272+ } ) ;
273+
274+ let callback_data = Box :: new ( WebFNativeFunctionContextData {
275+ func : general_callback,
276+ } ) ;
277+ let callback_context_data_ptr = Box :: into_raw ( callback_data) ;
278+ let callback_context = Box :: new ( WebFNativeFunctionContext {
279+ callback : invoke_webf_native_function,
280+ free_ptr : release_webf_native_function,
281+ ptr : callback_context_data_ptr,
282+ } ) ;
283+ let callback_context_ptr = Box :: into_raw ( callback_context) ;
284+
285+ unsafe {
286+ ( ( * self . method_pointer ) . set_run_rust_future_tasks ) ( self . ptr , callback_context_ptr, exception_state. ptr ) ;
287+ }
288+
289+ if exception_state. has_exception ( ) {
290+ unsafe {
291+ let _ = Box :: from_raw ( callback_context_ptr) ;
292+ let _ = Box :: from_raw ( callback_context_data_ptr) ;
293+ }
294+ return Err ( exception_state. stringify ( self ) ) ;
295+ }
296+
297+ Ok ( ( ) )
298+
299+ }
300+
262301}
263302
264303impl Drop for ExecutingContext {
@@ -271,3 +310,13 @@ impl Drop for ExecutingContext {
271310 }
272311 }
273312}
313+
314+ impl Clone for ExecutingContext {
315+ fn clone ( & self ) -> Self {
316+ ExecutingContext {
317+ ptr : self . ptr ,
318+ method_pointer : self . method_pointer ,
319+ status : self . status ,
320+ }
321+ }
322+ }
0 commit comments