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

Last change on this file since 30810 was 30810, checked in by [email protected], 17 years ago

Remove unused header includes from interpreter.cpp.

Reviewed by Darin.

  • kjs/interpreter.cpp: Remove unused header includes.
  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 2007 Apple Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "interpreter.h"
25
26#include "ExecState.h"
27#include "JSGlobalObject.h"
28#include "Parser.h"
29#include "debugger.h"
30#include <stdio.h>
31
32#if !PLATFORM(WIN_OS)
33#include <unistd.h>
34#endif
35
36namespace KJS {
37
38Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UString& code)
39{
40 return checkSyntax(exec, sourceURL, startingLineNumber, code.data(), code.size());
41}
42
43Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength)
44{
45 JSLock lock;
46
47 int errLine;
48 UString errMsg;
49 RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(sourceURL, startingLineNumber, code, codeLength, 0, &errLine, &errMsg);
50 if (!progNode)
51 return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, 0, sourceURL));
52 return Completion(Normal);
53}
54
55Completion Interpreter::evaluate(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV)
56{
57 return evaluate(exec, sourceURL, startingLineNumber, code.data(), code.size(), thisV);
58}
59
60Completion Interpreter::evaluate(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength, JSValue* thisV)
61{
62 JSLock lock;
63
64 JSGlobalObject* globalObject = exec->dynamicGlobalObject();
65
66 if (globalObject->recursion() >= 20)
67 return Completion(Throw, Error::create(exec, GeneralError, "Recursion too deep"));
68
69 // parse the source code
70 int sourceId;
71 int errLine;
72 UString errMsg;
73 RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(sourceURL, startingLineNumber, code, codeLength, &sourceId, &errLine, &errMsg);
74
75 // notify debugger that source has been parsed
76 if (globalObject->debugger()) {
77 bool cont = globalObject->debugger()->sourceParsed(exec, sourceId, sourceURL, UString(code, codeLength), startingLineNumber, errLine, errMsg);
78 if (!cont)
79 return Completion(Break);
80 }
81
82 // no program node means a syntax error occurred
83 if (!progNode)
84 return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, sourceId, sourceURL));
85
86 exec->clearException();
87
88 globalObject->incRecursion();
89
90 JSObject* thisObj = globalObject;
91
92 // "this" must be an object... use same rules as Function.prototype.apply()
93 if (thisV && !thisV->isUndefinedOrNull())
94 thisObj = thisV->toObject(exec);
95
96 Completion res;
97 if (exec->hadException())
98 // the thisV->toObject() conversion above might have thrown an exception - if so, propagate it
99 res = Completion(Throw, exec->exception());
100 else {
101 // execute the code
102 InterpreterExecState newExec(globalObject, thisObj, progNode.get());
103 JSValue* value = progNode->execute(&newExec);
104 res = Completion(newExec.completionType(), value);
105 }
106
107 globalObject->decRecursion();
108
109 if (shouldPrintExceptions() && res.complType() == Throw) {
110 JSLock lock;
111 ExecState* exec = globalObject->globalExec();
112 CString f = sourceURL.UTF8String();
113 CString message = res.value()->toObject(exec)->toString(exec).UTF8String();
114 int line = res.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
115#if PLATFORM(WIN_OS)
116 printf("%s line %d: %s\n", f.c_str(), line, message.c_str());
117#else
118 printf("[%d] %s line %d: %s\n", getpid(), f.c_str(), line, message.c_str());
119#endif
120 }
121
122 return res;
123}
124
125static bool printExceptions = false;
126
127bool Interpreter::shouldPrintExceptions()
128{
129 return printExceptions;
130}
131
132void Interpreter::setShouldPrintExceptions(bool print)
133{
134 printExceptions = print;
135}
136
137} // namespace KJS
Note: See TracBrowser for help on using the repository browser.