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
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "bindings/v8/V8DOMConfiguration.h"
     31 
     32 #include "bindings/v8/V8Binding.h"
     33 
     34 namespace WebCore {
     35 
     36 void V8DOMConfiguration::batchConfigureAttributes(v8::Handle<v8::ObjectTemplate> instance, v8::Handle<v8::ObjectTemplate> prototype, const BatchedAttribute* attributes, size_t attributeCount, v8::Isolate* isolate, WrapperWorldType currentWorldType)
     37 {
     38     for (size_t i = 0; i < attributeCount; ++i)
     39         configureAttribute(instance, prototype, attributes[i], isolate, currentWorldType);
     40 }
     41 
     42 void V8DOMConfiguration::batchConfigureConstants(v8::Handle<v8::FunctionTemplate> functionDescriptor, v8::Handle<v8::ObjectTemplate> prototype, const BatchedConstant* constants, size_t constantCount, v8::Isolate* isolate)
     43 {
     44     for (size_t i = 0; i < constantCount; ++i) {
     45         const BatchedConstant* constant = &constants[i];
     46         functionDescriptor->Set(v8::String::NewSymbol(constant->name), v8::Integer::New(constant->value, isolate), v8::ReadOnly);
     47         prototype->Set(v8::String::NewSymbol(constant->name), v8::Integer::New(constant->value, isolate), v8::ReadOnly);
     48     }
     49 }
     50 
     51 void V8DOMConfiguration::batchConfigureCallbacks(v8::Handle<v8::ObjectTemplate> prototype, v8::Handle<v8::Signature> signature, v8::PropertyAttribute attributes, const BatchedMethod* callbacks, size_t callbackCount, v8::Isolate*, WrapperWorldType currentWorldType)
     52 {
     53     for (size_t i = 0; i < callbackCount; ++i) {
     54         v8::FunctionCallback callback = callbacks[i].callback;
     55         if (currentWorldType == MainWorld && callbacks[i].callbackForMainWorld)
     56             callback = callbacks[i].callbackForMainWorld;
     57         prototype->Set(v8::String::NewSymbol(callbacks[i].name), v8::FunctionTemplate::New(callback, v8Undefined(), signature, callbacks[i].length), attributes);
     58     }
     59 }
     60 
     61 v8::Local<v8::Signature> V8DOMConfiguration::configureTemplate(v8::Handle<v8::FunctionTemplate> functionDescriptor, const char* interfaceName, v8::Handle<v8::FunctionTemplate> parentClass,
     62     size_t fieldCount, const BatchedAttribute* attributes, size_t attributeCount, const BatchedMethod* callbacks, size_t callbackCount, v8::Isolate* isolate, WrapperWorldType currentWorldType)
     63 {
     64     functionDescriptor->SetClassName(v8::String::NewSymbol(interfaceName));
     65     v8::Local<v8::ObjectTemplate> instance = functionDescriptor->InstanceTemplate();
     66     instance->SetInternalFieldCount(fieldCount);
     67     if (!parentClass.IsEmpty()) {
     68         functionDescriptor->Inherit(parentClass);
     69         // Marks the prototype object as one of native-backed objects.
     70         // This is needed since bug 110436 asks WebKit to tell native-initiated prototypes from pure-JS ones.
     71         // This doesn't mark kinds "root" classes like Node, where setting this changes prototype chain structure.
     72         v8::Local<v8::ObjectTemplate> prototype = functionDescriptor->PrototypeTemplate();
     73         prototype->SetInternalFieldCount(v8PrototypeInternalFieldcount);
     74     }
     75 
     76     if (attributeCount)
     77         batchConfigureAttributes(instance, functionDescriptor->PrototypeTemplate(), attributes, attributeCount, isolate, currentWorldType);
     78     v8::Local<v8::Signature> defaultSignature = v8::Signature::New(functionDescriptor);
     79     if (callbackCount)
     80         batchConfigureCallbacks(functionDescriptor->PrototypeTemplate(), defaultSignature, static_cast<v8::PropertyAttribute>(v8::DontDelete), callbacks, callbackCount, isolate, currentWorldType);
     81     return defaultSignature;
     82 }
     83 
     84 } // namespace WebCore
     85