2 * Copyright (C) 2009 Apple 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
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
31 #include <wtf/ByteArray.h>
35 class JSByteArray : public JSNonFinalObject {
36 friend class JSGlobalData;
38 typedef JSNonFinalObject Base;
40 bool canAccessIndex(unsigned i) { return i < m_storage->length(); }
41 JSValue getIndex(ExecState*, unsigned i)
43 ASSERT(canAccessIndex(i));
44 return jsNumber(m_storage->data()[i]);
47 void setIndex(unsigned i, int value)
49 ASSERT(canAccessIndex(i));
56 m_storage->data()[i] = static_cast<unsigned char>(value);
59 void setIndex(unsigned i, double value)
61 ASSERT(canAccessIndex(i));
62 if (!(value > 0)) // Clamp NaN to 0
66 m_storage->data()[i] = static_cast<unsigned char>(value + 0.5);
69 void setIndex(ExecState* exec, unsigned i, JSValue value)
71 double byteValue = value.toNumber(exec);
72 if (exec->hadException())
74 if (canAccessIndex(i))
75 setIndex(i, byteValue);
79 JSByteArray(ExecState*, Structure*, ByteArray* storage);
82 static JSByteArray* create(ExecState* exec, Structure* structure, ByteArray* storage)
84 JSByteArray* array = new (allocateCell<JSByteArray>(*exec->heap())) JSByteArray(exec, structure, storage);
85 array->finishCreation(exec);
89 static Structure* createStructure(JSGlobalData&, JSGlobalObject*, JSValue prototype, const JSC::ClassInfo* = &s_info);
91 static bool getOwnPropertySlot(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);
92 static bool getOwnPropertySlotByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);
93 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&);
94 static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);
95 static void putByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::JSValue);
97 static void getOwnPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, EnumerationMode);
99 static JS_EXPORTDATA const ClassInfo s_info;
101 size_t length() const { return m_storage->length(); }
103 WTF::ByteArray* storage() const { return m_storage.get(); }
106 virtual ~JSByteArray();
109 static size_t offsetOfStorage() { return OBJECT_OFFSETOF(JSByteArray, m_storage); }
112 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesGetPropertyNames | JSObject::StructureFlags;
114 void finishCreation(ExecState* exec)
116 Base::finishCreation(exec->globalData());
117 putDirect(exec->globalData(), exec->globalData().propertyNames->length, jsNumber(m_storage->length()), ReadOnly | DontDelete);
121 JSByteArray(VPtrStealingHackType)
122 : JSNonFinalObject(VPtrStealingHack)
126 RefPtr<WTF::ByteArray> m_storage;
129 JSByteArray* asByteArray(JSValue value);
130 inline JSByteArray* asByteArray(JSValue value)
132 return static_cast<JSByteArray*>(value.asCell());
135 inline bool isJSByteArray(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsByteArrayVPtr; }
139 #endif // JSByteArray_h