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