1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
5 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
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., 59 Temple Place - Suite 330,
|
---|
20 | * Boston, MA 02111-1307, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifndef _KJS_INTERPRETER_H_
|
---|
25 | #define _KJS_INTERPRETER_H_
|
---|
26 |
|
---|
27 | #include "value.h"
|
---|
28 | #include "object.h"
|
---|
29 | #include "types.h"
|
---|
30 |
|
---|
31 | namespace KJS {
|
---|
32 |
|
---|
33 | class ContextImp;
|
---|
34 | class ExecStateImp;
|
---|
35 | class InterpreterImp;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Represents an execution context, as specified by section 10 of the ECMA
|
---|
39 | * spec.
|
---|
40 | *
|
---|
41 | * An execution context contains information about the current state of the
|
---|
42 | * script - the scope for variable lookup, the value of "this", etc. A new
|
---|
43 | * execution context is entered whenever global code is executed (e.g. with
|
---|
44 | * @ref Interpreter::evaluate()), a function is called (see @ref
|
---|
45 | * Object::call()), or the builtin "eval" function is executed.
|
---|
46 | *
|
---|
47 | * Most inheritable functions in the KJS api take a @ref ExecState pointer as
|
---|
48 | * their first parameter. This can be used to obtain a handle to the current
|
---|
49 | * execution context.
|
---|
50 | *
|
---|
51 | * Note: Context objects are wrapper classes/smart pointers for the internal
|
---|
52 | * KJS ContextImp type. When one context variable is assigned to another, it
|
---|
53 | * is still referencing the same internal object.
|
---|
54 | */
|
---|
55 | class Context {
|
---|
56 | public:
|
---|
57 | Context(ContextImp *);
|
---|
58 | Context(const Context &c);
|
---|
59 | Context& operator=(const Context &c);
|
---|
60 | virtual ~Context();
|
---|
61 |
|
---|
62 | bool isNull() const;
|
---|
63 | ContextImp *imp() const;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Returns the scope chain for this execution context. This is used for
|
---|
67 | * variable lookup, with the list being searched from start to end until a
|
---|
68 | * variable is found.
|
---|
69 | *
|
---|
70 | * @return The execution context's scope chain
|
---|
71 | */
|
---|
72 | const List scopeChain() const;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Returns the variable object for the execution context. This contains a
|
---|
76 | * property for each variable declared in the execution context.
|
---|
77 | *
|
---|
78 | * @return The execution context's variable object
|
---|
79 | */
|
---|
80 | Object variableObject() const;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Returns the "this" value for the execution context. This is the value
|
---|
84 | * returned when a script references the special variable "this". It should
|
---|
85 | * always be an Object, unless application-specific code has passed in a
|
---|
86 | * different type.
|
---|
87 | *
|
---|
88 | * The object that is used as the "this" value depends on the type of
|
---|
89 | * execution context - for global contexts, the global object is used. For
|
---|
90 | * function objewcts, the value is given by the caller (e.g. in the case of
|
---|
91 | * obj.func(), obj would be the "this" value). For code executed by the
|
---|
92 | * built-in "eval" function, the this value is the same as the calling
|
---|
93 | * context.
|
---|
94 | *
|
---|
95 | * @return The execution context's "this" value
|
---|
96 | */
|
---|
97 | Object thisValue() const;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Returns the context from which the current context was invoked. For
|
---|
101 | * global code this will be a null context (i.e. one for which @ref
|
---|
102 | * isNull() returns true). You should check @ref isNull() on the returned
|
---|
103 | * value before calling any of it's methods.
|
---|
104 | *
|
---|
105 | * @return The calling execution context
|
---|
106 | */
|
---|
107 | const Context callingContext() const;
|
---|
108 | private:
|
---|
109 | ContextImp *rep;
|
---|
110 | };
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Interpreter objects can be used to evaluate ECMAScript code. Each
|
---|
114 | * interpreter has a global object which is used for the purposes of code
|
---|
115 | * evaluation, and also provides access to built-in properties such as
|
---|
116 | * " Object" and "Number".
|
---|
117 | */
|
---|
118 | class Interpreter {
|
---|
119 | public:
|
---|
120 | /**
|
---|
121 | * Creates a new interpreter. The supplied object will be used as the global
|
---|
122 | * object for all scripts executed with this interpreter. During
|
---|
123 | * constuction, all the standard properties such as "Object" and "Number"
|
---|
124 | * will be added to the global object.
|
---|
125 | *
|
---|
126 | * Note: You should not use the same global object for multiple
|
---|
127 | * interpreters.
|
---|
128 | *
|
---|
129 | * This is due do the fact that the built-in properties are set in the
|
---|
130 | * constructor, and if these objects have been modified from another
|
---|
131 | * interpreter (e.g. a script modifying String.prototype), the changes will
|
---|
132 | * be overridden.
|
---|
133 | *
|
---|
134 | * @param global The object to use as the global object for this interpreter
|
---|
135 | */
|
---|
136 | Interpreter(const Object &global);
|
---|
137 | /**
|
---|
138 | * Creates a new interpreter. A global object will be created and
|
---|
139 | * initialized with the standard global properties.
|
---|
140 | */
|
---|
141 | Interpreter();
|
---|
142 | virtual ~Interpreter();
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Returns the object that is used as the global object during all script
|
---|
146 | * execution performed by this interpreter
|
---|
147 | */
|
---|
148 | Object globalObject() const;
|
---|
149 |
|
---|
150 | void initGlobalObject();
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the execution state object which can be used to execute
|
---|
154 | * scripts using this interpreter at a the "global" level, i.e. one
|
---|
155 | * with a execution context that has the global object as the "this"
|
---|
156 | * value, and who's scope chain contains only the global object.
|
---|
157 | *
|
---|
158 | * Note: this pointer remains constant for the life of the interpreter
|
---|
159 | * and should not be manually deleted.
|
---|
160 | *
|
---|
161 | * @return The interpreter global execution state object
|
---|
162 | */
|
---|
163 | ExecState *globalExec();
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Parses the supplied ECMAScript code and checks for syntax errors.
|
---|
167 | *
|
---|
168 | * @param code The code to check
|
---|
169 | * @return true if there were no syntax errors in the code, otherwise false
|
---|
170 | */
|
---|
171 | bool checkSyntax(const UString &code);
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Evaluates the supplied ECMAScript code.
|
---|
175 | *
|
---|
176 | * Since this method returns a Completion, you should check the type of
|
---|
177 | * completion to detect an error or before attempting to access the returned
|
---|
178 | * value. For example, if an error occurs during script execution and is not
|
---|
179 | * caught by the script, the completion type will be Throw.
|
---|
180 | *
|
---|
181 | * If the supplied code is invalid, a SyntaxError will be thrown.
|
---|
182 | *
|
---|
183 | * @param code The code to evaluate
|
---|
184 | * @param thisV The value to pass in as the "this" value for the script
|
---|
185 | * execution. This should either be Null() or an Object.
|
---|
186 | * @return A completion object representing the result of the execution.
|
---|
187 | */
|
---|
188 | Completion evaluate(const UString &code, const Value &thisV = Value());
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * @internal
|
---|
192 | *
|
---|
193 | * Returns the implementation object associated with this interpreter.
|
---|
194 | * Only useful for internal KJS operations.
|
---|
195 | */
|
---|
196 | InterpreterImp *imp();
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Returns the builtin "Object" object. This is the object that was set
|
---|
200 | * as a property of the global object during construction; if the property
|
---|
201 | * is replaced by script code, this method will still return the original
|
---|
202 | * object.
|
---|
203 | *
|
---|
204 | * @return The builtin "Object" object
|
---|
205 | */
|
---|
206 | Object builtinObject() const;
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Returns the builtin "Function" object.
|
---|
210 | */
|
---|
211 | Object builtinFunction() const;
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Returns the builtin "Array" object.
|
---|
215 | */
|
---|
216 | Object builtinArray() const;
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Returns the builtin "Boolean" object.
|
---|
221 | */
|
---|
222 | Object builtinBoolean() const;
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Returns the builtin "String" object.
|
---|
226 | */
|
---|
227 | Object builtinString() const;
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Returns the builtin "Number" object.
|
---|
231 | */
|
---|
232 | Object builtinNumber() const;
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Returns the builtin "Date" object.
|
---|
236 | */
|
---|
237 | Object builtinDate() const;
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Returns the builtin "RegExp" object.
|
---|
241 | */
|
---|
242 | Object builtinRegExp() const;
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Returns the builtin "Error" object.
|
---|
246 | */
|
---|
247 | Object builtinError() const;
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Returns the builtin "Object.prototype" object.
|
---|
251 | */
|
---|
252 | Object builtinObjectPrototype() const;
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Returns the builtin "Function.prototype" object.
|
---|
256 | */
|
---|
257 | Object builtinFunctionPrototype() const;
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Returns the builtin "Array.prototype" object.
|
---|
261 | */
|
---|
262 | Object builtinArrayPrototype() const;
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Returns the builtin "Boolean.prototype" object.
|
---|
266 | */
|
---|
267 | Object builtinBooleanPrototype() const;
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Returns the builtin "String.prototype" object.
|
---|
271 | */
|
---|
272 | Object builtinStringPrototype() const;
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Returns the builtin "Number.prototype" object.
|
---|
276 | */
|
---|
277 | Object builtinNumberPrototype() const;
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Returns the builtin "Date.prototype" object.
|
---|
281 | */
|
---|
282 | Object builtinDatePrototype() const;
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Returns the builtin "RegExp.prototype" object.
|
---|
286 | */
|
---|
287 | Object builtinRegExpPrototype() const;
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Returns the builtin "Error.prototype" object.
|
---|
291 | */
|
---|
292 | Object builtinErrorPrototype() const;
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * The initial value of "Error" global property
|
---|
296 | */
|
---|
297 | Object builtinEvalError() const;
|
---|
298 | Object builtinRangeError() const;
|
---|
299 | Object builtinReferenceError() const;
|
---|
300 | Object builtinSyntaxError() const;
|
---|
301 | Object builtinTypeError() const;
|
---|
302 | Object builtinURIError() const;
|
---|
303 |
|
---|
304 | Object builtinEvalErrorPrototype() const;
|
---|
305 | Object builtinRangeErrorPrototype() const;
|
---|
306 | Object builtinReferenceErrorPrototype() const;
|
---|
307 | Object builtinSyntaxErrorPrototype() const;
|
---|
308 | Object builtinTypeErrorPrototype() const;
|
---|
309 | Object builtinURIErrorPrototype() const;
|
---|
310 |
|
---|
311 | enum CompatMode { NativeMode, IECompat, NetscapeCompat };
|
---|
312 | /**
|
---|
313 | * Call this to enable a compatibility mode with another browser.
|
---|
314 | * (by default konqueror is in "native mode").
|
---|
315 | * Currently, in KJS, this only changes the behaviour of Date::getYear()
|
---|
316 | * which returns the full year under IE.
|
---|
317 | */
|
---|
318 | void setCompatMode(CompatMode mode);
|
---|
319 | CompatMode compatMode() const;
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Called by InterpreterImp during the mark phase of the garbage collector
|
---|
323 | * Default implementation does nothing, this exist for classes that reimplement Interpreter.
|
---|
324 | */
|
---|
325 | virtual void mark() {}
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Provides a way to distinguish derived classes.
|
---|
329 | * Only useful if you reimplement Interpreter and if different kind of
|
---|
330 | * interpreters are created in the same process.
|
---|
331 | * The base class returns 0, the ECMA-bindings interpreter returns 1.
|
---|
332 | */
|
---|
333 | virtual int rtti() { return 0; }
|
---|
334 |
|
---|
335 | #ifdef KJS_DEBUG_MEM
|
---|
336 | /**
|
---|
337 | * @internal
|
---|
338 | */
|
---|
339 | static void finalCheck();
|
---|
340 | #endif
|
---|
341 | private:
|
---|
342 | InterpreterImp *rep;
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * This constructor is not implemented, in order to prevent
|
---|
346 | * copy-construction of Interpreter objects. You should always pass around
|
---|
347 | * pointers to an interpreter instance instead.
|
---|
348 | */
|
---|
349 | Interpreter(const Interpreter&);
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * This constructor is not implemented, in order to prevent assignment of
|
---|
353 | * Interpreter objects. You should always pass around pointers to an
|
---|
354 | * interpreter instance instead.
|
---|
355 | */
|
---|
356 | Interpreter operator=(const Interpreter&);
|
---|
357 | protected:
|
---|
358 | virtual void virtual_hook( int id, void* data );
|
---|
359 | };
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Represents the current state of script execution. This object allows you
|
---|
363 | * obtain a handle the interpreter that is currently executing the script,
|
---|
364 | * and also the current execution state context.
|
---|
365 | */
|
---|
366 | class ExecState {
|
---|
367 | friend class InterpreterImp;
|
---|
368 | friend class FunctionImp;
|
---|
369 | friend class GlobalFuncImp;
|
---|
370 | public:
|
---|
371 | virtual ~ExecState();
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Returns the interpreter associated with this execution state
|
---|
375 | *
|
---|
376 | * @return The interpreter executing the script
|
---|
377 | */
|
---|
378 | Interpreter *interpreter() const;
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Returns the execution context associated with this execution state
|
---|
382 | *
|
---|
383 | * @return The current execution state context
|
---|
384 | */
|
---|
385 | const Context context() const;
|
---|
386 |
|
---|
387 | void setException(const Value &e);
|
---|
388 | void clearException();
|
---|
389 | Value exception() const;
|
---|
390 | bool hadException() const;
|
---|
391 |
|
---|
392 | private:
|
---|
393 | ExecState(Interpreter *interp, ContextImp *con);
|
---|
394 | ExecStateImp *rep;
|
---|
395 | };
|
---|
396 |
|
---|
397 | }; // namespace
|
---|
398 |
|
---|
399 | #endif // _KJS_INTERPRETER_H_
|
---|