2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "Interpreter.h"
27 #include "JSFunction.h"
28 #include "JSGlobalObject.h"
36 class Recompiler : public MarkedBlock::VoidFunctor {
38 Recompiler(Debugger*);
40 void operator()(JSCell*);
43 typedef HashSet<FunctionExecutable*> FunctionExecutableSet;
44 typedef HashMap<SourceProvider*, ExecState*> SourceProviderMap;
47 FunctionExecutableSet m_functionExecutables;
48 SourceProviderMap m_sourceProviders;
51 inline Recompiler::Recompiler(Debugger* debugger)
52 : m_debugger(debugger)
56 inline Recompiler::~Recompiler()
58 // Call sourceParsed() after reparsing all functions because it will execute
59 // JavaScript in the inspector.
60 SourceProviderMap::const_iterator end = m_sourceProviders.end();
61 for (SourceProviderMap::const_iterator iter = m_sourceProviders.begin(); iter != end; ++iter)
62 m_debugger->sourceParsed(iter->second, iter->first, -1, UString());
65 inline void Recompiler::operator()(JSCell* cell)
67 if (!cell->inherits(&JSFunction::s_info))
70 JSFunction* function = asFunction(cell);
71 if (function->executable()->isHostFunction())
74 FunctionExecutable* executable = function->jsExecutable();
76 // Check if the function is already in the set - if so,
77 // we've already retranslated it, nothing to do here.
78 if (!m_functionExecutables.add(executable).second)
81 ExecState* exec = function->scope()->globalObject->JSGlobalObject::globalExec();
82 executable->discardCode();
83 if (m_debugger == function->scope()->globalObject->debugger())
84 m_sourceProviders.add(executable->source().provider(), exec);
93 HashSet<JSGlobalObject*>::iterator end = m_globalObjects.end();
94 for (HashSet<JSGlobalObject*>::iterator it = m_globalObjects.begin(); it != end; ++it)
95 (*it)->setDebugger(0);
98 void Debugger::attach(JSGlobalObject* globalObject)
100 ASSERT(!globalObject->debugger());
101 globalObject->setDebugger(this);
102 m_globalObjects.add(globalObject);
105 void Debugger::detach(JSGlobalObject* globalObject)
107 ASSERT(m_globalObjects.contains(globalObject));
108 m_globalObjects.remove(globalObject);
109 globalObject->setDebugger(0);
112 void Debugger::recompileAllJSFunctions(JSGlobalData* globalData)
114 // If JavaScript is running, it's not safe to recompile, since we'll end
115 // up throwing away code that is live on the stack.
116 ASSERT(!globalData->dynamicGlobalObject);
117 if (globalData->dynamicGlobalObject)
120 Recompiler recompiler(this);
121 globalData->heap.forEachCell(recompiler);
124 JSValue evaluateInGlobalCallFrame(const UString& script, JSValue& exception, JSGlobalObject* globalObject)
126 CallFrame* globalCallFrame = globalObject->globalExec();
127 JSGlobalData& globalData = globalObject->globalData();
129 EvalExecutable* eval = EvalExecutable::create(globalCallFrame, makeSource(script), false);
131 exception = globalData.exception;
132 globalData.exception = JSValue();
136 JSValue result = globalData.interpreter->execute(eval, globalCallFrame, globalObject, globalCallFrame->scopeChain());
137 if (globalData.exception) {
138 exception = globalData.exception;
139 globalData.exception = JSValue();