Is it valid to not close a scope?

42 views
Skip to first unread message

bodo....@enabre.com

unread,
Apr 14, 2013, 11:30:09 AM4/14/13
to v8-u...@googlegroups.com
Hello,

I want to use a function which acts as internal exception shortcut unfortunately I cannot pass the current scope so I would create a new one.
Is this whole strategy valid? If not how would it look right?

#include <v8.h>
#include <node.h>
 
inline Local<Value> ThrowTypeError(const char* message) {
HandleScope scope;
ThrowException(Exception::TypeError(String::New(message)));
 
return scope.Close(Undefined());
};
 
Handle<Value> SomeMethod(const Arguments &args) {
HandleScope scope:
 
if (!args[0]->IsObject() || args[0]->IsArray())
return ThrowTypeError("Argument must be a object literal.");
return scope.Close(args[0]->ToObject());
};
 
NODE_MODULE(test, init);


Bodo

Stephan Beal

unread,
Apr 14, 2013, 11:37:11 AM4/14/13
to v8-u...@googlegroups.com
On Sun, Apr 14, 2013 at 5:30 PM, <bodo....@enabre.com> wrote:
Hello,

I want to use a function which acts as internal exception shortcut unfortunately I cannot pass the current scope so I would create a new one.
Is this whole strategy valid? If not how would it look right?

My understanding, based on previous discussions on this list, is that a HandleScope is not needed at all if the code in question is being called from JS space (because such a call has an implicit HandleScope around it). Based on my own experience, your InvocationCallback and exception-thrower look perfectly koster to me, but i would drop both of the HandleScopes - IMO they are not needed because your InvocationCallback will be called from JS-space in response to a Function call, and such calls have an implicit Scope provided by the engine.
 
Handle<Value> SomeMethod(const Arguments &args) {
HandleScope scope:
 
if (!args[0]->IsObject() || args[0]->IsArray())

if i'm not mistaken (and i might be), IsObject() will(???) also return true for an Array because an Array is-a Object.
 
return ThrowTypeError("Argument must be a object literal.");
return scope.Close(args[0]->ToObject());
};
 
NODE_MODULE(test, init);

Bodo Kaiser

unread,
Apr 14, 2013, 11:51:30 AM4/14/13
to v8-u...@googlegroups.com
I require scope.Close() to return a value. My sorrow is that I could get problems with GC because I did not close the scope or something.
But you do not confirm these sorrows?

To the second:
Yes that is why there is args[0]->IsArray()

Regards


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/Q5D2lsbYb1M/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Stephan Beal

unread,
Apr 14, 2013, 11:59:13 AM4/14/13
to v8-u...@googlegroups.com
On Sun, Apr 14, 2013 at 5:51 PM, Bodo Kaiser <bodo....@enabre.com> wrote:
I require scope.Close() to return a value

That's a common misconception. i've been using v8 for 4+ years now and i _never_ use HandleScopes (and can return values just fine). A HandleScope, from what i can gather, is just a hint to the GC to help clean up temporary/local-only values.
 
. My sorrow is that I could get problems with GC because I did not close the scope or something.
But you do not confirm these sorrows?

When you call a function from JS:

myFunction()

and that then calls your C++ InvocationCallback, there is an implicit scope being created in the call, so there is no need to create an extra one in your app (unless you are calling the code from C++ without first going through the JS engine, but such cases are rare in practice).

 
To the second:
Yes that is why there is args[0]->IsArray()

That's my point: IsArray() is useless here because the order of operations ensure that IsObject() will always trump IsArray(). i.e. if it's an array then IsObject() will return true and the IsArray() call is not needed (it will never actually be called due to the short-circuiting logic of the OR operator). (NOTE, however, that i am ASSUMING (without having tested), that IsObject() will indeed return true for a Function or Array Object. The API docs are not clear on this distinction.)

Bodo Kaiser

unread,
Apr 14, 2013, 12:06:36 PM4/14/13
to v8-u...@googlegroups.com
Good to know that returning would work too. I am quite new to v8.

I think you oversaw the "!" (so "!obj->IsObject()).
OR will stop on the first "true" else it will go through each expression. !obj->IsObject() will so filter the primitive values else I agree :)
Regards

Bodo Kaiser

unread,
Apr 14, 2013, 12:07:49 PM4/14/13
to v8-u...@googlegroups.com
By the way do you have a shortcut of doing:

obj->Get(String::New("key"));

Is this a use case for "inline functions" or is there something better?

Am 14.04.2013 um 17:59 schrieb Stephan Beal <sgb...@googlemail.com>:

Stephan Beal

unread,
Apr 14, 2013, 12:32:31 PM4/14/13
to v8-u...@googlegroups.com
On Sun, Apr 14, 2013 at 6:06 PM, Bodo Kaiser <bodo....@enabre.com> wrote:
I think you oversaw the "!" (so "!obj->IsObject()).
OR will stop on the first "true" else it will go through each expression. !obj->IsObject() will so filter the primitive values else I agree :)

Doh, you're absolutely right. Mae culpa!
 
Regarding:

obj->Get(String::New("key"));

Now, i don't know a shortcut for that. :(
Reply all
Reply to author
Forward
0 new messages