Skip to content

Commit 13a9931

Browse files
committed
Provide documentation section on This vs. Holder
1 parent 0b4a813 commit 13a9931

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

doc/object_wrappers.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ class ObjectWrap {
5454
5555
See the Node documentation on [Wrapping C++ Objects](https://nodejs.org/api/addons.html#addons_wrapping_c_objects) for more details.
5656
57+
### This vs. Holder
58+
59+
When calling `Unwrap`, it is important that the argument is indeed some JavaScript object which got wrapped by a `Wrap` call for this class or any derived class.
60+
The `Signature` installed by [`Nan::SetPrototypeMethod()`](methods.md#api_nan_set_prototype_method) does ensure that `info.Holder()` is just such an instance.
61+
In Node 0.12 and later, `info.This()` will also be of such a type, since otherwise the invocation will get rejected.
62+
However, in Node 0.10 and before it was possible to invoke a method on a JavaScript object which just had the extension type in its prototype chain.
63+
In such a situation, calling `Unwrap` on `info.This()` will likely lead to a failed assertion causing a crash, but could lead to even more serious corruption.
64+
65+
On the other hand, calling `Unwrap` in an [accessor](methods.md#api_nan_set_accessor) should not use `Holder()` if the accessor is defined on the prototype.
66+
So either define your accessors on the instance template,
67+
or use `This()` after verifying that it is indeed a valid object.
68+
5769
### Examples
5870
5971
#### Basic

0 commit comments

Comments
 (0)