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