DebuggerVariableJSObject.cpp 2.2 KB

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