KJS

kjsinterpreter.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2008 Harri Porten ([email protected])
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 #ifndef KJSINTERPRETER_H
23 #define KJSINTERPRETER_H
24 
25 #include "kjsapi_export.h"
26 #include "kjsobject.h"
27 #include "kjscontext.h"
28 
29 class KJSPrototype;
30 class KJSInterpreter;
31 class KJSInterpreterHandle;
32 class KJSResultHandle;
33 
34 /**
35  * A class representing the result of a script evaluation.
36  */
37 class KJSAPI_EXPORT KJSResult
38 {
39  friend class KJSInterpreter;
40 public:
41  /**
42  * Constructs a default result object.
43  */
44  KJSResult();
45  /**
46  * Constructs a copy of another result object.
47  */
48  KJSResult(const KJSResult &);
49  /**
50  * Assigns the properties of another result object to this one.
51  */
52  KJSResult &operator=(const KJSResult &);
53  /**
54  * Frees resources held by this result object.
55  */
56  ~KJSResult();
57  /**
58  * Returns true if the script evaluation has caused an exception.
59  */
60  bool isException() const;
61 
62  /**
63  * Returns the error message if this is an exception result.
64  */
65  QString errorMessage() const;
66  /*
67  * If the evaluation was successful, i.e. isException() is false
68  * this function returns the value returned by the script. Can be
69  * an "undefined" (isUndefined()) value.
70  */
71  KJSObject value() const;
72 
73 private:
74  KJSResultHandle *hnd;
75 };
76 
77 /**
78  * A class representing a JavaScript interpreter
79  *
80  * @short JavaScript interpreter
81  */
82 class KJSAPI_EXPORT KJSInterpreter
83 {
84  friend class KJSResult;
85  friend class KJSPrototype;
86  friend class KJSContext;
87 public:
88  /**
89  * Constructs an interpreter with a default global object.
90  */
92  /**
93  * Constructs an interpreter with a custom global object.
94  */
95  KJSInterpreter(const KJSGlobalObject &global);
96  /**
97  * Creates a copy of another interpreter.
98  */
99  KJSInterpreter(const KJSInterpreter &other);
100  /**
101  * Assign another interpreter instance to this object.
102  */
103  KJSInterpreter &operator=(const KJSInterpreter &other);
104  /**
105  * Destructs this interpreter and frees resources it has
106  * allocated. This renders any still existing objects referencing
107  * those invalid.
108  */
109  ~KJSInterpreter();
110 
111  /**
112  * Returns a handle to the global execution context.
113  */
114  KJSContext *globalContext();
115  /**
116  * @overload
117  */
118  const KJSContext *globalContext() const;
119  /**
120  * Returns the object that is used as the global object during all
121  * script execution performed by this interpreter,
122  */
123  KJSObject globalObject();
124  /**
125  * Evaluates a piece of code with a "this" set to (optionally set)
126  * value. The sourceURL and startingLineNumber parameters are used
127  * to provide information about the origin of a parse error or
128  * runtime exception.
129  */
130  KJSResult evaluate(const QString &sourceURL, int startingLineNumber,
131  const QString &code,
132  KJSObject *thisValue = nullptr);
133  /**
134  * @overload
135  */
136  KJSResult evaluate(const QString &code,
137  KJSObject *thisValue = nullptr);
138 
139  /**
140  * Call this function in preparation of startTimeoutCheck() to set
141  * the number of milliseconds that a script evaluation is allowed
142  * to take at most. This will protect the user from slow and
143  * long-running scripts or even infinite loops.
144  *
145  * A 0 msecs value (the default setting) means no timeout at all.
146  *
147  * @param mSecs The number of milliseconds
148  */
149  void setTimeoutTime(unsigned mSecs);
150  /**
151  * Start measuring executing time until the timeout value
152  * specified via setTimeoutTime().
153  *
154  * In case the timeout expires a script error with the message
155  * "Execution timeout. Aborting." will be thrown upon evaluation
156  * of the next script expression or statement.
157  */
158  void startTimeoutCheck();
159  /**
160  * Stops measurement of execution time after the initial
161  * startTimeoutCheck() call. Call this function after a evaluate()
162  * or if the check should temporarily be disabled. When showing a
163  * message box to the user for example.
164  */
165  void stopTimeoutCheck();
166 
167  /**
168  * Reformat the given script code to an easy to read format with
169  * only one statement per line. This can be useful when debugging
170  * a script that was e.g. condensed into a single line to a single
171  * line. While comments will be removed the script will remain
172  * unchanged semantically.
173  *
174  * @param codeIn The code to be reformatted
175  * @param codeOut Points to string holding the result.
176  * @param errLine Will hold the line of a parse error
177  * @param errMsg Will hold the message of a parse error
178  *
179  * @return Returns true if the reformatting was successful, false
180  * on a parse error.
181  */
182  static bool normalizeCode(const QString &codeIn, QString *codeOut,
183  int *errLine = nullptr, QString *errMsg = nullptr);
184 private:
185  KJSInterpreter(KJSInterpreterHandle *h);
186  KJSInterpreterHandle *hnd;
187  KJSContext globCtx;
188 };
189 
190 #endif
A class representing a JavaScript interpreter.
A class representing a JavaScript prototype object.
Definition: kjsprototype.h:38
KJSInterpreter & operator=(const KJSInterpreter &other)
Assign another interpreter instance to this object.
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
A class representing a global object of an execution environment.
Definition: kjsobject.h:279
A class representing a JavaScript execution context.
Definition: kjscontext.h:41
A class representing a JavaScript value.
Definition: kjsobject.h:48
A class representing the result of a script evaluation.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 03:56:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.