DebuggerVariableJSObject.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/Completion.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/PrimitiveString.h>
  12. #include <LibJS/Runtime/PropertyKey.h>
  13. namespace HackStudio {
  14. DebuggerVariableJSObject* DebuggerVariableJSObject::create(DebuggerGlobalJSObject& global_object, const Debug::DebugInfo::VariableInfo& variable_info)
  15. {
  16. return global_object.heap().allocate<DebuggerVariableJSObject>(global_object, variable_info, *global_object.object_prototype());
  17. }
  18. DebuggerVariableJSObject::DebuggerVariableJSObject(const Debug::DebugInfo::VariableInfo& variable_info, JS::Object& prototype)
  19. : JS::Object(prototype)
  20. , m_variable_info(variable_info)
  21. {
  22. }
  23. DebuggerVariableJSObject::~DebuggerVariableJSObject()
  24. {
  25. }
  26. JS::ThrowCompletionOr<bool> DebuggerVariableJSObject::internal_set(const JS::PropertyKey& property_key, JS::Value value, JS::Value)
  27. {
  28. auto& vm = this->vm();
  29. if (!property_key.is_string())
  30. return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Invalid variable name {}", property_key.to_string()));
  31. auto name = property_key.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. return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Variable of type {} has no property {}", m_variable_info.type_name, property_key));
  37. auto& member = **it;
  38. auto new_value = debugger_object().js_to_debugger(value, member);
  39. if (!new_value.has_value())
  40. 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));
  41. Debugger::the().session()->poke(member.location_data.address, new_value.value());
  42. return true;
  43. }
  44. DebuggerGlobalJSObject& DebuggerVariableJSObject::debugger_object() const
  45. {
  46. return static_cast<DebuggerGlobalJSObject&>(global_object());
  47. }
  48. }