From 2645dfafcfe52c3ef3556cadca55fb7697e2627d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 4 Nov 2020 21:13:07 +0000 Subject: [PATCH] LibJS: Implement Object(value) constructor Not sure why we didn't have this yet, it's super simple :^) --- Libraries/LibJS/Runtime/ObjectConstructor.cpp | 5 ++++- Libraries/LibJS/Tests/builtins/Object/Object.js | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 2eda8632414..f2e1bdd3540 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -66,7 +66,10 @@ ObjectConstructor::~ObjectConstructor() Value ObjectConstructor::call() { - return Object::create_empty(global_object()); + auto value = vm().argument(0); + if (value.is_nullish()) + return Object::create_empty(global_object()); + return value.to_object(global_object()); } Value ObjectConstructor::construct(Function&) diff --git a/Libraries/LibJS/Tests/builtins/Object/Object.js b/Libraries/LibJS/Tests/builtins/Object/Object.js index 3ff78623dae..544704c1f0c 100644 --- a/Libraries/LibJS/Tests/builtins/Object/Object.js +++ b/Libraries/LibJS/Tests/builtins/Object/Object.js @@ -5,4 +5,9 @@ test("basic functionality", () => { expect(typeof Object()).toBe("object"); expect(typeof new Object()).toBe("object"); + + expect(typeof Object(42)).toBe("object"); + expect(Object(42).valueOf()).toBe(42); + expect(typeof Object("foo")).toBe("object"); + expect(Object("foo").valueOf()).toBe("foo"); });