mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 18:38:12 +00:00
LibJS: Fix ProxyObject get/set with symbol property name
We can't assume that property names can be converted to strings anymore, as we have symbols. Use name.to_value() instead. This makes something like this possible: new Proxy(Object, { get(t, p) { return t[p] } })[Symbol.hasInstance]
This commit is contained in:
parent
44e38b8457
commit
2cf8649d0e
3 changed files with 8 additions and 5 deletions
|
@ -229,7 +229,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const Prop
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()));
|
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), name.to_value(vm));
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
if (!trap_result.is_object() && !trap_result.is_undefined()) {
|
if (!trap_result.is_object() && !trap_result.is_undefined()) {
|
||||||
|
@ -337,7 +337,7 @@ bool ProxyObject::has_property(const PropertyName& name) const
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()));
|
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), name.to_value(vm));
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return false;
|
return false;
|
||||||
if (!trap_result.to_boolean()) {
|
if (!trap_result.to_boolean()) {
|
||||||
|
@ -376,7 +376,7 @@ Value ProxyObject::get(const PropertyName& name, Value) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()), Value(const_cast<ProxyObject*>(this)));
|
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), name.to_value(vm), Value(const_cast<ProxyObject*>(this)));
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
auto target_desc = m_target.get_own_property_descriptor(name);
|
auto target_desc = m_target.get_own_property_descriptor(name);
|
||||||
|
@ -411,7 +411,7 @@ bool ProxyObject::put(const PropertyName& name, Value value, Value)
|
||||||
vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "set");
|
vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "set");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()), value, Value(const_cast<ProxyObject*>(this)));
|
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), name.to_value(vm), value, Value(const_cast<ProxyObject*>(this)));
|
||||||
if (vm.exception() || !trap_result.to_boolean())
|
if (vm.exception() || !trap_result.to_boolean())
|
||||||
return false;
|
return false;
|
||||||
auto target_desc = m_target.get_own_property_descriptor(name);
|
auto target_desc = m_target.get_own_property_descriptor(name);
|
||||||
|
@ -446,7 +446,7 @@ Value ProxyObject::delete_property(const PropertyName& name)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()));
|
auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), name.to_value(vm));
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
if (!trap_result.to_boolean())
|
if (!trap_result.to_boolean())
|
||||||
|
|
|
@ -38,6 +38,7 @@ describe("[[Get]] trap normal behavior", () => {
|
||||||
expect(p.baz).toBe(3);
|
expect(p.baz).toBe(3);
|
||||||
expect(p.qux).toBe(3);
|
expect(p.qux).toBe(3);
|
||||||
expect(p.test).toBeUndefined();
|
expect(p.test).toBeUndefined();
|
||||||
|
expect(p[Symbol.hasInstance]).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,8 @@ describe("[[Set]] trap normal behavior", () => {
|
||||||
expect(p.foo).toBe(20);
|
expect(p.foo).toBe(20);
|
||||||
p.foo = 10;
|
p.foo = 10;
|
||||||
expect(p.foo).toBe(10);
|
expect(p.foo).toBe(10);
|
||||||
|
p[Symbol.hasInstance] = "foo"
|
||||||
|
expect(p[Symbol.hasInstance]).toBe("foo");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue