[webkit-dev] when can I treat a JSValueRef as a JSObjectRef?

Darin Adler darin at apple.com
Mon May 4 10:30:32 PDT 2009


On May 4, 2009, at 10:17 AM, Patrick Mueller wrote:

> Since JSValueRef and JSObjectRef are typedef's to the same  
> OpaqueJSValue  structure (pointer)

It’s easy to get the wrong idea about this by looking at the  
implementation. These two typedefs use the same structure, but they  
use const in an unusual way to make the compiler check the  
relationship between the types. Because of how the types are defined  
all JSObjectRef values work as JSValueRef values without any type cast.

There are many JSValueRef values that are not JSObjectRef values, so  
converting from one to the other requires a type cast. If you want to  
use a JSValueRef as a JSObjectRef, you need to call JSValueIsObject  
and if it’s true, then you can cast the JSValueRef to a JSObjectRef.  
Or you can use JSValueGetType, but that could be less efficient so you  
should use it only if you care which specific type it is if it’s not a  
JSObjectRef.

> It would be nice to see this documented a little better.

What documentation were you reading? Since WebKit is an open source  
project, if you have improvements to, say, the comments in the header,  
you can submit them as a patch. Most of the mechanics are covered at <http://webkit.org/coding/contributing.html 
 >, although some of the steps don’t apply for changes that are  
entirely in comments.

If this was some Apple documentation you were reading, not generated  
directly from the header file, then you could instead file a bug at http://bugreport.apple.com 
  suggesting improvements.

> My current guess is that if JSValueGetType() returns kJSTypeObject  
> for a JSValueRef, you can safely treat it as a JSObjectRef.  If it  
> doesn't, then you can't.

Yes, that’s right.

     -- Darin



More information about the webkit-dev mailing list