1/*
2 * Copyright (C) 2010 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 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "DataView.h"
28
29#include "V8ArrayBufferViewCustom.h"
30#include "V8Binding.h"
31#include "V8BindingMacros.h"
32#include "V8DataView.h"
33#include "V8Proxy.h"
34
35namespace WebCore {
36
37v8::Handle<v8::Value> V8DataView::constructorCallback(const v8::Arguments& args)
38{
39    INC_STATS("DOM.DataView.Constructor");
40
41    if (!args.IsConstructCall())
42        return throwError("DOM object constructor cannot be called as a function", V8Proxy::SyntaxError);
43
44    if (args[0]->IsNull() || !V8ArrayBuffer::HasInstance(args[0]))
45        return V8Proxy::throwTypeError();
46    return constructWebGLArrayWithArrayBufferArgument<DataView, char>(args, &info, v8::kExternalByteArray, false);
47}
48
49v8::Handle<v8::Value> toV8(DataView* impl)
50{
51    if (!impl)
52        return v8::Null();
53    return V8DataView::wrap(impl);
54}
55
56v8::Handle<v8::Value> V8DataView::getInt8Callback(const v8::Arguments& args)
57{
58    INC_STATS("DOM.DataView.getInt8");
59    if (args.Length() < 1)
60        return throwError("Not enough arguments", V8Proxy::SyntaxError);
61
62    DataView* imp = V8DataView::toNative(args.Holder());
63    ExceptionCode ec = 0;
64    EXCEPTION_BLOCK(unsigned, byteOffset, toUInt32(args[0]));
65    int8_t result = imp->getInt8(byteOffset, ec);
66    if (UNLIKELY(ec)) {
67        V8Proxy::setDOMException(ec);
68        return v8::Handle<v8::Value>();
69    }
70    return v8::Integer::New(result);
71}
72
73v8::Handle<v8::Value> V8DataView::getUint8Callback(const v8::Arguments& args)
74{
75    INC_STATS("DOM.DataView.getUint8");
76    if (args.Length() < 1)
77        return throwError("Not enough arguments", V8Proxy::SyntaxError);
78
79    DataView* imp = V8DataView::toNative(args.Holder());
80    ExceptionCode ec = 0;
81    EXCEPTION_BLOCK(unsigned, byteOffset, toUInt32(args[0]));
82    uint8_t result = imp->getUint8(byteOffset, ec);
83    if (UNLIKELY(ec)) {
84        V8Proxy::setDOMException(ec);
85        return v8::Handle<v8::Value>();
86    }
87    return v8::Integer::New(result);
88}
89
90v8::Handle<v8::Value> V8DataView::setInt8Callback(const v8::Arguments& args)
91{
92    INC_STATS("DOM.DataView.setInt8");
93    if (args.Length() < 2)
94        return throwError("Not enough arguments", V8Proxy::SyntaxError);
95
96    DataView* imp = V8DataView::toNative(args.Holder());
97    ExceptionCode ec = 0;
98    EXCEPTION_BLOCK(unsigned, byteOffset, toUInt32(args[0]));
99    EXCEPTION_BLOCK(int, value, toInt32(args[1]));
100    imp->setInt8(byteOffset, static_cast<int8_t>(value), ec);
101    if (UNLIKELY(ec))
102        V8Proxy::setDOMException(ec);
103    return v8::Handle<v8::Value>();
104}
105
106v8::Handle<v8::Value> V8DataView::setUint8Callback(const v8::Arguments& args)
107{
108    INC_STATS("DOM.DataView.setUint8");
109    if (args.Length() < 2)
110        return throwError("Not enough arguments", V8Proxy::SyntaxError);
111
112    DataView* imp = V8DataView::toNative(args.Holder());
113    ExceptionCode ec = 0;
114    EXCEPTION_BLOCK(unsigned, byteOffset, toUInt32(args[0]));
115    EXCEPTION_BLOCK(int, value, toInt32(args[1]));
116    imp->setUint8(byteOffset, static_cast<uint8_t>(value), ec);
117    if (UNLIKELY(ec))
118        V8Proxy::setDOMException(ec);
119    return v8::Handle<v8::Value>();
120}
121
122} // namespace WebCore
123