interpreter.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
00006  *  Copyright (C) 2003 Apple Computer, Inc.
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Library General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Library General Public License
00019  *  along with this library; see the file COPYING.LIB.  If not, write to
00020  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  *  Boston, MA 02110-1301, USA.
00022  *
00023  */
00024 
00025 #include "value.h"
00026 #include "object.h"
00027 #include "types.h"
00028 #include "interpreter.h"
00029 
00030 #include <assert.h>
00031 #include <math.h>
00032 #include <stdio.h>
00033 
00034 #include "internal.h"
00035 #include "collector.h"
00036 #include "operations.h"
00037 #include "error_object.h"
00038 #include "debugger.h"
00039 #include "nodes.h"
00040 #include "context.h"
00041 
00042 using namespace KJS;
00043 
00044 // ------------------------------ Context --------------------------------------
00045 
00046 const ScopeChain &Context::scopeChain() const
00047 {
00048   return rep->scopeChain();
00049 }
00050 
00051 Object Context::variableObject() const
00052 {
00053   return rep->variableObject();
00054 }
00055 
00056 Object Context::thisValue() const
00057 {
00058   return rep->thisValue();
00059 }
00060 
00061 const Context Context::callingContext() const
00062 {
00063   return rep->callingContext();
00064 }
00065 
00066 CodeType Context::codeType() const
00067 {
00068   return rep->codeType();
00069 }
00070 
00071 int Context::sourceId() const
00072 {
00073   return rep->sourceId;
00074 }
00075 
00076 int Context::curStmtFirstLine() const
00077 {
00078   return rep->line0;
00079 }
00080 
00081 int Context::curStmtLastLine() const
00082 {
00083   return rep->line1;
00084 }
00085 
00086 Object Context::function() const
00087 {
00088   return Object(rep->function());
00089 }
00090 
00091 Identifier Context::functionName() const
00092 {
00093   return rep->functionName;
00094 }
00095 
00096 List Context::args() const
00097 {
00098   return rep->args;
00099 }
00100 
00101 bool KJS::operator==(const Context &c1, const Context &c2)
00102 {
00103   return (c1.imp() == c2.imp());
00104 }
00105 
00106 bool KJS::operator!=(const Context &c1, const Context &c2)
00107 {
00108   return (c1.imp() != c2.imp());
00109 }
00110 
00111 // ------------------------------ Interpreter ---------------------------------
00112 
00113 Interpreter::Interpreter(const Object &global)
00114 {
00115   rep = new InterpreterImp(this,global);
00116 }
00117 
00118 Interpreter::Interpreter()
00119 {
00120   Object global(new ObjectImp());
00121   rep = new InterpreterImp(this,global);
00122 }
00123 
00124 Interpreter::~Interpreter()
00125 {
00126   delete rep;
00127 }
00128 
00129 Object &Interpreter::globalObject() const
00130 {
00131   return rep->globalObject();
00132 }
00133 
00134 void Interpreter::initGlobalObject()
00135 {
00136   rep->initGlobalObject();
00137 }
00138 
00139 void Interpreter::lock()
00140 {
00141   InterpreterImp::lock();
00142 }
00143 
00144 void Interpreter::unlock()
00145 {
00146   InterpreterImp::unlock();
00147 }
00148 
00149 ExecState *Interpreter::globalExec()
00150 {
00151   return rep->globalExec();
00152 }
00153 
00154 bool Interpreter::checkSyntax(const UString &code, int *errLine, UString *errMsg)
00155 {
00156   return rep->checkSyntax(code,errLine,errMsg);
00157 }
00158 
00159 bool Interpreter::checkSyntax(const UString &code)
00160 {
00161   return rep->checkSyntax(code);
00162 }
00163 
00164 Completion Interpreter::evaluate(const UString &code, const Value &thisV)
00165 {
00166   return rep->evaluate(code,thisV);
00167 }
00168 
00169 InterpreterImp *Interpreter::imp()
00170 {
00171   return rep;
00172 }
00173 
00174 Object Interpreter::builtinObject() const
00175 {
00176   return rep->builtinObject();
00177 }
00178 
00179 Object Interpreter::builtinFunction() const
00180 {
00181   return rep->builtinFunction();
00182 }
00183 
00184 Object Interpreter::builtinArray() const
00185 {
00186   return rep->builtinArray();
00187 }
00188 
00189 Object Interpreter::builtinBoolean() const
00190 {
00191   return rep->builtinBoolean();
00192 }
00193 
00194 Object Interpreter::builtinString() const
00195 {
00196   return rep->builtinString();
00197 }
00198 
00199 Object Interpreter::builtinNumber() const
00200 {
00201   return rep->builtinNumber();
00202 }
00203 
00204 Object Interpreter::builtinDate() const
00205 {
00206   return rep->builtinDate();
00207 }
00208 
00209 Object Interpreter::builtinRegExp() const
00210 {
00211   return rep->builtinRegExp();
00212 }
00213 
00214 Object Interpreter::builtinError() const
00215 {
00216   return rep->builtinError();
00217 }
00218 
00219 Object Interpreter::builtinObjectPrototype() const
00220 {
00221   return rep->builtinObjectPrototype();
00222 }
00223 
00224 Object Interpreter::builtinFunctionPrototype() const
00225 {
00226   return rep->builtinFunctionPrototype();
00227 }
00228 
00229 Object Interpreter::builtinArrayPrototype() const
00230 {
00231   return rep->builtinArrayPrototype();
00232 }
00233 
00234 Object Interpreter::builtinBooleanPrototype() const
00235 {
00236   return rep->builtinBooleanPrototype();
00237 }
00238 
00239 Object Interpreter::builtinStringPrototype() const
00240 {
00241   return rep->builtinStringPrototype();
00242 }
00243 
00244 Object Interpreter::builtinNumberPrototype() const
00245 {
00246   return rep->builtinNumberPrototype();
00247 }
00248 
00249 Object Interpreter::builtinDatePrototype() const
00250 {
00251   return rep->builtinDatePrototype();
00252 }
00253 
00254 Object Interpreter::builtinRegExpPrototype() const
00255 {
00256   return rep->builtinRegExpPrototype();
00257 }
00258 
00259 Object Interpreter::builtinErrorPrototype() const
00260 {
00261   return rep->builtinErrorPrototype();
00262 }
00263 
00264 Object Interpreter::builtinEvalError() const
00265 {
00266   return rep->builtinEvalError();
00267 }
00268 
00269 Object Interpreter::builtinRangeError() const
00270 {
00271   return rep->builtinRangeError();
00272 }
00273 
00274 Object Interpreter::builtinReferenceError() const
00275 {
00276   return rep->builtinReferenceError();
00277 }
00278 
00279 Object Interpreter::builtinSyntaxError() const
00280 {
00281   return rep->builtinSyntaxError();
00282 }
00283 
00284 Object Interpreter::builtinTypeError() const
00285 {
00286   return rep->builtinTypeError();
00287 }
00288 
00289 Object Interpreter::builtinURIError() const
00290 {
00291   return rep->builtinURIError();
00292 }
00293 
00294 Object Interpreter::builtinEvalErrorPrototype() const
00295 {
00296   return rep->builtinEvalErrorPrototype();
00297 }
00298 
00299 Object Interpreter::builtinRangeErrorPrototype() const
00300 {
00301   return rep->builtinRangeErrorPrototype();
00302 }
00303 
00304 Object Interpreter::builtinReferenceErrorPrototype() const
00305 {
00306   return rep->builtinReferenceErrorPrototype();
00307 }
00308 
00309 Object Interpreter::builtinSyntaxErrorPrototype() const
00310 {
00311   return rep->builtinSyntaxErrorPrototype();
00312 }
00313 
00314 Object Interpreter::builtinTypeErrorPrototype() const
00315 {
00316   return rep->builtinTypeErrorPrototype();
00317 }
00318 
00319 Object Interpreter::builtinURIErrorPrototype() const
00320 {
00321   return rep->builtinURIErrorPrototype();
00322 }
00323 
00324 void Interpreter::setCompatMode(CompatMode mode)
00325 {
00326   rep->setCompatMode(mode);
00327 }
00328 
00329 Interpreter::CompatMode Interpreter::compatMode() const
00330 {
00331   return rep->compatMode();
00332 }
00333 
00334 bool Interpreter::collect()
00335 {
00336   return Collector::collect();
00337 }
00338 
00339 #ifdef KJS_DEBUG_MEM
00340 #include "lexer.h"
00341 void Interpreter::finalCheck()
00342 {
00343   fprintf(stderr,"Interpreter::finalCheck()\n");
00344   // Garbage collect - as many times as necessary
00345   // (we could delete an object which was holding another object, so
00346   // the deref() will happen too late for deleting the impl of the 2nd object).
00347   while( Collector::collect() )
00348     ;
00349 
00350   Node::finalCheck();
00351   Collector::finalCheck();
00352   Lexer::globalClear();
00353   UString::globalClear();
00354 }
00355 #endif
00356 
00357 // ------------------------------ ExecState --------------------------------------
00358 
00359 void ExecState::setException(const Value &e)
00360 {
00361   if (e.isValid()) {
00362     Debugger *dbg = _interpreter->imp()->debugger();
00363     if (dbg)
00364       dbg->exception(this,e,_context->inTryCatch());
00365   }
00366   _exception = e;
00367 }
00368 
00369 void ExecState::clearException()
00370 {
00371   terminate_request = false;
00372   _exception = Value();
00373 }
00374 
00375 bool ExecState::terminate_request = false;
00376 
00377 static bool defaultConfirm() { return true; }
00378 
00379 bool (*ExecState::confirmTerminate)() = defaultConfirm;
00380 
00381 bool ExecState::hadException()
00382 {
00383   if (terminate_request) {
00384     if (confirmTerminate())
00385       _exception = Error::create((ExecState*)this);
00386     terminate_request = false;
00387   }
00388   return _exception.isValid();
00389 }
00390 
00391 void Interpreter::virtual_hook( int, void* )
00392 { /*BASE::virtual_hook( id, data );*/ }
00393 
00394 
00395 Interpreter *ExecState::lexicalInterpreter() const
00396 {
00397   // TODO: use proper implementation
00398 #if 1
00399   return dynamicInterpreter();
00400 #else
00401   if (!_context) {
00402     return dynamicInterpreter();
00403   }
00404 
00405   InterpreterImp *result = InterpreterImp::interpreterWithGlobalObject(_context->scopeChain().bottom());
00406 
00407   if (!result) {
00408     return dynamicInterpreter();
00409   }
00410 
00411   return result->interpreter();
00412 #endif
00413 }
KDE Home | KDE Accessibility Home | Description of Access Keys