src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / base-object-inl.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #ifndef SRC_BASE_OBJECT_INL_H_
23 #define SRC_BASE_OBJECT_INL_H_
24
25 #include "base-object.h"
26 #include "util.h"
27 #include "util-inl.h"
28 #include "v8.h"
29
30 #include <assert.h>
31
32 namespace node {
33
34 inline BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> handle)
35     : handle_(env->isolate(), handle),
36       env_(env) {
37   assert(!handle.IsEmpty());
38 }
39
40
41 inline BaseObject::~BaseObject() {
42   assert(handle_.IsEmpty());
43 }
44
45
46 inline v8::Persistent<v8::Object>& BaseObject::persistent() {
47   return handle_;
48 }
49
50
51 inline v8::Local<v8::Object> BaseObject::object() {
52   return PersistentToLocal(env_->isolate(), handle_);
53 }
54
55
56 inline Environment* BaseObject::env() const {
57   return env_;
58 }
59
60
61 template <typename Type>
62 inline void BaseObject::WeakCallback(
63     const v8::WeakCallbackData<v8::Object, Type>& data) {
64   Type* self = data.GetParameter();
65   self->persistent().Reset();
66   delete self;
67 }
68
69
70 template <typename Type>
71 inline void BaseObject::MakeWeak(Type* ptr) {
72   v8::HandleScope scope(env_->isolate());
73   v8::Local<v8::Object> handle = object();
74   assert(handle->InternalFieldCount() > 0);
75   Wrap<Type>(handle, ptr);
76   handle_.MarkIndependent();
77   handle_.SetWeak<Type>(ptr, WeakCallback<Type>);
78 }
79
80
81 inline void BaseObject::ClearWeak() {
82   handle_.ClearWeak();
83 }
84
85 }  // namespace node
86
87 #endif  // SRC_BASE_OBJECT_INL_H_