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 Steet, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, 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("[%d] %s:%s\n", getpid(), f, message);
|
---|
134 |
|
---|
135 | free(f);
|
---|
136 | unlock();
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | return comp;
|
---|
141 | }
|
---|
142 |
|
---|
143 | InterpreterImp *Interpreter::imp()
|
---|
144 | {
|
---|
145 | return rep;
|
---|
146 | }
|
---|
147 |
|
---|
148 | Object Interpreter::builtinObject() const
|
---|
149 | {
|
---|
150 | return rep->builtinObject();
|
---|
151 | }
|
---|
152 |
|
---|
153 | Object Interpreter::builtinFunction() const
|
---|
154 | {
|
---|
155 | return rep->builtinFunction();
|
---|
156 | }
|
---|
157 |
|
---|
158 | Object Interpreter::builtinArray() const
|
---|
159 | {
|
---|
160 | return rep->builtinArray();
|
---|
161 | }
|
---|
162 |
|
---|
163 | Object Interpreter::builtinBoolean() const
|
---|
164 | {
|
---|
165 | return rep->builtinBoolean();
|
---|
166 | }
|
---|
167 |
|
---|
168 | Object Interpreter::builtinString() const
|
---|
169 | {
|
---|
170 | return rep->builtinString();
|
---|
171 | }
|
---|
172 |
|
---|
173 | Object Interpreter::builtinNumber() const
|
---|
174 | {
|
---|
175 | return rep->builtinNumber();
|
---|
176 | }
|
---|
177 |
|
---|
178 | Object Interpreter::builtinDate() const
|
---|
179 | {
|
---|
180 | return rep->builtinDate();
|
---|
181 | }
|
---|
182 |
|
---|
183 | Object Interpreter::builtinRegExp() const
|
---|
184 | {
|
---|
185 | return rep->builtinRegExp();
|
---|
186 | }
|
---|
187 |
|
---|
188 | Object Interpreter::builtinError() const
|
---|
189 | {
|
---|
190 | return rep->builtinError();
|
---|
191 | }
|
---|
192 |
|
---|
193 | Object Interpreter::builtinObjectPrototype() const
|
---|
194 | {
|
---|
195 | return rep->builtinObjectPrototype();
|
---|
196 | }
|
---|
197 |
|
---|
198 | Object Interpreter::builtinFunctionPrototype() const
|
---|
199 | {
|
---|
200 | return rep->builtinFunctionPrototype();
|
---|
201 | }
|
---|
202 |
|
---|
203 | Object Interpreter::builtinArrayPrototype() const
|
---|
204 | {
|
---|
205 | return rep->builtinArrayPrototype();
|
---|
206 | }
|
---|
207 |
|
---|
208 | Object Interpreter::builtinBooleanPrototype() const
|
---|
209 | {
|
---|
210 | return rep->builtinBooleanPrototype();
|
---|
211 | }
|
---|
212 |
|
---|
213 | Object Interpreter::builtinStringPrototype() const
|
---|
214 | {
|
---|
215 | return rep->builtinStringPrototype();
|
---|
216 | }
|
---|
217 |
|
---|
218 | Object Interpreter::builtinNumberPrototype() const
|
---|
219 | {
|
---|
220 | return rep->builtinNumberPrototype();
|
---|
221 | }
|
---|
222 |
|
---|
223 | Object Interpreter::builtinDatePrototype() const
|
---|
224 | {
|
---|
225 | return rep->builtinDatePrototype();
|
---|
226 | }
|
---|
227 |
|
---|
228 | Object Interpreter::builtinRegExpPrototype() const
|
---|
229 | {
|
---|
230 | return rep->builtinRegExpPrototype();
|
---|
231 | }
|
---|
232 |
|
---|
233 | Object Interpreter::builtinErrorPrototype() const
|
---|
234 | {
|
---|
235 | return rep->builtinErrorPrototype();
|
---|
236 | }
|
---|
237 |
|
---|
238 | Object Interpreter::builtinEvalError() const
|
---|
239 | {
|
---|
240 | return rep->builtinEvalError();
|
---|
241 | }
|
---|
242 |
|
---|
243 | Object Interpreter::builtinRangeError() const
|
---|
244 | {
|
---|
245 | return rep->builtinRangeError();
|
---|
246 | }
|
---|
247 |
|
---|
248 | Object Interpreter::builtinReferenceError() const
|
---|
249 | {
|
---|
250 | return rep->builtinReferenceError();
|
---|
251 | }
|
---|
252 |
|
---|
253 | Object Interpreter::builtinSyntaxError() const
|
---|
254 | {
|
---|
255 | return rep->builtinSyntaxError();
|
---|
256 | }
|
---|
257 |
|
---|
258 | Object Interpreter::builtinTypeError() const
|
---|
259 | {
|
---|
260 | return rep->builtinTypeError();
|
---|
261 | }
|
---|
262 |
|
---|
263 | Object Interpreter::builtinURIError() const
|
---|
264 | {
|
---|
265 | return rep->builtinURIError();
|
---|
266 | }
|
---|
267 |
|
---|
268 | Object Interpreter::builtinEvalErrorPrototype() const
|
---|
269 | {
|
---|
270 | return rep->builtinEvalErrorPrototype();
|
---|
271 | }
|
---|
272 |
|
---|
273 | Object Interpreter::builtinRangeErrorPrototype() const
|
---|
274 | {
|
---|
275 | return rep->builtinRangeErrorPrototype();
|
---|
276 | }
|
---|
277 |
|
---|
278 | Object Interpreter::builtinReferenceErrorPrototype() const
|
---|
279 | {
|
---|
280 | return rep->builtinReferenceErrorPrototype();
|
---|
281 | }
|
---|
282 |
|
---|
283 | Object Interpreter::builtinSyntaxErrorPrototype() const
|
---|
284 | {
|
---|
285 | return rep->builtinSyntaxErrorPrototype();
|
---|
286 | }
|
---|
287 |
|
---|
288 | Object Interpreter::builtinTypeErrorPrototype() const
|
---|
289 | {
|
---|
290 | return rep->builtinTypeErrorPrototype();
|
---|
291 | }
|
---|
292 |
|
---|
293 | Object Interpreter::builtinURIErrorPrototype() const
|
---|
294 | {
|
---|
295 | return rep->builtinURIErrorPrototype();
|
---|
296 | }
|
---|
297 |
|
---|
298 | void Interpreter::setCompatMode(CompatMode mode)
|
---|
299 | {
|
---|
300 | rep->setCompatMode(mode);
|
---|
301 | }
|
---|
302 |
|
---|
303 | Interpreter::CompatMode Interpreter::compatMode() const
|
---|
304 | {
|
---|
305 | return rep->compatMode();
|
---|
306 | }
|
---|
307 |
|
---|
308 | #ifdef KJS_DEBUG_MEM
|
---|
309 | #include "lexer.h"
|
---|
310 | void Interpreter::finalCheck()
|
---|
311 | {
|
---|
312 | fprintf(stderr,"Interpreter::finalCheck()\n");
|
---|
313 | // Garbage collect - as many times as necessary
|
---|
314 | // (we could delete an object which was holding another object, so
|
---|
315 | // the deref() will happen too late for deleting the impl of the 2nd object).
|
---|
316 | while( Collector::collect() )
|
---|
317 | ;
|
---|
318 |
|
---|
319 | Node::finalCheck();
|
---|
320 | Collector::finalCheck();
|
---|
321 | Lexer::globalClear();
|
---|
322 | UString::globalClear();
|
---|
323 | }
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | #if APPLE_CHANGES
|
---|
327 | static bool printExceptions = false;
|
---|
328 |
|
---|
329 | bool Interpreter::shouldPrintExceptions()
|
---|
330 | {
|
---|
331 | return printExceptions;
|
---|
332 | }
|
---|
333 |
|
---|
334 | void Interpreter::setShouldPrintExceptions(bool print)
|
---|
335 | {
|
---|
336 | printExceptions = print;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | void *Interpreter::createLanguageInstanceForValue (ExecState *exec, Bindings::Instance::BindingLanguage language, const Object &value, const Bindings::RootObject *origin, const Bindings::RootObject *current)
|
---|
341 | {
|
---|
342 | return Bindings::Instance::createLanguageInstanceForValue (exec, language, value, origin, current);
|
---|
343 | }
|
---|
344 |
|
---|
345 | #endif
|
---|
346 |
|
---|
347 | void Interpreter::saveBuiltins (SavedBuiltins &builtins) const
|
---|
348 | {
|
---|
349 | rep->saveBuiltins(builtins);
|
---|
350 | }
|
---|
351 |
|
---|
352 | void Interpreter::restoreBuiltins (const SavedBuiltins &builtins)
|
---|
353 | {
|
---|
354 | rep->restoreBuiltins(builtins);
|
---|
355 | }
|
---|
356 |
|
---|
357 | SavedBuiltins::SavedBuiltins() :
|
---|
358 | _internal(0)
|
---|
359 | {
|
---|
360 | }
|
---|
361 |
|
---|
362 | SavedBuiltins::~SavedBuiltins()
|
---|
363 | {
|
---|
364 | delete _internal;
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 | void Interpreter::virtual_hook( int, void* )
|
---|
369 | { /*BASE::virtual_hook( id, data );*/ }
|
---|
370 |
|
---|
371 |
|
---|
372 | Interpreter *ExecState::lexicalInterpreter() const
|
---|
373 | {
|
---|
374 | if (!_context) {
|
---|
375 | return dynamicInterpreter();
|
---|
376 | }
|
---|
377 |
|
---|
378 | InterpreterImp *result = InterpreterImp::interpreterWithGlobalObject(_context->scopeChain().bottom());
|
---|
379 |
|
---|
380 | if (!result) {
|
---|
381 | return dynamicInterpreter();
|
---|
382 | }
|
---|
383 |
|
---|
384 | return result->interpreter();
|
---|
385 | }
|
---|