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