• 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
kjsembed.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 "kjsembed.h"
23 #include "binding_support.h"
24 
25 #include "qobject_binding.h"
26 #include "variant_binding.h"
27 #include "static_binding.h"
28 
29 #include "iosupport.h"
30 #include "quiloader_binding.h"
31 #ifdef KJSEMBED_FORMBUILDER_BINDING
32 #include "qformbuilder_binding.h"
33 #endif
34 #include "qpainter_binding.h"
35 #include "qwidget_binding.h"
36 #include "qaction_binding.h"
37 #include "qlayout_binding.h"
38 #include "svg_binding.h"
39 #include "filedialog_binding.h"
40 #include "settings.h"
41 #include "fileio.h"
42 #include "color.h"
43 #include "rect.h"
44 #include "size.h"
45 #include "point.h"
46 #include "image.h"
47 #include "pixmap.h"
48 #include "brush.h"
49 #include "pen.h"
50 #include "font.h"
51 #include "dom.h"
52 #include "url.h"
53 #include "application.h"
54 
55 #include "builtins.h"
56 
57 #include <kjs/interpreter.h>
58 #include <kjs/completion.h>
59 
60 #include <QtCore/QFile>
61 #include <QtCore/QTextStream>
62 #include <QtCore/QObject>
63 
64 #include <QtCore/QDebug>
65 
66 
73 namespace KJS {
74 #ifndef QTONLY_WEBKIT
75  UString::UString( const QString &d )
76  {
77  uint len = d.length();
78  UChar *dat = static_cast<UChar*>(fastMalloc(sizeof(UChar)*len));
79  memcpy( dat, d.unicode(), len * sizeof(UChar) );
80  m_rep = UString::Rep::create(dat, len);
81  }
82 
83  QString UString::qstring() const
84  {
85  return QString((QChar*) data(), size());
86  }
87 
88  QString Identifier::qstring() const
89  {
90  return QString((QChar*) data(), size());
91  }
92 #endif
93 }
94 
95 namespace KJSEmbed {
96 
97 class EnginePrivate {
98 public:
99  EnginePrivate ( )
100  {
101  m_interpreter = new KJS::Interpreter( );
102  m_interpreter->initGlobalObject();
103  m_interpreter->ref();
104  }
105  ~EnginePrivate()
106  {
107  m_interpreter->deref();
108  }
109  KJS::Interpreter *m_interpreter;
110  KJS::Completion m_currentResult;
111  bool m_bindingsEnabled;
112 };
113 
114 void setup( KJS::ExecState *exec, KJS::JSObject *parent )
115 {
116  StaticBinding::publish( exec, parent, IoFactory::methods() ); // Global methods
117  StaticBinding::publish( exec, parent, FileDialog::methods() ); // Global methods
118  StaticBinding::publish( exec, parent, BuiltinsFactory::methods() ); // Global methods
119  StaticConstructor::add( exec, parent, FileIO::constructor() ); // Ctor
120  StaticConstructor::add( exec, parent, DomNode::constructor() ); // Ctor
121  StaticConstructor::add( exec, parent, DomDocument::constructor() ); // Ctor
122  StaticConstructor::add( exec, parent, DomElement::constructor() ); // Ctor
123  StaticConstructor::add( exec, parent, DomAttr::constructor() ); // Ctor
124  StaticConstructor::add( exec, parent, DomDocumentType::constructor() ); // Ctor
125  StaticConstructor::add( exec, parent, DomNodeList::constructor() ); // Ctor
126  StaticConstructor::add( exec, parent, DomNamedNodeMap::constructor() ); // Ctor
127  StaticConstructor::add( exec, parent, DomText::constructor() ); // Ctor
128  StaticConstructor::add( exec, parent, Url::constructor() ); // Ctor
129  StaticConstructor::add( exec, parent, SettingsBinding::constructor() ); // Ctor
130  StaticConstructor::add( exec, parent, CoreApplicationBinding::constructor() );
131  StaticConstructor::add( exec, parent, Point::constructor() ); // Ctor
132  StaticConstructor::add( exec, parent, Size::constructor() ); // Ctor
133  StaticConstructor::add( exec, parent, Rect::constructor() ); // Ctor
134  StaticConstructor::add( exec, parent, Color::constructor() ); // Ctor
135 
136  // check if this is a GUI application
137  QApplication* app = ::qobject_cast<QApplication*>(QCoreApplication::instance());
138  if (app && (app->type() != QApplication::Tty))
139  {
140  //qDebug("Loading GUI Bindings");
141 
142 #ifdef KJSEMBED_FORMBUILDER_BINDING
143  StaticConstructor::add( exec, parent, FormBuilder::constructor() ); // Ctor
144 #endif
145  StaticConstructor::add( exec, parent, UiLoaderBinding::constructor() ); // Ctor
146  StaticConstructor::add( exec, parent, QWidgetBinding::constructor() ); // Ctor
147  StaticConstructor::add( exec, parent, Layout::constructor() ); // Ctor
148  StaticConstructor::add( exec, parent, Action::constructor() ); // Ctor
149  StaticConstructor::add( exec, parent, ActionGroup::constructor() ); // Ctor
150  StaticConstructor::add( exec, parent, Font::constructor() ); // Ctor
151  StaticConstructor::add( exec, parent, Pen::constructor() ); // Ctor
152  StaticConstructor::add( exec, parent, Brush::constructor() ); // Ctor
153  StaticConstructor::add( exec, parent, Image::constructor() ); // Ctor
154  StaticConstructor::add( exec, parent, Pixmap::constructor() ); // Ctor
155  StaticConstructor::add( exec, parent, Painter::constructor() ); // Ctor
156  StaticConstructor::add( exec, parent, SvgRenderer::constructor() ); // Ctor
157  StaticConstructor::add( exec, parent, SvgWidget::constructor() ); // Ctor
158  StaticConstructor::add( exec, parent, ApplicationBinding::constructor() );
159  }
160 }
161 
162 Engine::Engine( bool enableBindings )
163 {
164  dptr = new EnginePrivate( );
165  if ( enableBindings )
166  setup( dptr->m_interpreter->globalExec(), dptr->m_interpreter->globalObject() );
167  dptr->m_bindingsEnabled = enableBindings;
168 }
169 
170 Engine::~Engine()
171 {
172  delete dptr;
173 }
174 
175 bool Engine::isBindingsEnabled() const
176 {
177  return dptr->m_bindingsEnabled;
178 }
179 
180 KJS::JSObject *Engine::addObject( QObject *obj, KJS::JSObject *parent, const KJS::UString &name ) const
181 {
182  KJS::ExecState *exec = dptr->m_interpreter->globalExec();
183  KJS::JSObject *returnObject = KJSEmbed::createQObject(exec , obj, KJSEmbed::ObjectBinding::CPPOwned );
184  KJS::Identifier jsName( !name.isEmpty() ? name : toUString(obj->objectName()) );
185 
186  parent->putDirect(jsName, returnObject, KJS::DontDelete|KJS::ReadOnly );
187  return returnObject;
188 }
189 
190 KJS::JSObject *Engine::addObject( QObject *obj, const KJS::UString &name ) const
191 {
192  return addObject( obj, dptr->m_interpreter->globalObject(), name );
193 }
194 
195 KJS::Completion Engine::completion() const
196 {
197  return dptr->m_currentResult;
198 }
199 
200 KJS::Interpreter *Engine::interpreter() const
201 {
202  return dptr->m_interpreter;
203 }
204 
205 KJS::Completion Engine::runFile( KJS::Interpreter *interpreter, const KJS::UString &fileName )
206 {
207 // qDebug() << "runFile: " << toQString(fileName);
208  KJS::UString code;
209  QFile file( toQString(fileName) );
210  if( file.open( QFile::ReadOnly ) )
211  {
212  QTextStream ts( &file );
213 
214  QString line;
215  while( !ts.atEnd() )
216  {
217  line = ts.readLine();
218  if( line[0] != '#' ) code += toUString(line + '\n');
219  }
220  file.close();
221  }
222  else
223  {
224  code = "println('Could not open file.');";
225  qWarning() << "Could not open file " << toQString(fileName);
226  }
227 
228 // qDebug() << "Loaded code: " << toQString(code);
229 
230  return interpreter->evaluate( fileName, 0, code, 0 );
231 }
232 
233 Engine::ExitStatus Engine::runFile( const KJS::UString &fileName )
234 {
235  dptr->m_currentResult = runFile( dptr->m_interpreter, fileName );
236 
237  if( dptr->m_currentResult.complType() == KJS::Normal )
238  return Engine::Success;
239  else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
240  return Engine::Success;
241  else
242  return Engine::Failure;
243 }
244 
245 Engine::ExitStatus Engine::execute( const KJS::UString &code )
246 {
247  dptr->m_currentResult = dptr->m_interpreter->evaluate(KJS::UString(""), 0, code, 0);
248  if( dptr->m_currentResult.complType() == KJS::Normal )
249  return Engine::Success;
250  else if ( dptr->m_currentResult.complType() == KJS::ReturnValue)
251  return Engine::Success;
252  else
253  return Engine::Failure;
254 }
255 
256 KJS::JSObject *Engine::construct( const KJS::UString &className, const KJS::List &args ) const
257 {
258  KJS::JSObject *global = dptr->m_interpreter->globalObject();
259  KJS::ExecState *exec = dptr->m_interpreter->globalExec();
260  return StaticConstructor::construct( exec, global, className, args );
261 }
262 
263 KJS::JSValue *Engine::callMethod( const KJS::UString &methodName, const KJS::List &args )
264 {
265  KJS::JSObject *global = dptr->m_interpreter->globalObject();
266  KJS::ExecState *exec = dptr->m_interpreter->globalExec();
267 
268  KJS::Identifier id = KJS::Identifier( KJS::UString( methodName ) );
269  KJS::JSObject *fun = global->get( exec, id )->toObject( exec );
270  KJS::JSValue *retValue;
271 
272  if ( !fun->implementsCall() ) {
273  QString msg = i18n( "%1 is not a function and cannot be called.", toQString(methodName) );
274  return throwError( exec, KJS::TypeError, msg );
275  }
276 
277  retValue = fun->call( exec, global, args );
278 
279  if( exec->hadException() )
280  return exec->exception();
281 
282  return retValue;
283 }
284 
285 KJS::JSValue *Engine::callMethod( KJS::JSObject *parent,
286  const KJS::UString &methodName, const KJS::List &args )
287 {
288  KJS::ExecState *exec = dptr->m_interpreter->globalExec();
289 
290  KJS::Identifier id = KJS::Identifier( methodName);
291  KJS::JSObject *fun = parent->get( exec, id )->toObject( exec );
292  KJS::JSValue *retValue;
293 
294  if ( !fun->implementsCall() ) {
295  QString msg = i18n( "%1 is not a function and cannot be called.", toQString(methodName) );
296  return throwError( exec, KJS::TypeError, msg );
297  }
298 
299  retValue = fun->call( exec, parent, args );
300 
301  if( exec->hadException() )
302  return exec->exception();
303 
304  return retValue;
305 }
306 
307 
308 } // namespace KJS
309 
310 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::FileDialog::methods
static const Method * methods()
Definition: filedialog_binding.h:36
KJSEmbed::Engine::execute
ExitStatus execute(const KJS::UString &code)
Execute a code string using the current interpreter.
Definition: kjsembed.cpp:245
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::Engine::addObject
KJS::JSObject * addObject(QObject *obj, const KJS::UString &name=KJS::UString()) const
publishes a QObject to the global context of the javascript interpereter.
Definition: kjsembed.cpp:190
quiloader_binding.h
qlayout_binding.h
application.h
KJSEmbed::Engine::Failure
Definition: kjsembed.h:65
point.h
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::Engine::construct
KJS::JSObject * construct(const KJS::UString &className, const KJS::List &args=KJS::List()) const
Create a new instance of an object that the Javascript engine knows about.
Definition: kjsembed.cpp:256
builtins.h
KJSEmbed::Engine::ExitStatus
ExitStatus
Status codes for script execution.
Definition: kjsembed.h:65
binding_support.h
QObject
KJSEmbed::Engine::interpreter
KJS::Interpreter * interpreter() const
Returns the current interpreter.
Definition: kjsembed.cpp:200
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
KJSEmbed::Engine::completion
KJS::Completion completion() const
Returns the Completion object for the last script executed.
Definition: kjsembed.cpp:195
brush.h
parent
QObject * parent
Definition: qaction_binding.cpp:48
KJSEmbed::Engine::runFile
ExitStatus runFile(const KJS::UString &file)
Execute the file with the specified name using the current interpreter.
Definition: kjsembed.cpp:233
DomDocumentNS::data
QString data
Definition: dom.cpp:436
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::setup
void setup(KJS::ExecState *exec, KJS::JSObject *parent)
Definition: kjsembed.cpp:114
KJSEmbed::createQObject
KJSEMBED_EXPORT KJS::JSObject * createQObject(KJS::ExecState *exec, QObject *value, KJSEmbed::ObjectBinding::Ownership owner=KJSEmbed::ObjectBinding::JSOwned)
Returns a binding object for the specified QObject.
Definition: qobject_binding.cpp:735
rect.h
image.h
KJSEmbed::Engine::Engine
Engine(bool enableBindings=true)
Constructs an embedded JS engine.
Definition: kjsembed.cpp:162
KJSEmbed::BuiltinsFactory::methods
static const Method * methods()
Definition: builtins.h:37
pixmap.h
qformbuilder_binding.h
qwidget_binding.h
KJSEmbed::ObjectBinding::CPPOwned
Definition: object_binding.h:91
file
END_OBJECT_METHOD QFile * file
Definition: fileio.cpp:63
iosupport.h
pen.h
fileio.h
size.h
KJSEmbed::Engine::Success
Definition: kjsembed.h:65
KJSEmbed::Engine::isBindingsEnabled
bool isBindingsEnabled() const
Returns true if the Engine was created with the bindings enabled.
Definition: kjsembed.cpp:175
filedialog_binding.h
qpainter_binding.h
color.h
dom.h
settings.h
svg_binding.h
List
Definition: variant_binding.cpp:130
KJSEmbed::Engine::callMethod
KJS::JSValue * callMethod(const KJS::UString &methodName, const KJS::List &args=KJS::List())
Execute a method at the global scope of the javascript interperter.
Definition: kjsembed.cpp:263
static_binding.h
KJSEmbed::Engine::~Engine
virtual ~Engine()
Clean up.
Definition: kjsembed.cpp:170
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
qobject_binding.h
KJSEmbed::IoFactory::methods
static const Method * methods()
Definition: iosupport.h:35
variant_binding.h
kjsembed.h
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
url.h
font.h
qaction_binding.h
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