[webkit-dev] Question about Constructors in WebKit JS Bindings

Adam Barth abarth at webkit.org
Mon Jun 22 23:58:07 PDT 2009

On Mon, Jun 22, 2009 at 11:07 PM, Maciej Stachowiak<mjs at apple.com> wrote:
> I suspect most JS bindings in WebKit use the original more complicated
> pattern Drew cited, instead of the more succinct but incorrect one.

There are some bugs on file about this.  I've been meaning to go
through and look at all these cases.

>> Here's how the two bindings relate (if you look at both bindings and
>> wonder):
>> v8::Context::GetEntered() ~~ exec->dynamicGlobalObject()
>> v8::Context::GetCalling() ~~ exec->lexicalGlobalObject()
>> v8::Context::GetCurrent() ~~ ????
>> It might be good to add a similar "GetCurrent" concept to JSC
>> (although, hopefully with a better name) so we can remove all this
>> manual, error-prone caching / mark()ing.
> What does the "current" context correspond to?

It's the context in which the method was defined.  For DOM methods, V8
treats them as being defined by the context of their owner document.

> How is it retrieved?

No idea.  I think what happens is you enter the context for a
particular frame before constructing all the objects / methods for
that frame.  Then the DOM methods just have a scope like regular
JavaScript methods.

> Guessing
> wildly based on the names, I'm assuming host functions in v8 have the
> equivalent of their own scope chain and thus in some sense their own lexical
> global object.

I'm not an expert on this, but this fits my mental model.  In general,
V8 tries to implement DOM objects as close to how user-defined objects
would be implemented as possible.

> We could do that for JSC, except that for most things it
> would be useless - it's only potentially useful for DOM-level explicit
> constructors, since JS-level ones are supposed to use the calling function's
> lexical global object as the basis for everything, and most other host
> functions don't need it at all.

That's possible.  I don't have a good idea of what it costs to
provide.  The benefit is it makes it easier to write correct bindings
code.  (Or maybe it makes it harder because you have more contexts to
choose from and choosing the right context is subtle.)

I don't really know how to solve this problem except by writing tests.
 However, tests don't really help when introducing new kinds of
objects (e.g., workers, database transactions).


More information about the webkit-dev mailing list