1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #ifndef SRC_UTIL_INL_H_
23 #define SRC_UTIL_INL_H_
31 template <class TypeName>
32 inline v8::Local<TypeName> PersistentToLocal(
34 const v8::Persistent<TypeName>& persistent) {
35 if (persistent.IsWeak()) {
36 return WeakPersistentToLocal(isolate, persistent);
38 return StrongPersistentToLocal(persistent);
42 template <class TypeName>
43 inline v8::Local<TypeName> StrongPersistentToLocal(
44 const v8::Persistent<TypeName>& persistent) {
45 return *reinterpret_cast<v8::Local<TypeName>*>(
46 const_cast<v8::Persistent<TypeName>*>(&persistent));
49 template <class TypeName>
50 inline v8::Local<TypeName> WeakPersistentToLocal(
52 const v8::Persistent<TypeName>& persistent) {
53 return v8::Local<TypeName>::New(isolate, persistent);
56 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
59 return v8::String::NewFromOneByte(isolate,
60 reinterpret_cast<const uint8_t*>(data),
61 v8::String::kNormalString,
65 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
66 const signed char* data,
68 return v8::String::NewFromOneByte(isolate,
69 reinterpret_cast<const uint8_t*>(data),
70 v8::String::kNormalString,
74 inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
75 const unsigned char* data,
77 return v8::String::NewFromOneByte(isolate,
78 reinterpret_cast<const uint8_t*>(data),
79 v8::String::kNormalString,
83 template <typename TypeName>
84 void Wrap(v8::Local<v8::Object> object, TypeName* pointer) {
85 assert(!object.IsEmpty());
86 assert(object->InternalFieldCount() > 0);
87 object->SetAlignedPointerInInternalField(0, pointer);
90 template <typename TypeName>
91 TypeName* Unwrap(v8::Local<v8::Object> object) {
92 assert(!object.IsEmpty());
93 assert(object->InternalFieldCount() > 0);
94 void* pointer = object->GetAlignedPointerFromInternalField(0);
95 return static_cast<TypeName*>(pointer);
100 #endif // SRC_UTIL_INL_H_