Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ impl BuiltinIntrinsicConstructor for TemporalPlainTimeConstructor {
}

impl TemporalPlainTimeConstructor {
/// ### [4.1.1 Temporal.PlainTime](https://tc39.es/proposal-temporal/#sec-temporal.plaintime)
/// ### [4.1.1 Temporal.PlainTime ( [ hour [ , minute [ , second [ , millisecond [ , microsecond [ , nanosecond ] ] ] ] ] ] )](https://tc39.es/proposal-temporal/#sec-temporal.plaintime)
fn constructor<'gc>(
agent: &mut Agent,
_: Value,
args: ArgumentsList,
new_target: Option<Object>,
mut gc: GcScope<'gc, '_>,
) -> JsResult<'gc, Value<'gc>> {
let hours = args.get(1).scope(agent, gc.nogc());
let minutes = args.get(2).scope(agent, gc.nogc());
let seconds = args.get(3).scope(agent, gc.nogc());
let milliseconds = args.get(4).scope(agent, gc.nogc());
let microseconds = args.get(5).scope(agent, gc.nogc());
let nanoseconds = args.get(6).scope(agent, gc.nogc());
let hour = args.get(0).scope(agent, gc.nogc());
let minute = args.get(1).scope(agent, gc.nogc());
let second = args.get(2).scope(agent, gc.nogc());
let millisecond = args.get(3).scope(agent, gc.nogc());
let microsecond = args.get(4).scope(agent, gc.nogc());
let nanosecond = args.get(5).scope(agent, gc.nogc());

let new_target = new_target.bind(gc.nogc());

Expand All @@ -58,64 +58,63 @@ impl TemporalPlainTimeConstructor {
let new_target = new_target.scope(agent, gc.nogc());

// 2. If hour is undefined, set hour to 0; else set hour to ? ToIntegerWithTruncation(hour).
let hour = if hours.get(agent).is_undefined() {
let hour = if hour.get(agent).is_undefined() {
0
} else {
u8::try_from(
to_integer_with_truncation(agent, hours.get(agent), gc.reborrow()).unbind()?,
to_integer_with_truncation(agent, hour.get(agent), gc.reborrow()).unbind()?,
)
.unwrap_or(u8::MAX)
};

// 3. If minute is undefined, set minute to 0; else set minute to ? ToIntegerWithTruncation(minute).
let minute = if minutes.get(agent).is_undefined() {
let minute = if minute.get(agent).is_undefined() {
0
} else {
u8::try_from(
to_integer_with_truncation(agent, minutes.get(agent), gc.reborrow()).unbind()?,
to_integer_with_truncation(agent, minute.get(agent), gc.reborrow()).unbind()?,
)
.unwrap_or(u8::MAX)
};

// 4. If second is undefined, set second to 0; else set second to ? ToIntegerWithTruncation(second).
let second = if seconds.get(agent).is_undefined() {
let second = if second.get(agent).is_undefined() {
0
} else {
u8::try_from(
to_integer_with_truncation(agent, seconds.get(agent), gc.reborrow()).unbind()?,
to_integer_with_truncation(agent, second.get(agent), gc.reborrow()).unbind()?,
)
.unwrap_or(u8::MAX)
};

// 5. If millisecond is undefined, set millisecond to 0; else set millisecond to ? ToIntegerWithTruncation(millisecond).
let millisecond = if milliseconds.get(agent).is_undefined() {
let millisecond = if millisecond.get(agent).is_undefined() {
0
} else {
u16::try_from(
to_integer_with_truncation(agent, milliseconds.get(agent), gc.reborrow())
to_integer_with_truncation(agent, millisecond.get(agent), gc.reborrow())
.unbind()?,
)
.unwrap_or(u16::MAX)
};

// 6. If microsecond is undefined, set microsecond to 0; else set microsecond to ? ToIntegerWithTruncation(microsecond).
let microsecond = if microseconds.get(agent).is_undefined() {
let microsecond = if microsecond.get(agent).is_undefined() {
0
} else {
u16::try_from(
to_integer_with_truncation(agent, microseconds.get(agent), gc.reborrow())
to_integer_with_truncation(agent, microsecond.get(agent), gc.reborrow())
.unbind()?,
)
.unwrap_or(u16::MAX)
};

// 7. If nanosecond is undefined, set nanosecond to 0; else set nanosecond to ? ToIntegerWithTruncation(nanosecond).
let nanosecond = if nanoseconds.get(agent).is_undefined() {
let nanosecond = if nanosecond.get(agent).is_undefined() {
0
} else {
u16::try_from(
to_integer_with_truncation(agent, nanoseconds.get(agent), gc.reborrow())
.unbind()?,
to_integer_with_truncation(agent, nanosecond.get(agent), gc.reborrow()).unbind()?,
)
.unwrap_or(u16::MAX)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ impl Builtin for TemporalPlainTimePrototypeToJSON {
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalPlainTimePrototype::to_json);
}

struct TemporalPlainTimePrototypeToLocaleString;
impl Builtin for TemporalPlainTimePrototypeToLocaleString {
const NAME: String<'static> = BUILTIN_STRING_MEMORY.toLocaleString;
const LENGTH: u8 = 0;
const BEHAVIOUR: Behaviour = Behaviour::Regular(TemporalPlainTimePrototype::to_locale_string);
}

struct TemporalPlainTimePrototypeToString;
impl Builtin for TemporalPlainTimePrototypeToString {
const NAME: String<'static> = BUILTIN_STRING_MEMORY.toString;
Expand Down Expand Up @@ -273,6 +280,29 @@ impl TemporalPlainTimePrototype {
}
}

/// ### [4.3.17 Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )](https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tolocalestring)
/// An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript
/// 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
/// in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
fn to_locale_string<'gc>(
agent: &mut Agent,
this_value: Value,
_args: ArgumentsList,
gc: GcScope<'gc, '_>,
) -> JsResult<'gc, Value<'gc>> {
let gc = gc.into_nogc();
// 1. Let plainTime be the this value.
let value = this_value.bind(gc);
// 2. Perform ? RequireInternalSlot(plainTime, [[InitializedTemporalTime]]).
let plain_time = require_internal_slot_temporal_plain_time(agent, value, gc)?;
// 3. Return TimeRecordToString(plainTime.[[Time]], auto).
let options: ToStringRoundingOptions = ToStringRoundingOptions::default(); // defaults Precision to Auto
match plain_time.inner_plain_time(agent).to_ixdtf_string(options) {
Ok(string) => Ok(Value::from_string(agent, string, gc)),
Err(err) => Err(temporal_err_to_js_err(agent, err, gc)),
}
}

/// ### [4.3.16 Temporal.PlainTime.prototype.toString ( [ options ] )](https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.tostring)
fn to_string<'gc>(
agent: &mut Agent,
Expand Down Expand Up @@ -365,7 +395,7 @@ impl TemporalPlainTimePrototype {
let plain_time_constructor = intrinsics.temporal_plain_time();

OrdinaryObjectBuilder::new_intrinsic_object(agent, realm, this)
.with_property_capacity(13)
.with_property_capacity(14)
.with_prototype(object_prototype)
.with_constructor_property(plain_time_constructor)
.with_builtin_function_getter_property::<TemporalPlainTimePrototypeGetHour>()
Expand All @@ -377,6 +407,7 @@ impl TemporalPlainTimePrototype {
.with_builtin_function_property::<TemporalPlainTimePrototypeAdd>()
.with_builtin_function_property::<TemporalPlainTimePrototypeSubtract>()
.with_builtin_function_property::<TemporalPlainTimePrototypeToJSON>()
.with_builtin_function_property::<TemporalPlainTimePrototypeToLocaleString>()
.with_builtin_function_property::<TemporalPlainTimePrototypeToString>()
.with_builtin_function_property::<TemporalPlainTimePrototypeValueOf>()
.with_property(|builder| {
Expand Down
Loading
Loading