From 7fb05eb8788491c978e3cd46d0233004b9942b83 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 15 Aug 2021 01:10:47 +0100 Subject: LibJS: Implement Temporal.PlainMonthDay.prototype.valueOf() --- .../LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp | 11 +++++++++++ .../Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js | 11 +++++++++++ 3 files changed, 23 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 73587cdff4..8df4dd72f0 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -30,6 +30,9 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable); + + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.valueOf, value_of, 0, attr); } static PlainMonthDay* typed_this(GlobalObject& global_object) @@ -90,4 +93,12 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter) return Value(calendar_day(global_object, calendar, *month_day)); } +// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of) +{ + // 1. Throw a TypeError exception. + vm.throw_exception(global_object, ErrorType::Convert, "Temporal.PlainMonthDay", "a primitive value"); + return {}; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 037d1c63be..1a3d09f13e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -22,6 +22,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); JS_DECLARE_NATIVE_FUNCTION(day_getter); + JS_DECLARE_NATIVE_FUNCTION(value_of); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js new file mode 100644 index 0000000000..f8f48e07fc --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js @@ -0,0 +1,11 @@ +describe("errors", () => { + test("throws TypeError", () => { + const plainMonthDay = new Temporal.PlainMonthDay(7, 6); + expect(() => { + plainMonthDay.valueOf(); + }).toThrowWithMessage( + TypeError, + "Cannot convert Temporal.PlainMonthDay to a primitive value" + ); + }); +}); -- cgit v1.2.3