Debugger.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  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)
  5. *
  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.
  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. * Lesser General Public License for more details.
  15. *
  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
  19. *
  20. */
  21. #include "config.h"
  22. #include "Debugger.h"
  23. #include "Error.h"
  24. #include "Interpreter.h"
  25. #include "JSFunction.h"
  26. #include "JSGlobalObject.h"
  27. #include "Operations.h"
  28. #include "Parser.h"
  29. #include "Protect.h"
  30. namespace {
  31. using namespace JSC;
  32. class Recompiler : public MarkedBlock::VoidFunctor {
  33. public:
  34. Recompiler(Debugger*);
  35. ~Recompiler();
  36. void operator()(JSCell*);
  37. private:
  38. typedef HashSet<FunctionExecutable*> FunctionExecutableSet;
  39. typedef HashMap<SourceProvider*, ExecState*> SourceProviderMap;
  40. Debugger* m_debugger;
  41. FunctionExecutableSet m_functionExecutables;
  42. SourceProviderMap m_sourceProviders;
  43. };
  44. inline Recompiler::Recompiler(Debugger* debugger)
  45. : m_debugger(debugger)
  46. {
  47. }
  48. inline Recompiler::~Recompiler()
  49. {
  50. // Call sourceParsed() after reparsing all functions because it will execute
  51. // JavaScript in the inspector.
  52. SourceProviderMap::const_iterator end = m_sourceProviders.end();
  53. for (SourceProviderMap::const_iterator iter = m_sourceProviders.begin(); iter != end; ++iter)
  54. m_debugger->sourceParsed(iter->value, iter->key, -1, String());
  55. }
  56. inline void Recompiler::operator()(JSCell* cell)
  57. {
  58. if (!cell->inherits(&JSFunction::s_info))
  59. return;
  60. JSFunction* function = jsCast<JSFunction*>(cell);
  61. if (function->executable()->isHostFunction())
  62. return;
  63. FunctionExecutable* executable = function->jsExecutable();
  64. // Check if the function is already in the set - if so,
  65. // we've already retranslated it, nothing to do here.
  66. if (!m_functionExecutables.add(executable).isNewEntry)
  67. return;
  68. ExecState* exec = function->scope()->globalObject()->JSGlobalObject::globalExec();
  69. executable->clearCodeIfNotCompiling();
  70. executable->clearUnlinkedCodeForRecompilationIfNotCompiling();
  71. if (m_debugger == function->scope()->globalObject()->debugger())
  72. m_sourceProviders.add(executable->source().provider(), exec);
  73. }
  74. } // namespace
  75. namespace JSC {
  76. Debugger::~Debugger()
  77. {
  78. HashSet<JSGlobalObject*>::iterator end = m_globalObjects.end();
  79. for (HashSet<JSGlobalObject*>::iterator it = m_globalObjects.begin(); it != end; ++it)
  80. (*it)->setDebugger(0);
  81. }
  82. void Debugger::attach(JSGlobalObject* globalObject)
  83. {
  84. ASSERT(!globalObject->debugger());
  85. globalObject->setDebugger(this);
  86. m_globalObjects.add(globalObject);
  87. }
  88. void Debugger::detach(JSGlobalObject* globalObject)
  89. {
  90. ASSERT(m_globalObjects.contains(globalObject));
  91. m_globalObjects.remove(globalObject);
  92. globalObject->setDebugger(0);
  93. }
  94. void Debugger::recompileAllJSFunctions(VM* vm)
  95. {
  96. // If JavaScript is running, it's not safe to recompile, since we'll end
  97. // up throwing away code that is live on the stack.
  98. ASSERT(!vm->dynamicGlobalObject);
  99. if (vm->dynamicGlobalObject)
  100. return;
  101. Recompiler recompiler(this);
  102. vm->heap.objectSpace().forEachLiveCell(recompiler);
  103. }
  104. JSValue evaluateInGlobalCallFrame(const String& script, JSValue& exception, JSGlobalObject* globalObject)
  105. {
  106. CallFrame* globalCallFrame = globalObject->globalExec();
  107. VM& vm = globalObject->vm();
  108. EvalExecutable* eval = EvalExecutable::create(globalCallFrame, vm.codeCache(), makeSource(script), false);
  109. if (!eval) {
  110. exception = vm.exception;
  111. vm.exception = JSValue();
  112. return exception;
  113. }
  114. JSValue result = vm.interpreter->execute(eval, globalCallFrame, globalObject, globalCallFrame->scope());
  115. if (vm.exception) {
  116. exception = vm.exception;
  117. vm.exception = JSValue();
  118. }
  119. ASSERT(result);
  120. return result;
  121. }
  122. } // namespace JSC