Comprehension: Working with contexts

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Comprehension: Working with contexts

Postby OneLiner » Wed Jan 30, 2019 7:55 am

I have a few comprehension questions regarding this section about "working with contexts".
https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration#markdown-header-working-with-contexts

A.) The upper section says...
V8 will be inside a context if the current code location has a CefV8Handler, CefV8Accessor or OnContextCreated()/OnContextReleased() callback higher in the call stack.

How can the code determine - if i don't know it anyway during programming - , if it's executed inside a V8-context,
meaning, that there is a CefV8Handler, CefV8Accessor or OnContextCreated()/OnContextReleased() callback higher in the call stack?

B.) Moreover, the upper section says...
Arrays, objects and functions may only be created, modified and, in the case of functions, executed, if V8 is inside a context. If V8 is not inside a context then the application needs to enter a context by calling Enter() and exit the context by calling Exit(). The Enter() and Exit() methods should only be used:

1.)When creating a V8 object, function or array outside of an existing context. For example, when creating a JS object in response to a native menu callback.

2.)When creating a V8 object, function or array in a context other than the current context. For example, if a call originating from frame1 needs to modify the context of frame2.


In my comprehension, the first thing is again, knowing if V8 is inside a context, AND if it is 'theRightContext'.
In case i am already in a context, AND it is theRightOne, THEN i should NOT call Enter() and Exit().
In case i am already in a context, BUT it is NOT theRightOne, i have to call theRightContext->Enter() and theRightContext->Exit().
AND of course In case i am NOT YET in a context i also have to call theRightContext->Enter() and theRightContext->Exit(), right?

C.)
"Arrays, objects and functions may only be created, modified and, in the case of functions, executed, if V8 is inside a context."

I understand this sentence as: if i want to perform a javascript-context-operation of any kind, right ?...

D.) There is no difference in what you can do using ExecuteFunction (A) and ExecuteFunctionWithContext (B),
except using B lets you choose the context upon which the operation gets performed, right?
OneLiner
Techie
 
Posts: 33
Joined: Mon Jan 28, 2019 5:58 am

Re: Comprehension: Working with contexts

Postby magreenblatt » Wed Jan 30, 2019 12:30 pm

You can use CefV8Context::InContext to test if you're in a context. When you register a callback from JavaScript keep a reference to the context as well. With ExecuteFunction you call Enter/Exit and with ExecuteFunctionWithContext you pass in the context as a parameter.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 32 guests