Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      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
     13  * distribution.
     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.
     17  *
     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.
     29  */
     30 
     31 #include "config.h"
     32 #include "bindings/v8/V8PerContextData.h"
     33 
     34 #include "bindings/v8/V8Binding.h"
     35 #include "bindings/v8/V8ObjectConstructor.h"
     36 #include "wtf/StringExtras.h"
     37 
     38 #include <stdlib.h>
     39 
     40 namespace WebCore {
     41 
     42 template<typename Map>
     43 static void disposeMapWithUnsafePersistentValues(Map* map)
     44 {
     45     typename Map::iterator it = map->begin();
     46     for (; it != map->end(); ++it)
     47         it->value.dispose();
     48     map->clear();
     49 }
     50 
     51 void V8PerContextData::dispose()
     52 {
     53     v8::HandleScope handleScope(m_isolate);
     54     V8PerContextDataHolder::from(v8::Local<v8::Context>::New(m_isolate, m_context))->setPerContextData(0);
     55 
     56     disposeMapWithUnsafePersistentValues(&m_wrapperBoilerplates);
     57     disposeMapWithUnsafePersistentValues(&m_constructorMap);
     58     m_customElementBindings.clear();
     59 
     60     m_context.Reset();
     61 }
     62 
     63 #define V8_STORE_PRIMORDIAL(name, Name) \
     64 { \
     65     ASSERT(m_##name##Prototype.isEmpty()); \
     66     v8::Handle<v8::String> symbol = v8::String::NewFromUtf8(m_isolate, #Name, v8::String::kInternalizedString); \
     67     if (symbol.IsEmpty()) \
     68         return false; \
     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()) \
     71         return false; \
     72     v8::Handle<v8::Value> prototypeValue = object->Get(prototypeString); \
     73     if (prototypeValue.IsEmpty()) \
     74         return false; \
     75     m_##name##Prototype.set(m_isolate, prototypeValue);  \
     76 }
     77 
     78 bool V8PerContextData::init()
     79 {
     80     v8::Handle<v8::Context> context = v8::Local<v8::Context>::New(m_isolate, m_context);
     81     V8PerContextDataHolder::from(context)->setPerContextData(this);
     82 
     83     v8::Handle<v8::String> prototypeString = v8AtomicString(m_isolate, "prototype");
     84     if (prototypeString.IsEmpty())
     85         return false;
     86 
     87     V8_STORE_PRIMORDIAL(error, Error);
     88 
     89     return true;
     90 }
     91 
     92 #undef V8_STORE_PRIMORDIAL
     93 
     94 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const WrapperTypeInfo* type)
     95 {
     96     ASSERT(!m_errorPrototype.isEmpty());
     97 
     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();
    104     }
    105     return v8::Local<v8::Object>();
    106 }
    107 
    108 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const WrapperTypeInfo* type)
    109 {
    110     ASSERT(!m_errorPrototype.isEmpty());
    111 
    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>();
    119 
    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);
    125     }
    126 
    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 == WrapperTypeErrorPrototype)
    135             prototypeObject->SetPrototype(m_errorPrototype.newLocal(m_isolate));
    136     }
    137 
    138     m_constructorMap.set(type, UnsafePersistent<v8::Function>(m_isolate, function));
    139 
    140     return function;
    141 }
    142 
    143 v8::Local<v8::Object> V8PerContextData::prototypeForType(const WrapperTypeInfo* type)
    144 {
    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>();
    149 }
    150 
    151 void V8PerContextData::addCustomElementBinding(CustomElementDefinition* definition, PassOwnPtr<CustomElementBinding> binding)
    152 {
    153     ASSERT(!m_customElementBindings->contains(definition));
    154     m_customElementBindings->add(definition, binding);
    155 }
    156 
    157 void V8PerContextData::clearCustomElementBinding(CustomElementDefinition* definition)
    158 {
    159     CustomElementBindingMap::iterator it = m_customElementBindings->find(definition);
    160     ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
    161     m_customElementBindings->remove(it);
    162 }
    163 
    164 CustomElementBinding* V8PerContextData::customElementBinding(CustomElementDefinition* definition)
    165 {
    166     CustomElementBindingMap::const_iterator it = m_customElementBindings->find(definition);
    167     ASSERT_WITH_SECURITY_IMPLICATION(it != m_customElementBindings->end());
    168     return it->value.get();
    169 }
    170 
    171 
    172 static v8::Handle<v8::Value> createDebugData(const char* worldName, int debugId, v8::Isolate* isolate)
    173 {
    174     char buffer[32];
    175     unsigned wanted;
    176     if (debugId == -1)
    177         wanted = snprintf(buffer, sizeof(buffer), "%s", worldName);
    178     else
    179         wanted = snprintf(buffer, sizeof(buffer), "%s,%d", worldName, debugId);
    180 
    181     if (wanted < sizeof(buffer))
    182         return v8AtomicString(isolate, buffer);
    183 
    184     return v8::Undefined(isolate);
    185 }
    186 
    187 static v8::Handle<v8::Value> debugData(v8::Handle<v8::Context> context)
    188 {
    189     v8::Context::Scope contextScope(context);
    190     return context->GetEmbedderData(v8ContextDebugIdIndex);
    191 }
    192 
    193 static void setDebugData(v8::Handle<v8::Context> context, v8::Handle<v8::Value> value)
    194 {
    195     v8::Context::Scope contextScope(context);
    196     context->SetEmbedderData(v8ContextDebugIdIndex, value);
    197 }
    198 
    199 bool V8PerContextDebugData::setContextDebugData(v8::Handle<v8::Context> context, const char* worldName, int debugId)
    200 {
    201     if (!debugData(context)->IsUndefined())
    202         return false;
    203     v8::HandleScope scope(context->GetIsolate());
    204     v8::Handle<v8::Value> debugData = createDebugData(worldName, debugId, context->GetIsolate());
    205     setDebugData(context, debugData);
    206     return true;
    207 }
    208 
    209 int V8PerContextDebugData::contextDebugId(v8::Handle<v8::Context> context)
    210 {
    211     v8::HandleScope scope(context->GetIsolate());
    212     v8::Handle<v8::Value> data = debugData(context);
    213 
    214     if (!data->IsString())
    215         return -1;
    216     v8::String::Utf8Value utf8(data);
    217     char* comma = strnstr(*utf8, ",", utf8.length());
    218     if (!comma)
    219         return -1;
    220     return atoi(comma + 1);
    221 }
    222 
    223 } // namespace WebCore
    224