File indexing completed on 2024-07-14 14:35:15

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef STATIC_BINDING_H
0024 #define STATIC_BINDING_H
0025 #include <QHash>
0026 
0027 #include <kjs/function.h>
0028 
0029 #include "binding_support.h"
0030 
0031 #define LengthFlags KJS::DontDelete|KJS::ReadOnly|KJS::DontEnum
0032 
0033 namespace KJSEmbed
0034 {
0035 /**
0036 * A binding method that is used in VariantBinding and ObjectBinding
0037 */
0038 class KJSEMBED_EXPORT StaticBinding : public KJS::InternalFunctionImp
0039 {
0040 public:
0041     /**
0042     * Create a new method.
0043     */
0044     StaticBinding(KJS::ExecState *exec, const Method *method);
0045     /**
0046     * Executes the callback for this method.
0047     */
0048     KJS::JSValue *callAsFunction(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args) override;
0049     bool implementsConstruct() const override
0050     {
0051         return false;
0052     }
0053 
0054     /**
0055     * Publishes an array of Methods to an object.  You should only ever need this method
0056     * to add methods to a binding.
0057     * @param object the object to add the methods to
0058     * @param methods an array of Method objects.
0059     */
0060     static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods);
0061 
0062 protected:
0063     const Method *m_method;
0064 };
0065 
0066 /**
0067 * A special method that will create other objects. If you want to have your binding to be able to create
0068 * instances of itself it must have at least one of these objects published at the global scope.
0069 */
0070 class KJSEMBED_EXPORT StaticConstructor : public KJS::InternalFunctionImp
0071 {
0072 public:
0073     /**
0074     * Create a new constructor
0075     */
0076     StaticConstructor(KJS::ExecState *exec, const Constructor *constructor);
0077 
0078     /**
0079     * Add static methods to the object.
0080     * @code
0081     * KJS::JSObject *ctor = StaticConstructor::add( exec, parent, TestPointer::constructor() ); // Ctor
0082     * ctor.addStaticMethods( exec, TestPointer::staticMethods() );
0083     * @endcode
0084     */
0085     void addStaticMethods(KJS::ExecState *exec, const Method *methods);
0086 
0087     bool implementsConstruct() const override
0088     {
0089         return true;
0090     }
0091     /**
0092     * Calls the callback that will in turn create a new instance of this object with
0093     * the arguments passed in with args.
0094     */
0095     KJS::JSObject *construct(KJS::ExecState *exec, const KJS::List &args) override;
0096     using KJS::JSObject::construct;
0097 
0098     KJS::JSValue *callAsFunction(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args) override
0099     {
0100         return construct(exec, args);
0101     }
0102 
0103     void setDefaultValue(KJS::JSValue *value);
0104     KJS::JSValue *defaultValue(KJS::ExecState *exec, KJS::JSType hint) const override;
0105 
0106     /**
0107     * Add the constructor to an object.  This is usually the global scope.
0108     */
0109     static KJS::JSObject *add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor);
0110     /**
0111     * This method is used to construct a KJS value from C++
0112     * @code
0113     * KJS::List args;
0114     * args.append("test");
0115     * KJS::Value myType = KJSEmbed::construct(exec, "MyType", args);
0116     * @endcode
0117     * is equivalent to the following javascript
0118     * @code
0119     * var myType = new MyType("test");
0120     * @endcode
0121     */
0122     static KJS::JSObject *construct(KJS::ExecState *exec, KJS::JSObject *parent,
0123                                     const KJS::UString &className, const KJS::List &args = KJS::List());
0124 
0125     static KJS::JSObject *bind(KJS::ExecState *exec, const QString &className, PointerBase &objPtr);
0126     static const Method *methods(const KJS::UString &className);
0127     static const Constructor *constructor(const KJS::UString &className);
0128 
0129 protected:
0130     const Constructor *m_constructor;
0131 
0132 private:
0133     KJS::JSValue *m_default;
0134 
0135 };
0136 
0137 }
0138 
0139 #endif
0140