Check Value.IsObject() and then use Value.ToObject().
I think you're doing it quite right. I must admit I've never dealt
with failed conversion.
I'm not sure what reasons for failure exist other than
input->ToString() failing - which is not an issue if it's already a
string.
> This is what I came up with from the sample code:
>
> const char* ToCString(const v8::String::Utf8Value& value) {
> return *value ? *value : "<string conversion failed>";
> }
>
> ...
>
> std::string my_str =
> std::string::New(ToCString(v8::String::Utf8Value(obj-
>>Get(v8::String::New("my_str")))));
>
> That seems kinda convoluted, but works. However, is there another more
> direct way?
>
> And what about extracting a Boolean? Integer? Float? Array? Haven't
> found any documentation about how to get at those other property
> types. Any help is greatly appreciated.
Same pattern. Either you can use the ECMA conversions via "ToNumber",
"ToBoolean", ... or Cast the handle after checking "IsXXX". Number has
"double Value()", Boolean has "bool Value()". Array has Length() and
Get(uint32).
That seems kinda convoluted, but works. However, is there another more
direct way?
And what about extracting a Boolean? Integer? Float? Array? Haven't
found any documentation about how to get at those other property
types. Any help is greatly appreciated.
> See the API docs forI've tried many times to find "the API docs". All I found is this:
http://code.google.com/apis/v8/intro.html
I've read through all the pages there, and I don't see any
documentation about these different API functions. What documentation
am I missing?
bool logging = (bool)obj->Get(v8::String::New("my_bool"))->ToBoolean();
And the compiler complains:
error: invalid cast from type v8::Local<v8::Boolean>to type bool
Sorry, I'm sure the documentation that I'm failing to find makes this
all perfectly clear,
but I'm having an extrodinary amount of
difficulty getting a type from the V8 version of that type
(v8::Boolean) to the actual c/c++ type (bool) so that I can do
something with it.
Again, any help or direction to specific
documentation is greatly appreciated.
I fully agree with you that the documentation is lacking. Really, v8.h
is your friend. There are lots of third-party articles about specific
things, but in the end, you'll need to look at the header and try your
way through. You seem to have grokked the Handle<X> ~ smart pointer
idea, so given a Handle<X> you want to look at X's interface.
Let's see:
V8EXPORT Local<Boolean> ToBoolean() const;
(What I think I've learnt is that the To* methods perform conversions
as described in ECMA 262.
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf.
E.g. String::New("3")->ToNumber() won't fail but convert to a Number
value. Other than the Casts mentioned above).
So now we have a Local<Boolean>.
...
class Boolean : public Primitive {
public:
V8EXPORT bool Value() const;
static inline Handle<Boolean> New(bool value);
};
i.e.
bool boolValue = v8Value->ToBoolean()->Value();
or there seems to be a shortcut:
V8EXPORT bool BooleanValue() const;
Matthias
>
> And the compiler complains:
>
> error: cannot convert v8::Local<v8::Boolean> to bool in
> initialization
>
> Then I tried:
>
> bool logging = (bool)obj->Get(v8::String::New("my_bool"))-
>>ToBoolean();
>
> And the compiler complains:
>
> error: invalid cast from type v8::Local<v8::Boolean>to type bool
>
> Sorry, I'm sure the documentation that I'm failing to find makes this
> all perfectly clear, but I'm having an extrodinary amount of
> difficulty getting a type from the V8 version of that type
> (v8::Boolean) to the actual c/c++ type (bool) so that I can do
> something with it. Again, any help or direction to specific
> documentation is greatly appreciated.
>
While all objects live in the V8 heap, the GC needs your help to
identify which objects you are referencing from native code. So you
don't get to point directly into the heap but through the Handle
indirection. The "Local" kind of handles does live on a stack, namely
in HandleScopes. Whenever a Local handle is created, it is allocated
within the currently active HandleScope. When the HandleScope closes,
it doesn't necessarily mean that the target of the reference is gone,
but that your Local is no longer known to V8 and it might relocate the
object or overwrite your Handle with something else.
So: never let a Local escape outside of its HandleScope. But how do
deal with returning a Handle from a nested scope? Use
"HandleScope::Close". It takes the argument, and returns an identical
Handle allocated in the parent scope, so you can safely leave the
current (A2_broken: L10: return handle_scope.Close(script->Run());).
If that helps, this is the equivalent of the Push/PopLocalFrame
functions of JNI.
I think you're just lucky that the getObj in B1&B2 work.
B2 doesn't look like the actual code anyway
(printf("$s\n",s3_2.c_str()); certainly won't print "undefined")
PS: Context::Scopes are thread-local, you don't need to open them in
every function you call. One outer Context::Scope is typically enough.
The same does hold for HandleScopes but here it is a good idea to
stack them and release references as early as possible.