tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSByteArray.h
1 /*
2  * Copyright (C) 2009 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 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.
24  */
25
26 #ifndef JSByteArray_h
27 #define JSByteArray_h
28
29 #include "JSObject.h"
30
31 #include <wtf/ByteArray.h>
32
33 namespace JSC {
34
35     class JSByteArray : public JSNonFinalObject {
36         friend class JSGlobalData;
37     public:
38         typedef JSNonFinalObject Base;
39
40         bool canAccessIndex(unsigned i) { return i < m_storage->length(); }
41         JSValue getIndex(ExecState*, unsigned i)
42         {
43             ASSERT(canAccessIndex(i));
44             return jsNumber(m_storage->data()[i]);
45         }
46
47         void setIndex(unsigned i, int value)
48         {
49             ASSERT(canAccessIndex(i));
50             if (value & ~0xFF) {
51                 if (value < 0)
52                     value = 0;
53                 else
54                     value = 255;
55             }
56             m_storage->data()[i] = static_cast<unsigned char>(value);
57         }
58         
59         void setIndex(unsigned i, double value)
60         {
61             ASSERT(canAccessIndex(i));
62             if (!(value > 0)) // Clamp NaN to 0
63                 value = 0;
64             else if (value > 255)
65                 value = 255;
66             m_storage->data()[i] = static_cast<unsigned char>(value + 0.5);
67         }
68         
69         void setIndex(ExecState* exec, unsigned i, JSValue value)
70         {
71             double byteValue = value.toNumber(exec);
72             if (exec->hadException())
73                 return;
74             if (canAccessIndex(i))
75                 setIndex(i, byteValue);
76         }
77
78     private:
79         JSByteArray(ExecState*, Structure*, ByteArray* storage);
80         
81     public:
82         static JSByteArray* create(ExecState* exec, Structure* structure, ByteArray* storage)
83         {
84             JSByteArray* array = new (allocateCell<JSByteArray>(*exec->heap())) JSByteArray(exec, structure, storage);
85             array->finishCreation(exec);
86             return array;
87         }
88
89         static Structure* createStructure(JSGlobalData&, JSGlobalObject*, JSValue prototype, const JSC::ClassInfo* = &s_info);
90
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);
96
97         static void getOwnPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, EnumerationMode);
98
99         static JS_EXPORTDATA const ClassInfo s_info;
100         
101         size_t length() const { return m_storage->length(); }
102
103         WTF::ByteArray* storage() const { return m_storage.get(); }
104
105 #if !ASSERT_DISABLED
106         virtual ~JSByteArray();
107 #endif
108
109         static size_t offsetOfStorage() { return OBJECT_OFFSETOF(JSByteArray, m_storage); }
110
111     protected:
112         static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesGetPropertyNames | JSObject::StructureFlags;
113
114         void finishCreation(ExecState* exec)
115         {
116             Base::finishCreation(exec->globalData());
117             putDirect(exec->globalData(), exec->globalData().propertyNames->length, jsNumber(m_storage->length()), ReadOnly | DontDelete);
118         }
119
120     private:
121         JSByteArray(VPtrStealingHackType)
122             : JSNonFinalObject(VPtrStealingHack)
123         {
124         }
125
126         RefPtr<WTF::ByteArray> m_storage;
127     };
128     
129     JSByteArray* asByteArray(JSValue value);
130     inline JSByteArray* asByteArray(JSValue value)
131     {
132         return static_cast<JSByteArray*>(value.asCell());
133     }
134
135     inline bool isJSByteArray(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsByteArrayVPtr; }
136
137 } // namespace JSC
138
139 #endif // JSByteArray_h