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