On Tuesday, April 23, 2013 8:58:37 AM UTC-7, Ben Noordhuis wrote: > > On Tue, Apr 23, 2013 at 5:49 PM, <[email protected] <javascript:>> > wrote: > > > > On Tuesday, April 23, 2013 6:47:51 AM UTC-7, Ben Noordhuis wrote: > >> > >> On Thu, Apr 4, 2013 at 12:47 PM, Olivier GEORGET > >> <[email protected]> wrote: > >> > Hi, > >> > > >> > I'm new to V8 and have only fiddled with it. > >> > Can someone please explain me the main difference between the Context > >> > access > >> > functions ? There is actually 3 of them : > >> > > >> > v8::Context::GetCurrent(); > >> > v8::Context::GetEntered(); > >> > v8::Context::GetCalling(); > >> > > >> > Can see the purpose of GetCalling, but the 2 first ones sound the > same > >> > to > >> > me, and the doc is very frugal about it :S > >> > > >> > In the following code, I would at first expect context == curCtxTest > : > >> > > >> >> v8::Handle<v8::Context> context = v8::Context::New(NULL, > >> >> globalObj); > >> >> > >> >> v8::Context::Scope context_scope(context); > >> >> v8::Handle<v8::Context> curCtxTest = v8::Context::GetCurrent(); > >> > > >> > > >> > Cheers, > >> > Olivier. > >> > >> Say you have three functions: > >> > >> * a(), compiled in context A > >> * b(), compiled in context B > >> * c(), compiled in context C > >> > >> Your native code looks like this: > >> > >> Persistent<Context> context_a = ...; > >> Local<Function> function_a = ...; > >> context_a->Enter(); > >> function_a->Call(context_a->Global(), 0, NULL); > >> context_a->Exit(); > >> > >> If a() calls b() which in turn calls c(), then the following is true in > >> c(): > >> > >> Context::GetCurrent() == C > >> Context::GetCalling() == B > >> Context::GetEntered() == A > >> > >> Why is that useful? I suspect that the primary use case in Chromium > >> is the ability to look up the right security token, depending on the > >> context in which a native function is called. > > > > > > Hopefully this helps the origina poster, but it confuses me. I > understand > > why > > Context::GetEntered() == A > > since we can read > > context_a->Enter(); > > but I can't figure out why the other facts are true. > > > > jjb > > A function is tied to the context it was created in. > > That's why Context::GetCalling() == B in c() because b() calls c(). > > Likewise, Context::GetCurrent() == C in c() because that's the context > it was created in (and belongs to.) >
Excellent, thanks! I think putting this example in the Embedder's Guide would help others. As APIs go, this one seems bizarre. As you say, every function has a Context, so -- assuming I understand now -- Context::GetCurrent() is the Context of the currently running function, Context::GetCalling() is the context of the caller of the current running function, and Context::GetEntered() is the context of the function at the entry point for the current call stack. It's puzzling that we get these via global calls on Context. jjb -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
