1/*
2 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 *  Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include "config.h"
21#include "JSImageConstructor.h"
22
23#include "HTMLImageElement.h"
24#include "HTMLNames.h"
25#include "JSHTMLImageElement.h"
26#include "JSNode.h"
27#include <runtime/Error.h>
28
29using namespace JSC;
30
31namespace WebCore {
32
33ASSERT_HAS_TRIVIAL_DESTRUCTOR(JSImageConstructor);
34
35const ClassInfo JSImageConstructor::s_info = { "ImageConstructor", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSImageConstructor) };
36
37JSImageConstructor::JSImageConstructor(Structure* structure, JSDOMGlobalObject* globalObject)
38    : DOMConstructorWithDocument(structure, globalObject)
39{
40}
41
42void JSImageConstructor::finishCreation(ExecState* exec, JSDOMGlobalObject* globalObject)
43{
44    Base::finishCreation(globalObject);
45    ASSERT(inherits(&s_info));
46    putDirect(exec->vm(), exec->propertyNames().prototype, JSHTMLImageElementPrototype::self(exec, globalObject), None);
47}
48
49static EncodedJSValue JSC_HOST_CALL constructImage(ExecState* exec)
50{
51    JSImageConstructor* jsConstructor = jsCast<JSImageConstructor*>(exec->callee());
52    Document* document = jsConstructor->document();
53    if (!document)
54        return throwVMError(exec, createReferenceError(exec, "Image constructor associated document is unavailable"));
55
56    // Calling toJS on the document causes the JS document wrapper to be
57    // added to the window object. This is done to ensure that JSDocument::visit
58    // will be called, which will cause the image element to be marked if necessary.
59    toJS(exec, jsConstructor->globalObject(), document);
60    int width;
61    int height;
62    int* optionalWidth = 0;
63    int* optionalHeight = 0;
64    if (exec->argumentCount() > 0) {
65        width = exec->argument(0).toInt32(exec);
66        optionalWidth = &width;
67    }
68    if (exec->argumentCount() > 1) {
69        height = exec->argument(1).toInt32(exec);
70        optionalHeight = &height;
71    }
72
73    return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(),
74        HTMLImageElement::createForJSConstructor(document, optionalWidth, optionalHeight))));
75}
76
77ConstructType JSImageConstructor::getConstructData(JSCell*, ConstructData& constructData)
78{
79    constructData.native.function = constructImage;
80    return ConstructTypeHost;
81}
82
83} // namespace WebCore
84