• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • kjsembed
  • kjsembed
static_binding.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
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#include "static_binding.h"
23#include <kjs/interpreter.h>
24#include <kjs/function_object.h>
25#include <QtCore/QDebug>
26
27namespace KJSEmbed {
28 static QHash<QString,const Constructor*> g_ctorHash;
29}
30
31using namespace KJSEmbed;
32
33StaticBinding::StaticBinding(KJS::ExecState *exec, const Method *method )
34 : KJS::InternalFunctionImp(static_cast<KJS::FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()),
35 method->name),
36 m_method(method)
37{
38 putDirect( exec->propertyNames().length, m_method->argc, LengthFlags );
39}
40
41KJS::JSValue *StaticBinding::callAsFunction( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args )
42{
43 if( m_method->call == 0 )
44 {
45 //throwError(exec, "Bad method id"); // NOTE: fix
46 KJS::throwError(exec, KJS::GeneralError, "Bad method id");
47 return KJS::jsNull();
48 }
49
50 KJS::JSValue *retValue = (*m_method->call)(exec,self,args);
51
52 if( exec->hadException() )
53 {
54 return KJS::jsNull();
55 }
56 return retValue;
57
58}
59
60void StaticBinding::publish( KJS::ExecState *exec, KJS::JSObject *object, const Method *methods )
61{
62 int idx = 0;
63 while( methods[idx].name != 0 )
64 {
65 object->put(exec, methods[idx].name, new StaticBinding(exec, &methods[idx]), methods[idx].flags);
66 idx++;
67 }
68}
69
70StaticConstructor::StaticConstructor(KJS::ExecState *exec, const Constructor *constructor )
71 : KJS::InternalFunctionImp(static_cast<KJS::FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()),
72 constructor->name),
73 m_constructor( constructor )
74{
75 putDirect( exec->propertyNames().length, m_constructor->argc, LengthFlags );
76 m_default = KJS::jsNull();
77}
78
79KJS::JSObject *StaticConstructor::construct( KJS::ExecState *exec, const KJS::List &args )
80{
81 return (*m_constructor->construct)(exec,args);
82}
83
84void StaticConstructor::setDefaultValue( KJS::JSValue *value )
85{
86 m_default = value;
87}
88
89KJS::JSValue *StaticConstructor::defaultValue( KJS::ExecState * exec, KJS::JSType hint ) const
90{
91 Q_UNUSED(exec);
92 Q_UNUSED(hint);
93 return m_default;
94}
95
96KJS::JSObject *StaticConstructor::add( KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor )
97{
98 KJS::JSObject *obj = new StaticConstructor(exec, constructor );
99 object->put(exec, constructor->name, obj);
100 if( constructor->staticMethods )
101 {
102 StaticBinding::publish( exec, obj, constructor->staticMethods );
103 }
104 /* crashes for some reason */
105 if( constructor->enumerators )
106 {
107 int idx = 0;
108 while( constructor->enumerators[idx].name != 0 )
109 {
110 obj->put( exec, constructor->enumerators[idx].name,
111 KJS::jsNumber(constructor->enumerators[idx].value), KJS::DontDelete|KJS::ReadOnly);
112 idx++;
113 }
114 }
115 // publish methods
116 KJSEmbed::g_ctorHash[constructor->name] = constructor;
117 return obj;
118}
119
120const Method *StaticConstructor::methods( const KJS::UString &className )
121{
122 return KJSEmbed::g_ctorHash[toQString(className)]->methods;
123}
124
125const Constructor *StaticConstructor::constructor( const KJS::UString &className )
126{
127 return KJSEmbed::g_ctorHash[toQString(className)];
128}
129
130KJS::JSObject* StaticConstructor::bind(KJS::ExecState* exec, const QString& className, PointerBase& objPtr)
131{
132 KJSEmbed::callBind mybind = KJSEmbed::g_ctorHash[className]->bind;
133// qDebug() << "StaticConstructor::bind() className=" << className << " mybind=" << mybind;
134 if (mybind)
135 return (*mybind)(exec, objPtr);
136
137 return 0;
138}
139
140KJS::JSObject *StaticConstructor::construct( KJS::ExecState *exec, KJS::JSObject *parent, const KJS::UString &className, const KJS::List &args )
141{
142// qDebug("StaticConstructor::construct('%s')", className.ascii() );
143 if( parent->hasProperty( exec, className.ascii() ) )
144 {
145 KJS::JSObject *ctor = parent->get(exec,className.ascii())->toObject(exec);
146 if( ctor )
147 {
148 return ctor->construct( exec, args );
149 }
150 }
151 qDebug("cannot create '%s'", className.ascii() );
152 return KJS::throwError( exec, KJS::TypeError, toUString(QString("Cannot create %1 objects from javascript.").arg(toQString(className)) ));
153}
154
155//kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::StaticBinding
A binding method that is used in VariantBinding and ObjectBinding.
Definition: static_binding.h:40
KJSEmbed::StaticBinding::callAsFunction
KJS::JSValue * callAsFunction(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
Executes the callback for this method.
Definition: static_binding.cpp:41
KJSEmbed::StaticBinding::m_method
const Method * m_method
Definition: static_binding.h:61
KJSEmbed::StaticBinding::publish
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
Definition: static_binding.cpp:60
KJSEmbed::StaticBinding::StaticBinding
StaticBinding(KJS::ExecState *exec, const Method *method)
Create a new method.
Definition: static_binding.cpp:33
KJSEmbed::StaticConstructor
A special method that will create other objects.
Definition: static_binding.h:69
KJSEmbed::StaticConstructor::methods
static const Method * methods(const KJS::UString &className)
Definition: static_binding.cpp:120
KJSEmbed::StaticConstructor::construct
KJS::JSObject * construct(KJS::ExecState *exec, const KJS::List &args)
Calls the callback that will in turn create a new instance of this object with the arguments passed i...
Definition: static_binding.cpp:79
KJSEmbed::StaticConstructor::bind
static KJS::JSObject * bind(KJS::ExecState *exec, const QString &className, PointerBase &objPtr)
Definition: static_binding.cpp:130
KJSEmbed::StaticConstructor::constructor
static const Constructor * constructor(const KJS::UString &className)
Definition: static_binding.cpp:125
KJSEmbed::StaticConstructor::add
static KJS::JSObject * add(KJS::ExecState *exec, KJS::JSObject *object, const Constructor *constructor)
Add the constructor to an object.
Definition: static_binding.cpp:96
KJSEmbed::StaticConstructor::setDefaultValue
void setDefaultValue(KJS::JSValue *value)
Definition: static_binding.cpp:84
KJSEmbed::StaticConstructor::m_constructor
const Constructor * m_constructor
Definition: static_binding.h:124
KJSEmbed::StaticConstructor::StaticConstructor
StaticConstructor(KJS::ExecState *exec, const Constructor *constructor)
Create a new constructor.
Definition: static_binding.cpp:70
KJSEmbed::StaticConstructor::defaultValue
KJS::JSValue * defaultValue(KJS::ExecState *exec, KJS::JSType hint) const
Definition: static_binding.cpp:89
KJSEmbed
Definition: application.h:33
KJSEmbed::g_ctorHash
static QHash< QString, const Constructor * > g_ctorHash
Definition: static_binding.cpp:28
KJSEmbed::callBind
KJS::JSObject *(* callBind)(KJS::ExecState *, PointerBase &)
Bind signature.
Definition: binding_support.h:335
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
KJS
Implement QString-KJS::UString conversion methods.
Definition: kjs_object_model.h:29
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
parent
QObject * parent
Definition: qaction_binding.cpp:48
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
value
QVariant value
Definition: settings.cpp:35
static_binding.h
LengthFlags
#define LengthFlags
Definition: static_binding.h:32
KJSEmbed::Constructor
Definition: binding_support.h:346
KJSEmbed::Constructor::argc
const int argc
Number of arguments.
Definition: binding_support.h:355
KJSEmbed::Constructor::staticMethods
const Method * staticMethods
Static methods on the object.
Definition: binding_support.h:371
KJSEmbed::Constructor::name
const char * name
The constructor name as it will appear in Javascript.
Definition: binding_support.h:351
KJSEmbed::Constructor::construct
const callConstructor construct
The callback for the constructor.
Definition: binding_support.h:367
KJSEmbed::Constructor::enumerators
const Enumerator * enumerators
Enumerators for the object.
Definition: binding_support.h:375
KJSEmbed::Enumerator::name
const char * name
Method name as will appear in javascript.
Definition: binding_support.h:322
KJSEmbed::Enumerator::value
const unsigned int value
Integer value.
Definition: binding_support.h:326
KJSEmbed::Method
Method structure.
Definition: binding_support.h:295
KJSEmbed::Method::argc
const int argc
Number of arguments.
Definition: binding_support.h:303
KJSEmbed::Method::call
const callMethod call
The callback for the method.
Definition: binding_support.h:311
PointerBase
Definition: pointer.h:32
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal