Getting the JS Context for a View
Most calls in JavaScriptCore requires an execution context. You can get the global JS context for the current page via View::LockJSContext()
.
///
/// Use LoadListener::OnDOMReady to wait for the DOM to load.
///
void MyApp::OnDOMReady(View* caller,
uint64_t frame_id,
bool is_main_frame,
const String& url) {
///
/// Acquire the JS execution context for the current page.
///
auto scoped_context = caller->LockJSContext();
///
/// Typecast to the underlying JSContextRef.
///
JSContextRef ctx = (*scoped_context);
/// Make calls to JavaScriptCore API here...
}
JS Context Lifetime
Just note that this context may change between page loads-- when the context changes all garbage-collected objects (JSValueRef
and JSObjectRef
) are no longer valid.
Updated about 1 year ago
What’s Next