2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/V8PerContextData.h"
34 #include "bindings/v8/V8Binding.h"
35 #include "bindings/v8/V8ObjectConstructor.h"
36 #include "wtf/StringExtras.h"
42 template<typename Map>
43 static void disposeMapWithUnsafePersistentValues(Map* map)
45 typename Map::iterator it = map->begin();
46 for (; it != map->end(); ++it)
51 void V8PerContextData::dispose()
53 v8::HandleScope handleScope(m_isolate);
54 V8PerContextDataHolder::from(v8::Local<v8::Context>::New(m_isolate, m_context))->setPerContextData(0);
56 disposeMapWithUnsafePersistentValues(&m_wrapperBoilerplates);
57 disposeMapWithUnsafePersistentValues(&m_constructorMap);
58 m_customElementBindings.clear();
63 #define V8_STORE_PRIMORDIAL(name, Name) \
65 ASSERT(m_##name##Prototype.isEmpty()); \
66 v8::Handle<v8::String> symbol = v8AtomicString(m_isolate, #Name); \
67 if (symbol.IsEmpty()) \
69 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(v8::Local<v8::Context>::New(m_isolate, m_context)->Global()->Get(symbol)); \
70 if (object.IsEmpty()) \
72 v8::Handle<v8::Value> prototypeValue = object->Get(prototypeString); \
73 if (prototypeValue.IsEmpty()) \
75 m_##name##Prototype.set(m_isolate, prototypeValue); \
78 bool V8PerContextData::init()
80 v8::Handle<v8::Context> context = v8::Local<v8::Context>::New(m_isolate, m_context);
81 V8PerContextDataHolder::from(context)->setPerContextData(this);
83 v8::Handle<v8::String> prototypeString = v8AtomicString(m_isolate, "prototype");
84 if (prototypeString.IsEmpty())
87 V8_STORE_PRIMORDIAL(error, Error);
92 #undef V8_STORE_PRIMORDIAL
94 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const WrapperTypeInfo* type)
96 ASSERT(!m_errorPrototype.isEmpty());
98 v8::Context::Scope scope(v8::Local<v8::Context>::New(m_isolate, m_context));
99 v8::Local<v8::Function> function = constructorForType(type);
100 v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(function);
101 if (!instanceTemplate.IsEmpty()) {
102 m_wrapperBoilerplates.set(type, UnsafePersistent<v8::Object>(m_isolate, instanceTemplate));
103 return instanceTemplate->Clone();
105 return v8::Local<v8::Object>();
108 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const WrapperTypeInfo* type)
110 ASSERT(!m_errorPrototype.isEmpty());
112 v8::Context::Scope scope(v8::Local<v8::Context>::New(m_isolate, m_context));
113 v8::Handle<v8::FunctionTemplate> functionTemplate = type->domTemplate(m_isolate, worldType(m_isolate));
114 // Getting the function might fail if we're running out of stack or memory.
115 v8::TryCatch tryCatch;
116 v8::Local<v8::Function> function = functionTemplate->GetFunction();
117 if (function.IsEmpty())
118 return v8::Local<v8::Function>();
120 if (type->parentClass) {
121 v8::Local<v8::Object> prototypeTemplate = constructorForType(type->parentClass);
122 if (prototypeTemplate.IsEmpty())
123 return v8::Local<v8::Function>();
124 function->SetPrototype(prototypeTemplate);
127 v8::Local<v8::Value> prototypeValue = function->Get(v8AtomicString(m_isolate, "prototype"));
128 if (!prototypeValue.IsEmpty() && prototypeValue->IsObject()) {
129 v8::Local<v8::Object> prototypeObject = v8::Local<v8::Object>::Cast(prototypeValue);
130 if (prototypeObject->InternalFieldCount() == v8PrototypeInternalFieldcount
131 && type->wrapperTypePrototype == WrapperTypeObjectPrototype)
132 prototypeObject->SetAlignedPointerInInternalField(v8PrototypeTypeIndex, const_cast<WrapperTypeInfo*>(type));
133 type->installPerContextEnabledMethods(prototypeObject, m_isolate);
134 if (type->wrapperTypePrototype == WrapperTypeExceptionPrototype)
135 prototypeObject->SetPrototype(m_errorPrototype.newLocal(m_isolate));
138 m_constructorMap.set(type, UnsafePersistent<v8::Function>(m_isolate, function));
143 v8::Local<v8::Object> V8PerContextData::prototypeForType(const WrapperTypeInfo* type)
145 v8::Local<v8::Object> constructor = constructorForType(type);
146 if (constructor.IsEmpty())
147 return v8::Local<v8::Object>();
148 return constructor->Get(v8String(m_isolate, "prototype")).As<v8::Object>();
151 void V8PerContextData::addCustomElementBinding(CustomElementDefinition* definition, PassOwnPtr<CustomElementBinding> binding)
153 ASSERT(!m_customElementBindings->contains(definition));
154 m_customElementBindings->add(definition, binding);
157 void V8PerContextData::clearCustomElementBinding(CustomElementDefinition* definition)
159 CustomElementBindingMap::iterator it = m_customElementBindings->find(definition);
160 ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
161 m_customElementBindings->remove(it);
164 CustomElementBinding* V8PerContextData::customElementBinding(CustomElementDefinition* definition)
166 CustomElementBindingMap::const_iterator it = m_customElementBindings->find(definition);
167 ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
168 return it->value.get();
172 static v8::Handle<v8::Value> createDebugData(const char* worldName, int debugId, v8::Isolate* isolate)
177 wanted = snprintf(buffer, sizeof(buffer), "%s", worldName);
179 wanted = snprintf(buffer, sizeof(buffer), "%s,%d", worldName, debugId);
181 if (wanted < sizeof(buffer))
182 return v8AtomicString(isolate, buffer);
184 return v8::Undefined(isolate);
187 static v8::Handle<v8::Value> debugData(v8::Handle<v8::Context> context)
189 v8::Context::Scope contextScope(context);
190 return context->GetEmbedderData(v8ContextDebugIdIndex);
193 static void setDebugData(v8::Handle<v8::Context> context, v8::Handle<v8::Value> value)
195 v8::Context::Scope contextScope(context);
196 context->SetEmbedderData(v8ContextDebugIdIndex, value);
199 bool V8PerContextDebugData::setContextDebugData(v8::Handle<v8::Context> context, const char* worldName, int debugId)
201 if (!debugData(context)->IsUndefined())
203 v8::HandleScope scope(context->GetIsolate());
204 v8::Handle<v8::Value> debugData = createDebugData(worldName, debugId, context->GetIsolate());
205 setDebugData(context, debugData);
209 int V8PerContextDebugData::contextDebugId(v8::Handle<v8::Context> context)
211 v8::HandleScope scope(context->GetIsolate());
212 v8::Handle<v8::Value> data = debugData(context);
214 if (!data->IsString())
216 v8::String::Utf8Value utf8(data);
217 char* comma = strnstr(*utf8, ",", utf8.length());
220 return atoi(comma + 1);
223 } // namespace WebCore