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

KJS-API

  • sources
  • kde-4.12
  • kdelibs
  • 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 
31 using namespace KJS;
32 
33 class KJSResultHandle
34 {
35 public:
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 
46 KJSResult::KJSResult()
47  : hnd(new KJSResultHandle())
48 {
49 }
50 
51 KJSResult::KJSResult(const KJSResult& r)
52 {
53  hnd = r.hnd;
54  hnd->ref();
55 }
56 
57 KJSResult& 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 
68 KJSResult::~KJSResult()
69 {
70  hnd->deref();
71 }
72 
73 bool KJSResult::isException() const
74 {
75  return !hnd->errMsg.isNull();
76 }
77 
78 QString KJSResult::errorMessage() const
79 {
80  return toQString(hnd->errMsg);
81 }
82 
83 KJSObject KJSResult::value() const
84 {
85  return hnd->val;
86 }
87 
88 KJSInterpreter::KJSInterpreter()
89  : globCtx(0)
90 {
91  Interpreter* ip = new Interpreter();
92  ip->ref();
93  hnd = INTERPRETER_HANDLE(ip);
94 }
95 
96 KJSInterpreter::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 
112 KJSInterpreter::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 
121 KJSInterpreter& 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 
134 KJSInterpreter::KJSInterpreter(KJSInterpreterHandle* h)
135  : hnd(h), globCtx(0)
136 {
137  Interpreter* ip = INTERPRETER(this);
138  globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
139 }
140 
141 KJSInterpreter::~KJSInterpreter()
142 {
143  Interpreter* ip = INTERPRETER(this);
144  ip->deref();
145  ip = 0;
146 }
147 
148 KJSContext* KJSInterpreter::globalContext()
149 {
150  Interpreter* ip = INTERPRETER(this);
151 
152  globCtx.hnd = EXECSTATE_HANDLE(ip->globalExec());
153  return &globCtx;
154 }
155 
156 KJSObject KJSInterpreter::globalObject()
157 {
158  Interpreter* ip = INTERPRETER(this);
159 
160  return KJSObject(JSVALUE_HANDLE(ip->globalObject()));
161 }
162 
163 KJSResult 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 
197 KJSResult KJSInterpreter::evaluate(const QString& code,
198  KJSObject* thisValue)
199 {
200  return evaluate("<string>", 0, code, thisValue);
201 }
202 
203 bool 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 
toQString
static QString toQString(const KJS::UString &s)
Definition: kjsprivate.h:60
KJSResult
A class representing the result of a script evaluation.
Definition: kjsinterpreter.h:37
KJSResult::errorMessage
QString errorMessage() const
Returns the error message if this is an exception result.
Definition: kjsinterpreter.cpp:78
KJSResult::KJSResult
KJSResult()
Constructs a default result object.
Definition: kjsinterpreter.cpp:46
KJSInterpreter::KJSInterpreter
KJSInterpreter()
Constructs an interpreter with a default global object.
Definition: kjsinterpreter.cpp:88
INTERPRETER_HANDLE
#define INTERPRETER_HANDLE(i)
Definition: kjsprivate.h:36
JSVALUE
#define JSVALUE(h)
Definition: kjsprivate.h:31
toUString
static KJS::UString toUString(const QString &s)
Definition: kjsprivate.h:45
KJSUndefined
A class representing an undefined JavaScript value.
Definition: kjsobject.h:188
KJSInterpreter::operator=
KJSInterpreter & operator=(const KJSInterpreter &other)
Assign another interpreter instance to this object.
Definition: kjsinterpreter.cpp:121
KJSGlobalObject
A class representing a global object of an execution environment.
Definition: kjsobject.h:280
KJSInterpreter::globalContext
KJSContext * globalContext()
Returns a handle to the global execution context.
Definition: kjsinterpreter.cpp:148
KJSResult::~KJSResult
~KJSResult()
Frees resources held by this result object.
Definition: kjsinterpreter.cpp:68
KJSContext
A class representing a JavaScript execution context.
Definition: kjscontext.h:39
KJSResult::operator=
KJSResult & operator=(const KJSResult &)
Assigns the properties of another result object to this one.
Definition: kjsinterpreter.cpp:57
EXECSTATE_HANDLE
#define EXECSTATE_HANDLE(c)
Definition: kjsprivate.h:33
KJSResult::isException
bool isException() const
Returns true if the script evaluation has caused an exception.
Definition: kjsinterpreter.cpp:73
kjsinterpreter.h
INTERPRETER
#define INTERPRETER(h)
Definition: kjsprivate.h:37
KJSInterpreter
A class representing a JavaScript interpreter.
Definition: kjsinterpreter.h:82
KJSObject::toString
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:181
KJSObject
A class representing a JavaScript value.
Definition: kjsobject.h:48
KJSInterpreter::~KJSInterpreter
~KJSInterpreter()
Destructs this interpreter and frees resources it has allocated.
Definition: kjsinterpreter.cpp:141
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
KJSResult::value
KJSObject value() const
Definition: kjsinterpreter.cpp:83
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
JSVALUE_HANDLE
#define JSVALUE_HANDLE(v)
Definition: kjsprivate.h:30
kjsprivate.h
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:58 by doxygen 1.8.7 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 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