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

KJS-API

  • kjs
  • api
kjsinterpreter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2008 Harri Porten (porten@kde.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "kjsinterpreter.h"
23#include "kjsprivate.h"
24#include "kjs/interpreter.h"
25#include "kjs/completion.h"
26#include "kjs/object.h"
27#include "kjs/JSVariableObject.h"
28#include <QString>
29#include <stdio.h>
30
31using namespace KJS;
32
33class KJSResultHandle
34{
35public:
36 KJSResultHandle() : rc(1), val(KJSUndefined()) { }
37
38 int rc;
39 KJSObject val;
40 UString errMsg;
41
42 void ref() { ++rc; }
43 void deref() { if (--rc == 0) delete this; }
44};
45
46KJSResult::KJSResult()
47 : hnd(new KJSResultHandle())
48{
49}
50
51KJSResult::KJSResult(const KJSResult& r)
52{
53 hnd = r.hnd;
54 hnd->ref();
55}
56
57KJSResult& KJSResult::operator=(const KJSResult& r)
58{
59 if (hnd != r.hnd) {
60 r.hnd->ref();
61 hnd->deref();
62 hnd = r.hnd;
63 }
64
65 return *this;
66}
67
68KJSResult::~KJSResult()
69{
70 hnd->deref();
71}
72
73bool KJSResult::isException() const
74{
75 return !hnd->errMsg.isNull();
76}
77
78QString KJSResult::errorMessage() const
79{
80 return toQString(hnd->errMsg);
81}
82
83KJSObject KJSResult::value() const
84{
85 return hnd->val;
86}
87
88KJSInterpreter::KJSInterpreter()
89 : globCtx(0)
90{
91 Interpreter* ip = new Interpreter();
92 ip->ref();
93 hnd = INTERPRETER_HANDLE(ip);
94}
95
96KJSInterpreter::KJSInterpreter(const KJSGlobalObject& global)
97 : globCtx(0)
98{
99 JSValue* gv = JSVALUE(&global);
100 assert(gv->isObject());
101 JSObject* go = static_cast<JSObject*>(gv);
102 assert(go->isGlobalObject());
103 Interpreter* ip = new Interpreter(static_cast<JSGlobalObject*>(go));
104 ip->ref();
105 assert(go->prototype()->isObject());
106 JSObject* p = static_cast<JSObject*>(go->prototype());
107 JSObject* objectProto = ip->builtinObjectPrototype();
108 p->setPrototype(objectProto);
109 hnd = INTERPRETER_HANDLE(ip);
110}
111
112KJSInterpreter::KJSInterpreter(const KJSInterpreter& other)
113 : globCtx(0)
114{
115 Interpreter* ip = INTERPRETER(&other);
116 ip->ref();
117 hnd = INTERPRETER_HANDLE(ip);
118 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
119}
120
121KJSInterpreter& KJSInterpreter::operator=(const KJSInterpreter& other)
122{
123 Interpreter* thisIp = INTERPRETER(this);
124 Interpreter* otherIp = INTERPRETER(&other);
125 if (otherIp != thisIp) {
126 otherIp->ref();
127 thisIp->deref();
128 hnd = INTERPRETER_HANDLE(otherIp);
129 globCtx.hnd = EXECSTATE_HANDLE(otherIp->globalExec());
130 }
131 return *this;
132}
133
134KJSInterpreter::KJSInterpreter(KJSInterpreterHandle* h)
135 : hnd(h), globCtx(0)
136{
137 Interpreter* ip = INTERPRETER(this);
138 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
139}
140
141KJSInterpreter::~KJSInterpreter()
142{
143 Interpreter* ip = INTERPRETER(this);
144 ip->deref();
145 ip = 0;
146}
147
148KJSContext* KJSInterpreter::globalContext()
149{
150 Interpreter* ip = INTERPRETER(this);
151
152 globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
153 return &globCtx;
154}
155
156KJSObject KJSInterpreter::globalObject()
157{
158 Interpreter* ip = INTERPRETER(this);
159
160 return KJSObject(JSVALUE_HANDLE(ip->globalObject()));
161}
162
163KJSResult KJSInterpreter::evaluate(const QString& sourceURL,
164 int startingLineNumber,
165 const QString& code,
166 KJSObject* thisValue)
167{
168 Interpreter* ip = INTERPRETER(this);
169
170 JSValue* tv = thisValue ? JSVALUE(thisValue) : 0;
171 KJS::Completion c = ip->evaluate(toUString(sourceURL), startingLineNumber,
172 toUString(code), tv);
173
174 KJSResult res;
175 if (c.complType() == Throw) {
176 ExecState* exec = ip->globalExec();
177 UString msg = c.value()->toString(exec);
178#if 0
179 JSObject* resObj = c.value()->toObject(exec);
180 CString message = resObj->toString(exec).UTF8String();
181 int line = resObj->toObject(exec)->get(exec, "line")->toUInt32(exec);
182
183 if (!sourceURL.isEmpty())
184 fprintf(stderr, "%s (line %d): ", qPrintable(sourceURL), line);
185 fprintf(stderr, "%s\n", msg.c_str());
186#endif
187 fprintf(stderr, "evaluate() threw an exception\n");
188 res.hnd->errMsg = msg;
189 } else {
190 if (c.isValueCompletion())
191 res.hnd->val = KJSObject(JSVALUE_HANDLE(c.value()));
192 }
193
194 return res;
195}
196
197KJSResult KJSInterpreter::evaluate(const QString& code,
198 KJSObject* thisValue)
199{
200 return evaluate("<string>", 0, code, thisValue);
201}
202
203bool KJSInterpreter::normalizeCode(const QString& code, QString* normalized,
204 int* errLine, QString* errMsg)
205{
206 assert(normalized);
207
208 UString codeOut, msg;
209 bool success = Interpreter::normalizeCode(toUString(code), &codeOut,
210 errLine, &msg);
211
212 *normalized = toQString(codeOut);
213 if (errMsg)
214 *errMsg = toQString(msg);
215
216 return success;
217}
218
KJSContext
A class representing a JavaScript execution context.
Definition: kjscontext.h:40
KJSGlobalObject
A class representing a global object of an execution environment.
Definition: kjsobject.h:281
KJSInterpreter
A class representing a JavaScript interpreter.
Definition: kjsinterpreter.h:83
KJSInterpreter::KJSInterpreter
KJSInterpreter()
Constructs an interpreter with a default global object.
Definition: kjsinterpreter.cpp:88
KJSInterpreter::evaluate
KJSResult evaluate(const QString &sourceURL, int startingLineNumber, const QString &code, KJSObject *thisValue=0)
Evaluates a piece of code with a "this" set to (optionally set) value.
Definition: kjsinterpreter.cpp:163
KJSInterpreter::globalContext
KJSContext * globalContext()
Returns a handle to the global execution context.
Definition: kjsinterpreter.cpp:148
KJSInterpreter::~KJSInterpreter
~KJSInterpreter()
Destructs this interpreter and frees resources it has allocated.
Definition: kjsinterpreter.cpp:141
KJSInterpreter::globalObject
KJSObject globalObject()
Returns the object that is used as the global object during all script execution performed by this in...
Definition: kjsinterpreter.cpp:156
KJSInterpreter::operator=
KJSInterpreter & operator=(const KJSInterpreter &other)
Assign another interpreter instance to this object.
Definition: kjsinterpreter.cpp:121
KJSInterpreter::normalizeCode
static bool normalizeCode(const QString &codeIn, QString *codeOut, int *errLine=0, QString *errMsg=0)
Reformat the given script code to an easy to read format with only one statement per line.
Definition: kjsinterpreter.cpp:203
KJSObject
A class representing a JavaScript value.
Definition: kjsobject.h:49
KJSObject::toString
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:181
KJSResult
A class representing the result of a script evaluation.
Definition: kjsinterpreter.h:38
KJSResult::isException
bool isException() const
Returns true if the script evaluation has caused an exception.
Definition: kjsinterpreter.cpp:73
KJSResult::operator=
KJSResult & operator=(const KJSResult &)
Assigns the properties of another result object to this one.
Definition: kjsinterpreter.cpp:57
KJSResult::~KJSResult
~KJSResult()
Frees resources held by this result object.
Definition: kjsinterpreter.cpp:68
KJSResult::errorMessage
QString errorMessage() const
Returns the error message if this is an exception result.
Definition: kjsinterpreter.cpp:78
KJSResult::value
KJSObject value() const
Definition: kjsinterpreter.cpp:83
KJSResult::KJSResult
KJSResult()
Constructs a default result object.
Definition: kjsinterpreter.cpp:46
KJSUndefined
A class representing an undefined JavaScript value.
Definition: kjsobject.h:189
kjsinterpreter.h
kjsprivate.h
toQString
static QString toQString(const KJS::UString &s)
Definition: kjsprivate.h:60
JSVALUE_HANDLE
#define JSVALUE_HANDLE(v)
Definition: kjsprivate.h:30
EXECSTATE_HANDLE
#define EXECSTATE_HANDLE(c)
Definition: kjsprivate.h:33
INTERPRETER_HANDLE
#define INTERPRETER_HANDLE(i)
Definition: kjsprivate.h:36
INTERPRETER
#define INTERPRETER(h)
Definition: kjsprivate.h:37
toUString
static KJS::UString toUString(const QString &s)
Definition: kjsprivate.h:45
JSVALUE
#define JSVALUE(h)
Definition: kjsprivate.h:31
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.

KJS-API

Skip menu "KJS-API"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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