Hey Ingwie!
Basically, other than Wrap and Unwrap methods, it has Ref, Unref, and
WeakCallback methods. Ref and Unref
correspondingly increase and decrease v8's reference count on handle_.
And it's method WeakCallback will be called by gc at some point after
there's no more references to it's handle_ member, and every call to
Ref is matched by call to Unref.
See object_wrap.{h,cc} for the gory details.
On 4 August 2014 21:44, Ingwie Phoenix <[email protected]> wrote:
> Hey Dmitry,
>
> thank you for your explanation, although this is not my first C++ addon. This
> time, I just wanted to create a more async module and wanted to hook this up
> to events. But obviously, as long as I am in C++-land, I cant seem to access
> the JS-land EventEmitter to let my object inherit from there.
>
> But while I am at it, what else does ObjectWrap offer, other than the own
> Wrap() method, that lets me put a C++ instance inside?
>
> Kind regards, Ingwie
> Am 04.08.2014 um 09:33 schrieb Dmitry Unkovsky <[email protected]>:
>
>> Hey Ingwie!
>>
>> You don't just create EventEmitter in C++. Well, technically you
>> could, but that's not how things are usually done in node-js addons.
>>
>> If you have your ToughClass in C++ class for heavylifting, it should
>> inherit from ObjectWrap, which enables your ToughClass to interact
>> with v8 GC and with other js parts of your application.
>>
>> Then, usually in ToughClass::Initialize method, you "manually", by
>> calling special v8 api methods,
>> create some js-style JSTough class constructor function, an you set all
>> methods that you want to be accessible from js-land on
>> JSTough.prototype (like this: NODE_SET_PROTOTYPE_METHOD(t,
>> "someMethod", ToughClass::someMethod)), and you set your C++ module's
>> `export.JSWrap` to this JSTough function.
>>
>> Then, what happens in runtime? In js, you do:
>> var JSTough = require("./path/to/tough_class").JSTough
>> var jsw = new JSTough()
>>
>> new JSTough() is usually bound to (static) ToughClass::New, which
>> creates ToughClass instance (say, `tc`), and sets it's handle_ member
>> to `jsw` (which, remember, is a js-object initized with new JSTough()
>> call).
>>
>> Then, in js-land, you set callbacks on `jsw`, like `jsw.on_something =
>> function(arg1, arg2){ ... }`, and call it's methods, like
>> `jsw.someMethod()` (which invokes ToughClass::someMethod). Sometime
>> later, you can call `on_something` callback from C++ land.
>>
>> If you want to have EventEmitter over this construction, you do some
>> other js class, this time in js-land:
>>
>> ```
>> function Tough(){
>> EventEmitter.apply(this, arguments)
>> this._handle = new JSTough()
>> this._handle.on_something = function(result){
>> this.emit("result", result)
>> }.bind(this)
>> }
>> ```
>>
>> This is just a quick overview of what happens somewhere under the
>> hood, just a brief picture. The details vary in 0.10 and 0.11
>> versions, but the overall flow is essentially the same.
>>
>> For details and real examples, please see the addons documentation for
>> your version of node http://nodejs.org/api/addons.html.
>>
>> You may find interesting `src/tcp_wrap.{h,cc}` and `lib/net.js` an
>> interesting reference, or, for more simple and straightforward code,
>> `src/node_buffer.{h,cc}`.
>>
>> HTH.
>>
>> --
>> Job board: http://jobs.nodejs.org/
>> New group rules:
>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>> Old group rules:
>> 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 unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/nodejs/CAM4U4uUYTDnzf5JqGQZdh%3DCF3Na6SNwrU0FMz3pDw%2B2x%2ByF7fg%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/96A75DE9-7817-4F11-8E0A-6FC7E0F48614%40googlemail.com.
> For more options, visit https://groups.google.com/d/optout.
--
DU
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/CAM4U4uXRFa-OcwDVPcUr5rx7JwXnRe_dX3EFfU61SnKrk0R%2B%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.