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

kjsembed

  • sources
  • kde-4.12
  • kdelibs
  • 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 
27 namespace KJSEmbed {
28  static QHash<QString,const Constructor*> g_ctorHash;
29 }
30 
31 using namespace KJSEmbed;
32 
33 StaticBinding::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 
41 KJS::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 
60 void 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 
70 StaticConstructor::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 
79 KJS::JSObject *StaticConstructor::construct( KJS::ExecState *exec, const KJS::List &args )
80 {
81  return (*m_constructor->construct)(exec,args);
82 }
83 
84 void StaticConstructor::setDefaultValue( KJS::JSValue *value )
85 {
86  m_default = value;
87 }
88 
89 KJS::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 
96 KJS::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 
120 const Method *StaticConstructor::methods( const KJS::UString &className )
121 {
122  return KJSEmbed::g_ctorHash[toQString(className)]->methods;
123 }
124 
125 const Constructor *StaticConstructor::constructor( const KJS::UString &className )
126 {
127  return KJSEmbed::g_ctorHash[toQString(className)];
128 }
129 
130 KJS::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 
140 KJS::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::Method::argc
const int argc
Number of arguments.
Definition: binding_support.h:303
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::Enumerator::name
const char * name
Method name as will appear in javascript.
Definition: binding_support.h:322
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::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
LengthFlags
#define LengthFlags
Definition: static_binding.h:32
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
PointerBase
Definition: pointer.h:31
parent
QObject * parent
Definition: qaction_binding.cpp:48
KJSEmbed::Constructor::argc
const int argc
Number of arguments.
Definition: binding_support.h:355
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
DomNodeNS::name
END_VALUE_METHOD QString name
Definition: dom.cpp:82
KJSEmbed::Method
Method structure.
Definition: binding_support.h:294
KJSEmbed::StaticConstructor::StaticConstructor
StaticConstructor(KJS::ExecState *exec, const Constructor *constructor)
Create a new constructor.
Definition: static_binding.cpp:70
KJSEmbed::callBind
KJS::JSObject *(* callBind)(KJS::ExecState *, PointerBase &)
Bind signature.
Definition: binding_support.h:335
KJSEmbed::Constructor
Definition: binding_support.h:345
KJSEmbed::Constructor::name
const char * name
The constructor name as it will appear in Javascript.
Definition: binding_support.h:351
KJSEmbed::StaticConstructor::setDefaultValue
void setDefaultValue(KJS::JSValue *value)
Definition: static_binding.cpp:84
NodeListNS::idx
END_VALUE_METHOD int idx
Definition: dom.cpp:691
KJSEmbed::Constructor::enumerators
const Enumerator * enumerators
Enumerators for the object.
Definition: binding_support.h:375
KJSEmbed::StaticConstructor::bind
static KJS::JSObject * bind(KJS::ExecState *exec, const QString &className, PointerBase &objPtr)
Definition: static_binding.cpp:130
List
Definition: variant_binding.cpp:130
KJSEmbed::Constructor::construct
const callConstructor construct
The callback for the constructor.
Definition: binding_support.h:367
KJSEmbed::StaticBinding::m_method
const Method * m_method
Definition: static_binding.h:61
KJSEmbed::Method::call
const callMethod call
The callback for the method.
Definition: binding_support.h:311
static_binding.h
KJSEmbed::StaticBinding::StaticBinding
StaticBinding(KJS::ExecState *exec, const Method *method)
Create a new method.
Definition: static_binding.cpp:33
KJSEmbed::StaticConstructor::defaultValue
KJS::JSValue * defaultValue(KJS::ExecState *exec, KJS::JSType hint) const
Definition: static_binding.cpp:89
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
KJSEmbed::StaticConstructor::methods
static const Method * methods(const KJS::UString &className)
Definition: static_binding.cpp:120
KJSEmbed::Enumerator::value
const unsigned int value
Integer value.
Definition: binding_support.h:326
KJSEmbed::Constructor::staticMethods
const Method * staticMethods
Static methods on the object.
Definition: binding_support.h:371
KJSEmbed::StaticConstructor::m_constructor
const Constructor * m_constructor
Definition: static_binding.h:124
value
QVariant value
Definition: settings.cpp:35
KJSEmbed::g_ctorHash
static QHash< QString, const Constructor * > g_ctorHash
Definition: static_binding.cpp:28
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
KJSEmbed::StaticConstructor::constructor
static const Constructor * constructor(const KJS::UString &className)
Definition: static_binding.cpp:125
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:53 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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