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