1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
5 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
6 | * Copyright (C) 2003 Apple Computer, Inc.
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Library General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Library General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Library General Public License
|
---|
19 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "interpreter.h"
|
---|
27 |
|
---|
28 | #include "SavedBuiltins.h"
|
---|
29 | #include "array_object.h"
|
---|
30 | #include "bool_object.h"
|
---|
31 | #include "collector.h"
|
---|
32 | #include "context.h"
|
---|
33 | #include "date_object.h"
|
---|
34 | #include "debugger.h"
|
---|
35 | #include "error_object.h"
|
---|
36 | #include "function_object.h"
|
---|
37 | #include "internal.h"
|
---|
38 | #include "math_object.h"
|
---|
39 | #include "nodes.h"
|
---|
40 | #include "number_object.h"
|
---|
41 | #include "object.h"
|
---|
42 | #include "object_object.h"
|
---|
43 | #include "operations.h"
|
---|
44 | #include "regexp_object.h"
|
---|
45 | #include "string_object.h"
|
---|
46 | #include "types.h"
|
---|
47 | #include "value.h"
|
---|
48 |
|
---|
49 | #if PLATFORM(MAC)
|
---|
50 | #include "runtime.h"
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #if HAVE(SYS_TIME_H)
|
---|
54 | #include <sys/time.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #include <assert.h>
|
---|
58 | #include <math.h>
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <signal.h>
|
---|
61 |
|
---|
62 | namespace KJS {
|
---|
63 |
|
---|
64 | class TimeoutChecker {
|
---|
65 | public:
|
---|
66 | void startTimeoutCheck(Interpreter*);
|
---|
67 | void stopTimeoutCheck(Interpreter*);
|
---|
68 | void pauseTimeoutCheck(Interpreter*);
|
---|
69 | void resumeTimeoutCheck(Interpreter*);
|
---|
70 |
|
---|
71 | private:
|
---|
72 | #if HAVE(SYS_TIME_H)
|
---|
73 | static Interpreter* s_executingInterpreter;
|
---|
74 | static void alarmHandler(int);
|
---|
75 |
|
---|
76 | Interpreter* m_oldInterpreter;
|
---|
77 | itimerval m_oldtv;
|
---|
78 | itimerval m_pausetv;
|
---|
79 | void (*m_oldAlarmHandler)(int);
|
---|
80 | #endif
|
---|
81 | };
|
---|
82 |
|
---|
83 | #if HAVE(SYS_TIME_H)
|
---|
84 | Interpreter* TimeoutChecker::s_executingInterpreter = 0;
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | void TimeoutChecker::startTimeoutCheck(Interpreter *interpreter)
|
---|
88 | {
|
---|
89 | if (!interpreter->m_timeoutTime)
|
---|
90 | return;
|
---|
91 |
|
---|
92 | interpreter->m_startTimeoutCheckCount++;
|
---|
93 |
|
---|
94 | #if HAVE(SYS_TIME_H)
|
---|
95 | if (s_executingInterpreter == interpreter)
|
---|
96 | return;
|
---|
97 |
|
---|
98 | // Block signals
|
---|
99 | m_oldAlarmHandler = signal(SIGALRM, SIG_IGN);
|
---|
100 |
|
---|
101 | m_oldInterpreter = s_executingInterpreter;
|
---|
102 | s_executingInterpreter = interpreter;
|
---|
103 |
|
---|
104 | itimerval tv = {
|
---|
105 | { interpreter->m_timeoutTime / 1000, (interpreter->m_timeoutTime % 1000) * 1000 },
|
---|
106 | { interpreter->m_timeoutTime / 1000, (interpreter->m_timeoutTime % 1000) * 1000 }
|
---|
107 | };
|
---|
108 | setitimer(ITIMER_REAL, &tv, &m_oldtv);
|
---|
109 |
|
---|
110 | // Unblock signals
|
---|
111 | signal(SIGALRM, alarmHandler);
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 |
|
---|
115 | void TimeoutChecker::stopTimeoutCheck(Interpreter* interpreter)
|
---|
116 | {
|
---|
117 | if (!interpreter->m_timeoutTime)
|
---|
118 | return;
|
---|
119 |
|
---|
120 | ASSERT(interpreter->m_startTimeoutCheckCount > 0);
|
---|
121 |
|
---|
122 | interpreter->m_startTimeoutCheckCount--;
|
---|
123 |
|
---|
124 | if (interpreter->m_startTimeoutCheckCount != 0)
|
---|
125 | return;
|
---|
126 |
|
---|
127 | #if HAVE(SYS_TIME_H)
|
---|
128 | signal(SIGALRM, SIG_IGN);
|
---|
129 |
|
---|
130 | s_executingInterpreter = m_oldInterpreter;
|
---|
131 |
|
---|
132 | setitimer(ITIMER_REAL, &m_oldtv, 0L);
|
---|
133 | signal(SIGALRM, m_oldAlarmHandler);
|
---|
134 | #endif
|
---|
135 | }
|
---|
136 |
|
---|
137 | #if HAVE(SYS_TIME_H)
|
---|
138 | void TimeoutChecker::alarmHandler(int)
|
---|
139 | {
|
---|
140 | s_executingInterpreter->m_timedOut = true;
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | void TimeoutChecker::pauseTimeoutCheck(Interpreter* interpreter)
|
---|
145 | {
|
---|
146 | if (interpreter->m_startTimeoutCheckCount == 0)
|
---|
147 | return;
|
---|
148 |
|
---|
149 | #if HAVE(SYS_TIME_H)
|
---|
150 | ASSERT(interpreter == s_executingInterpreter);
|
---|
151 |
|
---|
152 | void (*currentSignalHandler)(int);
|
---|
153 |
|
---|
154 | // Block signal
|
---|
155 | currentSignalHandler = signal(SIGALRM, SIG_IGN);
|
---|
156 |
|
---|
157 | if (currentSignalHandler != alarmHandler) {
|
---|
158 | signal(SIGALRM, currentSignalHandler);
|
---|
159 | return;
|
---|
160 | }
|
---|
161 |
|
---|
162 | setitimer(ITIMER_REAL, 0L, &m_pausetv);
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | interpreter->m_pauseTimeoutCheckCount++;
|
---|
166 | }
|
---|
167 |
|
---|
168 | void TimeoutChecker::resumeTimeoutCheck(Interpreter* interpreter)
|
---|
169 | {
|
---|
170 | if (interpreter->m_startTimeoutCheckCount == 0)
|
---|
171 | return;
|
---|
172 |
|
---|
173 | #if HAVE(SYS_TIME_H)
|
---|
174 | ASSERT(interpreter == s_executingInterpreter);
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | interpreter->m_pauseTimeoutCheckCount--;
|
---|
178 |
|
---|
179 | if (interpreter->m_pauseTimeoutCheckCount != 0)
|
---|
180 | return;
|
---|
181 |
|
---|
182 | #if HAVE(SYS_TIME_H)
|
---|
183 | void (*currentSignalHandler)(int);
|
---|
184 |
|
---|
185 | // Check so we have the right handler
|
---|
186 | currentSignalHandler = signal(SIGALRM, SIG_IGN);
|
---|
187 |
|
---|
188 | if (currentSignalHandler != SIG_IGN) {
|
---|
189 | signal(SIGALRM, currentSignalHandler);
|
---|
190 | return;
|
---|
191 | }
|
---|
192 |
|
---|
193 | setitimer(ITIMER_REAL, &m_pausetv, 0L);
|
---|
194 |
|
---|
195 | // Unblock signal
|
---|
196 | currentSignalHandler = signal(SIGALRM, alarmHandler);
|
---|
197 | #endif
|
---|
198 | }
|
---|
199 |
|
---|
200 | Interpreter* Interpreter::s_hook = 0;
|
---|
201 |
|
---|
202 | typedef HashMap<JSObject*, Interpreter*> InterpreterMap;
|
---|
203 | static inline InterpreterMap &interpreterMap()
|
---|
204 | {
|
---|
205 | static InterpreterMap* map = new InterpreterMap;
|
---|
206 | return* map;
|
---|
207 | }
|
---|
208 |
|
---|
209 | Interpreter::Interpreter(JSObject* globalObject)
|
---|
210 | : m_timeoutTime(0)
|
---|
211 | , m_globalExec(this, 0)
|
---|
212 | , m_globalObject(globalObject)
|
---|
213 | , m_argumentsPropertyName(&argumentsPropertyName)
|
---|
214 | , m_specialPrototypePropertyName(&specialPrototypePropertyName)
|
---|
215 | , m_timeoutChecker(0)
|
---|
216 | , m_timedOut(false)
|
---|
217 | , m_startTimeoutCheckCount(0)
|
---|
218 | , m_pauseTimeoutCheckCount(0)
|
---|
219 | {
|
---|
220 | init();
|
---|
221 | }
|
---|
222 |
|
---|
223 | Interpreter::Interpreter()
|
---|
224 | : m_timeoutTime(0)
|
---|
225 | , m_globalExec(this, 0)
|
---|
226 | , m_globalObject(new JSObject())
|
---|
227 | , m_argumentsPropertyName(&argumentsPropertyName)
|
---|
228 | , m_specialPrototypePropertyName(&specialPrototypePropertyName)
|
---|
229 | , m_timeoutChecker(0)
|
---|
230 | , m_timedOut(false)
|
---|
231 | , m_startTimeoutCheckCount(0)
|
---|
232 | , m_pauseTimeoutCheckCount(0)
|
---|
233 | {
|
---|
234 | init();
|
---|
235 | }
|
---|
236 |
|
---|
237 | void Interpreter::init()
|
---|
238 | {
|
---|
239 | JSLock lock;
|
---|
240 |
|
---|
241 | m_recursion = 0;
|
---|
242 | m_debugger= 0;
|
---|
243 | m_context = 0;
|
---|
244 | m_compatMode = NativeMode;
|
---|
245 |
|
---|
246 | interpreterMap().set(m_globalObject, this);
|
---|
247 |
|
---|
248 | if (s_hook) {
|
---|
249 | prev = s_hook;
|
---|
250 | next = s_hook->next;
|
---|
251 | s_hook->next->prev = this;
|
---|
252 | s_hook->next = this;
|
---|
253 | } else {
|
---|
254 | // This is the first interpreter
|
---|
255 | s_hook = next = prev = this;
|
---|
256 | }
|
---|
257 |
|
---|
258 | initGlobalObject();
|
---|
259 | }
|
---|
260 |
|
---|
261 | Interpreter::~Interpreter()
|
---|
262 | {
|
---|
263 | JSLock lock;
|
---|
264 |
|
---|
265 | ASSERT (m_startTimeoutCheckCount == 0);
|
---|
266 | ASSERT (m_pauseTimeoutCheckCount == 0);
|
---|
267 |
|
---|
268 | delete m_timeoutChecker;
|
---|
269 |
|
---|
270 | if (m_debugger)
|
---|
271 | m_debugger->detach(this);
|
---|
272 |
|
---|
273 | next->prev = prev;
|
---|
274 | prev->next = next;
|
---|
275 | s_hook = next;
|
---|
276 | if (s_hook == this)
|
---|
277 | {
|
---|
278 | // This was the last interpreter
|
---|
279 | s_hook = 0;
|
---|
280 | }
|
---|
281 |
|
---|
282 | interpreterMap().remove(m_globalObject);
|
---|
283 | }
|
---|
284 |
|
---|
285 | JSObject* Interpreter::globalObject() const
|
---|
286 | {
|
---|
287 | return m_globalObject;
|
---|
288 | }
|
---|
289 |
|
---|
290 | void Interpreter::initGlobalObject()
|
---|
291 | {
|
---|
292 | Identifier::init();
|
---|
293 |
|
---|
294 | FunctionPrototype *funcProto = new FunctionPrototype(&m_globalExec);
|
---|
295 | m_FunctionPrototype = funcProto;
|
---|
296 | ObjectPrototype *objProto = new ObjectPrototype(&m_globalExec, funcProto);
|
---|
297 | m_ObjectPrototype = objProto;
|
---|
298 | funcProto->setPrototype(m_ObjectPrototype);
|
---|
299 |
|
---|
300 | ArrayPrototype *arrayProto = new ArrayPrototype(&m_globalExec, objProto);
|
---|
301 | m_ArrayPrototype = arrayProto;
|
---|
302 | StringPrototype *stringProto = new StringPrototype(&m_globalExec, objProto);
|
---|
303 | m_StringPrototype = stringProto;
|
---|
304 | BooleanPrototype *booleanProto = new BooleanPrototype(&m_globalExec, objProto, funcProto);
|
---|
305 | m_BooleanPrototype = booleanProto;
|
---|
306 | NumberPrototype *numberProto = new NumberPrototype(&m_globalExec, objProto, funcProto);
|
---|
307 | m_NumberPrototype = numberProto;
|
---|
308 | DatePrototype *dateProto = new DatePrototype(&m_globalExec, objProto);
|
---|
309 | m_DatePrototype = dateProto;
|
---|
310 | RegExpPrototype *regexpProto = new RegExpPrototype(&m_globalExec, objProto, funcProto);
|
---|
311 | m_RegExpPrototype = regexpProto;
|
---|
312 | ErrorPrototype *errorProto = new ErrorPrototype(&m_globalExec, objProto, funcProto);
|
---|
313 | m_ErrorPrototype = errorProto;
|
---|
314 |
|
---|
315 | JSObject* o = m_globalObject;
|
---|
316 | while (o->prototype()->isObject())
|
---|
317 | o = static_cast<JSObject*>(o->prototype());
|
---|
318 | o->setPrototype(m_ObjectPrototype);
|
---|
319 |
|
---|
320 | // Constructors (Object, Array, etc.)
|
---|
321 | m_Object = new ObjectObjectImp(&m_globalExec, objProto, funcProto);
|
---|
322 | m_Function = new FunctionObjectImp(&m_globalExec, funcProto);
|
---|
323 | m_Array = new ArrayObjectImp(&m_globalExec, funcProto, arrayProto);
|
---|
324 | m_String = new StringObjectImp(&m_globalExec, funcProto, stringProto);
|
---|
325 | m_Boolean = new BooleanObjectImp(&m_globalExec, funcProto, booleanProto);
|
---|
326 | m_Number = new NumberObjectImp(&m_globalExec, funcProto, numberProto);
|
---|
327 | m_Date = new DateObjectImp(&m_globalExec, funcProto, dateProto);
|
---|
328 | m_RegExp = new RegExpObjectImp(&m_globalExec, funcProto, regexpProto);
|
---|
329 | m_Error = new ErrorObjectImp(&m_globalExec, funcProto, errorProto);
|
---|
330 |
|
---|
331 | // Error object prototypes
|
---|
332 | m_EvalErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, EvalError, "EvalError", "EvalError");
|
---|
333 | m_RangeErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, RangeError, "RangeError", "RangeError");
|
---|
334 | m_ReferenceErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, ReferenceError, "ReferenceError", "ReferenceError");
|
---|
335 | m_SyntaxErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, SyntaxError, "SyntaxError", "SyntaxError");
|
---|
336 | m_TypeErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, TypeError, "TypeError", "TypeError");
|
---|
337 | m_UriErrorPrototype = new NativeErrorPrototype(&m_globalExec, errorProto, URIError, "URIError", "URIError");
|
---|
338 |
|
---|
339 | // Error objects
|
---|
340 | m_EvalError = new NativeErrorImp(&m_globalExec, funcProto, m_EvalErrorPrototype);
|
---|
341 | m_RangeError = new NativeErrorImp(&m_globalExec, funcProto, m_RangeErrorPrototype);
|
---|
342 | m_ReferenceError = new NativeErrorImp(&m_globalExec, funcProto, m_ReferenceErrorPrototype);
|
---|
343 | m_SyntaxError = new NativeErrorImp(&m_globalExec, funcProto, m_SyntaxErrorPrototype);
|
---|
344 | m_TypeError = new NativeErrorImp(&m_globalExec, funcProto, m_TypeErrorPrototype);
|
---|
345 | m_UriError = new NativeErrorImp(&m_globalExec, funcProto, m_UriErrorPrototype);
|
---|
346 |
|
---|
347 | // ECMA 15.3.4.1
|
---|
348 | funcProto->put(&m_globalExec, constructorPropertyName, m_Function, DontEnum);
|
---|
349 |
|
---|
350 | m_globalObject->put(&m_globalExec, "Object", m_Object, DontEnum);
|
---|
351 | m_globalObject->put(&m_globalExec, "Function", m_Function, DontEnum);
|
---|
352 | m_globalObject->put(&m_globalExec, "Array", m_Array, DontEnum);
|
---|
353 | m_globalObject->put(&m_globalExec, "Boolean", m_Boolean, DontEnum);
|
---|
354 | m_globalObject->put(&m_globalExec, "String", m_String, DontEnum);
|
---|
355 | m_globalObject->put(&m_globalExec, "Number", m_Number, DontEnum);
|
---|
356 | m_globalObject->put(&m_globalExec, "Date", m_Date, DontEnum);
|
---|
357 | m_globalObject->put(&m_globalExec, "RegExp", m_RegExp, DontEnum);
|
---|
358 | m_globalObject->put(&m_globalExec, "Error", m_Error, DontEnum);
|
---|
359 | // Using Internal for those to have something != 0
|
---|
360 | // (see kjs_window). Maybe DontEnum would be ok too ?
|
---|
361 | m_globalObject->put(&m_globalExec, "EvalError",m_EvalError, Internal);
|
---|
362 | m_globalObject->put(&m_globalExec, "RangeError",m_RangeError, Internal);
|
---|
363 | m_globalObject->put(&m_globalExec, "ReferenceError",m_ReferenceError, Internal);
|
---|
364 | m_globalObject->put(&m_globalExec, "SyntaxError",m_SyntaxError, Internal);
|
---|
365 | m_globalObject->put(&m_globalExec, "TypeError",m_TypeError, Internal);
|
---|
366 | m_globalObject->put(&m_globalExec, "URIError",m_UriError, Internal);
|
---|
367 |
|
---|
368 | // Set the constructorPropertyName property of all builtin constructors
|
---|
369 | objProto->put(&m_globalExec, constructorPropertyName, m_Object, DontEnum | DontDelete | ReadOnly);
|
---|
370 | funcProto->put(&m_globalExec, constructorPropertyName, m_Function, DontEnum | DontDelete | ReadOnly);
|
---|
371 | arrayProto->put(&m_globalExec, constructorPropertyName, m_Array, DontEnum | DontDelete | ReadOnly);
|
---|
372 | booleanProto->put(&m_globalExec, constructorPropertyName, m_Boolean, DontEnum | DontDelete | ReadOnly);
|
---|
373 | stringProto->put(&m_globalExec, constructorPropertyName, m_String, DontEnum | DontDelete | ReadOnly);
|
---|
374 | numberProto->put(&m_globalExec, constructorPropertyName, m_Number, DontEnum | DontDelete | ReadOnly);
|
---|
375 | dateProto->put(&m_globalExec, constructorPropertyName, m_Date, DontEnum | DontDelete | ReadOnly);
|
---|
376 | regexpProto->put(&m_globalExec, constructorPropertyName, m_RegExp, DontEnum | DontDelete | ReadOnly);
|
---|
377 | errorProto->put(&m_globalExec, constructorPropertyName, m_Error, DontEnum | DontDelete | ReadOnly);
|
---|
378 | m_EvalErrorPrototype->put(&m_globalExec, constructorPropertyName, m_EvalError, DontEnum | DontDelete | ReadOnly);
|
---|
379 | m_RangeErrorPrototype->put(&m_globalExec, constructorPropertyName, m_RangeError, DontEnum | DontDelete | ReadOnly);
|
---|
380 | m_ReferenceErrorPrototype->put(&m_globalExec, constructorPropertyName, m_ReferenceError, DontEnum | DontDelete | ReadOnly);
|
---|
381 | m_SyntaxErrorPrototype->put(&m_globalExec, constructorPropertyName, m_SyntaxError, DontEnum | DontDelete | ReadOnly);
|
---|
382 | m_TypeErrorPrototype->put(&m_globalExec, constructorPropertyName, m_TypeError, DontEnum | DontDelete | ReadOnly);
|
---|
383 | m_UriErrorPrototype->put(&m_globalExec, constructorPropertyName, m_UriError, DontEnum | DontDelete | ReadOnly);
|
---|
384 |
|
---|
385 | // built-in values
|
---|
386 | m_globalObject->put(&m_globalExec, "NaN", jsNaN(), DontEnum|DontDelete);
|
---|
387 | m_globalObject->put(&m_globalExec, "Infinity", jsNumber(Inf), DontEnum|DontDelete);
|
---|
388 | m_globalObject->put(&m_globalExec, "undefined", jsUndefined(), DontEnum|DontDelete);
|
---|
389 |
|
---|
390 | // built-in functions
|
---|
391 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::Eval, 1, "eval"), DontEnum);
|
---|
392 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::ParseInt, 2, "parseInt"), DontEnum);
|
---|
393 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::ParseFloat, 1, "parseFloat"), DontEnum);
|
---|
394 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::IsNaN, 1, "isNaN"), DontEnum);
|
---|
395 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::IsFinite, 1, "isFinite"), DontEnum);
|
---|
396 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::Escape, 1, "escape"), DontEnum);
|
---|
397 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::UnEscape, 1, "unescape"), DontEnum);
|
---|
398 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::DecodeURI, 1, "decodeURI"), DontEnum);
|
---|
399 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::DecodeURIComponent, 1, "decodeURIComponent"), DontEnum);
|
---|
400 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::EncodeURI, 1, "encodeURI"), DontEnum);
|
---|
401 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::EncodeURIComponent, 1, "encodeURIComponent"), DontEnum);
|
---|
402 | #ifndef NDEBUG
|
---|
403 | m_globalObject->putDirectFunction(new GlobalFuncImp(&m_globalExec, funcProto, GlobalFuncImp::KJSPrint, 1, "kjsprint"), DontEnum);
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | // built-in objects
|
---|
407 | m_globalObject->put(&m_globalExec, "Math", new MathObjectImp(&m_globalExec, objProto), DontEnum);
|
---|
408 | }
|
---|
409 |
|
---|
410 | ExecState* Interpreter::globalExec()
|
---|
411 | {
|
---|
412 | return &m_globalExec;
|
---|
413 | }
|
---|
414 |
|
---|
415 | bool Interpreter::checkSyntax(const UString &code)
|
---|
416 | {
|
---|
417 | JSLock lock;
|
---|
418 |
|
---|
419 | // Parser::parse() returns 0 in a syntax error occurs
|
---|
420 | RefPtr<ProgramNode> progNode = Parser::parse(UString(), 0, code.data(), code.size(), 0, 0, 0);
|
---|
421 | return progNode;
|
---|
422 | }
|
---|
423 |
|
---|
424 | Completion Interpreter::evaluate(const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV)
|
---|
425 | {
|
---|
426 | return evaluate(sourceURL, startingLineNumber, code.data(), code.size(), thisV);
|
---|
427 | }
|
---|
428 |
|
---|
429 | Completion Interpreter::evaluate(const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength, JSValue* thisV)
|
---|
430 | {
|
---|
431 | JSLock lock;
|
---|
432 |
|
---|
433 | // prevent against infinite recursion
|
---|
434 | if (m_recursion >= 20)
|
---|
435 | return Completion(Throw, Error::create(&m_globalExec, GeneralError, "Recursion too deep"));
|
---|
436 |
|
---|
437 | // parse the source code
|
---|
438 | int sid;
|
---|
439 | int errLine;
|
---|
440 | UString errMsg;
|
---|
441 | RefPtr<ProgramNode> progNode = Parser::parse(sourceURL, startingLineNumber, code, codeLength, &sid, &errLine, &errMsg);
|
---|
442 |
|
---|
443 | // notify debugger that source has been parsed
|
---|
444 | if (m_debugger) {
|
---|
445 | bool cont = m_debugger->sourceParsed(&m_globalExec, sid, sourceURL, UString(code, codeLength), startingLineNumber, errLine, errMsg);
|
---|
446 | if (!cont)
|
---|
447 | return Completion(Break);
|
---|
448 | }
|
---|
449 |
|
---|
450 | // no program node means a syntax error occurred
|
---|
451 | if (!progNode)
|
---|
452 | return Completion(Throw, Error::create(&m_globalExec, SyntaxError, errMsg, errLine, sid, &sourceURL));
|
---|
453 |
|
---|
454 | m_globalExec.clearException();
|
---|
455 |
|
---|
456 | m_recursion++;
|
---|
457 |
|
---|
458 | JSObject* globalObj = m_globalObject;
|
---|
459 | JSObject* thisObj = globalObj;
|
---|
460 |
|
---|
461 | // "this" must be an object... use same rules as Function.prototype.apply()
|
---|
462 | if (thisV && !thisV->isUndefinedOrNull())
|
---|
463 | thisObj = thisV->toObject(&m_globalExec);
|
---|
464 |
|
---|
465 | Completion res;
|
---|
466 | if (m_globalExec.hadException())
|
---|
467 | // the thisV->toObject() conversion above might have thrown an exception - if so, propagate it
|
---|
468 | res = Completion(Throw, m_globalExec.exception());
|
---|
469 | else {
|
---|
470 | // execute the code
|
---|
471 | Context ctx(globalObj, this, thisObj, progNode.get());
|
---|
472 | ExecState newExec(this, &ctx);
|
---|
473 | progNode->processVarDecls(&newExec);
|
---|
474 | res = progNode->execute(&newExec);
|
---|
475 | }
|
---|
476 |
|
---|
477 | m_recursion--;
|
---|
478 |
|
---|
479 | if (shouldPrintExceptions() && res.complType() == Throw) {
|
---|
480 | JSLock lock;
|
---|
481 | ExecState* exec = globalExec();
|
---|
482 | CString f = sourceURL.UTF8String();
|
---|
483 | CString message = res.value()->toObject(exec)->toString(exec).UTF8String();
|
---|
484 | int line = res.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
|
---|
485 | #if PLATFORM(WIN_OS)
|
---|
486 | printf("%s line %d: %s\n", f.c_str(), line, message.c_str());
|
---|
487 | #else
|
---|
488 | printf("[%d] %s line %d: %s\n", getpid(), f.c_str(), line, message.c_str());
|
---|
489 | #endif
|
---|
490 | }
|
---|
491 |
|
---|
492 | return res;
|
---|
493 | }
|
---|
494 |
|
---|
495 | JSObject *Interpreter::builtinObject() const
|
---|
496 | {
|
---|
497 | return m_Object;
|
---|
498 | }
|
---|
499 |
|
---|
500 | JSObject *Interpreter::builtinFunction() const
|
---|
501 | {
|
---|
502 | return m_Function;
|
---|
503 | }
|
---|
504 |
|
---|
505 | JSObject *Interpreter::builtinArray() const
|
---|
506 | {
|
---|
507 | return m_Array;
|
---|
508 | }
|
---|
509 |
|
---|
510 | JSObject *Interpreter::builtinBoolean() const
|
---|
511 | {
|
---|
512 | return m_Boolean;
|
---|
513 | }
|
---|
514 |
|
---|
515 | JSObject *Interpreter::builtinString() const
|
---|
516 | {
|
---|
517 | return m_String;
|
---|
518 | }
|
---|
519 |
|
---|
520 | JSObject *Interpreter::builtinNumber() const
|
---|
521 | {
|
---|
522 | return m_Number;
|
---|
523 | }
|
---|
524 |
|
---|
525 | JSObject *Interpreter::builtinDate() const
|
---|
526 | {
|
---|
527 | return m_Date;
|
---|
528 | }
|
---|
529 |
|
---|
530 | JSObject *Interpreter::builtinRegExp() const
|
---|
531 | {
|
---|
532 | return m_RegExp;
|
---|
533 | }
|
---|
534 |
|
---|
535 | JSObject *Interpreter::builtinError() const
|
---|
536 | {
|
---|
537 | return m_Error;
|
---|
538 | }
|
---|
539 |
|
---|
540 | JSObject *Interpreter::builtinObjectPrototype() const
|
---|
541 | {
|
---|
542 | return m_ObjectPrototype;
|
---|
543 | }
|
---|
544 |
|
---|
545 | JSObject *Interpreter::builtinFunctionPrototype() const
|
---|
546 | {
|
---|
547 | return m_FunctionPrototype;
|
---|
548 | }
|
---|
549 |
|
---|
550 | JSObject *Interpreter::builtinArrayPrototype() const
|
---|
551 | {
|
---|
552 | return m_ArrayPrototype;
|
---|
553 | }
|
---|
554 |
|
---|
555 | JSObject *Interpreter::builtinBooleanPrototype() const
|
---|
556 | {
|
---|
557 | return m_BooleanPrototype;
|
---|
558 | }
|
---|
559 |
|
---|
560 | JSObject *Interpreter::builtinStringPrototype() const
|
---|
561 | {
|
---|
562 | return m_StringPrototype;
|
---|
563 | }
|
---|
564 |
|
---|
565 | JSObject *Interpreter::builtinNumberPrototype() const
|
---|
566 | {
|
---|
567 | return m_NumberPrototype;
|
---|
568 | }
|
---|
569 |
|
---|
570 | JSObject *Interpreter::builtinDatePrototype() const
|
---|
571 | {
|
---|
572 | return m_DatePrototype;
|
---|
573 | }
|
---|
574 |
|
---|
575 | JSObject *Interpreter::builtinRegExpPrototype() const
|
---|
576 | {
|
---|
577 | return m_RegExpPrototype;
|
---|
578 | }
|
---|
579 |
|
---|
580 | JSObject *Interpreter::builtinErrorPrototype() const
|
---|
581 | {
|
---|
582 | return m_ErrorPrototype;
|
---|
583 | }
|
---|
584 |
|
---|
585 | JSObject *Interpreter::builtinEvalError() const
|
---|
586 | {
|
---|
587 | return m_EvalError;
|
---|
588 | }
|
---|
589 |
|
---|
590 | JSObject *Interpreter::builtinRangeError() const
|
---|
591 | {
|
---|
592 | return m_RangeError;
|
---|
593 | }
|
---|
594 |
|
---|
595 | JSObject *Interpreter::builtinReferenceError() const
|
---|
596 | {
|
---|
597 | return m_ReferenceError;
|
---|
598 | }
|
---|
599 |
|
---|
600 | JSObject *Interpreter::builtinSyntaxError() const
|
---|
601 | {
|
---|
602 | return m_SyntaxError;
|
---|
603 | }
|
---|
604 |
|
---|
605 | JSObject *Interpreter::builtinTypeError() const
|
---|
606 | {
|
---|
607 | return m_TypeError;
|
---|
608 | }
|
---|
609 |
|
---|
610 | JSObject *Interpreter::builtinURIError() const
|
---|
611 | {
|
---|
612 | return m_UriError;
|
---|
613 | }
|
---|
614 |
|
---|
615 | JSObject *Interpreter::builtinEvalErrorPrototype() const
|
---|
616 | {
|
---|
617 | return m_EvalErrorPrototype;
|
---|
618 | }
|
---|
619 |
|
---|
620 | JSObject *Interpreter::builtinRangeErrorPrototype() const
|
---|
621 | {
|
---|
622 | return m_RangeErrorPrototype;
|
---|
623 | }
|
---|
624 |
|
---|
625 | JSObject *Interpreter::builtinReferenceErrorPrototype() const
|
---|
626 | {
|
---|
627 | return m_ReferenceErrorPrototype;
|
---|
628 | }
|
---|
629 |
|
---|
630 | JSObject *Interpreter::builtinSyntaxErrorPrototype() const
|
---|
631 | {
|
---|
632 | return m_SyntaxErrorPrototype;
|
---|
633 | }
|
---|
634 |
|
---|
635 | JSObject *Interpreter::builtinTypeErrorPrototype() const
|
---|
636 | {
|
---|
637 | return m_TypeErrorPrototype;
|
---|
638 | }
|
---|
639 |
|
---|
640 | JSObject *Interpreter::builtinURIErrorPrototype() const
|
---|
641 | {
|
---|
642 | return m_UriErrorPrototype;
|
---|
643 | }
|
---|
644 |
|
---|
645 | bool Interpreter::collect()
|
---|
646 | {
|
---|
647 | return Collector::collect();
|
---|
648 | }
|
---|
649 |
|
---|
650 | void Interpreter::mark(bool)
|
---|
651 | {
|
---|
652 | if (m_context)
|
---|
653 | m_context->mark();
|
---|
654 | if (m_globalObject && !m_globalObject->marked())
|
---|
655 | m_globalObject->mark();
|
---|
656 | if (m_globalExec.exception() && !m_globalExec.exception()->marked())
|
---|
657 | m_globalExec.exception()->mark();
|
---|
658 | }
|
---|
659 |
|
---|
660 | Interpreter* Interpreter::interpreterWithGlobalObject(JSObject* globalObject)
|
---|
661 | {
|
---|
662 | return interpreterMap().get(globalObject);
|
---|
663 | }
|
---|
664 |
|
---|
665 | #ifdef KJS_DEBUG_MEM
|
---|
666 | #include "lexer.h"
|
---|
667 | void Interpreter::finalCheck()
|
---|
668 | {
|
---|
669 | fprintf(stderr,"Interpreter::finalCheck()\n");
|
---|
670 | Collector::collect();
|
---|
671 |
|
---|
672 | Node::finalCheck();
|
---|
673 | Collector::finalCheck();
|
---|
674 | Lexer::globalClear();
|
---|
675 | UString::globalClear();
|
---|
676 | }
|
---|
677 | #endif
|
---|
678 |
|
---|
679 | static bool printExceptions = false;
|
---|
680 |
|
---|
681 | bool Interpreter::shouldPrintExceptions()
|
---|
682 | {
|
---|
683 | return printExceptions;
|
---|
684 | }
|
---|
685 |
|
---|
686 | void Interpreter::setShouldPrintExceptions(bool print)
|
---|
687 | {
|
---|
688 | printExceptions = print;
|
---|
689 | }
|
---|
690 |
|
---|
691 | // bindings are OS X WebKit-only for now
|
---|
692 | #if PLATFORM(MAC)
|
---|
693 | void *Interpreter::createLanguageInstanceForValue(ExecState *exec, int language, JSObject *value, const Bindings::RootObject *origin, const Bindings::RootObject *current)
|
---|
694 | {
|
---|
695 | return Bindings::Instance::createLanguageInstanceForValue (exec, (Bindings::Instance::BindingLanguage)language, value, origin, current);
|
---|
696 | }
|
---|
697 | #endif
|
---|
698 |
|
---|
699 | void Interpreter::saveBuiltins (SavedBuiltins& builtins) const
|
---|
700 | {
|
---|
701 | if (!builtins._internal)
|
---|
702 | builtins._internal = new SavedBuiltinsInternal;
|
---|
703 |
|
---|
704 | builtins._internal->m_Object = m_Object;
|
---|
705 | builtins._internal->m_Function = m_Function;
|
---|
706 | builtins._internal->m_Array = m_Array;
|
---|
707 | builtins._internal->m_Boolean = m_Boolean;
|
---|
708 | builtins._internal->m_String = m_String;
|
---|
709 | builtins._internal->m_Number = m_Number;
|
---|
710 | builtins._internal->m_Date = m_Date;
|
---|
711 | builtins._internal->m_RegExp = m_RegExp;
|
---|
712 | builtins._internal->m_Error = m_Error;
|
---|
713 |
|
---|
714 | builtins._internal->m_ObjectPrototype = m_ObjectPrototype;
|
---|
715 | builtins._internal->m_FunctionPrototype = m_FunctionPrototype;
|
---|
716 | builtins._internal->m_ArrayPrototype = m_ArrayPrototype;
|
---|
717 | builtins._internal->m_BooleanPrototype = m_BooleanPrototype;
|
---|
718 | builtins._internal->m_StringPrototype = m_StringPrototype;
|
---|
719 | builtins._internal->m_NumberPrototype = m_NumberPrototype;
|
---|
720 | builtins._internal->m_DatePrototype = m_DatePrototype;
|
---|
721 | builtins._internal->m_RegExpPrototype = m_RegExpPrototype;
|
---|
722 | builtins._internal->m_ErrorPrototype = m_ErrorPrototype;
|
---|
723 |
|
---|
724 | builtins._internal->m_EvalError = m_EvalError;
|
---|
725 | builtins._internal->m_RangeError = m_RangeError;
|
---|
726 | builtins._internal->m_ReferenceError = m_ReferenceError;
|
---|
727 | builtins._internal->m_SyntaxError = m_SyntaxError;
|
---|
728 | builtins._internal->m_TypeError = m_TypeError;
|
---|
729 | builtins._internal->m_UriError = m_UriError;
|
---|
730 |
|
---|
731 | builtins._internal->m_EvalErrorPrototype = m_EvalErrorPrototype;
|
---|
732 | builtins._internal->m_RangeErrorPrototype = m_RangeErrorPrototype;
|
---|
733 | builtins._internal->m_ReferenceErrorPrototype = m_ReferenceErrorPrototype;
|
---|
734 | builtins._internal->m_SyntaxErrorPrototype = m_SyntaxErrorPrototype;
|
---|
735 | builtins._internal->m_TypeErrorPrototype = m_TypeErrorPrototype;
|
---|
736 | builtins._internal->m_UriErrorPrototype = m_UriErrorPrototype;
|
---|
737 | }
|
---|
738 |
|
---|
739 | void Interpreter::restoreBuiltins (const SavedBuiltins& builtins)
|
---|
740 | {
|
---|
741 | if (!builtins._internal)
|
---|
742 | return;
|
---|
743 |
|
---|
744 | m_Object = builtins._internal->m_Object;
|
---|
745 | m_Function = builtins._internal->m_Function;
|
---|
746 | m_Array = builtins._internal->m_Array;
|
---|
747 | m_Boolean = builtins._internal->m_Boolean;
|
---|
748 | m_String = builtins._internal->m_String;
|
---|
749 | m_Number = builtins._internal->m_Number;
|
---|
750 | m_Date = builtins._internal->m_Date;
|
---|
751 | m_RegExp = builtins._internal->m_RegExp;
|
---|
752 | m_Error = builtins._internal->m_Error;
|
---|
753 |
|
---|
754 | m_ObjectPrototype = builtins._internal->m_ObjectPrototype;
|
---|
755 | m_FunctionPrototype = builtins._internal->m_FunctionPrototype;
|
---|
756 | m_ArrayPrototype = builtins._internal->m_ArrayPrototype;
|
---|
757 | m_BooleanPrototype = builtins._internal->m_BooleanPrototype;
|
---|
758 | m_StringPrototype = builtins._internal->m_StringPrototype;
|
---|
759 | m_NumberPrototype = builtins._internal->m_NumberPrototype;
|
---|
760 | m_DatePrototype = builtins._internal->m_DatePrototype;
|
---|
761 | m_RegExpPrototype = builtins._internal->m_RegExpPrototype;
|
---|
762 | m_ErrorPrototype = builtins._internal->m_ErrorPrototype;
|
---|
763 |
|
---|
764 | m_EvalError = builtins._internal->m_EvalError;
|
---|
765 | m_RangeError = builtins._internal->m_RangeError;
|
---|
766 | m_ReferenceError = builtins._internal->m_ReferenceError;
|
---|
767 | m_SyntaxError = builtins._internal->m_SyntaxError;
|
---|
768 | m_TypeError = builtins._internal->m_TypeError;
|
---|
769 | m_UriError = builtins._internal->m_UriError;
|
---|
770 |
|
---|
771 | m_EvalErrorPrototype = builtins._internal->m_EvalErrorPrototype;
|
---|
772 | m_RangeErrorPrototype = builtins._internal->m_RangeErrorPrototype;
|
---|
773 | m_ReferenceErrorPrototype = builtins._internal->m_ReferenceErrorPrototype;
|
---|
774 | m_SyntaxErrorPrototype = builtins._internal->m_SyntaxErrorPrototype;
|
---|
775 | m_TypeErrorPrototype = builtins._internal->m_TypeErrorPrototype;
|
---|
776 | m_UriErrorPrototype = builtins._internal->m_UriErrorPrototype;
|
---|
777 | }
|
---|
778 |
|
---|
779 | void Interpreter::startTimeoutCheck()
|
---|
780 | {
|
---|
781 | if (!m_timeoutChecker)
|
---|
782 | m_timeoutChecker = new TimeoutChecker;
|
---|
783 |
|
---|
784 | m_timeoutChecker->startTimeoutCheck(this);
|
---|
785 | }
|
---|
786 |
|
---|
787 | void Interpreter::stopTimeoutCheck()
|
---|
788 | {
|
---|
789 | ASSERT(m_timeoutChecker);
|
---|
790 |
|
---|
791 | m_timeoutChecker->stopTimeoutCheck(this);
|
---|
792 | }
|
---|
793 |
|
---|
794 | void Interpreter::pauseTimeoutCheck()
|
---|
795 | {
|
---|
796 | ASSERT(m_timeoutChecker);
|
---|
797 |
|
---|
798 | m_timeoutChecker->pauseTimeoutCheck(this);
|
---|
799 | }
|
---|
800 |
|
---|
801 | void Interpreter::resumeTimeoutCheck()
|
---|
802 | {
|
---|
803 | ASSERT(m_timeoutChecker);
|
---|
804 |
|
---|
805 | m_timeoutChecker->resumeTimeoutCheck(this);
|
---|
806 | }
|
---|
807 |
|
---|
808 | bool Interpreter::handleTimeout()
|
---|
809 | {
|
---|
810 | m_timedOut = false;
|
---|
811 |
|
---|
812 | pauseTimeoutCheck();
|
---|
813 | bool retval = shouldInterruptScript();
|
---|
814 | resumeTimeoutCheck();
|
---|
815 |
|
---|
816 | return retval;
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | SavedBuiltins::SavedBuiltins() :
|
---|
821 | _internal(0)
|
---|
822 | {
|
---|
823 | }
|
---|
824 |
|
---|
825 | SavedBuiltins::~SavedBuiltins()
|
---|
826 | {
|
---|
827 | delete _internal;
|
---|
828 | }
|
---|
829 |
|
---|
830 | }
|
---|