How to properly pass Local<Object> as function param?

80 views
Skip to first unread message

Ryan Cole

unread,
Nov 11, 2012, 7:54:17 PM11/11/12
to nod...@googlegroups.com
In a C++ addon, I've got a function in which I instantiate a Local<Object>. I want to pass that as an argument to another function. Should the argument be passed as a Local<Object>, or Persistent<Object>? I'm unsure of when I should use either of those.

Thanks

Ben Noordhuis

unread,
Nov 11, 2012, 8:19:04 PM11/11/12
to nod...@googlegroups.com
Immediate function call, not asynchronous? You can pass the object as
a Local<>.

Ryan Cole

unread,
Nov 11, 2012, 8:30:04 PM11/11/12
to nod...@googlegroups.com
Ya immediate function call. My bad for not mentioning that. Ok, I had it passing as a Local, but I didn't know if that was bad or not.

I have to admit, the docs for v8, node and libuv are all difficult to merge into one as a beginner. I'm honestly just brute forcing my knowledge of these libraries as much as I can. I make attempts to scan each set of docs for information as to how to do things, and when I cant find it there I go to github looking for examples using code search, and if that fails I go to google. When those all fail I generally go to IRC or here. It's a slow painful progression but it's basically the only way I know how to, at the moment.

Ryan

Roger WANG

unread,
Nov 11, 2012, 8:35:21 PM11/11/12
to nod...@googlegroups.com
Ryan Cole <ry...@rycole.com> writes:

> Ya immediate function call. My bad for not mentioning that. Ok, I had it
> passing as a Local, but I didn't know if that was bad or not.
>
> I have to admit, the docs for v8, node and libuv are all difficult to merge
> into one as a beginner. I'm honestly just brute forcing my knowledge of
> these libraries as much as I can. I make attempts to scan each set of docs
> for information as to how to do things, and when I cant find it there I go
> to github looking for examples using code search, and if that fails I go to
> google. When those all fail I generally go to IRC or here. It's a slow
> painful progression but it's basically the only way I know how to, at the
> moment.

Regarding V8 beginners, there is some examples in
v8/test/cctest/test-api.cc which could be helpful to learn how to do
various things.

--
Roger WANG Intel Open Source Technology Center

Ben Noordhuis

unread,
Nov 11, 2012, 9:53:00 PM11/11/12
to nod...@googlegroups.com
For libuv there is [1]. People mention that reading the tests[2]
helps to create a mental picture of how everything ties together.

[1] http://nikhilm.github.com/uvbook/
[2] https://github.com/joyent/libuv/tree/master/test

Ryan Cole

unread,
Nov 11, 2012, 10:16:17 PM11/11/12
to nod...@googlegroups.com
Ok awesome, thanks for the reads. Also, just to deliberately ask, what about in async function calls? I'm assuming I'd not use Local<Object>? For example, if I were using the baton pattern, would my baton struct use a Local<Object>? I've seen examples for callback functions that use Persistent<Function>, so that makes me think I'd use Persistent<Object> to cross those function calls?

Thanks,
Ryan

Ben Noordhuis

unread,
Nov 11, 2012, 10:19:26 PM11/11/12
to nod...@googlegroups.com
On Mon, Nov 12, 2012 at 4:16 AM, Ryan Cole <ry...@rycole.com> wrote:
> Ok awesome, thanks for the reads. Also, just to deliberately ask, what about
> in async function calls? I'm assuming I'd not use Local<Object>? For
> example, if I were using the baton pattern, would my baton struct use a
> Local<Object>? I've seen examples for callback functions that use
> Persistent<Function>, so that makes me think I'd use Persistent<Object> to
> cross those function calls?

Yes, Persistent<Object>.

Ryan Cole

unread,
Nov 11, 2012, 10:34:13 PM11/11/12
to nod...@googlegroups.com
Ok thanks for the help. I think through this code I'm writing I've at least nailed down when to use those two handle types. Thanks again!

Ryan

Marcel Laverdet

unread,
Nov 12, 2012, 2:51:43 AM11/12/12
to nodejs
You should probably just be using Handle<> in most of your function parameters. The only reason to specify Local<> or Persistent<> instead of the more generic superclass Handle<> is when you need to manage the lifetime of the object. It's generally not the callee's job to manage the memory of the underlying handle, so it doesn't need to know if a given handle is local or persistent.

If it's async you should still probably use Handle<> for the argument, but then you can create your own persistent reference to it via Persistent<>::New(handle).


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply all
Reply to author
Forward
0 new messages