| 1 | /* |
| 2 | * Copyright (C) 2015, 2016 Canon Inc. All rights reserved. |
| 3 | * Copyright (C) 2016 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 | #pragma once |
| 21 | |
| 22 | #include "JSDOMConstructorWithDocument.h" |
| 23 | |
| 24 | namespace WebCore { |
| 25 | |
| 26 | // FIMXE: Why can't named constructors be used with workers? |
| 27 | template<typename JSClass> class JSDOMNamedConstructor : public JSDOMConstructorWithDocument { |
| 28 | public: |
| 29 | using Base = JSDOMConstructorWithDocument; |
| 30 | |
| 31 | static JSDOMNamedConstructor* create(JSC::VM&, JSC::Structure*, JSDOMGlobalObject&); |
| 32 | static JSC::Structure* createStructure(JSC::VM&, JSC::JSGlobalObject&, JSC::JSValue prototype); |
| 33 | |
| 34 | DECLARE_INFO; |
| 35 | |
| 36 | // Must be defined for each specialization class. |
| 37 | static JSC::JSValue prototypeForStructure(JSC::VM&, const JSDOMGlobalObject&); |
| 38 | |
| 39 | private: |
| 40 | JSDOMNamedConstructor(JSC::Structure* structure, JSDOMGlobalObject& globalObject) |
| 41 | : Base(structure, globalObject) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void finishCreation(JSC::VM&, JSDOMGlobalObject&); |
| 46 | static JSC::ConstructType getConstructData(JSC::JSCell*, JSC::ConstructData&); |
| 47 | |
| 48 | // Usually defined for each specialization class. |
| 49 | void initializeProperties(JSC::VM&, JSDOMGlobalObject&) { } |
| 50 | // Must be defined for each specialization class. |
| 51 | static JSC::EncodedJSValue JSC_HOST_CALL construct(JSC::ExecState*); |
| 52 | }; |
| 53 | |
| 54 | template<typename JSClass> inline JSDOMNamedConstructor<JSClass>* JSDOMNamedConstructor<JSClass>::create(JSC::VM& vm, JSC::Structure* structure, JSDOMGlobalObject& globalObject) |
| 55 | { |
| 56 | JSDOMNamedConstructor* constructor = new (NotNull, JSC::allocateCell<JSDOMNamedConstructor>(vm.heap)) JSDOMNamedConstructor(structure, globalObject); |
| 57 | constructor->finishCreation(vm, globalObject); |
| 58 | return constructor; |
| 59 | } |
| 60 | |
| 61 | template<typename JSClass> inline JSC::Structure* JSDOMNamedConstructor<JSClass>::createStructure(JSC::VM& vm, JSC::JSGlobalObject& globalObject, JSC::JSValue prototype) |
| 62 | { |
| 63 | return JSC::Structure::create(vm, &globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info()); |
| 64 | } |
| 65 | |
| 66 | template<typename JSClass> inline void JSDOMNamedConstructor<JSClass>::finishCreation(JSC::VM& vm, JSDOMGlobalObject& globalObject) |
| 67 | { |
| 68 | Base::finishCreation(globalObject); |
| 69 | ASSERT(inherits(vm, info())); |
| 70 | initializeProperties(vm, globalObject); |
| 71 | } |
| 72 | |
| 73 | template<typename JSClass> inline JSC::ConstructType JSDOMNamedConstructor<JSClass>::getConstructData(JSC::JSCell*, JSC::ConstructData& constructData) |
| 74 | { |
| 75 | constructData.native.function = construct; |
| 76 | return JSC::ConstructType::Host; |
| 77 | } |
| 78 | |
| 79 | } // namespace WebCore |
| 80 | |