KJsEmbed

static_binding.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef STATIC_BINDING_H
24 #define STATIC_BINDING_H
25 #include <QHash>
26 
27 #include <kjs/function.h>
28 
29 #include "binding_support.h"
30 
31 #define LengthFlags KJS::DontDelete|KJS::ReadOnly|KJS::DontEnum
32 
33 namespace KJSEmbed
34 {
35 /**
36 * A binding method that is used in VariantBinding and ObjectBinding
37 */
38 class KJSEMBED_EXPORT StaticBinding : public KJS::InternalFunctionImp
39 {
40 public:
41  /**
42  * Create a new method.
43  */
44  StaticBinding(KJS::ExecState *exec, const Method *method);
45  /**
46  * Executes the callback for this method.
47  */
48  KJS::JSValue *callAsFunction(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args) override;
49  bool implementsConstruct() const override
50  {
51  return false;
52  }
53 
54  /**
55  * Publishes an array of Methods to an object. You should only ever need this method
56  * to add methods to a binding.
57  * @param object the object to add the methods to
58  * @param methods an array of Method objects.
59  */
60  static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods);
61 
62 protected:
63  const Method *m_method;
64 };
65 
66 /**
67 * A special method that will create other objects. If you want to have your binding to be able to create
68 * instances of itself it must have at least one of these objects published at the global scope.
69 */
70 class KJSEMBED_EXPORT StaticConstructor : public KJS::InternalFunctionImp
71 {
72 public:
73  /**
74  * Create a new constructor
75  */
76  StaticConstructor(KJS::ExecState *exec, const Constructor *constructor);
77 
78  /**
79  * Add static methods to the object.
80  * @code
81  * KJS::JSObject *ctor = StaticConstructor::add( exec, parent, TestPointer::constructor() ); // Ctor
82  * ctor.addStaticMethods( exec, TestPointer::staticMethods() );
83  * @endcode
84  */
85  void addStaticMethods(KJS::ExecState *exec, const Method *methods);
86 
87  bool implementsConstruct() const override
88  {
89  return true;
90  }
91  /**
92  * Calls the callback that will in turn create a new instance of this object with
93  * the arguments passed in with args.
94  */
95  KJS::JSObject *construct(KJS::ExecState *exec, const KJS::List &args) override;
96  using KJS::JSObject::construct;
97 
98  KJS::JSValue *callAsFunction(KJS::ExecState *exec, KJS::JSObject * /*self*/, const KJS::List &args) override
99  {
100  return construct(exec, args);
101  }
102 
103  void setDefaultValue(KJS::JSValue *value);
104  KJS::JSValue *defaultValue(KJS::ExecState *exec, KJS::JSType hint) const override;
105 
106  /**
107  * Add the constructor to an object. This is usually the global scope.
108  */
109  static KJS::JSObject *add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor);
110  /**
111  * This method is used to construct a KJS value from C++
112  * @code
113  * KJS::List args;
114  * args.append("test");
115  * KJS::Value myType = KJSEmbed::construct(exec, "MyType", args);
116  * @endcode
117  * is equivalent to the following javascript
118  * @code
119  * var myType = new MyType("test");
120  * @endcode
121  */
122  static KJS::JSObject *construct(KJS::ExecState *exec, KJS::JSObject *parent,
123  const KJS::UString &className, const KJS::List &args = KJS::List());
124 
125  static KJS::JSObject *bind(KJS::ExecState *exec, const QString &className, PointerBase &objPtr);
126  static const Method *methods(const KJS::UString &className);
127  static const Constructor *constructor(const KJS::UString &className);
128 
129 protected:
130  const Constructor *m_constructor;
131 
132 private:
133  KJS::JSValue *m_default;
134 
135 };
136 
137 }
138 
139 #endif
140 
A binding method that is used in VariantBinding and ObjectBinding.
A special method that will create other objects.
Method structure.
virtual QVariant callAsFunction(ScriptableExtension *callerPrincipal, quint64 objId, const ArgList &args)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.