From fff7aceb9dc8c0ef9b86e5408f7be51ab95865af Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 16 Apr 2021 01:33:12 +0300 Subject: [PATCH] LibJS: Accept symbol property in ObjectPrototype::hasOwnProperty This is used by discord.com and allowed by the specification (https://tc39.es/ecma262/#sec-topropertykey) --- Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp | 4 ++-- .../Tests/builtins/Object/Object.prototype.hasOwnProperty.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp index 1292271367..bb4803016b 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -66,10 +66,10 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::has_own_property) auto* this_object = vm.this_value(global_object).to_object(global_object); if (!this_object) return {}; - auto name = vm.argument(0).to_string(global_object); + auto string_or_symbol = StringOrSymbol::from_value(global_object, vm.argument(0)); if (vm.exception()) return {}; - return Value(this_object->has_own_property(name)); + return Value(this_object->has_own_property(string_or_symbol)); } JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.hasOwnProperty.js b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.hasOwnProperty.js index 11063f42ec..fa5e4a8393 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.hasOwnProperty.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.hasOwnProperty.js @@ -10,4 +10,9 @@ test("basic functionality", () => { o.undefined = 2; expect(o.hasOwnProperty()).toBeTrue(); expect(o.hasOwnProperty(undefined)).toBeTrue(); + + var testSymbol = Symbol("real"); + o[testSymbol] = 3; + expect(o.hasOwnProperty(testSymbol)).toBeTrue(); + expect(o.hasOwnProperty(Symbol("fake"))).toBeFalse(); });