Skip to content

Commit 7db4972

Browse files
committed
Fix observable behavior of Intl.DateTimeFormat constructor
1 parent c77daad commit 7db4972

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

core/engine/src/builtins/intl/date_time_format/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ impl BuiltInConstructor for DateTimeFormat {
177177
let options = args.get_or_undefined(1);
178178

179179
// 2. Let dateTimeFormat be ? CreateDateTimeFormat(newTarget, locales, options, any, date).
180+
let prototype = get_prototype_from_constructor(
181+
new_target_inner,
182+
StandardConstructors::date_time_format,
183+
context,
184+
)?;
180185
let dtf = create_date_time_format(
181186
locales,
182187
options,
183188
FormatType::Any,
184189
FormatDefaults::Date,
185190
context,
186191
)?;
187-
let prototype = get_prototype_from_constructor(
188-
new_target_inner,
189-
StandardConstructors::date_time_format,
190-
context,
191-
)?;
192192
let date_time_format = JsObject::from_proto_and_data(prototype, dtf);
193193

194194
// 3. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
@@ -534,6 +534,11 @@ pub(crate) fn create_date_time_format(
534534
defaults: FormatDefaults,
535535
context: &mut Context,
536536
) -> JsResult<DateTimeFormat> {
537+
// NOTE: The below step's code was moved out into constructor to prevent unnecessary JsObject allocation when we create dtf internally
538+
// (e.g. toLocaleString methods of Date and Temporal objects)
539+
// 1. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.DateTimeFormat.prototype%",
540+
// « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]],
541+
// [[HourCycle]], [[DateStyle]], [[TimeStyle]], [[DateTimeFormat]], [[BoundFormat]] »).
537542
// 2. Let hour12 be undefined. <- TODO
538543
// 3. Let modifyResolutionOptions be a new Abstract Closure with parameters (options) that captures hour12 and performs the following steps when called:
539544
// a. Set hour12 to options.[[hour12]].

core/engine/src/builtins/intl/date_time_format/tests.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,31 @@ fn date_to_locale_string() {
8888
TestAction::assert_eq("result === '6:07\u{202f}AM'", true),
8989
]);
9090
}
91+
92+
#[cfg(feature = "intl_bundled")]
93+
#[test]
94+
fn dtf_ctor_observable_behavior() {
95+
run_test_actions([
96+
TestAction::run(indoc! {"
97+
const expected = [];
98+
99+
const proxyConstructor = new Proxy(Intl.DateTimeFormat, {
100+
get(target, prop) {
101+
if (prop === 'prototype') {
102+
expected.push('prototype-access');
103+
}
104+
return target[prop];
105+
}
106+
});
107+
108+
try {
109+
new proxyConstructor('en', { timeZone: 'Invalid/Zone' });
110+
} catch (e) {
111+
expected.push('error-thrown');
112+
}
113+
"}),
114+
TestAction::assert_eq("expected.length === 2", true),
115+
TestAction::assert_eq("expected[0] === 'prototype-access'", true),
116+
TestAction::assert_eq("expected[1] === 'error-thrown'", true),
117+
]);
118+
}

0 commit comments

Comments
 (0)