Skip to content

Commit d8fd7cd

Browse files
LeuisKenandycall
authored andcommitted
feat: use future for async storage
1 parent 26c4e9c commit d8fd7cd

File tree

14 files changed

+214
-26
lines changed

14 files changed

+214
-26
lines changed

bridge/core/api/executing_context.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,12 @@ void ExecutingContextWebFMethods::ClearInterval(ExecutingContext* context,
132132
WindowOrWorkerGlobalScope::clearInterval(context, interval_id, shared_exception_state->exception_state);
133133
}
134134

135+
void ExecutingContextWebFMethods::SetRunRustFutureTasks(ExecutingContext* context,
136+
WebFNativeFunctionContext* callback_context,
137+
SharedExceptionState* shared_exception_state) {
138+
auto callback_impl = WebFNativeFunction::Create(callback_context, shared_exception_state);
139+
140+
context->SetRunRustFutureTasks(callback_impl);
141+
}
142+
135143
} // namespace webf

bridge/core/executing_context.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ void ExecutingContext::EnqueueMicrotask(MicrotaskCallback callback, void* data)
392392
JS_FreeValue(ctx(), proxy_data);
393393
}
394394

395+
void ExecutingContext::SetRunRustFutureTasks(const std::shared_ptr<WebFNativeFunction>& run_future_task) {
396+
run_rust_future_tasks_ = run_future_task;
397+
}
398+
399+
void ExecutingContext::RunRustFutureTasks() {
400+
run_rust_future_tasks_->Invoke(this, 0, nullptr);
401+
}
402+
395403
void ExecutingContext::DrainPendingPromiseJobs() {
396404
// should executing pending promise jobs.
397405
JSContext* pctx;

bridge/core/executing_context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class ExecutingContext {
101101
void ReportError(JSValueConst error, char** rust_errmsg, uint32_t* rust_errmsg_length);
102102
void DrainMicrotasks();
103103
void EnqueueMicrotask(MicrotaskCallback callback, void* data = nullptr);
104+
void SetRunRustFutureTasks(const std::shared_ptr<WebFNativeFunction>& run_rust_future_tasks);
105+
void RunRustFutureTasks();
104106
void DefineGlobalProperty(const char* prop, JSValueConst value);
105107
ExecutionContextData* contextData();
106108
uint8_t* DumpByteCode(const char* code, uint32_t codeLength, const char* sourceURL, uint64_t* bytecodeLength);
@@ -225,6 +227,9 @@ class ExecutingContext {
225227

226228
// Rust methods ptr should keep alive when ExecutingContext is disposing.
227229
const std::unique_ptr<ExecutingContextWebFMethods> public_method_ptr_ = nullptr;
230+
231+
// Rust future task queue run trigger
232+
std::shared_ptr<WebFNativeFunction> run_rust_future_tasks_;
228233
};
229234

230235
class ObjectProperty {

bridge/core/frame/dom_timer.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void DOMTimer::Fire() {
3939
}
4040
} else if (auto* callback = DynamicTo<WebFNativeFunction>(callback_.get())) {
4141
callback->Invoke(context_, 0, nullptr);
42+
context_->RunRustFutureTasks();
4243
}
4344
}
4445

bridge/core/frame/module_manager.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ NativeValue* handleInvokeModuleTransientCallback(void* ptr,
9292
}
9393
context->dartIsolateContext()->profiler()->FinishTrackSteps();
9494
context->dartIsolateContext()->profiler()->FinishTrackAsyncEvaluation();
95+
context->RunRustFutureTasks();
9596
return return_value;
9697
}
9798
}

bridge/core/native/native_loader.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static void ExecuteNativeLibrary(PluginLibraryEntryPoint entry_point,
3737
native_library_load_context->context, native_library_load_context->context->publicMethodPtr(),
3838
native_library_load_context->context->status()};
3939
void* result = entry_point(entry_data);
40+
native_library_load_context->context->RunRustFutureTasks();
4041
}
4142

4243
delete native_library_load_context;

bridge/core/page.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ NativeValue* WebFPage::invokeModuleEvent(SharedNativeString* native_module_name,
122122
NativeValue tmp = callback->Invoke(context_, 2, params);
123123
auto* return_value = static_cast<NativeValue*>(dart_malloc(sizeof(NativeValue)));
124124
memcpy(return_value, &tmp, sizeof(NativeValue));
125+
context_->RunRustFutureTasks();
125126
return return_value;
126127
}
127128
}

bridge/include/plugin_api/executing_context.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ using PublicContextSetInterval = int32_t (*)(ExecutingContext*,
4141
SharedExceptionState*);
4242
using PublicContextClearTimeout = void (*)(ExecutingContext*, int32_t, SharedExceptionState*);
4343
using PublicContextClearInterval = void (*)(ExecutingContext*, int32_t, SharedExceptionState*);
44+
using PublicContextSetRunRustFutureTasks = void (*)(ExecutingContext*,
45+
WebFNativeFunctionContext*,
46+
SharedExceptionState*);
4447

4548
// Memory aligned and readable from WebF side.
4649
// Only C type member can be included in this class, any C++ type and classes can is not allowed to use here.
@@ -77,6 +80,9 @@ struct ExecutingContextWebFMethods {
7780
static void ClearInterval(ExecutingContext* context,
7881
int32_t interval_id,
7982
SharedExceptionState* shared_exception_state);
83+
static void SetRunRustFutureTasks(ExecutingContext* context,
84+
WebFNativeFunctionContext* callback_context,
85+
SharedExceptionState* shared_exception_state);
8086

8187
double version{1.0};
8288
PublicContextGetDocument context_get_document{document};
@@ -92,6 +98,7 @@ struct ExecutingContextWebFMethods {
9298
PublicContextSetInterval context_set_interval{SetInterval};
9399
PublicContextClearTimeout context_clear_timeout{ClearTimeout};
94100
PublicContextClearInterval context_clear_interval{ClearInterval};
101+
PublicContextSetRunRustFutureTasks context_set_run_rust_future_tasks{SetRunRustFutureTasks};
95102
};
96103

97104
} // namespace webf

bridge/rusty_webf_sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "Apache-2.0"
1010

1111
[dependencies]
1212
libc = "0.2.0"
13-
13+
futures = "0.3"
1414

1515
[dependencies.windows]
1616
version = "0.58.0"

bridge/rusty_webf_sys/src/executing_context.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2728
pub type TimeoutCallback = Box<dyn Fn()>;
2829
pub 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

264303
impl 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

Comments
 (0)