source: webkit/trunk/JavaScriptCore/kjs/interpreter.cpp@ 12031

Last change on this file since 12031 was 12031, checked in by staikos, 19 years ago

Reviewed by Darin.

Add collector helper to interpreter in order to facilitate visibility rules in
KDE.

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
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
43namespace KJS {
44
45// ------------------------------ Context --------------------------------------
46
47const ScopeChain &Context::scopeChain() const
48{
49 return rep->scopeChain();
50}
51
52JSObject *Context::variableObject() const
53{
54 return rep->variableObject();
55}
56
57JSObject *Context::thisValue() const
58{
59 return rep->thisValue();
60}
61
62const Context Context::callingContext() const
63{
64 return rep->callingContext();
65}
66
67// ------------------------------ Interpreter ----------------------------------
68
69Interpreter::Interpreter(JSObject *global)
70 : rep(0)
71 , m_argumentsPropertyName(&argumentsPropertyName)
72 , m_specialPrototypePropertyName(&specialPrototypePropertyName)
73{
74 rep = new InterpreterImp(this, global);
75}
76
77Interpreter::Interpreter()
78 : rep(0)
79 , m_argumentsPropertyName(&argumentsPropertyName)
80 , m_specialPrototypePropertyName(&specialPrototypePropertyName)
81{
82 rep = new InterpreterImp(this, new JSObject);
83}
84
85Interpreter::~Interpreter()
86{
87 delete rep;
88}
89
90JSObject *Interpreter::globalObject() const
91{
92 return rep->globalObject();
93}
94
95void Interpreter::initGlobalObject()
96{
97 rep->initGlobalObject();
98}
99
100ExecState *Interpreter::globalExec()
101{
102 return rep->globalExec();
103}
104
105bool Interpreter::checkSyntax(const UString &code)
106{
107 return rep->checkSyntax(code);
108}
109
110Completion Interpreter::evaluate(const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV)
111{
112 return evaluate(sourceURL, startingLineNumber, code.data(), code.size());
113}
114
115Completion 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
134JSObject *Interpreter::builtinObject() const
135{
136 return rep->builtinObject();
137}
138
139JSObject *Interpreter::builtinFunction() const
140{
141 return rep->builtinFunction();
142}
143
144JSObject *Interpreter::builtinArray() const
145{
146 return rep->builtinArray();
147}
148
149JSObject *Interpreter::builtinBoolean() const
150{
151 return rep->builtinBoolean();
152}
153
154JSObject *Interpreter::builtinString() const
155{
156 return rep->builtinString();
157}
158
159JSObject *Interpreter::builtinNumber() const
160{
161 return rep->builtinNumber();
162}
163
164JSObject *Interpreter::builtinDate() const
165{
166 return rep->builtinDate();
167}
168
169JSObject *Interpreter::builtinRegExp() const
170{
171 return rep->builtinRegExp();
172}
173
174JSObject *Interpreter::builtinError() const
175{
176 return rep->builtinError();
177}
178
179JSObject *Interpreter::builtinObjectPrototype() const
180{
181 return rep->builtinObjectPrototype();
182}
183
184JSObject *Interpreter::builtinFunctionPrototype() const
185{
186 return rep->builtinFunctionPrototype();
187}
188
189JSObject *Interpreter::builtinArrayPrototype() const
190{
191 return rep->builtinArrayPrototype();
192}
193
194JSObject *Interpreter::builtinBooleanPrototype() const
195{
196 return rep->builtinBooleanPrototype();
197}
198
199JSObject *Interpreter::builtinStringPrototype() const
200{
201 return rep->builtinStringPrototype();
202}
203
204JSObject *Interpreter::builtinNumberPrototype() const
205{
206 return rep->builtinNumberPrototype();
207}
208
209JSObject *Interpreter::builtinDatePrototype() const
210{
211 return rep->builtinDatePrototype();
212}
213
214JSObject *Interpreter::builtinRegExpPrototype() const
215{
216 return rep->builtinRegExpPrototype();
217}
218
219JSObject *Interpreter::builtinErrorPrototype() const
220{
221 return rep->builtinErrorPrototype();
222}
223
224JSObject *Interpreter::builtinEvalError() const
225{
226 return rep->builtinEvalError();
227}
228
229JSObject *Interpreter::builtinRangeError() const
230{
231 return rep->builtinRangeError();
232}
233
234JSObject *Interpreter::builtinReferenceError() const
235{
236 return rep->builtinReferenceError();
237}
238
239JSObject *Interpreter::builtinSyntaxError() const
240{
241 return rep->builtinSyntaxError();
242}
243
244JSObject *Interpreter::builtinTypeError() const
245{
246 return rep->builtinTypeError();
247}
248
249JSObject *Interpreter::builtinURIError() const
250{
251 return rep->builtinURIError();
252}
253
254JSObject *Interpreter::builtinEvalErrorPrototype() const
255{
256 return rep->builtinEvalErrorPrototype();
257}
258
259JSObject *Interpreter::builtinRangeErrorPrototype() const
260{
261 return rep->builtinRangeErrorPrototype();
262}
263
264JSObject *Interpreter::builtinReferenceErrorPrototype() const
265{
266 return rep->builtinReferenceErrorPrototype();
267}
268
269JSObject *Interpreter::builtinSyntaxErrorPrototype() const
270{
271 return rep->builtinSyntaxErrorPrototype();
272}
273
274JSObject *Interpreter::builtinTypeErrorPrototype() const
275{
276 return rep->builtinTypeErrorPrototype();
277}
278
279JSObject *Interpreter::builtinURIErrorPrototype() const
280{
281 return rep->builtinURIErrorPrototype();
282}
283
284void Interpreter::setCompatMode(CompatMode mode)
285{
286 rep->setCompatMode(mode);
287}
288
289Interpreter::CompatMode Interpreter::compatMode() const
290{
291 return rep->compatMode();
292}
293
294bool Interpreter::collect()
295{
296 return Collector::collect();
297}
298
299#ifdef KJS_DEBUG_MEM
300#include "lexer.h"
301void 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
313static bool printExceptions = false;
314
315bool Interpreter::shouldPrintExceptions()
316{
317 return printExceptions;
318}
319
320void Interpreter::setShouldPrintExceptions(bool print)
321{
322 printExceptions = print;
323}
324
325#if __APPLE__
326void *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
333void Interpreter::saveBuiltins (SavedBuiltins &builtins) const
334{
335 rep->saveBuiltins(builtins);
336}
337
338void Interpreter::restoreBuiltins (const SavedBuiltins &builtins)
339{
340 rep->restoreBuiltins(builtins);
341}
342
343SavedBuiltins::SavedBuiltins() :
344 _internal(0)
345{
346}
347
348SavedBuiltins::~SavedBuiltins()
349{
350 delete _internal;
351}
352
353
354void Interpreter::virtual_hook( int, void* )
355{ /*BASE::virtual_hook( id, data );*/ }
356
357
358Interpreter *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}
Note: See TracBrowser for help on using the repository browser.