2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2003 Apple Computer, Inc.
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.
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.
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.
24 #ifndef _KJS_INTERPRETER_H_
25 #define _KJS_INTERPRETER_H_
27 #include "ExecState.h"
47 * Interpreter objects can be used to evaluate ECMAScript code. Each
48 * interpreter has a global object which is used for the purposes of code
49 * evaluation, and also provides access to built-in properties such as
50 * " Object" and "Number".
52 class KJS_EXPORT Interpreter
{
53 friend class Collector
;
54 friend class TimeoutChecker
;
57 * Creates a new interpreter. The supplied object will be used as the global
58 * object for all scripts executed with this interpreter. During
59 * construction, all the standard properties such as "Object" and "Number"
60 * will be added to the global object.
62 * Note: You should not use the same global object for multiple
65 * This is due do the fact that the built-in properties are set in the
66 * constructor, and if these objects have been modified from another
67 * interpreter (e.g. a script modifying String.prototype), the changes will
70 * @param global The object to use as the global object for this interpreter
72 Interpreter(JSObject
* globalObject
);
74 * Creates a new interpreter. A global object will be created and
75 * initialized with the standard global properties.
80 * Returns the object that is used as the global object during all script
81 * execution performed by this interpreter
83 JSObject
* globalObject() const;
84 void initGlobalObject();
87 * Returns the execution state object which can be used to execute
88 * scripts using this interpreter at a the "global" level, i.e. one
89 * with a execution context that has the global object as the "this"
90 * value, and who's scope chain contains only the global object.
92 * Note: this pointer remains constant for the life of the interpreter
93 * and should not be manually deleted.
95 * @return The interpreter global execution state object
97 virtual ExecState
*globalExec();
100 * Sets the package instance that will be used to resolve the
101 * first level of identifiers of import statements.
103 * If no package is set which will make any "import" script
104 * statement fail with an error. This is the default in e.g. a
105 * Web browser where package imports should be disabled for
108 void setGlobalPackage(Package
* p
);
111 * Returns the package that was installed to handle top level
112 * package requests. Returns 0, the default, when no package was
115 * @return The global package
117 Package
* globalPackage();
120 * Parses the supplied ECMAScript code and checks for syntax errors.
122 * @param code The code to check
123 * @return A normal completion if there were no syntax errors in the code,
124 * otherwise a throw completion with the syntax error as its value.
126 Completion
checkSyntax(const UString
& sourceURL
, int startingLineNumber
, const UString
& code
);
127 Completion
checkSyntax(const UString
& sourceURL
, int startingLineNumber
, const UChar
* code
, int codeLength
);
130 * Evaluates the supplied ECMAScript code.
132 * Since this method returns a Completion, you should check the type of
133 * completion to detect an error or before attempting to access the returned
134 * value. For example, if an error occurs during script execution and is not
135 * caught by the script, the completion type will be Throw.
137 * If the supplied code is invalid, a SyntaxError will be thrown.
139 * @param code The code to evaluate
140 * @param thisV The value to pass in as the "this" value for the script
141 * execution. This should either be jsNull() or an Object.
142 * @return A completion object representing the result of the execution.
144 Completion
evaluate(const UString
& sourceURL
, int startingLineNumber
, const UChar
* code
, int codeLength
, JSValue
* thisV
= 0);
145 Completion
evaluate(const UString
& sourceURL
, int startingLineNumber
, const UString
& code
, JSValue
* thisV
= 0);
148 * Returns the builtin "Object" object. This is the object that was set
149 * as a property of the global object during construction; if the property
150 * is replaced by script code, this method will still return the original
153 * @return The builtin "Object" object
155 JSObject
*builtinObject() const;
158 * Returns the builtin "Function" object.
160 JSObject
*builtinFunction() const;
163 * Returns the builtin "Array" object.
165 JSObject
*builtinArray() const;
168 * Returns the builtin "Boolean" object.
170 JSObject
*builtinBoolean() const;
173 * Returns the builtin "String" object.
175 JSObject
*builtinString() const;
178 * Returns the builtin "Number" object.
180 JSObject
*builtinNumber() const;
183 * Returns the builtin "Date" object.
185 JSObject
*builtinDate() const;
188 * Returns the builtin "RegExp" object.
190 JSObject
*builtinRegExp() const;
193 * Returns the builtin "Error" object.
195 JSObject
*builtinError() const;
198 * Returns the builtin "Object.prototype" object.
200 JSObject
*builtinObjectPrototype() const;
203 * Returns the builtin "Function.prototype" object.
205 JSObject
*builtinFunctionPrototype() const;
208 * Returns the builtin "Array.prototype" object.
210 JSObject
*builtinArrayPrototype() const;
213 * Returns the builtin "Boolean.prototype" object.
215 JSObject
*builtinBooleanPrototype() const;
218 * Returns the builtin "String.prototype" object.
220 JSObject
*builtinStringPrototype() const;
223 * Returns the builtin "Number.prototype" object.
225 JSObject
*builtinNumberPrototype() const;
228 * Returns the builtin "Date.prototype" object.
230 JSObject
*builtinDatePrototype() const;
233 * Returns the builtin "RegExp.prototype" object.
235 JSObject
*builtinRegExpPrototype() const;
238 * Returns the builtin "Error.prototype" object.
240 JSObject
*builtinErrorPrototype() const;
243 * The initial value of "Error" global property
245 JSObject
*builtinEvalError() const;
246 JSObject
*builtinRangeError() const;
247 JSObject
*builtinReferenceError() const;
248 JSObject
*builtinSyntaxError() const;
249 JSObject
*builtinTypeError() const;
250 JSObject
*builtinURIError() const;
252 JSObject
*builtinEvalErrorPrototype() const;
253 JSObject
*builtinRangeErrorPrototype() const;
254 JSObject
*builtinReferenceErrorPrototype() const;
255 JSObject
*builtinSyntaxErrorPrototype() const;
256 JSObject
*builtinTypeErrorPrototype() const;
257 JSObject
*builtinURIErrorPrototype() const;
259 enum CompatMode
{ NativeMode
, IECompat
, NetscapeCompat
};
261 * Call this to enable a compatibility mode with another browser.
262 * (by default konqueror is in "native mode").
263 * Currently, in KJS, this only changes the behavior of Date::getYear()
264 * which returns the full year under IE.
266 void setCompatMode(CompatMode mode
) { m_compatMode
= mode
; }
267 CompatMode
compatMode() const { return m_compatMode
; }
270 * Run the garbage collection. Returns true when at least one object
271 * was collected; false otherwise.
273 static bool collect();
276 * Called during the mark phase of the garbage collector. Subclasses
277 * implementing custom mark methods must make sure to chain to this one.
279 virtual void mark(bool currentThreadIsMainThread
);
282 * Provides a way to distinguish derived classes.
283 * Only useful if you reimplement Interpreter and if different kind of
284 * interpreters are created in the same process.
285 * The base class returns 0, the ECMA-bindings interpreter returns 1.
287 virtual int rtti() { return 0; }
293 static void finalCheck();
296 static bool shouldPrintExceptions();
297 static void setShouldPrintExceptions(bool);
299 void saveBuiltins (SavedBuiltins
&) const;
300 void restoreBuiltins (const SavedBuiltins
&);
303 * Determine if the value is a global object (for any interpreter). This may
304 * be difficult to determine for multiple uses of JSC in a process that are
305 * logically independent of each other. In the case of a Web browser, this method
306 * is used to determine if an object is the Window object so we can perform
309 virtual bool isGlobalObject(JSValue
*) { return false; }
312 * Find the interpreter for a particular global object. This should really
313 * be a static method, but we can't do that is C++. Again, as with isGlobalObject()
314 * implementation really need to know about all instances of Interpreter
315 * created in an application to correctly implement this method. The only
316 * override of this method is currently in WebCore.
318 virtual Interpreter
* interpreterForGlobalObject(const JSValue
*) { return 0; }
321 * Determine if the it is 'safe' to execute code in the target interpreter from an
322 * object that originated in this interpreter. This check is used to enforce WebCore
323 * cross frame security rules. In particular, attempts to access 'bound' objects are
324 * not allowed unless isSafeScript returns true.
326 virtual bool isSafeScript(const Interpreter
*) { return true; }
329 virtual void *createLanguageInstanceForValue(ExecState
*, int language
, JSObject
* value
, const Bindings::RootObject
* origin
, const Bindings::RootObject
* current
);
332 // Chained list of interpreters (ring)
333 static Interpreter
* firstInterpreter() { return s_hook
; }
334 Interpreter
* nextInterpreter() const { return next
; }
335 Interpreter
* prevInterpreter() const { return prev
; }
337 Debugger
* debugger() const { return m_debugger
; }
338 void setDebugger(Debugger
* d
) { m_debugger
= d
; }
340 void setContext(Context
* c
) { m_context
= c
; }
341 Context
* context() const { return m_context
; }
343 static Interpreter
* interpreterWithGlobalObject(JSObject
*);
345 void setTimeoutTime(unsigned timeoutTime
) { m_timeoutTime
= timeoutTime
; }
347 void startTimeoutCheck();
348 void stopTimeoutCheck();
350 void pauseTimeoutCheck();
351 void resumeTimeoutCheck();
355 void ref() { ++m_refCount
; }
356 void deref() { if (--m_refCount
<= 0) delete this; }
357 int refCount() const { return m_refCount
; }
360 virtual ~Interpreter(); // only deref should delete us
361 virtual bool shouldInterruptScript() const { return true; }
366 bool handleTimeout();
368 void printException(const Completion
& c
, const UString
& sourceURL
);
371 * This constructor is not implemented, in order to prevent
372 * copy-construction of Interpreter objects. You should always pass around
373 * pointers to an interpreter instance instead.
375 Interpreter(const Interpreter
&);
378 * This operator is not implemented, in order to prevent assignment of
379 * Interpreter objects. You should always pass around pointers to an
380 * interpreter instance instead.
382 Interpreter
operator=(const Interpreter
&);
386 ExecState m_globalExec
;
387 JSObject
* m_globalObject
;
390 // Chained list of interpreters (ring) - for collector
391 static Interpreter
* s_hook
;
392 Interpreter
*next
, *prev
;
396 Debugger
* m_debugger
;
398 CompatMode m_compatMode
;
400 TimeoutChecker
* m_timeoutChecker
;
403 unsigned m_startTimeoutCheckCount
;
404 unsigned m_pauseTimeoutCheckCount
;
406 ProtectedPtr
<JSObject
> m_Object
;
407 ProtectedPtr
<JSObject
> m_Function
;
408 ProtectedPtr
<JSObject
> m_Array
;
409 ProtectedPtr
<JSObject
> m_Boolean
;
410 ProtectedPtr
<JSObject
> m_String
;
411 ProtectedPtr
<JSObject
> m_Number
;
412 ProtectedPtr
<JSObject
> m_Date
;
413 ProtectedPtr
<JSObject
> m_RegExp
;
414 ProtectedPtr
<JSObject
> m_Error
;
416 ProtectedPtr
<JSObject
> m_ObjectPrototype
;
417 ProtectedPtr
<JSObject
> m_FunctionPrototype
;
418 ProtectedPtr
<JSObject
> m_ArrayPrototype
;
419 ProtectedPtr
<JSObject
> m_BooleanPrototype
;
420 ProtectedPtr
<JSObject
> m_StringPrototype
;
421 ProtectedPtr
<JSObject
> m_NumberPrototype
;
422 ProtectedPtr
<JSObject
> m_DatePrototype
;
423 ProtectedPtr
<JSObject
> m_RegExpPrototype
;
424 ProtectedPtr
<JSObject
> m_ErrorPrototype
;
426 ProtectedPtr
<JSObject
> m_EvalError
;
427 ProtectedPtr
<JSObject
> m_RangeError
;
428 ProtectedPtr
<JSObject
> m_ReferenceError
;
429 ProtectedPtr
<JSObject
> m_SyntaxError
;
430 ProtectedPtr
<JSObject
> m_TypeError
;
431 ProtectedPtr
<JSObject
> m_UriError
;
433 ProtectedPtr
<JSObject
> m_EvalErrorPrototype
;
434 ProtectedPtr
<JSObject
> m_RangeErrorPrototype
;
435 ProtectedPtr
<JSObject
> m_ReferenceErrorPrototype
;
436 ProtectedPtr
<JSObject
> m_SyntaxErrorPrototype
;
437 ProtectedPtr
<JSObject
> m_TypeErrorPrototype
;
438 ProtectedPtr
<JSObject
> m_UriErrorPrototype
;
441 inline bool Interpreter::checkTimeout()
446 return handleTimeout();
450 * Interface to set enhanced Unicode support functions. By default
451 * the interpreter will use the standard C library functions.
455 class KJS_EXPORT UnicodeSupport
460 typedef bool (*CharCategoryFunction
)(int c
);
461 static void setIdentStartChecker(CharCategoryFunction f
);
462 static void setIdentPartChecker(CharCategoryFunction f
);
464 typedef int (*StringConversionFunction
)(uint16_t* str
, int strLength
,
465 uint16_t*& destIfNeeded
);
466 static void setToLowerFunction(StringConversionFunction f
);
467 static void setToUpperFunction(StringConversionFunction f
);
473 #endif // _KJS_INTERPRETER_H_