• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 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 
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;
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 20:18:42 by doxygen 1.8.3.1 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.10.4 API Reference

Skip menu "kdelibs-4.10.4 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