| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #include "wtf/text/AtomicString.h" | 38 #include "wtf/text/AtomicString.h" |
| 39 #include <v8.h> | 39 #include <v8.h> |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 class Node; | 43 class Node; |
| 44 struct WrapperTypeInfo; | 44 struct WrapperTypeInfo; |
| 45 | 45 |
| 46 class V8DOMWrapper { | 46 class V8DOMWrapper { |
| 47 public: | 47 public: |
| 48 static v8::Local<v8::Object> createWrapper(v8::Isolate*, v8::Handle<v8::Obje
ct> creationContext, const WrapperTypeInfo*, ScriptWrappable*); | 48 static v8::Local<v8::Object> createWrapper(v8::Isolate*, v8::Local<v8::Objec
t> creationContext, const WrapperTypeInfo*, ScriptWrappable*); |
| 49 static bool isWrapper(v8::Isolate*, v8::Local<v8::Value>); | 49 static bool isWrapper(v8::Isolate*, v8::Local<v8::Value>); |
| 50 | 50 |
| 51 static v8::Handle<v8::Object> associateObjectWithWrapper(v8::Isolate*, Scrip
tWrappable*, const WrapperTypeInfo*, v8::Handle<v8::Object>); | 51 static v8::Local<v8::Object> associateObjectWithWrapper(v8::Isolate*, Script
Wrappable*, const WrapperTypeInfo*, v8::Local<v8::Object>); |
| 52 static v8::Handle<v8::Object> associateObjectWithWrapper(v8::Isolate*, Node*
, const WrapperTypeInfo*, v8::Handle<v8::Object>); | 52 static v8::Local<v8::Object> associateObjectWithWrapper(v8::Isolate*, Node*,
const WrapperTypeInfo*, v8::Local<v8::Object>); |
| 53 static void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, Sc
riptWrappable*); | 53 static void setNativeInfo(v8::Local<v8::Object>, const WrapperTypeInfo*, Scr
iptWrappable*); |
| 54 static void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*); | 54 static void clearNativeInfo(v8::Local<v8::Object>, const WrapperTypeInfo*); |
| 55 // hasInternalFieldsSet only checks if the value has the internal fields for | 55 // hasInternalFieldsSet only checks if the value has the internal fields for |
| 56 // wrapper obejct and type, and does not check if it's valid or not. The | 56 // wrapper obejct and type, and does not check if it's valid or not. The |
| 57 // value may not be a Blink's wrapper object. In order to make sure of it, | 57 // value may not be a Blink's wrapper object. In order to make sure of it, |
| 58 // Use isWrapper function instead. | 58 // Use isWrapper function instead. |
| 59 static bool hasInternalFieldsSet(v8::Local<v8::Value>); | 59 static bool hasInternalFieldsSet(v8::Local<v8::Value>); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const Wr
apperTypeInfo* wrapperTypeInfo, ScriptWrappable* scriptWrappable) | 62 inline void V8DOMWrapper::setNativeInfo(v8::Local<v8::Object> wrapper, const Wra
pperTypeInfo* wrapperTypeInfo, ScriptWrappable* scriptWrappable) |
| 63 { | 63 { |
| 64 ASSERT(wrapper->InternalFieldCount() >= 2); | 64 ASSERT(wrapper->InternalFieldCount() >= 2); |
| 65 ASSERT(scriptWrappable); | 65 ASSERT(scriptWrappable); |
| 66 ASSERT(wrapperTypeInfo); | 66 ASSERT(wrapperTypeInfo); |
| 67 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, scriptWra
ppable); | 67 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, scriptWra
ppable); |
| 68 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); | 68 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 69 } | 69 } |
| 70 | 70 |
| 71 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const
WrapperTypeInfo* wrapperTypeInfo) | 71 inline void V8DOMWrapper::clearNativeInfo(v8::Local<v8::Object> wrapper, const W
rapperTypeInfo* wrapperTypeInfo) |
| 72 { | 72 { |
| 73 ASSERT(wrapper->InternalFieldCount() >= 2); | 73 ASSERT(wrapper->InternalFieldCount() >= 2); |
| 74 ASSERT(wrapperTypeInfo); | 74 ASSERT(wrapperTypeInfo); |
| 75 // clearNativeInfo() is used only by NP objects, which are not garbage colle
cted. | 75 // clearNativeInfo() is used only by NP objects, which are not garbage colle
cted. |
| 76 ASSERT(wrapperTypeInfo->gcType == WrapperTypeInfo::RefCountedObject); | 76 ASSERT(wrapperTypeInfo->gcType == WrapperTypeInfo::RefCountedObject); |
| 77 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); | 77 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<
WrapperTypeInfo*>(wrapperTypeInfo)); |
| 78 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0); | 78 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0); |
| 79 } | 79 } |
| 80 | 80 |
| 81 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(v8::Isola
te* isolate, ScriptWrappable* impl, const WrapperTypeInfo* wrapperTypeInfo, v8::
Handle<v8::Object> wrapper) | 81 inline v8::Local<v8::Object> V8DOMWrapper::associateObjectWithWrapper(v8::Isolat
e* isolate, ScriptWrappable* impl, const WrapperTypeInfo* wrapperTypeInfo, v8::L
ocal<v8::Object> wrapper) |
| 82 { | 82 { |
| 83 wrapperTypeInfo->refObject(impl); | 83 wrapperTypeInfo->refObject(impl); |
| 84 setNativeInfo(wrapper, wrapperTypeInfo, impl); | 84 setNativeInfo(wrapper, wrapperTypeInfo, impl); |
| 85 ASSERT(hasInternalFieldsSet(wrapper)); | 85 ASSERT(hasInternalFieldsSet(wrapper)); |
| 86 DOMDataStore::setWrapper(impl, wrapper, isolate, wrapperTypeInfo); | 86 DOMDataStore::setWrapper(impl, wrapper, isolate, wrapperTypeInfo); |
| 87 return wrapper; | 87 return wrapper; |
| 88 } | 88 } |
| 89 | 89 |
| 90 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(v8::Isola
te* isolate, Node* node, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::
Object> wrapper) | 90 inline v8::Local<v8::Object> V8DOMWrapper::associateObjectWithWrapper(v8::Isolat
e* isolate, Node* node, const WrapperTypeInfo* wrapperTypeInfo, v8::Local<v8::Ob
ject> wrapper) |
| 91 { | 91 { |
| 92 wrapperTypeInfo->refObject(ScriptWrappable::fromNode(node)); | 92 wrapperTypeInfo->refObject(ScriptWrappable::fromNode(node)); |
| 93 setNativeInfo(wrapper, wrapperTypeInfo, ScriptWrappable::fromNode(node)); | 93 setNativeInfo(wrapper, wrapperTypeInfo, ScriptWrappable::fromNode(node)); |
| 94 ASSERT(hasInternalFieldsSet(wrapper)); | 94 ASSERT(hasInternalFieldsSet(wrapper)); |
| 95 DOMDataStore::setWrapper(node, wrapper, isolate, wrapperTypeInfo); | 95 DOMDataStore::setWrapper(node, wrapper, isolate, wrapperTypeInfo); |
| 96 return wrapper; | 96 return wrapper; |
| 97 } | 97 } |
| 98 | 98 |
| 99 class V8WrapperInstantiationScope { | 99 class V8WrapperInstantiationScope { |
| 100 public: | 100 public: |
| 101 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isol
ate* isolate) | 101 V8WrapperInstantiationScope(v8::Local<v8::Object> creationContext, v8::Isola
te* isolate) |
| 102 : m_didEnterContext(false) | 102 : m_didEnterContext(false) |
| 103 , m_context(isolate->GetCurrentContext()) | 103 , m_context(isolate->GetCurrentContext()) |
| 104 { | 104 { |
| 105 // creationContext should not be empty. Because if we have an | 105 // creationContext should not be empty. Because if we have an |
| 106 // empty creationContext, we will end up creating | 106 // empty creationContext, we will end up creating |
| 107 // a new object in the context currently entered. This is wrong. | 107 // a new object in the context currently entered. This is wrong. |
| 108 RELEASE_ASSERT(!creationContext.IsEmpty()); | 108 RELEASE_ASSERT(!creationContext.IsEmpty()); |
| 109 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationCon
text(); | 109 v8::Local<v8::Context> contextForWrapper = creationContext->CreationCont
ext(); |
| 110 // For performance, we enter the context only if the currently running c
ontext | 110 // For performance, we enter the context only if the currently running c
ontext |
| 111 // is different from the context that we are about to enter. | 111 // is different from the context that we are about to enter. |
| 112 if (contextForWrapper == m_context) | 112 if (contextForWrapper == m_context) |
| 113 return; | 113 return; |
| 114 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper); | 114 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper); |
| 115 m_didEnterContext = true; | 115 m_didEnterContext = true; |
| 116 m_context->Enter(); | 116 m_context->Enter(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 ~V8WrapperInstantiationScope() | 119 ~V8WrapperInstantiationScope() |
| 120 { | 120 { |
| 121 if (!m_didEnterContext) | 121 if (!m_didEnterContext) |
| 122 return; | 122 return; |
| 123 m_context->Exit(); | 123 m_context->Exit(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 v8::Handle<v8::Context> context() const { return m_context; } | 126 v8::Local<v8::Context> context() const { return m_context; } |
| 127 | 127 |
| 128 private: | 128 private: |
| 129 bool m_didEnterContext; | 129 bool m_didEnterContext; |
| 130 v8::Handle<v8::Context> m_context; | 130 v8::Local<v8::Context> m_context; |
| 131 }; | 131 }; |
| 132 | 132 |
| 133 } // namespace blink | 133 } // namespace blink |
| 134 | 134 |
| 135 #endif // V8DOMWrapper_h | 135 #endif // V8DOMWrapper_h |
| OLD | NEW |