Skip to content

Commit 962df72

Browse files
authored
[Beam] Format source files and suppress build warnings (#4356)
1 parent 959d760 commit 962df72

33 files changed

+2099
-1037
lines changed

src/Fable.Build/Test/Beam.fs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ let handle (args: string list) =
7575
let fileName = Path.GetFileName(erlFile)
7676

7777
try
78-
Command.Run("erlc", fileName, workingDirectory = fableModulesLibDir)
78+
Command.Run(
79+
"erlc",
80+
$"+nowarn_ignored +nowarn_failed +nowarn_shadow_vars {fileName}",
81+
workingDirectory = fableModulesLibDir
82+
)
7983
with _ ->
8084
printfn $"WARNING: erlc failed for {fileName} (library), skipping"
8185
compileErrors <- compileErrors + 1
@@ -87,7 +91,11 @@ let handle (args: string list) =
8791
let fileName = Path.GetFileName(erlFile)
8892

8993
try
90-
Command.Run("erlc", $"-pa fable_modules/fable-library-beam {fileName}", workingDirectory = buildDir)
94+
Command.Run(
95+
"erlc",
96+
$"+nowarn_ignored +nowarn_failed +nowarn_shadow_vars -pa fable_modules/fable-library-beam {fileName}",
97+
workingDirectory = buildDir
98+
)
9199
with _ ->
92100
printfn $"WARNING: erlc failed for {fileName}, skipping"
93101
compileErrors <- compileErrors + 1

src/fable-library-beam/fable_async.erl

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
-module(fable_async).
2-
-export([start_immediate/1, start_immediate/2,
3-
run_synchronously/1, run_synchronously/2,
4-
start_with_continuations/4, start_with_continuations/5,
5-
sleep/1, parallel/1, sequential/1,
6-
catch_async/1, ignore/1, from_continuations/1,
7-
start_as_task/1,
8-
cancellation_token/0, create_cancellation_token/0, create_cancellation_token/1,
9-
wrap_error/1]).
10-
11-
-type async_ctx() :: #{on_success := fun(), on_error := fun(), on_cancel := fun(), cancel_token := reference() | undefined}.
2+
-export([
3+
start_immediate/1, start_immediate/2,
4+
run_synchronously/1, run_synchronously/2,
5+
start_with_continuations/4, start_with_continuations/5,
6+
sleep/1,
7+
parallel/1,
8+
sequential/1,
9+
catch_async/1,
10+
ignore/1,
11+
from_continuations/1,
12+
start_as_task/1,
13+
cancellation_token/0,
14+
create_cancellation_token/0, create_cancellation_token/1,
15+
wrap_error/1
16+
]).
17+
18+
-type async_ctx() :: #{
19+
on_success := fun(),
20+
on_error := fun(),
21+
on_cancel := fun(),
22+
cancel_token := reference() | undefined
23+
}.
1224
-type async(T) :: fun((async_ctx()) -> T).
1325

1426
-spec start_immediate(async(term())) -> term().
1527
-spec start_immediate(async(term()), reference() | undefined) -> term().
1628
-spec run_synchronously(async(term())) -> term().
1729
-spec run_synchronously(async(term()), reference() | undefined) -> term().
1830
-spec start_with_continuations(async(term()), fun(), fun(), fun()) -> term().
19-
-spec start_with_continuations(async(term()), fun(), fun(), fun(), reference() | undefined) -> term().
31+
-spec start_with_continuations(async(term()), fun(), fun(), fun(), reference() | undefined) ->
32+
term().
2033
-spec sleep(non_neg_integer()) -> async(ok).
2134
-spec parallel(list() | reference()) -> async(reference()).
2235
-spec sequential(list()) -> async(list()).
@@ -30,10 +43,12 @@
3043

3144
%% Default context: run inline in current process
3245
default_ctx(CancelToken) ->
33-
#{on_success => fun(_) -> ok end,
34-
on_error => fun(E) -> erlang:error(E) end,
35-
on_cancel => fun(_) -> ok end,
36-
cancel_token => CancelToken}.
46+
#{
47+
on_success => fun(_) -> ok end,
48+
on_error => fun(E) -> erlang:error(E) end,
49+
on_cancel => fun(_) -> ok end,
50+
cancel_token => CancelToken
51+
}.
3752

3853
%% StartImmediate: run with default context (fire-and-forget)
3954
start_immediate(Computation) -> start_immediate(Computation, undefined).
@@ -45,12 +60,16 @@ run_synchronously(Computation) -> run_synchronously(Computation, undefined).
4560
run_synchronously(Computation, CancelToken) ->
4661
%% Use a unique ref as key to store result in process dict
4762
Ref = make_ref(),
48-
Ctx = #{on_success => fun(V) -> put(Ref, {ok, V}) end,
49-
on_error => fun(E) -> put(Ref, {error, E}) end,
50-
on_cancel => fun(_) -> put(Ref, {cancelled}) end,
51-
cancel_token => CancelToken},
52-
try Computation(Ctx)
53-
catch _:Err -> put(Ref, {error, Err})
63+
Ctx = #{
64+
on_success => fun(V) -> put(Ref, {ok, V}) end,
65+
on_error => fun(E) -> put(Ref, {error, E}) end,
66+
on_cancel => fun(_) -> put(Ref, {cancelled}) end,
67+
cancel_token => CancelToken
68+
},
69+
try
70+
Computation(Ctx)
71+
catch
72+
_:Err -> put(Ref, {error, Err})
5473
end,
5574
Result = erase(Ref),
5675
case Result of
@@ -64,10 +83,16 @@ run_synchronously(Computation, CancelToken) ->
6483
start_with_continuations(Comp, OnSuccess, OnError, OnCancel) ->
6584
start_with_continuations(Comp, OnSuccess, OnError, OnCancel, undefined).
6685
start_with_continuations(Comp, OnSuccess, OnError, OnCancel, Token) ->
67-
Ctx = #{on_success => OnSuccess, on_error => OnError,
68-
on_cancel => OnCancel, cancel_token => Token},
69-
try Comp(Ctx)
70-
catch _:Err -> OnError(Err)
86+
Ctx = #{
87+
on_success => OnSuccess,
88+
on_error => OnError,
89+
on_cancel => OnCancel,
90+
cancel_token => Token
91+
},
92+
try
93+
Comp(Ctx)
94+
catch
95+
_:Err -> OnError(Err)
7196
end.
7297

7398
%% Sleep: pause current process, with cancellation support
@@ -119,16 +144,20 @@ parallel(Computations) ->
119144
fun(Ctx) ->
120145
Self = self(),
121146
Indexed = lists:zip(lists:seq(1, length(Computations)), Computations),
122-
lists:foreach(fun({Idx, Comp}) ->
123-
spawn(fun() ->
124-
try
125-
Result = fable_async:run_synchronously(Comp),
126-
Self ! {async_parallel, Idx, {ok, Result}}
127-
catch _:Err ->
128-
Self ! {async_parallel, Idx, {error, Err}}
129-
end
130-
end)
131-
end, Indexed),
147+
lists:foreach(
148+
fun({Idx, Comp}) ->
149+
spawn(fun() ->
150+
try
151+
Result = fable_async:run_synchronously(Comp),
152+
Self ! {async_parallel, Idx, {ok, Result}}
153+
catch
154+
_:Err ->
155+
Self ! {async_parallel, Idx, {error, Err}}
156+
end
157+
end)
158+
end,
159+
Indexed
160+
),
132161
Results = collect_parallel(length(Computations), #{}),
133162
case Results of
134163
{ok, Map} ->
@@ -139,7 +168,8 @@ parallel(Computations) ->
139168
end
140169
end.
141170

142-
collect_parallel(0, Acc) -> {ok, Acc};
171+
collect_parallel(0, Acc) ->
172+
{ok, Acc};
143173
collect_parallel(N, Acc) ->
144174
receive
145175
{async_parallel, Idx, {ok, Val}} ->
@@ -165,8 +195,10 @@ catch_async(Computation) ->
165195
on_cancel => maps:get(on_cancel, Ctx),
166196
cancel_token => maps:get(cancel_token, Ctx)
167197
},
168-
try Computation(Ctx2)
169-
catch _:Err -> (maps:get(on_success, Ctx))({choice2_of2, wrap_error(Err)})
198+
try
199+
Computation(Ctx2)
200+
catch
201+
_:Err -> (maps:get(on_success, Ctx))({choice2_of2, wrap_error(Err)})
170202
end
171203
end.
172204

src/fable-library-beam/fable_async_builder.erl

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
-module(fable_async_builder).
2-
-export([singleton/0, singleton/1, task/0, task/1, run/1,
3-
delay/1, return/1, return_from/1,
4-
bind/2, combine/2, zero/0, zero/1,
5-
try_with/2, try_finally/2, using/2,
6-
while/2, for/2]).
2+
-export([
3+
singleton/0, singleton/1,
4+
task/0, task/1,
5+
run/1,
6+
delay/1,
7+
return/1,
8+
return_from/1,
9+
bind/2,
10+
combine/2,
11+
zero/0, zero/1,
12+
try_with/2,
13+
try_finally/2,
14+
using/2,
15+
while/2,
16+
for/2
17+
]).
718

819
-spec singleton() -> async_builder.
920
-spec singleton(term()) -> async_builder.
@@ -52,11 +63,15 @@ delay(Generator) ->
5263
%% Bind: monadic bind — run computation, pass result to binder
5364
bind(Computation, Binder) ->
5465
fun(Ctx) ->
55-
Ctx2 = Ctx#{on_success => fun(X) ->
56-
try (Binder(X))(Ctx)
57-
catch _:Err -> (maps:get(on_error, Ctx))(Err)
66+
Ctx2 = Ctx#{
67+
on_success => fun(X) ->
68+
try
69+
(Binder(X))(Ctx)
70+
catch
71+
_:Err -> (maps:get(on_error, Ctx))(Err)
72+
end
5873
end
59-
end},
74+
},
6075
Computation(Ctx2)
6176
end.
6277

@@ -68,18 +83,26 @@ combine(Comp1, Comp2) ->
6883
%% Wrap raw errors (binaries from failwith) as exception maps so .Message works.
6984
try_with(Computation, Handler) ->
7085
fun(Ctx) ->
71-
Ctx2 = Ctx#{on_error => fun(Err) ->
72-
Err2 = fable_async:wrap_error(Err),
73-
try (Handler(Err2))(Ctx)
74-
catch _:Err3 -> (maps:get(on_error, Ctx))(Err3)
75-
end
76-
end},
77-
try Computation(Ctx2)
78-
catch _:Err ->
79-
Err2 = fable_async:wrap_error(Err),
80-
try (Handler(Err2))(Ctx)
81-
catch _:Err3 -> (maps:get(on_error, Ctx))(Err3)
86+
Ctx2 = Ctx#{
87+
on_error => fun(Err) ->
88+
Err2 = fable_async:wrap_error(Err),
89+
try
90+
(Handler(Err2))(Ctx)
91+
catch
92+
_:Err3 -> (maps:get(on_error, Ctx))(Err3)
93+
end
8294
end
95+
},
96+
try
97+
Computation(Ctx2)
98+
catch
99+
_:Err ->
100+
Err2 = fable_async:wrap_error(Err),
101+
try
102+
(Handler(Err2))(Ctx)
103+
catch
104+
_:Err3 -> (maps:get(on_error, Ctx))(Err3)
105+
end
83106
end
84107
end.
85108

@@ -88,12 +111,25 @@ try_with(Computation, Handler) ->
88111
try_finally(Computation, Compensation) ->
89112
fun(Ctx) ->
90113
Ctx2 = Ctx#{
91-
on_success => fun(X) -> Compensation(ok), (maps:get(on_success, Ctx))(X) end,
92-
on_error => fun(E) -> Compensation(ok), (maps:get(on_error, Ctx))(E) end,
93-
on_cancel => fun(E) -> Compensation(ok), (maps:get(on_cancel, Ctx))(E) end
114+
on_success => fun(X) ->
115+
Compensation(ok),
116+
(maps:get(on_success, Ctx))(X)
117+
end,
118+
on_error => fun(E) ->
119+
Compensation(ok),
120+
(maps:get(on_error, Ctx))(E)
121+
end,
122+
on_cancel => fun(E) ->
123+
Compensation(ok),
124+
(maps:get(on_cancel, Ctx))(E)
125+
end
94126
},
95-
try Computation(Ctx2)
96-
catch _:Err -> Compensation(ok), (maps:get(on_error, Ctx))(Err)
127+
try
128+
Computation(Ctx2)
129+
catch
130+
_:Err ->
131+
Compensation(ok),
132+
(maps:get(on_error, Ctx))(Err)
97133
end
98134
end.
99135

@@ -114,5 +150,5 @@ for(List, Body) when is_reference(List) ->
114150
for(List, Body) ->
115151
case List of
116152
[] -> zero();
117-
[H|T] -> bind(Body(H), fun(_) -> for(T, Body) end)
153+
[H | T] -> bind(Body(H), fun(_) -> for(T, Body) end)
118154
end.

src/fable-library-beam/fable_bit_converter.erl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
-module(fable_bit_converter).
2-
-export([get_bytes/2, get_bytes_bool/1, to_int/3, to_uint/3, to_float/3,
3-
to_boolean/2, to_string/1, to_string/2, to_string/3,
4-
int64_bits_to_double/1, double_to_int64_bits/1]).
2+
-export([
3+
get_bytes/2,
4+
get_bytes_bool/1,
5+
to_int/3,
6+
to_uint/3,
7+
to_float/3,
8+
to_boolean/2,
9+
to_string/1, to_string/2, to_string/3,
10+
int64_bits_to_double/1,
11+
double_to_int64_bits/1
12+
]).
513

614
-spec get_bytes(integer() | float(), integer()) -> tuple().
715
-spec get_bytes_bool(boolean()) -> tuple().

0 commit comments

Comments
 (0)