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.
///
/// This call will lock the execution context for the current
/// thread as long as the Ref<> is alive.
///
Ref<JSContext> context = caller->LockJSContext();
///
/// Get the underlying JSContextRef for use with the
/// JavaScriptCore C API.
///
JSContextRef ctx = context.get();
/// Use the context in JavaScriptCore API calls 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 almost 3 years ago