Skip to content

Commit 81e737b

Browse files
committed
Guard function_exported? probes with Code.ensure_loaded? so cold modules take the right branch
1 parent 1ca9f08 commit 81e737b

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

lib/localize/calendar.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,8 @@ defmodule Localize.Calendar do
10491049
defp resolve_locale_id(locale), do: Localize.Locale.cldr_locale_id_from(locale)
10501050

10511051
defp calendar_type_from(%{calendar: calendar}) do
1052+
Code.ensure_loaded?(calendar)
1053+
10521054
if function_exported?(calendar, :cldr_calendar_type, 0) do
10531055
calendar.cldr_calendar_type()
10541056
else
@@ -1065,6 +1067,8 @@ defmodule Localize.Calendar do
10651067
# year>0 → era 1 fallback is only safe for Gregorian.
10661068
defp day_of_era(%{year: year, month: month, day: day, calendar: calendar})
10671069
when is_atom(calendar) and calendar != Calendar.ISO do
1070+
Code.ensure_loaded?(calendar)
1071+
10681072
if function_exported?(calendar, :year_of_era, 3) do
10691073
case calendar.year_of_era(year, month, day) do
10701074
{era_year, era} when is_integer(era) -> {era_year, era}
@@ -1097,6 +1101,8 @@ defmodule Localize.Calendar do
10971101

10981102
defp iso_day_of_week(%{year: year, month: month, day: day, calendar: calendar})
10991103
when is_integer(year) and is_integer(month) and is_integer(day) do
1104+
Code.ensure_loaded?(calendar)
1105+
11001106
if function_exported?(calendar, :day_of_week, 4) do
11011107
calendar.day_of_week(year, month, day, :monday) |> elem(0)
11021108
else

lib/localize/datetime/formatter.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ defmodule Localize.DateTime.Formatter do
343343
# a signed proleptic year like "-1 BC".
344344
defp era_year(%{year: year, month: month, day: day, calendar: calendar})
345345
when is_atom(calendar) do
346+
Code.ensure_loaded?(calendar)
347+
346348
cond do
347349
function_exported?(calendar, :calendar_year, 3) ->
348350
calendar.calendar_year(year, month, day)
@@ -1212,6 +1214,8 @@ defmodule Localize.DateTime.Formatter do
12121214
end
12131215

12141216
defp iso_week_of_year(%{year: year, month: month, day: day, calendar: calendar} = date) do
1217+
Code.ensure_loaded?(calendar)
1218+
12151219
if function_exported?(calendar, :iso_week_of_year, 3) do
12161220
case calendar.iso_week_of_year(year, month, day) do
12171221
{y, w} when is_integer(y) and is_integer(w) -> {y, w}
@@ -1237,6 +1241,8 @@ defmodule Localize.DateTime.Formatter do
12371241

12381242
defp compute_day_of_year(%{year: year, month: month, day: day, calendar: calendar})
12391243
when is_integer(year) and is_integer(month) and is_integer(day) do
1244+
Code.ensure_loaded?(calendar)
1245+
12401246
if function_exported?(calendar, :day_of_year, 3) do
12411247
calendar.day_of_year(year, month, day)
12421248
else

lib/localize/locale/provider.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ defmodule Localize.Locale.Provider do
172172
"""
173173
@spec allow_download?(module()) :: boolean()
174174
def allow_download?(provider_module \\ configured_provider()) do
175+
Code.ensure_loaded?(provider_module)
176+
175177
if function_exported?(provider_module, :allow_download?, 0) do
176178
provider_module.allow_download?()
177179
else

0 commit comments

Comments
 (0)