From 33eea1f72293e711faeb774381ecdb23392dc260 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 13 Jun 2021 01:04:52 +0100 Subject: [PATCH] LibJS: Fix Object.prototype.valueOf() behavior No idea why it was implemented like this, but a value_of() call on the coerced object is not part of the spec. Also added some tests. --- Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp | 5 +---- .../Tests/builtins/Object/Object.prototype.valueOf.js | 8 ++++++++ 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.valueOf.js diff --git a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp index 0868993331..03d4d4a13a 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -106,10 +106,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_locale_string) // 20.1.3.7 Object.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-object.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::value_of) { - auto* this_object = vm.this_value(global_object).to_object(global_object); - if (!this_object) - return {}; - return this_object->value_of(); + return vm.this_value(global_object).to_object(global_object); } // 20.1.3.4 Object.prototype.propertyIsEnumerable ( V ), https://tc39.es/ecma262/#sec-object.prototype.propertyisenumerable diff --git a/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.valueOf.js b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.valueOf.js new file mode 100644 index 0000000000..35e46130bf --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.prototype.valueOf.js @@ -0,0 +1,8 @@ +test("basic functionality", () => { + expect(Object.prototype.valueOf).toHaveLength(0); + + const o = {}; + expect(o.valueOf()).toBe(o); + + expect(Object.prototype.valueOf.call(42)).toEqual(new Number(42)); +});