Skip to content

Commit 25bbb39

Browse files
committed
feat(ecmascript): PlainTime.prototype.toLocaleString
1 parent 285a37e commit 25bbb39

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

nova_vm/src/ecmascript/builtins/temporal/plain_time/plain_time_prototype.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
use temporal_rs::{options::ToStringRoundingOptions};
6+
57
use crate::{
68
ecmascript::{
79
Agent, ArgumentsList, BUILTIN_STRING_MEMORY, Behaviour, Builtin, BuiltinGetter, JsResult,
810
PropertyKey, Realm, String, Value,
911
builders::OrdinaryObjectBuilder,
1012
builtins::temporal::plain_time::{
1113
add_duration_to_time, require_internal_slot_temporal_plain_time,
12-
},
14+
}, temporal_err_to_js_err,
1315
},
1416
engine::{Bindable, GcScope, NoGcScope},
1517
heap::WellKnownSymbols,
@@ -90,6 +92,13 @@ impl Builtin for TemporalPlainTimePrototypeSubtract {
9092
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalPlainTimePrototype::subtract);
9193
}
9294

95+
struct TemporalPlainTimePrototypeToLocaleString;
96+
impl Builtin for TemporalPlainTimePrototypeToLocaleString {
97+
const NAME: String<'static> = BUILTIN_STRING_MEMORY.toLocaleString;
98+
const LENGTH: u8 = 0;
99+
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalPlainTimePrototype::to_locale_string);
100+
}
101+
93102
impl TemporalPlainTimePrototype {
94103
/// ### [4.3.4 get Temporal.PlainTime.prototype.minute](https://tc39.es/proposal-temporal/#sec-get-temporal.plaintime.prototype.minute)
95104
pub(crate) fn get_minute<'gc>(
@@ -228,14 +237,44 @@ impl TemporalPlainTimePrototype {
228237
.map(Value::from)
229238
}
230239

240+
/// ### [4.3.17 Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )](https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tolocalestring)
241+
/// An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript
242+
/// implementation does not include the ECMA-402 API the following specification of this method is used. The meanings of the optional parameters to this method are defined
243+
/// in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
244+
fn to_locale_string<'gc>(
245+
agent: &mut Agent,
246+
this_value: Value,
247+
_args: ArgumentsList,
248+
gc: GcScope<'gc, '_>,
249+
) -> JsResult<'gc, Value<'gc>> {
250+
// 1. Let plainTime be the this value.
251+
let value = this_value.bind(gc.nogc());
252+
// 2. Perform ? RequireInternalSlot(plainTime, [[InitializedTemporalTime]]).
253+
let plain_time = require_internal_slot_temporal_plain_time(agent, value, gc.nogc())
254+
.unbind()?
255+
.bind(gc.nogc());
256+
// 3. Return TimeRecordToString(plainTime.[[Time]], auto).
257+
let options: ToStringRoundingOptions = ToStringRoundingOptions::default(); // defaults Precision to Auto
258+
match plain_time
259+
.inner_plain_time(agent)
260+
.to_ixdtf_string(options)
261+
{
262+
Ok(string) => Ok(Value::from_string(agent, string, gc.into_nogc())),
263+
Err(err) => Err(temporal_err_to_js_err(agent, err, gc.into_nogc())),
264+
}
265+
}
266+
267+
268+
269+
231270
pub(crate) fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>, _: NoGcScope) {
232271
let intrinsics = agent.get_realm_record_by_id(realm).intrinsics();
233272
let this = intrinsics.temporal_plain_time_prototype();
234273
let object_prototype = intrinsics.object_prototype();
235274
let plain_time_constructor = intrinsics.temporal_plain_time();
236275

237276
OrdinaryObjectBuilder::new_intrinsic_object(agent, realm, this)
238-
.with_property_capacity(10)
277+
.with_property_capacity(11)
239278
.with_prototype(object_prototype)
240279
.with_constructor_property(plain_time_constructor)
241280
.with_builtin_function_getter_property::<TemporalPlainTimePrototypeGetHour>()
@@ -246,6 +285,7 @@ impl TemporalPlainTimePrototype {
246285
.with_builtin_function_getter_property::<TemporalPlainTimePrototypeGetMillisecond>()
247286
.with_builtin_function_property::<TemporalPlainTimePrototypeAdd>()
248287
.with_builtin_function_property::<TemporalPlainTimePrototypeSubtract>()
288+
.with_builtin_function_property::<TemporalPlainTimePrototypeToLocaleString>()
249289
.with_property(|builder| {
250290
builder
251291
.with_key(WellKnownSymbols::ToStringTag.into())

0 commit comments

Comments
 (0)