interpreter.cpp
// -*- c-basic-offset: 2 -*-
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "value.h"
#include "object.h"
#include "types.h"
#include "interpreter.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include "internal.h"
#include "collector.h"
#include "operations.h"
#include "error_object.h"
#include "debugger.h"
#include "nodes.h"
#include "context.h"
using namespace KJS;
// ------------------------------ Context --------------------------------------
const ScopeChain &Context::scopeChain() const
{
return rep->scopeChain();
}
Object Context::variableObject() const
{
return rep->variableObject();
}
Object Context::thisValue() const
{
return rep->thisValue();
}
const Context Context::callingContext() const
{
return rep->callingContext();
}
CodeType Context::codeType() const
{
return rep->codeType();
}
int Context::sourceId() const
{
return rep->sourceId;
}
int Context::curStmtFirstLine() const
{
return rep->line0;
}
int Context::curStmtLastLine() const
{
return rep->line1;
}
Object Context::function() const
{
return Object(rep->function());
}
Identifier Context::functionName() const
{
return rep->functionName;
}
List Context::args() const
{
return rep->args;
}
bool KJS::operator==(const Context &c1, const Context &c2)
{
return (c1.imp() == c2.imp());
}
bool KJS::operator!=(const Context &c1, const Context &c2)
{
return (c1.imp() != c2.imp());
}
// ------------------------------ Interpreter ---------------------------------
Interpreter::Interpreter(const Object &global)
{
rep = new InterpreterImp(this,global);
}
Interpreter::Interpreter()
{
Object global(new ObjectImp());
rep = new InterpreterImp(this,global);
}
Interpreter::~Interpreter()
{
delete rep;
}
Object &Interpreter::globalObject() const
{
return rep->globalObject();
}
void Interpreter::initGlobalObject()
{
rep->initGlobalObject();
}
void Interpreter::lock()
{
InterpreterImp::lock();
}
void Interpreter::unlock()
{
InterpreterImp::unlock();
}
ExecState *Interpreter::globalExec()
{
return rep->globalExec();
}
bool Interpreter::checkSyntax(const UString &code, int *errLine, UString *errMsg)
{
return rep->checkSyntax(code,errLine,errMsg);
}
bool Interpreter::checkSyntax(const UString &code)
{
return rep->checkSyntax(code);
}
Completion Interpreter::evaluate(const UString &code, const Value &thisV)
{
return rep->evaluate(code,thisV);
}
InterpreterImp *Interpreter::imp()
{
return rep;
}
Object Interpreter::builtinObject() const
{
return rep->builtinObject();
}
Object Interpreter::builtinFunction() const
{
return rep->builtinFunction();
}
Object Interpreter::builtinArray() const
{
return rep->builtinArray();
}
Object Interpreter::builtinBoolean() const
{
return rep->builtinBoolean();
}
Object Interpreter::builtinString() const
{
return rep->builtinString();
}
Object Interpreter::builtinNumber() const
{
return rep->builtinNumber();
}
Object Interpreter::builtinDate() const
{
return rep->builtinDate();
}
Object Interpreter::builtinRegExp() const
{
return rep->builtinRegExp();
}
Object Interpreter::builtinError() const
{
return rep->builtinError();
}
Object Interpreter::builtinObjectPrototype() const
{
return rep->builtinObjectPrototype();
}
Object Interpreter::builtinFunctionPrototype() const
{
return rep->builtinFunctionPrototype();
}
Object Interpreter::builtinArrayPrototype() const
{
return rep->builtinArrayPrototype();
}
Object Interpreter::builtinBooleanPrototype() const
{
return rep->builtinBooleanPrototype();
}
Object Interpreter::builtinStringPrototype() const
{
return rep->builtinStringPrototype();
}
Object Interpreter::builtinNumberPrototype() const
{
return rep->builtinNumberPrototype();
}
Object Interpreter::builtinDatePrototype() const
{
return rep->builtinDatePrototype();
}
Object Interpreter::builtinRegExpPrototype() const
{
return rep->builtinRegExpPrototype();
}
Object Interpreter::builtinErrorPrototype() const
{
return rep->builtinErrorPrototype();
}
Object Interpreter::builtinEvalError() const
{
return rep->builtinEvalError();
}
Object Interpreter::builtinRangeError() const
{
return rep->builtinRangeError();
}
Object Interpreter::builtinReferenceError() const
{
return rep->builtinReferenceError();
}
Object Interpreter::builtinSyntaxError() const
{
return rep->builtinSyntaxError();
}
Object Interpreter::builtinTypeError() const
{
return rep->builtinTypeError();
}
Object Interpreter::builtinURIError() const
{
return rep->builtinURIError();
}
Object Interpreter::builtinEvalErrorPrototype() const
{
return rep->builtinEvalErrorPrototype();
}
Object Interpreter::builtinRangeErrorPrototype() const
{
return rep->builtinRangeErrorPrototype();
}
Object Interpreter::builtinReferenceErrorPrototype() const
{
return rep->builtinReferenceErrorPrototype();
}
Object Interpreter::builtinSyntaxErrorPrototype() const
{
return rep->builtinSyntaxErrorPrototype();
}
Object Interpreter::builtinTypeErrorPrototype() const
{
return rep->builtinTypeErrorPrototype();
}
Object Interpreter::builtinURIErrorPrototype() const
{
return rep->builtinURIErrorPrototype();
}
void Interpreter::setCompatMode(CompatMode mode)
{
rep->setCompatMode(mode);
}
Interpreter::CompatMode Interpreter::compatMode() const
{
return rep->compatMode();
}
bool Interpreter::collect()
{
return Collector::collect();
}
#ifdef KJS_DEBUG_MEM
#include "lexer.h"
void Interpreter::finalCheck()
{
fprintf(stderr,"Interpreter::finalCheck()\n");
// Garbage collect - as many times as necessary
// (we could delete an object which was holding another object, so
// the deref() will happen too late for deleting the impl of the 2nd object).
while( Collector::collect() )
;
Node::finalCheck();
Collector::finalCheck();
Lexer::globalClear();
UString::globalClear();
}
#endif
// ------------------------------ ExecState --------------------------------------
void ExecState::setException(const Value &e)
{
if (e.isValid()) {
Debugger *dbg = _interpreter->imp()->debugger();
if (dbg)
dbg->exception(this,e,_context->inTryCatch());
}
_exception = e;
}
void ExecState::clearException()
{
terminate_request = false;
_exception = Value();
}
bool ExecState::terminate_request = false;
static bool defaultConfirm() { return true; }
bool (*ExecState::confirmTerminate)() = defaultConfirm;
bool ExecState::hadException()
{
if (terminate_request) {
terminate_request = false;
if (confirmTerminate())
_exception = Error::create((ExecState*)this);
}
return _exception.isValid();
}
void Interpreter::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }
Interpreter *ExecState::lexicalInterpreter() const
{
// TODO: use proper implementation
#if 1
return dynamicInterpreter();
#else
if (!_context) {
return dynamicInterpreter();
}
InterpreterImp *result = InterpreterImp::interpreterWithGlobalObject(_context->scopeChain().bottom());
if (!result) {
return dynamicInterpreter();
}
return result->interpreter();
#endif
}
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::Interpreter::builtinObject
Object builtinObject() const
Returns the builtin "Object" object.
Definition: interpreter.cpp:174
KJS::Interpreter::builtinBooleanPrototype
Object builtinBooleanPrototype() const
Returns the builtin "Boolean.prototype" object.
Definition: interpreter.cpp:234
KJS::Context::curStmtFirstLine
int curStmtFirstLine() const
The line number on which the current statement begins.
Definition: interpreter.cpp:76
KJS::Context::curStmtLastLine
int curStmtLastLine() const
The line number on which the current statement ends.
Definition: interpreter.cpp:81
KJS::ExecState::dynamicInterpreter
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: interpreter.h:453
KJS::Context::functionName
Identifier functionName() const
In the case of FunctionCode, the name of the function being called.
Definition: interpreter.cpp:91
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::Collector::collect
static bool collect()
Run the garbage collection.
Definition: collector.cpp:153
KJS::Context::args
List args() const
In the case of FunctionCode, the arguments passed to the function.
Definition: interpreter.cpp:96
KJS::Interpreter::globalObject
Object & globalObject() const
Returns the object that is used as the global object during all script execution performed by this in...
Definition: interpreter.cpp:129
KJS::Interpreter::builtinArrayPrototype
Object builtinArrayPrototype() const
Returns the builtin "Array.prototype" object.
Definition: interpreter.cpp:229
KJS::Interpreter::builtinFunction
Object builtinFunction() const
Returns the builtin "Function" object.
Definition: interpreter.cpp:179
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:395
KJS::Context::callingContext
const Context callingContext() const
Returns the context from which the current context was invoked.
Definition: interpreter.cpp:61
KJS::Interpreter::builtinDatePrototype
Object builtinDatePrototype() const
Returns the builtin "Date.prototype" object.
Definition: interpreter.cpp:249
KJS::Interpreter::builtinRegExpPrototype
Object builtinRegExpPrototype() const
Returns the builtin "RegExp.prototype" object.
Definition: interpreter.cpp:254
KJS::Error::create
static Object create(ExecState *exec, ErrorType errtype=GeneralError, const char *message=0, int lineno=-1, int sourceId=-1)
Factory method for error objects.
Definition: object.cpp:504
KJS::Interpreter::builtinDate
Object builtinDate() const
Returns the builtin "Date" object.
Definition: interpreter.cpp:204
KJS::Interpreter::builtinBoolean
Object builtinBoolean() const
Returns the builtin "Boolean" object.
Definition: interpreter.cpp:189
KJS::Interpreter::globalExec
ExecState * globalExec()
Returns the execution state object which can be used to execute scripts using this interpreter at a t...
Definition: interpreter.cpp:149
KJS::Interpreter
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:173
KJS::Object
Represents an Object.
Definition: object.h:82
KJS::Context
Represents an execution context, as specified by section 10 of the ECMA spec.
Definition: interpreter.h:73
KJS::Context::function
Object function() const
In the case of FunctionCode, the function objects being called.
Definition: interpreter.cpp:86
KJS::Interpreter::builtinError
Object builtinError() const
Returns the builtin "Error" object.
Definition: interpreter.cpp:214
KJS::Interpreter::builtinNumber
Object builtinNumber() const
Returns the builtin "Number" object.
Definition: interpreter.cpp:199
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::Interpreter::builtinErrorPrototype
Object builtinErrorPrototype() const
Returns the builtin "Error.prototype" object.
Definition: interpreter.cpp:259
KJS::Context::thisValue
Object thisValue() const
Returns the "this" value for the execution context.
Definition: interpreter.cpp:56
KJS::Interpreter::evaluate
Completion evaluate(const UString &code, const Value &thisV=Value())
Evaluates the supplied ECMAScript code.
Definition: interpreter.cpp:164
KJS::Interpreter::builtinEvalError
Object builtinEvalError() const
The initial value of "Error" global property.
Definition: interpreter.cpp:264
KJS::Completion
Completion objects are used to convey the return status and value from functions. ...
Definition: completion.h:49
KJS::Interpreter::builtinObjectPrototype
Object builtinObjectPrototype() const
Returns the builtin "Object.prototype" object.
Definition: interpreter.cpp:219
KJS::Context::codeType
CodeType codeType() const
The type of code being executed in this context.
Definition: interpreter.cpp:66
KJS::Interpreter::checkSyntax
bool checkSyntax(const UString &code, int *errLine, UString *errMsg)
Parses the supplied ECMAScript code and checks for syntax errors.
Definition: interpreter.cpp:154
KJS::List
Native list type.
Definition: list.h:48
KJS::Interpreter::builtinString
Object builtinString() const
Returns the builtin "String" object.
Definition: interpreter.cpp:194
KJS::Context::scopeChain
const ScopeChain & scopeChain() const
Returns the scope chain for this execution context.
Definition: interpreter.cpp:46
KJS::Interpreter::builtinArray
Object builtinArray() const
Returns the builtin "Array" object.
Definition: interpreter.cpp:184
KJS::Interpreter::builtinFunctionPrototype
Object builtinFunctionPrototype() const
Returns the builtin "Function.prototype" object.
Definition: interpreter.cpp:224
KJS::Interpreter::builtinNumberPrototype
Object builtinNumberPrototype() const
Returns the builtin "Number.prototype" object.
Definition: interpreter.cpp:244
KJS::Interpreter::builtinStringPrototype
Object builtinStringPrototype() const
Returns the builtin "String.prototype" object.
Definition: interpreter.cpp:239
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:182
KJS::Interpreter::builtinRegExp
Object builtinRegExp() const
Returns the builtin "RegExp" object.
Definition: interpreter.cpp:209
KJS::Context::sourceId
int sourceId() const
The identifier of the source code fragment containing the code being executed.
Definition: interpreter.cpp:71
KJS::Context::variableObject
Object variableObject() const
Returns the variable object for the execution context.
Definition: interpreter.cpp:51
KJS::Interpreter::setCompatMode
void setCompatMode(CompatMode mode)
Call this to enable a compatibility mode with another browser.
Definition: interpreter.cpp:324
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439
KJS::Interpreter::collect
static bool collect()
Run the garbage collection.
Definition: interpreter.cpp:334
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Interpreter::Interpreter
Interpreter()
Creates a new interpreter.
Definition: interpreter.cpp:118

KDE Home | KDE Accessibility Home | Description of Access Keys