From f57ff634328d2f23bd0179b6f1f5fa7feb21f611 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 18 Nov 2024 15:47:11 -0500 Subject: [PATCH] LibJS: Implement Temporal.Duration.prototype.valueOf --- Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp | 8 ++++++++ Libraries/LibJS/Runtime/Temporal/DurationPrototype.h | 1 + .../Temporal/Duration/Duration.prototype.valueOf.js | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.valueOf.js diff --git a/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index 4447cb4fa7c..2fdeba377c5 100644 --- a/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -46,6 +46,7 @@ void DurationPrototype::initialize(Realm& realm) define_native_function(realm, vm.names.toString, to_string, 0, attr); define_native_function(realm, vm.names.toJSON, to_json, 0, attr); define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr); + define_native_function(realm, vm.names.valueOf, value_of, 0, attr); } // 7.3.3 get Temporal.Duration.prototype.years, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.years @@ -582,4 +583,11 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_locale_string) return PrimitiveString::create(vm, temporal_duration_to_string(duration, Auto {})); } +// 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof +JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::value_of) +{ + // 1. Throw a TypeError exception. + return vm.throw_completion(ErrorType::Convert, "Temporal.Duration", "a primitive value"); +} + } diff --git a/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h index 7faa80248e8..f6420de0f7a 100644 --- a/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h @@ -40,6 +40,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); + JS_DECLARE_NATIVE_FUNCTION(value_of); }; } diff --git a/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.valueOf.js b/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.valueOf.js new file mode 100644 index 00000000000..fb7a69cb6e4 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.valueOf.js @@ -0,0 +1,7 @@ +describe("errors", () => { + test("throws TypeError", () => { + expect(() => { + new Temporal.Duration().valueOf(); + }).toThrowWithMessage(TypeError, "Cannot convert Temporal.Duration to a primitive value"); + }); +});