From 28e64fc701fc4a5f2f2e2dafe841465fe87e036c Mon Sep 17 00:00:00 2001 From: CYBAI Date: Thu, 8 Aug 2019 12:29:06 +0900 Subject: [PATCH] Assert incumbent global is always some in get_incumbent_global hook While working on module script, I'd like to use Promise with a custom callback with type `Box` which means we didn't use web API callbacks as native handlers. However, we'll get a panic from `enqueue_promise_job` and the panic says we have a `null` incumbent global. The main problem here is, the Promise API is strongly tied to JS engine and it always assumes there's a meaningful answer for "what specific global is this promise associated with". So, when I don't use the Promise for a specific web API, our engine cannot find a proper incumbent global for us so that we get the `null` incumbent global panic. To make us catch this case easier in the future, we should add the assertion inside `get_incumbent_global` hook so that we can know this quickly next time. Ref: https://mozilla.logbot.info/servo/20190807#c16525481 --- components/script/script_runtime.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index fb94d30fafd..5a9fdb0676b 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -142,7 +142,11 @@ pub trait ScriptPort { unsafe extern "C" fn get_incumbent_global(_: *const c_void, _: *mut RawJSContext) -> *mut JSObject { wrap_panic( AssertUnwindSafe(|| { - GlobalScope::incumbent() + let incumbent_global = GlobalScope::incumbent(); + + assert!(incumbent_global.is_some()); + + incumbent_global .map(|g| g.reflector().get_jsobject().get()) .unwrap_or(ptr::null_mut()) }),