mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-03 01:38:52 +00:00
HackStudio: Implement custom JS -> C++ "proxy" objects
This patch adds a custom JS Object type that will convert written properties to their C++ equivalents, reflecting JS writes back to the debugging session. This is better than a simple proxy because printing this custom object works as expected because properties still exist on the object as existing handlers expect.
This commit is contained in:
parent
60d329a186
commit
4f2c0e9968
Notes:
sideshowbarker
2024-07-18 19:06:09 +09:00
Author: https://github.com/FalseHonesty
Commit: 4f2c0e9968
Pull-request: https://github.com/SerenityOS/serenity/pull/6285
Reviewed-by: https://github.com/itamar8910 ✅
Reviewed-by: https://github.com/linusg
5 changed files with 111 additions and 36 deletions
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Matthew Olsson <matthewcolsson@gmail.com>
|
||||
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "DebuggerVariableJSObject.h"
|
||||
#include "Debugger.h"
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/PrimitiveString.h>
|
||||
#include <LibJS/Runtime/PropertyName.h>
|
||||
|
||||
namespace HackStudio {
|
||||
|
||||
DebuggerVariableJSObject* DebuggerVariableJSObject::create(DebuggerGlobalJSObject& global_object, const Debug::DebugInfo::VariableInfo& variable_info)
|
||||
{
|
||||
return global_object.heap().allocate<DebuggerVariableJSObject>(global_object, variable_info, *global_object.object_prototype());
|
||||
}
|
||||
|
||||
DebuggerVariableJSObject::DebuggerVariableJSObject(const Debug::DebugInfo::VariableInfo& variable_info, JS::Object& prototype)
|
||||
: JS::Object(prototype)
|
||||
, m_variable_info(variable_info)
|
||||
{
|
||||
}
|
||||
|
||||
DebuggerVariableJSObject::~DebuggerVariableJSObject()
|
||||
{
|
||||
}
|
||||
|
||||
bool DebuggerVariableJSObject::put(const JS::PropertyName& name, JS::Value value, JS::Value receiver)
|
||||
{
|
||||
if (m_is_writing_properties)
|
||||
return JS::Object::put(name, value, receiver);
|
||||
|
||||
if (!name.is_string()) {
|
||||
vm().throw_exception<JS::TypeError>(global_object(), String::formatted("Invalid variable name {}", name.to_string()));
|
||||
return false;
|
||||
}
|
||||
|
||||
auto property_name = name.as_string();
|
||||
auto it = m_variable_info.members.find_if([&](auto& variable) {
|
||||
return variable->name == property_name;
|
||||
});
|
||||
|
||||
if (it.is_end()) {
|
||||
vm().throw_exception<JS::TypeError>(global_object(), String::formatted("Variable of type {} has no property {}", m_variable_info.type_name, property_name));
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& member = **it;
|
||||
auto new_value = debugger_object().js_to_debugger(value, member);
|
||||
if (!new_value.has_value()) {
|
||||
auto string_error = String::formatted("Cannot convert JS value {} to variable {} of type {}", value.to_string_without_side_effects(), name.as_string(), member.type_name);
|
||||
vm().throw_exception<JS::TypeError>(global_object(), string_error);
|
||||
return false;
|
||||
}
|
||||
Debugger::the().session()->poke((u32*)member.location_data.address, new_value.value());
|
||||
return true;
|
||||
}
|
||||
|
||||
DebuggerGlobalJSObject& DebuggerVariableJSObject::debugger_object() const
|
||||
{
|
||||
return static_cast<DebuggerGlobalJSObject&>(global_object());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue