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., 59 Temple Place - Suite 330,
|
---|
21 | * Boston, MA 02111-1307, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "value.h"
|
---|
26 | #include "object.h"
|
---|
27 | #include "types.h"
|
---|
28 | #include "interpreter.h"
|
---|
29 |
|
---|
30 | #include <assert.h>
|
---|
31 | #include <math.h>
|
---|
32 | #include <stdio.h>
|
---|
33 |
|
---|
34 | #include "internal.h"
|
---|
35 | #include "collector.h"
|
---|
36 | #include "operations.h"
|
---|
37 | #include "error_object.h"
|
---|
38 | #include "nodes.h"
|
---|
39 | #include "context.h"
|
---|
40 |
|
---|
41 | using namespace KJS;
|
---|
42 |
|
---|
43 | // ------------------------------ Context --------------------------------------
|
---|
44 |
|
---|
45 | const ScopeChain &Context::scopeChain() const
|
---|
46 | {
|
---|
47 | return rep->scopeChain();
|
---|
48 | }
|
---|
49 |
|
---|
50 | Object Context::variableObject() const
|
---|
51 | {
|
---|
52 | return rep->variableObject();
|
---|
53 | }
|
---|
54 |
|
---|
55 | Object Context::thisValue() const
|
---|
56 | {
|
---|
57 | return rep->thisValue();
|
---|
58 | }
|
---|
59 |
|
---|
60 | const Context Context::callingContext() const
|
---|
61 | {
|
---|
62 | return rep->callingContext();
|
---|
63 | }
|
---|
64 |
|
---|
65 | // ------------------------------ Interpreter ----------------------------------
|
---|
66 |
|
---|
67 | Interpreter::Interpreter(const Object &global) : rep(0)
|
---|
68 | {
|
---|
69 | rep = new InterpreterImp(this,global);
|
---|
70 | }
|
---|
71 |
|
---|
72 | Interpreter::Interpreter()
|
---|
73 | {
|
---|
74 | Object global(new ObjectImp());
|
---|
75 | rep = new InterpreterImp(this,global);
|
---|
76 | }
|
---|
77 |
|
---|
78 | Interpreter::~Interpreter()
|
---|
79 | {
|
---|
80 | delete rep;
|
---|
81 | }
|
---|
82 |
|
---|
83 | Object &Interpreter::globalObject() const
|
---|
84 | {
|
---|
85 | return rep->globalObject();
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Interpreter::initGlobalObject()
|
---|
89 | {
|
---|
90 | rep->initGlobalObject();
|
---|
91 | }
|
---|
92 |
|
---|
93 | void Interpreter::lock()
|
---|
94 | {
|
---|
95 | InterpreterImp::lock();
|
---|
96 | }
|
---|
97 |
|
---|
98 | void Interpreter::unlock()
|
---|
99 | {
|
---|
100 | InterpreterImp::unlock();
|
---|
101 | }
|
---|
102 |
|
---|
103 | int Interpreter::lockCount()
|
---|
104 | {
|
---|
105 | return InterpreterImp::lockCount();
|
---|
106 | }
|
---|
107 |
|
---|
108 | ExecState *Interpreter::globalExec()
|
---|
109 | {
|
---|
110 | return rep->globalExec();
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool Interpreter::checkSyntax(const UString &code)
|
---|
114 | {
|
---|
115 | return rep->checkSyntax(code);
|
---|
116 | }
|
---|
117 |
|
---|
118 | Completion Interpreter::evaluate(const UString &code, const Value &thisV, const UString &)
|
---|
119 | {
|
---|
120 | return evaluate(UString(), 0, code, thisV);
|
---|
121 | }
|
---|
122 |
|
---|
123 | Completion Interpreter::evaluate(const UString &sourceURL, int startingLineNumber, const UString &code, const Value &thisV)
|
---|
124 | {
|
---|
125 | Completion comp = rep->evaluate(code,thisV, sourceURL, startingLineNumber);
|
---|
126 |
|
---|
127 | #if APPLE_CHANGES
|
---|
128 | if (shouldPrintExceptions() && comp.complType() == Throw) {
|
---|
129 | lock();
|
---|
130 | ExecState *exec = rep->globalExec();
|
---|
131 | char *f = strdup(sourceURL.ascii());
|
---|
132 | const char *message = comp.value().toObject(exec).toString(exec).ascii();
|
---|
133 | printf("%s:%s\n", f, message);
|
---|
134 | free(f);
|
---|
135 | unlock();
|
---|
136 | }
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | return comp;
|
---|
140 | }
|
---|
141 |
|
---|
142 | InterpreterImp *Interpreter::imp()
|
---|
143 | {
|
---|
144 | return rep;
|
---|
145 | }
|
---|
146 |
|
---|
147 | Object Interpreter::builtinObject() const
|
---|
148 | {
|
---|
149 | return rep->builtinObject();
|
---|
150 | }
|
---|
151 |
|
---|
152 | Object Interpreter::builtinFunction() const
|
---|
153 | {
|
---|
154 | return rep->builtinFunction();
|
---|
155 | }
|
---|
156 |
|
---|
157 | Object Interpreter::builtinArray() const
|
---|
158 | {
|
---|
159 | return rep->builtinArray();
|
---|
160 | }
|
---|
161 |
|
---|
162 | Object Interpreter::builtinBoolean() const
|
---|
163 | {
|
---|
164 | return rep->builtinBoolean();
|
---|
165 | }
|
---|
166 |
|
---|
167 | Object Interpreter::builtinString() const
|
---|
168 | {
|
---|
169 | return rep->builtinString();
|
---|
170 | }
|
---|
171 |
|
---|
172 | Object Interpreter::builtinNumber() const
|
---|
173 | {
|
---|
174 | return rep->builtinNumber();
|
---|
175 | }
|
---|
176 |
|
---|
177 | Object Interpreter::builtinDate() const
|
---|
178 | {
|
---|
179 | return rep->builtinDate();
|
---|
180 | }
|
---|
181 |
|
---|
182 | Object Interpreter::builtinRegExp() const
|
---|
183 | {
|
---|
184 | return rep->builtinRegExp();
|
---|
185 | }
|
---|
186 |
|
---|
187 | Object Interpreter::builtinError() const
|
---|
188 | {
|
---|
189 | return rep->builtinError();
|
---|
190 | }
|
---|
191 |
|
---|
192 | Object Interpreter::builtinObjectPrototype() const
|
---|
193 | {
|
---|
194 | return rep->builtinObjectPrototype();
|
---|
195 | }
|
---|
196 |
|
---|
197 | Object Interpreter::builtinFunctionPrototype() const
|
---|
198 | {
|
---|
199 | return rep->builtinFunctionPrototype();
|
---|
200 | }
|
---|
201 |
|
---|
202 | Object Interpreter::builtinArrayPrototype() const
|
---|
203 | {
|
---|
204 | return rep->builtinArrayPrototype();
|
---|
205 | }
|
---|
206 |
|
---|
207 | Object Interpreter::builtinBooleanPrototype() const
|
---|
208 | {
|
---|
209 | return rep->builtinBooleanPrototype();
|
---|
210 | }
|
---|
211 |
|
---|
212 | Object Interpreter::builtinStringPrototype() const
|
---|
213 | {
|
---|
214 | return rep->builtinStringPrototype();
|
---|
215 | }
|
---|
216 |
|
---|
217 | Object Interpreter::builtinNumberPrototype() const
|
---|
218 | {
|
---|
219 | return rep->builtinNumberPrototype();
|
---|
220 | }
|
---|
221 |
|
---|
222 | Object Interpreter::builtinDatePrototype() const
|
---|
223 | {
|
---|
224 | return rep->builtinDatePrototype();
|
---|
225 | }
|
---|
226 |
|
---|
227 | Object Interpreter::builtinRegExpPrototype() const
|
---|
228 | {
|
---|
229 | return rep->builtinRegExpPrototype();
|
---|
230 | }
|
---|
231 |
|
---|
232 | Object Interpreter::builtinErrorPrototype() const
|
---|
233 | {
|
---|
234 | return rep->builtinErrorPrototype();
|
---|
235 | }
|
---|
236 |
|
---|
237 | Object Interpreter::builtinEvalError() const
|
---|
238 | {
|
---|
239 | return rep->builtinEvalError();
|
---|
240 | }
|
---|
241 |
|
---|
242 | Object Interpreter::builtinRangeError() const
|
---|
243 | {
|
---|
244 | return rep->builtinRangeError();
|
---|
245 | }
|
---|
246 |
|
---|
247 | Object Interpreter::builtinReferenceError() const
|
---|
248 | {
|
---|
249 | return rep->builtinReferenceError();
|
---|
250 | }
|
---|
251 |
|
---|
252 | Object Interpreter::builtinSyntaxError() const
|
---|
253 | {
|
---|
254 | return rep->builtinSyntaxError();
|
---|
255 | }
|
---|
256 |
|
---|
257 | Object Interpreter::builtinTypeError() const
|
---|
258 | {
|
---|
259 | return rep->builtinTypeError();
|
---|
260 | }
|
---|
261 |
|
---|
262 | Object Interpreter::builtinURIError() const
|
---|
263 | {
|
---|
264 | return rep->builtinURIError();
|
---|
265 | }
|
---|
266 |
|
---|
267 | Object Interpreter::builtinEvalErrorPrototype() const
|
---|
268 | {
|
---|
269 | return rep->builtinEvalErrorPrototype();
|
---|
270 | }
|
---|
271 |
|
---|
272 | Object Interpreter::builtinRangeErrorPrototype() const
|
---|
273 | {
|
---|
274 | return rep->builtinRangeErrorPrototype();
|
---|
275 | }
|
---|
276 |
|
---|
277 | Object Interpreter::builtinReferenceErrorPrototype() const
|
---|
278 | {
|
---|
279 | return rep->builtinReferenceErrorPrototype();
|
---|
280 | }
|
---|
281 |
|
---|
282 | Object Interpreter::builtinSyntaxErrorPrototype() const
|
---|
283 | {
|
---|
284 | return rep->builtinSyntaxErrorPrototype();
|
---|
285 | }
|
---|
286 |
|
---|
287 | Object Interpreter::builtinTypeErrorPrototype() const
|
---|
288 | {
|
---|
289 | return rep->builtinTypeErrorPrototype();
|
---|
290 | }
|
---|
291 |
|
---|
292 | Object Interpreter::builtinURIErrorPrototype() const
|
---|
293 | {
|
---|
294 | return rep->builtinURIErrorPrototype();
|
---|
295 | }
|
---|
296 |
|
---|
297 | void Interpreter::setCompatMode(CompatMode mode)
|
---|
298 | {
|
---|
299 | rep->setCompatMode(mode);
|
---|
300 | }
|
---|
301 |
|
---|
302 | Interpreter::CompatMode Interpreter::compatMode() const
|
---|
303 | {
|
---|
304 | return rep->compatMode();
|
---|
305 | }
|
---|
306 |
|
---|
307 | #ifdef KJS_DEBUG_MEM
|
---|
308 | #include "lexer.h"
|
---|
309 | void Interpreter::finalCheck()
|
---|
310 | {
|
---|
311 | fprintf(stderr,"Interpreter::finalCheck()\n");
|
---|
312 | // Garbage collect - as many times as necessary
|
---|
313 | // (we could delete an object which was holding another object, so
|
---|
314 | // the deref() will happen too late for deleting the impl of the 2nd object).
|
---|
315 | while( Collector::collect() )
|
---|
316 | ;
|
---|
317 |
|
---|
318 | Node::finalCheck();
|
---|
319 | Collector::finalCheck();
|
---|
320 | Lexer::globalClear();
|
---|
321 | UString::globalClear();
|
---|
322 | }
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | #if APPLE_CHANGES
|
---|
326 | static bool printExceptions = false;
|
---|
327 |
|
---|
328 | bool Interpreter::shouldPrintExceptions()
|
---|
329 | {
|
---|
330 | return printExceptions;
|
---|
331 | }
|
---|
332 |
|
---|
333 | void Interpreter::setShouldPrintExceptions(bool print)
|
---|
334 | {
|
---|
335 | printExceptions = print;
|
---|
336 | }
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | void Interpreter::saveBuiltins (SavedBuiltins &builtins) const
|
---|
340 | {
|
---|
341 | rep->saveBuiltins(builtins);
|
---|
342 | }
|
---|
343 |
|
---|
344 | void Interpreter::restoreBuiltins (const SavedBuiltins &builtins)
|
---|
345 | {
|
---|
346 | rep->restoreBuiltins(builtins);
|
---|
347 | }
|
---|
348 |
|
---|
349 | SavedBuiltins::SavedBuiltins() :
|
---|
350 | _internal(0)
|
---|
351 | {
|
---|
352 | }
|
---|
353 |
|
---|
354 | SavedBuiltins::~SavedBuiltins()
|
---|
355 | {
|
---|
356 | delete _internal;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | void Interpreter::virtual_hook( int, void* )
|
---|
361 | { /*BASE::virtual_hook( id, data );*/ }
|
---|
362 |
|
---|
363 |
|
---|
364 | Interpreter *ExecState::lexicalInterpreter() const
|
---|
365 | {
|
---|
366 | if (!_context) {
|
---|
367 | return dynamicInterpreter();
|
---|
368 | }
|
---|
369 |
|
---|
370 | InterpreterImp *result = InterpreterImp::interpreterWithGlobalObject(_context->scopeChain().bottom());
|
---|
371 |
|
---|
372 | if (!result) {
|
---|
373 | return dynamicInterpreter();
|
---|
374 | }
|
---|
375 |
|
---|
376 | return result->interpreter();
|
---|
377 | }
|
---|