DebuggerVariableJSObject.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <matthewcolsson@gmail.com>
  3. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "DebuggerVariableJSObject.h"
  9. #include "Debugger.h"
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/PrimitiveString.h>
  13. #include <LibJS/Runtime/PropertyKey.h>
  14. namespace HackStudio {
  15. DebuggerVariableJSObject* DebuggerVariableJSObject::create(DebuggerGlobalJSObject& global_object, Debug::DebugInfo::VariableInfo const& variable_info)
  16. {
  17. return global_object.heap().allocate<DebuggerVariableJSObject>(global_object, variable_info, *global_object.object_prototype());
  18. }
  19. DebuggerVariableJSObject::DebuggerVariableJSObject(Debug::DebugInfo::VariableInfo const& variable_info, JS::Object& prototype)
  20. : JS::Object(prototype)
  21. , m_variable_info(variable_info)
  22. {
  23. }
  24. JS::ThrowCompletionOr<bool> DebuggerVariableJSObject::internal_set(const JS::PropertyKey& property_key, JS::Value value, JS::Value)
  25. {
  26. auto& vm = this->vm();
  27. if (!property_key.is_string())
  28. return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Invalid variable name {}", property_key.to_string()));
  29. auto name = property_key.as_string();
  30. auto it = m_variable_info.members.find_if([&](auto& variable) {
  31. return variable->name == name;
  32. });
  33. if (it.is_end())
  34. return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Variable of type {} has no property {}", m_variable_info.type_name, property_key));
  35. auto& member = **it;
  36. auto new_value = debugger_object().js_to_debugger(value, member);
  37. if (!new_value.has_value())
  38. return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Cannot convert JS value {} to variable {} of type {}", value.to_string_without_side_effects(), name, member.type_name));
  39. Debugger::the().session()->poke(member.location_data.address, new_value.value());
  40. return true;
  41. }
  42. DebuggerGlobalJSObject& DebuggerVariableJSObject::debugger_object() const
  43. {
  44. return static_cast<DebuggerGlobalJSObject&>(global_object());
  45. }
  46. }