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 
29 using namespace JSC;
30 
31 namespace WebCore {
32 
33 ASSERT_CLASS_FITS_IN_CELL(JSImageConstructor);
34 
35 const ClassInfo JSImageConstructor::s_info = { "ImageConstructor", &DOMConstructorWithDocument::s_info, 0, 0 };
36 
JSImageConstructor(ExecState * exec,Structure * structure,JSDOMGlobalObject * globalObject)37 JSImageConstructor::JSImageConstructor(ExecState* exec, Structure* structure, JSDOMGlobalObject* globalObject)
38     : DOMConstructorWithDocument(structure, globalObject)
39 {
40     ASSERT(inherits(&s_info));
41     putDirect(exec->globalData(), exec->propertyNames().prototype, JSHTMLImageElementPrototype::self(exec, globalObject), None);
42 }
43 
constructImage(ExecState * exec)44 static EncodedJSValue JSC_HOST_CALL constructImage(ExecState* exec)
45 {
46     JSImageConstructor* jsConstructor = static_cast<JSImageConstructor*>(exec->callee());
47     Document* document = jsConstructor->document();
48     if (!document)
49         return throwVMError(exec, createReferenceError(exec, "Image constructor associated document is unavailable"));
50 
51     // Calling toJS on the document causes the JS document wrapper to be
52     // added to the window object. This is done to ensure that JSDocument::visit
53     // will be called, which will cause the image element to be marked if necessary.
54     toJS(exec, jsConstructor->globalObject(), document);
55     int width;
56     int height;
57     int* optionalWidth = 0;
58     int* optionalHeight = 0;
59     if (exec->argumentCount() > 0) {
60         width = exec->argument(0).toInt32(exec);
61         optionalWidth = &width;
62     }
63     if (exec->argumentCount() > 1) {
64         height = exec->argument(1).toInt32(exec);
65         optionalHeight = &height;
66     }
67 
68     return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(),
69         HTMLImageElement::createForJSConstructor(document, optionalWidth, optionalHeight))));
70 }
71 
getConstructData(ConstructData & constructData)72 ConstructType JSImageConstructor::getConstructData(ConstructData& constructData)
73 {
74     constructData.native.function = constructImage;
75     return ConstructTypeHost;
76 }
77 
78 } // namespace WebCore
79