Home | History | Annotate | Download | only in bridge
      1 /*
      2  * Copyright (C) 2003, 2008 Apple 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  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "runtime_array.h"
     28 
     29 #include <runtime/ArrayPrototype.h>
     30 #include <runtime/Error.h>
     31 #include <runtime/PropertyNameArray.h>
     32 #include "JSDOMBinding.h"
     33 
     34 using namespace WebCore;
     35 
     36 namespace JSC {
     37 
     38 const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &JSArray::s_info, 0, 0 };
     39 
     40 RuntimeArray::RuntimeArray(ExecState* exec, Bindings::Array* array)
     41     // FIXME: deprecatedGetDOMStructure uses the prototype off of the wrong global object
     42     // We need to pass in the right global object for "array".
     43     : JSArray(exec->globalData(), deprecatedGetDOMStructure<RuntimeArray>(exec))
     44 {
     45     ASSERT(inherits(&s_info));
     46     setSubclassData(array);
     47 }
     48 
     49 RuntimeArray::~RuntimeArray()
     50 {
     51     delete getConcreteArray();
     52 }
     53 
     54 JSValue RuntimeArray::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
     55 {
     56     RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
     57     return jsNumber(thisObj->getLength());
     58 }
     59 
     60 JSValue RuntimeArray::indexGetter(ExecState* exec, JSValue slotBase, unsigned index)
     61 {
     62     RuntimeArray* thisObj = static_cast<RuntimeArray*>(asObject(slotBase));
     63     return thisObj->getConcreteArray()->valueAt(exec, index);
     64 }
     65 
     66 void RuntimeArray::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
     67 {
     68     unsigned length = getLength();
     69     for (unsigned i = 0; i < length; ++i)
     70         propertyNames.add(Identifier::from(exec, i));
     71 
     72     if (mode == IncludeDontEnumProperties)
     73         propertyNames.add(exec->propertyNames().length);
     74 
     75     JSObject::getOwnPropertyNames(exec, propertyNames, mode);
     76 }
     77 
     78 bool RuntimeArray::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
     79 {
     80     if (propertyName == exec->propertyNames().length) {
     81         slot.setCacheableCustom(this, lengthGetter);
     82         return true;
     83     }
     84 
     85     bool ok;
     86     unsigned index = propertyName.toArrayIndex(ok);
     87     if (ok) {
     88         if (index < getLength()) {
     89             slot.setCustomIndex(this, index, indexGetter);
     90             return true;
     91         }
     92     }
     93 
     94     return JSObject::getOwnPropertySlot(exec, propertyName, slot);
     95 }
     96 
     97 bool RuntimeArray::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
     98 {
     99     if (propertyName == exec->propertyNames().length) {
    100         PropertySlot slot;
    101         slot.setCustom(this, lengthGetter);
    102         descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
    103         return true;
    104     }
    105 
    106     bool ok;
    107     unsigned index = propertyName.toArrayIndex(ok);
    108     if (ok) {
    109         if (index < getLength()) {
    110             PropertySlot slot;
    111             slot.setCustomIndex(this, index, indexGetter);
    112             descriptor.setDescriptor(slot.getValue(exec, propertyName), DontDelete | DontEnum);
    113             return true;
    114         }
    115     }
    116 
    117     return JSObject::getOwnPropertyDescriptor(exec, propertyName, descriptor);
    118 }
    119 
    120 bool RuntimeArray::getOwnPropertySlot(ExecState *exec, unsigned index, PropertySlot& slot)
    121 {
    122     if (index < getLength()) {
    123         slot.setCustomIndex(this, index, indexGetter);
    124         return true;
    125     }
    126 
    127     return JSObject::getOwnPropertySlot(exec, index, slot);
    128 }
    129 
    130 void RuntimeArray::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
    131 {
    132     if (propertyName == exec->propertyNames().length) {
    133         throwError(exec, createRangeError(exec, "Range error"));
    134         return;
    135     }
    136 
    137     bool ok;
    138     unsigned index = propertyName.toArrayIndex(ok);
    139     if (ok) {
    140         getConcreteArray()->setValueAt(exec, index, value);
    141         return;
    142     }
    143 
    144     JSObject::put(exec, propertyName, value, slot);
    145 }
    146 
    147 void RuntimeArray::put(ExecState* exec, unsigned index, JSValue value)
    148 {
    149     if (index >= getLength()) {
    150         throwError(exec, createRangeError(exec, "Range error"));
    151         return;
    152     }
    153 
    154     getConcreteArray()->setValueAt(exec, index, value);
    155 }
    156 
    157 bool RuntimeArray::deleteProperty(ExecState*, const Identifier&)
    158 {
    159     return false;
    160 }
    161 
    162 bool RuntimeArray::deleteProperty(ExecState*, unsigned)
    163 {
    164     return false;
    165 }
    166 
    167 }
    168