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

kjsembed

  • 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
73namespace 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
95namespace KJSEmbed {
96
97class EnginePrivate {
98public:
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
114void 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
162Engine::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
170Engine::~Engine()
171{
172 delete dptr;
173}
174
175bool Engine::isBindingsEnabled() const
176{
177 return dptr->m_bindingsEnabled;
178}
179
180KJS::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
190KJS::JSObject *Engine::addObject( QObject *obj, const KJS::UString &name ) const
191{
192 return addObject( obj, dptr->m_interpreter->globalObject(), name );
193}
194
195KJS::Completion Engine::completion() const
196{
197 return dptr->m_currentResult;
198}
199
200KJS::Interpreter *Engine::interpreter() const
201{
202 return dptr->m_interpreter;
203}
204
205KJS::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
233Engine::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
245Engine::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
256KJS::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
263KJS::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
285KJS::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;
application.h
binding_support.h
brush.h
builtins.h
KJSEmbed::BuiltinsFactory::methods
static const Method * methods()
Definition: builtins.h:37
KJSEmbed::Engine::~Engine
virtual ~Engine()
Clean up.
Definition: kjsembed.cpp:170
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
KJSEmbed::Engine::interpreter
KJS::Interpreter * interpreter() const
Returns the current interpreter.
Definition: kjsembed.cpp:200
KJSEmbed::Engine::runFile
ExitStatus runFile(const KJS::UString &file)
Execute the file with the specified name using the current interpreter.
Definition: kjsembed.cpp:233
KJSEmbed::Engine::completion
KJS::Completion completion() const
Returns the Completion object for the last script executed.
Definition: kjsembed.cpp:195
KJSEmbed::Engine::execute
ExitStatus execute(const KJS::UString &code)
Execute a code string using the current interpreter.
Definition: kjsembed.cpp:245
KJSEmbed::Engine::ExitStatus
ExitStatus
Status codes for script execution.
Definition: kjsembed.h:65
KJSEmbed::Engine::Success
@ Success
Definition: kjsembed.h:65
KJSEmbed::Engine::Failure
@ Failure
Definition: kjsembed.h:65
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
KJSEmbed::Engine::Engine
Engine(bool enableBindings=true)
Constructs an embedded JS engine.
Definition: kjsembed.cpp:162
KJSEmbed::Engine::isBindingsEnabled
bool isBindingsEnabled() const
Returns true if the Engine was created with the bindings enabled.
Definition: kjsembed.cpp:175
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
KJSEmbed::FileDialog::methods
static const Method * methods()
Definition: filedialog_binding.h:36
KJSEmbed::IoFactory::methods
static const Method * methods()
Definition: iosupport.h:35
KJSEmbed::ObjectBinding::CPPOwned
@ CPPOwned
Definition: object_binding.h:91
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
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
QObject
color.h
dom.h
filedialog_binding.h
file
END_OBJECT_METHOD QFile * file
Definition: fileio.cpp:63
fileio.h
font.h
image.h
iosupport.h
kjsembed.h
DomDocumentNS::data
QString data
Definition: dom.cpp:436
KJSEmbed
Definition: application.h:33
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
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
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
KJSEmbed::setup
void setup(KJS::ExecState *exec, KJS::JSObject *parent)
Definition: kjsembed.cpp:114
KJS
Implement QString-KJS::UString conversion methods.
Definition: kjs_object_model.h:29
pen.h
pixmap.h
point.h
parent
QObject * parent
Definition: qaction_binding.cpp:48
qaction_binding.h
qformbuilder_binding.h
qlayout_binding.h
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
qobject_binding.h
qpainter_binding.h
quiloader_binding.h
qwidget_binding.h
rect.h
settings.h
size.h
static_binding.h
svg_binding.h
url.h
variant_binding.h
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