diff options
author | Linus Groh <mail@linusgroh.de> | 2020-11-04 21:55:15 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-04 23:06:44 +0100 |
commit | 2cf8649d0ecd4d2428a469e2fe0e027e3fc1457b (patch) | |
tree | 98714fc94e3f305bfbf8df9a0cde3a7290eb32a0 | |
parent | 44e38b8457c62a8e3ecfa84a542011121ae6d782 (diff) | |
download | serenity-2cf8649d0ecd4d2428a469e2fe0e027e3fc1457b.zip |
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]
-rw-r--r-- | Libraries/LibJS/Runtime/ProxyObject.cpp | 10 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-get.js | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-set.js | 2 |
3 files changed, 8 insertions, 5 deletions
diff --git a/Libraries/LibJS/Runtime/ProxyObject.cpp b/Libraries/LibJS/Runtime/ProxyObject.cpp index 4c16e01e23..02cd11634f 100644 --- a/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -229,7 +229,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const Prop 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()) return {}; if (!trap_result.is_object() && !trap_result.is_undefined()) { @@ -337,7 +337,7 @@ bool ProxyObject::has_property(const PropertyName& name) const 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()) return false; if (!trap_result.to_boolean()) { @@ -376,7 +376,7 @@ Value ProxyObject::get(const PropertyName& name, Value) const 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()) return {}; 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"); 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()) return false; auto target_desc = m_target.get_own_property_descriptor(name); @@ -446,7 +446,7 @@ Value ProxyObject::delete_property(const PropertyName& name) 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()) return {}; if (!trap_result.to_boolean()) diff --git a/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-get.js b/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-get.js index 0bae932340..a1fa8499c2 100644 --- a/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-get.js +++ b/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-get.js @@ -38,6 +38,7 @@ describe("[[Get]] trap normal behavior", () => { expect(p.baz).toBe(3); expect(p.qux).toBe(3); expect(p.test).toBeUndefined(); + expect(p[Symbol.hasInstance]).toBeUndefined(); }); }); diff --git a/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-set.js b/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-set.js index 868741181f..c78235884e 100644 --- a/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-set.js +++ b/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-set.js @@ -40,6 +40,8 @@ describe("[[Set]] trap normal behavior", () => { expect(p.foo).toBe(20); p.foo = 10; expect(p.foo).toBe(10); + p[Symbol.hasInstance] = "foo" + expect(p[Symbol.hasInstance]).toBe("foo"); }); }); |