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"
29 #include "Operations.h"
37 class Recompiler
: public MarkedBlock::VoidFunctor
{
40 Recompiler(JSC::Debugger
*);
42 Recompiler(Debugger
*);
45 void operator()(JSCell
*);
48 typedef HashSet
<FunctionExecutable
*> FunctionExecutableSet
;
49 typedef HashMap
<SourceProvider
*, ExecState
*> SourceProviderMap
;
52 JSC::Debugger
* m_debugger
;
56 FunctionExecutableSet m_functionExecutables
;
57 SourceProviderMap m_sourceProviders
;
61 inline Recompiler::Recompiler(JSC::Debugger
* debugger
)
63 inline Recompiler::Recompiler(Debugger
* debugger
)
65 : m_debugger(debugger
)
69 inline Recompiler::~Recompiler()
71 // Call sourceParsed() after reparsing all functions because it will execute
72 // JavaScript in the inspector.
73 SourceProviderMap::const_iterator end
= m_sourceProviders
.end();
74 for (SourceProviderMap::const_iterator iter
= m_sourceProviders
.begin(); iter
!= end
; ++iter
)
75 m_debugger
->sourceParsed(iter
->value
, iter
->key
, -1, String());
78 inline void Recompiler::operator()(JSCell
* cell
)
80 if (!cell
->inherits(&JSFunction::s_info
))
83 JSFunction
* function
= jsCast
<JSFunction
*>(cell
);
84 if (function
->executable()->isHostFunction())
87 FunctionExecutable
* executable
= function
->jsExecutable();
89 // Check if the function is already in the set - if so,
90 // we've already retranslated it, nothing to do here.
91 if (!m_functionExecutables
.add(executable
).isNewEntry
)
94 ExecState
* exec
= function
->scope()->globalObject()->JSGlobalObject::globalExec();
95 executable
->clearCodeIfNotCompiling();
96 executable
->clearUnlinkedCodeForRecompilationIfNotCompiling();
97 if (m_debugger
== function
->scope()->globalObject()->debugger())
98 m_sourceProviders
.add(executable
->source().provider(), exec
);
105 Debugger::~Debugger()
107 HashSet
<JSGlobalObject
*>::iterator end
= m_globalObjects
.end();
108 for (HashSet
<JSGlobalObject
*>::iterator it
= m_globalObjects
.begin(); it
!= end
; ++it
)
109 (*it
)->setDebugger(0);
112 void Debugger::attach(JSGlobalObject
* globalObject
)
114 ASSERT(!globalObject
->debugger());
115 globalObject
->setDebugger(this);
116 m_globalObjects
.add(globalObject
);
119 void Debugger::detach(JSGlobalObject
* globalObject
)
121 ASSERT(m_globalObjects
.contains(globalObject
));
122 m_globalObjects
.remove(globalObject
);
123 globalObject
->setDebugger(0);
126 void Debugger::recompileAllJSFunctions(VM
* vm
)
128 // If JavaScript is running, it's not safe to recompile, since we'll end
129 // up throwing away code that is live on the stack.
130 ASSERT(!vm
->dynamicGlobalObject
);
131 if (vm
->dynamicGlobalObject
)
134 Recompiler
recompiler(this);
135 vm
->heap
.objectSpace().forEachLiveCell(recompiler
);
138 JSValue
evaluateInGlobalCallFrame(const String
& script
, JSValue
& exception
, JSGlobalObject
* globalObject
)
140 CallFrame
* globalCallFrame
= globalObject
->globalExec();
141 VM
& vm
= globalObject
->vm();
143 EvalExecutable
* eval
= EvalExecutable::create(globalCallFrame
, vm
.codeCache(), makeSource(script
), false);
145 exception
= vm
.exception
;
146 vm
.exception
= JSValue();
150 JSValue result
= vm
.interpreter
->execute(eval
, globalCallFrame
, globalObject
, globalCallFrame
->scope());
152 exception
= vm
.exception
;
153 vm
.exception
= JSValue();