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

Last change on this file since 29825 was 29810, checked in by Darin Adler, 17 years ago

Reviewed by Eric Seidel.

  • JavaScriptCore.exp: Export the GlobalExecState constructor instead of the global flavor of the ExecState constructor. It'd probably be cleaner to not export either one, but JSGlobalObject inlines the code that constructs the ExecState. If we changed that, we could remove this export.
  • JavaScriptCore.xcodeproj/project.pbxproj: Re-sorted a few things and put the new source files into the kjs group rather than at the top level.
  • kjs/ExecState.cpp: (KJS::ExecState::ExecState): Marked inline and updated for data member name changes. This is now only for use for the derived classes. Also removed code that sets the unused m_savedExec data member for the global case. That data member is only used for the other two types. (KJS::ExecState::~ExecState): Marked inline and removed all the code. The derived class destructors now inclde the appropriate code. (KJS::ExecState::lexicalGlobalObject): Removed unneeded special case for an empty scope chain. The bottom function already returns 0 for that case, so the general case code handles it fine. Also changed to use data members directly rather than calling functions. (KJS::GlobalExecState::GlobalExecState): Added. Calls through to the base class constructor. (KJS::GlobalExecState::~GlobalExecState): Added. (KJS::InterpreterExecState::InterpreterExecState): Added. Moved code to manipulate activeExecStates here since we don't want to have to check for the special case of globalExec. (KJS::InterpreterExecState::~InterpreterExecState): Added. (KJS::EvalExecState::EvalExecState): Added. (KJS::EvalExecState::~EvalExecState): Added. (KJS::FunctionExecState::FunctionExecState): Added. (KJS::FunctionExecState::~FunctionExecState): Added.
  • kjs/ExecState.h: Tweaked the header, includes, and declarations a bit. Made ExecState inherit from Noncopyable. Reformatted some comments and made them a bit more brief. Rearranged declarations a little bit and removed unused savedExec function. Changed seenLabels function to return a reference rather than a pointer. Made constructors and destructor protected, and also did the same with all data members. Renamed m_thisVal to m_thisValue and ls to m_labelStack. Added three new derived classes for each of the types of ExecState. The primary goal here was to remove a branch from the code in the destructor, but it's also clearer than overloading the arguments to the ExecState constructor.
  • kjs/JSGlobalObject.cpp: (KJS::getCurrentTime): Fixed formatting. (KJS::JSGlobalObject::pushActivation): Removed parentheses that don't make the expression clearer -- other similar sites didn't have these parentheses, even the one a couple lines earlier that sets stackEntry. (KJS::JSGlobalObject::tearOffActivation): Got rid of unneeded static_cast (I think I mentioned this during patch review) and used an early exit so that the entire contents of the function aren't nested inside an if statement. Also removed the check of codeType, instead checking Activation for 0. For now, I kept the codeType check, but inside an assertion.
  • kjs/JSGlobalObject.h: Changed type of globalExec to GlobalExecState.
  • kjs/function.cpp: (KJS::FunctionImp::callAsFunction): Changed type to FunctionExecState. (KJS::GlobalFuncImp::callAsFunction): Changed type to EvalExecState.
  • kjs/interpreter.cpp: (KJS::Interpreter::evaluate): Changed type to GlobalExecState.
  • kjs/nodes.cpp: (KJS::ContinueNode::execute): Changed code since seenLabels() returns a reference now instead of a pointer. (KJS::BreakNode::execute): Ditto. (KJS::LabelNode::execute): Ditto.
  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 * Copyright (C) 2001 Peter Kelly ([email protected])
6 * Copyright (C) 2003, 2007 Apple Inc.
7 *
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.
12 *
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.
17 *
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.
22 *
23 */
24
25#include "config.h"
26#include "interpreter.h"
27
28#include "ExecState.h"
29#include "JSGlobalObject.h"
30#include "Parser.h"
31#include "SavedBuiltins.h"
32#include "array_object.h"
33#include "bool_object.h"
34#include "collector.h"
35#include "date_object.h"
36#include "debugger.h"
37#include "error_object.h"
38#include "function_object.h"
39#include "internal.h"
40#include "math_object.h"
41#include "nodes.h"
42#include "number_object.h"
43#include "object.h"
44#include "object_object.h"
45#include "operations.h"
46#include "regexp_object.h"
47#include "runtime.h"
48#include "string_object.h"
49#include "types.h"
50#include "value.h"
51#include <math.h>
52#include <signal.h>
53#include <stdio.h>
54#include <wtf/Assertions.h>
55
56namespace KJS {
57
58Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UString& code)
59{
60 return checkSyntax(exec, sourceURL, startingLineNumber, code.data(), code.size());
61}
62
63Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength)
64{
65 JSLock lock;
66
67 int errLine;
68 UString errMsg;
69 RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(sourceURL, startingLineNumber, code, codeLength, 0, &errLine, &errMsg);
70 if (!progNode)
71 return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, 0, sourceURL));
72 return Completion(Normal);
73}
74
75Completion Interpreter::evaluate(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV)
76{
77 return evaluate(exec, sourceURL, startingLineNumber, code.data(), code.size(), thisV);
78}
79
80Completion Interpreter::evaluate(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UChar* code, int codeLength, JSValue* thisV)
81{
82 JSLock lock;
83
84 JSGlobalObject* globalObject = exec->dynamicGlobalObject();
85
86 if (globalObject->recursion() >= 20)
87 return Completion(Throw, Error::create(exec, GeneralError, "Recursion too deep"));
88
89 // parse the source code
90 int sourceId;
91 int errLine;
92 UString errMsg;
93 RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(sourceURL, startingLineNumber, code, codeLength, &sourceId, &errLine, &errMsg);
94
95 // notify debugger that source has been parsed
96 if (globalObject->debugger()) {
97 bool cont = globalObject->debugger()->sourceParsed(exec, sourceId, sourceURL, UString(code, codeLength), startingLineNumber, errLine, errMsg);
98 if (!cont)
99 return Completion(Break);
100 }
101
102 // no program node means a syntax error occurred
103 if (!progNode)
104 return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, sourceId, sourceURL));
105
106 exec->clearException();
107
108 globalObject->incRecursion();
109
110 JSObject* thisObj = globalObject;
111
112 // "this" must be an object... use same rules as Function.prototype.apply()
113 if (thisV && !thisV->isUndefinedOrNull())
114 thisObj = thisV->toObject(exec);
115
116 Completion res;
117 if (exec->hadException())
118 // the thisV->toObject() conversion above might have thrown an exception - if so, propagate it
119 res = Completion(Throw, exec->exception());
120 else {
121 // execute the code
122 InterpreterExecState newExec(globalObject, thisObj, progNode.get());
123 JSValue* value = progNode->execute(&newExec);
124 res = Completion(newExec.completionType(), value);
125 }
126
127 globalObject->decRecursion();
128
129 if (shouldPrintExceptions() && res.complType() == Throw) {
130 JSLock lock;
131 ExecState* exec = globalObject->globalExec();
132 CString f = sourceURL.UTF8String();
133 CString message = res.value()->toObject(exec)->toString(exec).UTF8String();
134 int line = res.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
135#if PLATFORM(WIN_OS)
136 printf("%s line %d: %s\n", f.c_str(), line, message.c_str());
137#else
138 printf("[%d] %s line %d: %s\n", getpid(), f.c_str(), line, message.c_str());
139#endif
140 }
141
142 return res;
143}
144
145static bool printExceptions = false;
146
147bool Interpreter::shouldPrintExceptions()
148{
149 return printExceptions;
150}
151
152void Interpreter::setShouldPrintExceptions(bool print)
153{
154 printExceptions = print;
155}
156
157} // namespace KJS
Note: See TracBrowser for help on using the repository browser.