]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/interpreter.cpp
f7ea2c55a9abd40d3166e9c57a349db78b7fe214
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 UString
& sourceURL
, int startingLineNumber
, const UString
& code
)
59 return checkSyntax(exec
, sourceURL
, startingLineNumber
, code
.data(), code
.size());
62 Completion
Interpreter::checkSyntax(ExecState
* exec
, const UString
& sourceURL
, int startingLineNumber
, const UChar
* code
, int codeLength
)
68 RefPtr
<ProgramNode
> progNode
= parser().parse
<ProgramNode
>(sourceURL
, startingLineNumber
, code
, codeLength
, 0, &errLine
, &errMsg
);
70 return Completion(Throw
, Error::create(exec
, SyntaxError
, errMsg
, errLine
, 0, sourceURL
));
71 return Completion(Normal
);
74 Completion
Interpreter::evaluate(ExecState
* exec
, const UString
& sourceURL
, int startingLineNumber
, const UString
& code
, JSValue
* thisV
)
76 return evaluate(exec
, sourceURL
, startingLineNumber
, code
.data(), code
.size(), thisV
);
79 Completion
Interpreter::evaluate(ExecState
* exec
, const UString
& sourceURL
, int startingLineNumber
, const UChar
* code
, int codeLength
, JSValue
* thisV
)
83 JSGlobalObject
* globalObject
= exec
->dynamicGlobalObject();
85 if (globalObject
->recursion() >= 20)
86 return Completion(Throw
, Error::create(exec
, GeneralError
, "Recursion too deep"));
88 // parse the source code
92 RefPtr
<ProgramNode
> progNode
= parser().parse
<ProgramNode
>(sourceURL
, startingLineNumber
, code
, codeLength
, &sourceId
, &errLine
, &errMsg
);
94 // notify debugger that source has been parsed
95 if (globalObject
->debugger()) {
96 bool cont
= globalObject
->debugger()->sourceParsed(exec
, sourceId
, sourceURL
, UString(code
, codeLength
), startingLineNumber
, errLine
, errMsg
);
98 return Completion(Break
);
101 // no program node means a syntax error occurred
103 return Completion(Throw
, Error::create(exec
, SyntaxError
, errMsg
, errLine
, sourceId
, sourceURL
));
105 exec
->clearException();
107 globalObject
->incRecursion();
109 JSObject
* thisObj
= globalObject
;
111 // "this" must be an object... use same rules as Function.prototype.apply()
112 if (thisV
&& !thisV
->isUndefinedOrNull())
113 thisObj
= thisV
->toObject(exec
);
116 if (exec
->hadException())
117 // the thisV->toObject() conversion above might have thrown an exception - if so, propagate it
118 res
= Completion(Throw
, exec
->exception());
121 InterpreterExecState
newExec(globalObject
, thisObj
, progNode
.get());
122 JSValue
* value
= progNode
->execute(&newExec
);
123 res
= Completion(newExec
.completionType(), value
);
126 globalObject
->decRecursion();
128 if (shouldPrintExceptions() && res
.complType() == Throw
) {
130 ExecState
* exec
= globalObject
->globalExec();
131 CString f
= sourceURL
.UTF8String();
132 CString message
= res
.value()->toObject(exec
)->toString(exec
).UTF8String();
133 int line
= res
.value()->toObject(exec
)->get(exec
, "line")->toUInt32(exec
);
135 printf("%s line %d: %s\n", f
.c_str(), line
, message
.c_str());
137 printf("[%d] %s line %d: %s\n", getpid(), f
.c_str(), line
, message
.c_str());
144 static bool printExceptions
= false;
146 bool Interpreter::shouldPrintExceptions()
148 return printExceptions
;
151 void Interpreter::setShouldPrintExceptions(bool print
)
153 printExceptions
= print
;