source: webkit/trunk/JavaScriptCore/kjs/interpreter.h@ 27128

Last change on this file since 27128 was 27100, checked in by mjs, 18 years ago

Windows build fix.

  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2001 Harri Porten ([email protected])
4 * Copyright (C) 2001 Peter Kelly ([email protected])
5 * Copyright (C) 2003 Apple Computer, Inc.
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 */
23
24#ifndef _KJS_INTERPRETER_H_
25#define _KJS_INTERPRETER_H_
26
27#include "ExecState.h"
28#include "protect.h"
29#include "value.h"
30#include "types.h"
31
32namespace KJS {
33
34 class ArrayObjectImp;
35 class ArrayPrototype;
36 class BooleanObjectImp;
37 class BooleanPrototype;
38 class DateObjectImp;
39 class DatePrototype;
40 class Debugger;
41 class ErrorObjectImp;
42 class ErrorPrototype;
43 class EvalError;
44 class EvalErrorPrototype;
45 class FunctionObjectImp;
46 class FunctionPrototype;
47 class JSGlobalObject;
48 class NativeErrorImp;
49 class NativeErrorPrototype;
50 class NumberObjectImp;
51 class NumberPrototype;
52 class ObjectObjectImp;
53 class ObjectPrototype;
54 class RangeError;
55 class RangeErrorPrototype;
56 class ReferenceError;
57 class ReferenceError;
58 class ReferenceErrorPrototype;
59 class RegExpObjectImp;
60 class RegExpPrototype;
61 class RuntimeMethod;
62 class SavedBuiltins;
63 class ScopeChain;
64 class StringObjectImp;
65 class StringPrototype;
66 class SyntaxErrorPrototype;
67 class TypeError;
68 class TypeErrorPrototype;
69 class UriError;
70 class UriErrorPrototype;
71
72 /**
73 * Interpreter objects can be used to evaluate ECMAScript code. Each
74 * interpreter has a global object which is used for the purposes of code
75 * evaluation, and also provides access to built-in properties such as
76 * " Object" and "Number".
77 */
78 class Interpreter {
79 friend class Collector;
80 public:
81 /**
82 * Creates a new interpreter. The supplied object will be used as the global
83 * object for all scripts executed with this interpreter. During
84 * constuction, all the standard properties such as "Object" and "Number"
85 * will be added to the global object.
86 *
87 * Note: You should not use the same global object for multiple
88 * interpreters.
89 *
90 * This is due do the fact that the built-in properties are set in the
91 * constructor, and if these objects have been modified from another
92 * interpreter (e.g. a script modifying String.prototype), the changes will
93 * be overridden.
94 *
95 * @param global The object to use as the global object for this interpreter
96 */
97 Interpreter(JSGlobalObject*);
98 /**
99 * Creates a new interpreter. A global object will be created and
100 * initialized with the standard global properties.
101 */
102 Interpreter();
103
104 /**
105 * Resets the global object's default properties and adds the default object
106 * prototype to its prototype chain.
107 */
108 void initGlobalObject();
109
110 /**
111 * Returns the object that is used as the global object during all script
112 * execution performed by this interpreter
113 */
114 JSGlobalObject* globalObject() const;
115
116 /**
117 * Returns the execution state object which can be used to execute
118 * scripts using this interpreter at a the "global" level, i.e. one
119 * with a execution context that has the global object as the "this"
120 * value, and who's scope chain contains only the global object.
121 *
122 * Note: this pointer remains constant for the life of the interpreter
123 * and should not be manually deleted.
124 *
125 * @return The interpreter global execution state object
126 */
127 virtual ExecState *globalExec();
128
129 /**
130 * Parses the supplied ECMAScript code and checks for syntax errors.
131 *
132 * @param code The code to check
133 * @return A normal completion if there were no syntax errors in the code,
134 * otherwise a throw completion with the syntax error as its value.
135 */
136 Completion checkSyntax(const UString& sourceURL, int startingLineNumber, const UString& code);
137 Completion checkSyntax(const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength);
138
139 /**
140 * Evaluates the supplied ECMAScript code.
141 *
142 * Since this method returns a Completion, you should check the type of
143 * completion to detect an error or before attempting to access the returned
144 * value. For example, if an error occurs during script execution and is not
145 * caught by the script, the completion type will be Throw.
146 *
147 * If the supplied code is invalid, a SyntaxError will be thrown.
148 *
149 * @param code The code to evaluate
150 * @param thisV The value to pass in as the "this" value for the script
151 * execution. This should either be jsNull() or an Object.
152 * @return A completion object representing the result of the execution.
153 */
154 Completion evaluate(const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength, JSValue* thisV = 0);
155 Completion evaluate(const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV = 0);
156
157 /**
158 * Returns the builtin "Object" object. This is the object that was set
159 * as a property of the global object during construction; if the property
160 * is replaced by script code, this method will still return the original
161 * object.
162 *
163 * @return The builtin "Object" object
164 */
165 JSObject *builtinObject() const;
166
167 /**
168 * Returns the builtin "Function" object.
169 */
170 JSObject *builtinFunction() const;
171
172 /**
173 * Returns the builtin "Array" object.
174 */
175 JSObject *builtinArray() const;
176
177 /**
178 * Returns the builtin "Boolean" object.
179 */
180 JSObject *builtinBoolean() const;
181
182 /**
183 * Returns the builtin "String" object.
184 */
185 JSObject *builtinString() const;
186
187 /**
188 * Returns the builtin "Number" object.
189 */
190 JSObject *builtinNumber() const;
191
192 /**
193 * Returns the builtin "Date" object.
194 */
195 JSObject *builtinDate() const;
196
197 /**
198 * Returns the builtin "RegExp" object.
199 */
200 JSObject *builtinRegExp() const;
201
202 /**
203 * Returns the builtin "Error" object.
204 */
205 JSObject *builtinError() const;
206
207 /**
208 * Returns the builtin "Object.prototype" object.
209 */
210 JSObject *builtinObjectPrototype() const;
211
212 /**
213 * Returns the builtin "Function.prototype" object.
214 */
215 JSObject *builtinFunctionPrototype() const;
216
217 /**
218 * Returns the builtin "Array.prototype" object.
219 */
220 JSObject *builtinArrayPrototype() const;
221
222 /**
223 * Returns the builtin "Boolean.prototype" object.
224 */
225 JSObject *builtinBooleanPrototype() const;
226
227 /**
228 * Returns the builtin "String.prototype" object.
229 */
230 JSObject *builtinStringPrototype() const;
231
232 /**
233 * Returns the builtin "Number.prototype" object.
234 */
235 JSObject *builtinNumberPrototype() const;
236
237 /**
238 * Returns the builtin "Date.prototype" object.
239 */
240 JSObject *builtinDatePrototype() const;
241
242 /**
243 * Returns the builtin "RegExp.prototype" object.
244 */
245 JSObject *builtinRegExpPrototype() const;
246
247 /**
248 * Returns the builtin "Error.prototype" object.
249 */
250 JSObject *builtinErrorPrototype() const;
251
252 /**
253 * The initial value of "Error" global property
254 */
255 JSObject *builtinEvalError() const;
256 JSObject *builtinRangeError() const;
257 JSObject *builtinReferenceError() const;
258 JSObject *builtinSyntaxError() const;
259 JSObject *builtinTypeError() const;
260 JSObject *builtinURIError() const;
261
262 JSObject *builtinEvalErrorPrototype() const;
263 JSObject *builtinRangeErrorPrototype() const;
264 JSObject *builtinReferenceErrorPrototype() const;
265 JSObject *builtinSyntaxErrorPrototype() const;
266 JSObject *builtinTypeErrorPrototype() const;
267 JSObject *builtinURIErrorPrototype() const;
268
269 enum CompatMode { NativeMode, IECompat, NetscapeCompat };
270 /**
271 * Call this to enable a compatibility mode with another browser.
272 * (by default konqueror is in "native mode").
273 * Currently, in KJS, this only changes the behavior of Date::getYear()
274 * which returns the full year under IE.
275 */
276 void setCompatMode(CompatMode mode) { m_compatMode = mode; }
277 CompatMode compatMode() const { return m_compatMode; }
278
279 /**
280 * Run the garbage collection. Returns true when at least one object
281 * was collected; false otherwise.
282 */
283 static bool collect();
284
285 /**
286 * Called during the mark phase of the garbage collector. Subclasses
287 * implementing custom mark methods must make sure to chain to this one.
288 */
289 virtual void mark();
290
291#ifdef KJS_DEBUG_MEM
292 /**
293 * @internal
294 */
295 static void finalCheck();
296#endif
297
298 static bool shouldPrintExceptions();
299 static void setShouldPrintExceptions(bool);
300
301 void saveBuiltins (SavedBuiltins&) const;
302 void restoreBuiltins (const SavedBuiltins&);
303
304 /**
305 * Determine if the it is 'safe' to execute code in the target interpreter from an
306 * object that originated in this interpreter. This check is used to enforce WebCore
307 * cross frame security rules. In particular, attempts to access 'bound' objects are
308 * not allowed unless isSafeScript returns true.
309 */
310 virtual bool isSafeScript(const Interpreter*) { return true; }
311
312 // Chained list of interpreters (ring)
313 static Interpreter* firstInterpreter() { return s_hook; }
314 Interpreter* nextInterpreter() const { return next; }
315 Interpreter* prevInterpreter() const { return prev; }
316
317 Debugger* debugger() const { return m_debugger; }
318 void setDebugger(Debugger* d) { m_debugger = d; }
319
320 void setCurrentExec(ExecState* exec) { m_currentExec = exec; }
321 ExecState* currentExec() const { return m_currentExec; }
322
323 void setTimeoutTime(unsigned timeoutTime) { m_timeoutTime = timeoutTime; }
324
325 void startTimeoutCheck();
326 void stopTimeoutCheck();
327
328 bool timedOut();
329
330 void ref() { ++m_refCount; }
331 void deref() { if (--m_refCount <= 0) delete this; }
332 int refCount() const { return m_refCount; }
333
334protected:
335 virtual ~Interpreter(); // only deref should delete us
336 virtual bool shouldInterruptScript() const { return true; }
337
338 unsigned m_timeoutTime;
339
340private:
341 void init();
342
343 void resetTimeoutCheck();
344 bool checkTimeout();
345
346 // Uncopyable
347 Interpreter(const Interpreter&);
348 Interpreter operator=(const Interpreter&);
349
350 int m_refCount;
351
352 ExecState* m_currentExec;
353 JSGlobalObject* m_globalObject;
354 ExecState m_globalExec;
355
356 // Chained list of interpreters (ring) - for collector
357 static Interpreter* s_hook;
358 Interpreter *next, *prev;
359
360 int m_recursion;
361
362 Debugger* m_debugger;
363 CompatMode m_compatMode;
364
365 unsigned m_timeAtLastCheckTimeout;
366 unsigned m_timeExecuting;
367 unsigned m_timeoutCheckCount;
368
369 unsigned m_tickCount;
370 unsigned m_ticksUntilNextTimeoutCheck;
371
372
373 ObjectObjectImp* m_Object;
374 FunctionObjectImp* m_Function;
375 ArrayObjectImp* m_Array;
376 BooleanObjectImp* m_Boolean;
377 StringObjectImp* m_String;
378 NumberObjectImp* m_Number;
379 DateObjectImp* m_Date;
380 RegExpObjectImp* m_RegExp;
381 ErrorObjectImp* m_Error;
382
383 ObjectPrototype* m_ObjectPrototype;
384 FunctionPrototype* m_FunctionPrototype;
385 ArrayPrototype* m_ArrayPrototype;
386 BooleanPrototype* m_BooleanPrototype;
387 StringPrototype* m_StringPrototype;
388 NumberPrototype* m_NumberPrototype;
389 DatePrototype* m_DatePrototype;
390 RegExpPrototype* m_RegExpPrototype;
391 ErrorPrototype* m_ErrorPrototype;
392
393 NativeErrorImp* m_EvalError;
394 NativeErrorImp* m_RangeError;
395 NativeErrorImp* m_ReferenceError;
396 NativeErrorImp* m_SyntaxError;
397 NativeErrorImp* m_TypeError;
398 NativeErrorImp* m_UriError;
399
400 NativeErrorPrototype* m_EvalErrorPrototype;
401 NativeErrorPrototype* m_RangeErrorPrototype;
402 NativeErrorPrototype* m_ReferenceErrorPrototype;
403 NativeErrorPrototype* m_SyntaxErrorPrototype;
404 NativeErrorPrototype* m_TypeErrorPrototype;
405 NativeErrorPrototype* m_UriErrorPrototype;
406 };
407
408 inline bool Interpreter::timedOut()
409 {
410 m_tickCount++;
411
412 if (m_tickCount != m_ticksUntilNextTimeoutCheck)
413 return false;
414
415 return checkTimeout();
416 }
417
418} // namespace
419
420#endif // _KJS_INTERPRETER_H_
Note: See TracBrowser for help on using the repository browser.