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

Last change on this file since 15990 was 15990, checked in by andersca, 19 years ago

2006-08-23 Anders Carlsson <[email protected]>

Reviewed by Darin.

Make the bindings compile without CoreFoundation.


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