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