New issue 879 by spen...@jacknife.org: Why does Handle<T>::operator*()
return T* instead of T& ?
http://code.google.com/p/v8/issues/detail?id=879
On classes aiming to act like pointers, such as v8::Handle, operator->()
usually returns T*, but doesn't operator*() usually return T&?
By returning T*, you have to do a double-dereference to get a reference to
the actual type:
void foo(const String &arg) {}
//...
Handle<String> x = someFunction();
foo(**x); // unfortunate
Am I missing something obvious? :)
Of course, changing this probably breaks a bunch of code :(
Thanks.
Comment #1 on issue 879 by kmil...@chromium.org: Why does
Handle<T>::operator*() return T* instead of T& ?
http://code.google.com/p/v8/issues/detail?id=879
You really want to pass a String* to function foo.
String* is sort of a fiction. It is a (slightly mangled) address of an
object in V8's heap. It is not really a pointer to a C++ object of type
String.
To have a reference to a String, there would have to be a String.
The v8-users and v8-dev mailing lists are better places than the bug
tracker to ask questions like this :)
I want to add that you should never work with pointers and refs to V8
objects, but use Handles instead. See the comment around line 160 in
include/v8.h:
All objects returned from v8 have to be tracked by the garbage
collector so that it knows that the objects are still alive. Also,
because the garbage collector may move objects, it is unsafe to
point directly to an object. Instead, all objects are stored in
handles which are known by the garbage collector and updated
whenever an object moves. Handles should always be passed by value
(except in cases like out-parameters) and they should never be
allocated on the heap.
I moved this discussion to the mailing list:
http://groups.google.com/group/v8-dev/browse_thread/thread/bee1a38f63a44dc8
However, there is a C++ class String
with real C++ functions on it. And a Handle<String> is just a (smart)
pointer to an instance of that class.
* It is safe to extract the object stored in the handle by
* dereferencing the handle (for instance, to extract the Object* from
* an Handle<Object>); the value will still be governed by a handle
* behind the scenes and the same rules apply to these values as to
* their handles.
(After all, when you dereference a String*, you get a
String&.)