]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/interpreter.cpp
1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6 * Copyright (C) 2003, 2007 Apple Inc.
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.
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.
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.
26 #include "interpreter.h"
28 #include "ExecState.h"
29 #include "JSGlobalObject.h"
31 #include "SavedBuiltins.h"
32 #include "array_object.h"
33 #include "bool_object.h"
34 #include "collector.h"
35 #include "date_object.h"
37 #include "error_object.h"
38 #include "function_object.h"
40 #include "math_object.h"
42 #include "number_object.h"
44 #include "object_object.h"
45 #include "operations.h"
46 #include "regexp_object.h"
48 #include "string_object.h"
53 #include <wtf/Assertions.h>
57 Completion
Interpreter::checkSyntax(ExecState
* exec
, const SourceCode
& source
)
63 RefPtr
<ProgramNode
> progNode
= parser().parse
<ProgramNode
>(source
, &errLine
, &errMsg
);
65 return Completion(Throw
, Error::create(exec
, SyntaxError
, errMsg
, errLine
, source
.provider()->asID(), source
.provider()->url()));
66 return Completion(Normal
);
69 Completion
Interpreter::evaluate(ExecState
* exec
, const SourceCode
& source
, JSValue
* thisV
)
73 JSGlobalObject
* globalObject
= exec
->dynamicGlobalObject();
75 if (globalObject
->recursion() >= 20)
76 return Completion(Throw
, Error::create(exec
, GeneralError
, "Recursion too deep"));
78 // parse the source code
81 RefPtr
<ProgramNode
> progNode
= parser().parse
<ProgramNode
>(source
, &errLine
, &errMsg
);
83 // removed debugger support
85 // no program node means a syntax error occurred
87 return Completion(Throw
, Error::create(exec
, SyntaxError
, errMsg
, errLine
, source
.provider()->asID(), source
.provider()->url()));
89 exec
->clearException();
91 globalObject
->incRecursion();
93 JSObject
* thisObj
= globalObject
;
95 // "this" must be an object... use same rules as Function.prototype.apply()
96 if (thisV
&& !thisV
->isUndefinedOrNull())
97 thisObj
= thisV
->toObject(exec
);
100 if (exec
->hadException())
101 // the thisV->toObject() conversion above might have thrown an exception - if so, propagate it
102 res
= Completion(Throw
, exec
->exception());
105 InterpreterExecState
newExec(globalObject
, thisObj
, progNode
.get());
106 JSValue
* value
= progNode
->execute(&newExec
);
107 res
= Completion(newExec
.completionType(), value
);
110 globalObject
->decRecursion();
112 if (shouldPrintExceptions() && res
.complType() == Throw
) {
114 ExecState
* exec
= globalObject
->globalExec();
115 CString f
= source
.provider()->url().UTF8String();
116 CString message
= res
.value()->toObject(exec
)->toString(exec
).UTF8String();
117 int line
= res
.value()->toObject(exec
)->get(exec
, "line")->toUInt32(exec
);
119 printf("%s line %d: %s\n", f
.c_str(), line
, message
.c_str());
121 printf("[%d] %s line %d: %s\n", getpid(), f
.c_str(), line
, message
.c_str());
128 static bool printExceptions
= false;
130 bool Interpreter::shouldPrintExceptions()
132 return printExceptions
;
135 void Interpreter::setShouldPrintExceptions(bool print
)
137 printExceptions
= print
;