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 "ByteArray.h"
|
---|
30 | #include "JSObject.h"
|
---|
31 |
|
---|
32 | namespace JSC {
|
---|
33 |
|
---|
34 | class JSByteArray : public JSObject {
|
---|
35 | friend class Interpreter;
|
---|
36 | public:
|
---|
37 | bool canAccessIndex(unsigned i) { return i < m_storage->length(); }
|
---|
38 | JSValuePtr getIndex(ExecState* exec, unsigned i)
|
---|
39 | {
|
---|
40 | ASSERT(canAccessIndex(i));
|
---|
41 | return jsNumber(exec, m_storage->data()[i]);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void setIndex(unsigned i, int value)
|
---|
45 | {
|
---|
46 | ASSERT(canAccessIndex(i));
|
---|
47 | if (value & ~0xFF) {
|
---|
48 | if (value < 0)
|
---|
49 | value = 0;
|
---|
50 | else
|
---|
51 | value = 255;
|
---|
52 | }
|
---|
53 | m_storage->data()[i] = static_cast<unsigned char>(value);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void setIndex(unsigned i, double value)
|
---|
57 | {
|
---|
58 | ASSERT(canAccessIndex(i));
|
---|
59 | if (!(value > 0)) // Clamp NaN to 0
|
---|
60 | value = 0;
|
---|
61 | else if (value > 255)
|
---|
62 | value = 255;
|
---|
63 | m_storage->data()[i] = static_cast<unsigned char>(value + 0.5);
|
---|
64 | }
|
---|
65 |
|
---|
66 | void setIndex(ExecState* exec, unsigned i, JSValuePtr value)
|
---|
67 | {
|
---|
68 | double byteValue = value->toNumber(exec);
|
---|
69 | if (exec->hadException())
|
---|
70 | return;
|
---|
71 | if (canAccessIndex(i))
|
---|
72 | setIndex(i, byteValue);
|
---|
73 | }
|
---|
74 |
|
---|
75 | JSByteArray(ExecState* exec, PassRefPtr<Structure>, ByteArray* storage, const JSC::ClassInfo* = &s_defaultInfo);
|
---|
76 | static PassRefPtr<Structure> createStructure(JSValuePtr prototype);
|
---|
77 |
|
---|
78 | virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);
|
---|
79 | virtual bool getOwnPropertySlot(JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);
|
---|
80 | virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValuePtr, JSC::PutPropertySlot&);
|
---|
81 | virtual void put(JSC::ExecState*, unsigned propertyName, JSC::JSValuePtr);
|
---|
82 |
|
---|
83 | virtual void getPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&);
|
---|
84 |
|
---|
85 | virtual const ClassInfo* classInfo() const { return m_classInfo; }
|
---|
86 | static const ClassInfo s_defaultInfo;
|
---|
87 |
|
---|
88 | size_t length() const { return m_storage->length(); }
|
---|
89 |
|
---|
90 | private:
|
---|
91 | enum VPtrStealingHackType { VPtrStealingHack };
|
---|
92 | JSByteArray(VPtrStealingHackType)
|
---|
93 | : JSObject(createStructure(jsNull()))
|
---|
94 | , m_classInfo(0)
|
---|
95 | {
|
---|
96 | }
|
---|
97 |
|
---|
98 | RefPtr<ByteArray> m_storage;
|
---|
99 | const ClassInfo* m_classInfo;
|
---|
100 | };
|
---|
101 |
|
---|
102 | JSByteArray* asByteArray(JSValuePtr value);
|
---|
103 | inline JSByteArray* asByteArray(JSValuePtr value)
|
---|
104 | {
|
---|
105 | return static_cast<JSByteArray*>(asCell(value));
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endif
|
---|