LibJS: Implement Temporal.Duration.prototype.valueOf

This commit is contained in:
Timothy Flynn 2024-11-18 15:47:11 -05:00 committed by Tim Flynn
parent c715711f88
commit f57ff63432
Notes: github-actions[bot] 2024-11-21 00:05:28 +00:00
3 changed files with 16 additions and 0 deletions

View file

@ -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.toString, to_string, 0, attr);
define_native_function(realm, vm.names.toJSON, to_json, 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.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 // 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 {})); 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<TypeError>(ErrorType::Convert, "Temporal.Duration", "a primitive value");
}
} }

View file

@ -40,6 +40,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json); JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
}; };
} }

View file

@ -0,0 +1,7 @@
describe("errors", () => {
test("throws TypeError", () => {
expect(() => {
new Temporal.Duration().valueOf();
}).toThrowWithMessage(TypeError, "Cannot convert Temporal.Duration to a primitive value");
});
});