Skip to content

Commit

Permalink
LibJS: Implement Temporal.Duration.prototype.years
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Jul 16, 2021
1 parent 23766f2 commit 300a22f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ namespace JS {
P(valueOf) \
P(values) \
P(warn) \
P(writable)
P(writable) \
P(years)

struct CommonPropertyNames {
PropertyName and_ { "and", PropertyName::StringMayBeNumber::No };
Expand Down
29 changes: 29 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/DurationPrototype.h>

namespace JS::Temporal {
Expand All @@ -23,6 +24,34 @@ void DurationPrototype::initialize(GlobalObject& global_object)

// 7.3.2 Temporal.Duration.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Duration"), Attribute::Configurable);

define_native_accessor(vm.names.years, years_getter, {}, Attribute::Configurable);
}

static Duration* typed_this(GlobalObject& global_object)
{
auto& vm = global_object.vm();
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
if (!is<Duration>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.Duration");
return {};
}
return static_cast<Duration*>(this_object);
}

// 7.3.3 get Temporal.Duration.prototype.years, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.years
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::years_getter)
{
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
auto* duration = typed_this(global_object);
if (vm.exception())
return {};

// 3. Return duration.[[Years]].
return Value(duration->years());
}

}
3 changes: 3 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class DurationPrototype final : public Object {
explicit DurationPrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~DurationPrototype() override = default;

private:
JS_DECLARE_NATIVE_FUNCTION(years_getter);
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const duration = new Temporal.Duration(123);
expect(duration.years).toBe(123);
});
});

test("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.Duration.prototype, "years", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
});
});

0 comments on commit 300a22f

Please sign in to comment.