|
| 1 | +%% EXPECT: bif_bitstring_dynamic_ok |
| 2 | +%% Regression: dynamic bit_size/1 and is_bitstring/1 calls must |
| 3 | +%% agree with direct calls, not raise undef. The runtime fallback table |
| 4 | +%% used to omit both BIFs. Run this same fixture on native Nera and BEAM. |
| 5 | +-module(bif_bitstring_dynamic). |
| 6 | +-export([test/0]). |
| 7 | + |
| 8 | +test() -> |
| 9 | + SizeCalls = calls(bit_size), |
| 10 | + TypeCalls = calls(is_bitstring), |
| 11 | + <<_, Tail/binary>> = id(<<1, 2, 3, 4>>), |
| 12 | + <<_:3, Bits/bits>> = id(<<1, 2, 3>>), |
| 13 | + Big = id(binary:copy(<<"x">>, 1000)), |
| 14 | + <<_:5/binary, Middle:990/binary, _/binary>> = Big, |
| 15 | + Bitstrings = id([<<>>, <<"a">>, <<"abc">>, <<1:1>>, <<1:7>>, |
| 16 | + <<1, 5:3>>, <<1:9>>, Tail, Bits, Big, Middle, |
| 17 | + <<Big/binary, 1:1>>]), |
| 18 | + lists:foreach(fun(B) -> |
| 19 | + %% Direct size semantics have their own bitstring_size_badarg fixture; |
| 20 | + %% this regression owns parity of the dynamic entry points. |
| 21 | + check_calls(bit_size, SizeCalls, B, {ok, bit_size(B)}), |
| 22 | + check_calls(is_bitstring, TypeCalls, B, {ok, true}) |
| 23 | + end, Bitstrings), |
| 24 | + |
| 25 | + NonBitstrings = id([abc, true, [], [1, 2], "abcd", [<<"ab">>], [a | b], |
| 26 | + 0, 42, -1, 1 bsl 70, 1.0, {}, {<<"a">>}, #{}, |
| 27 | + #{a => <<"a">>}, self(), make_ref(), |
| 28 | + fun() -> ok end, fun id/1]), |
| 29 | + lists:foreach(fun(X) -> |
| 30 | + check_calls(bit_size, SizeCalls, X, {error, badarg}), |
| 31 | + check_calls(is_bitstring, TypeCalls, X, {ok, false}) |
| 32 | + end, NonBitstrings), |
| 33 | + |
| 34 | + %% Real higher-order consumers of auto-imported and remote fun references. |
| 35 | + [0, 24, 9, 1] = lists:map(fun bit_size/1, id([<<>>, <<"abc">>, <<1:9>>, <<1:1>>])), |
| 36 | + [true, false, true] = lists:map(fun erlang:is_bitstring/1, |
| 37 | + id([<<1, 5:3>>, [1, 2], <<>>])), |
| 38 | + %% A caught badarg does not prevent a later dynamic call from succeeding. |
| 39 | + Remote = fun erlang:bit_size/1, |
| 40 | + {error, badarg} = outcome(Remote, id(not_bits)), |
| 41 | + 11 = apply(erlang, bit_size, [id(<<1, 5:3>>)]), |
| 42 | + io:format("bif_bitstring_dynamic_ok~n"), |
| 43 | + ok. |
| 44 | + |
| 45 | +calls(bit_size) -> |
| 46 | + Mod = id(erlang), |
| 47 | + [{local_fun, fun bit_size/1}, |
| 48 | + {remote_fun, fun erlang:bit_size/1}, |
| 49 | + {variable_module, fun(B) -> Mod:bit_size(B) end} |
| 50 | + | dynamic_calls(bit_size)]; |
| 51 | +calls(is_bitstring) -> |
| 52 | + Mod = id(erlang), |
| 53 | + [{local_fun, fun is_bitstring/1}, |
| 54 | + {remote_fun, fun erlang:is_bitstring/1}, |
| 55 | + {variable_module, fun(B) -> Mod:is_bitstring(B) end} |
| 56 | + | dynamic_calls(is_bitstring)]. |
| 57 | + |
| 58 | +dynamic_calls(Name) -> |
| 59 | + Mod = id(erlang), |
| 60 | + Function = id(Name), |
| 61 | + Dynamic = fun Mod:Function/1, |
| 62 | + [{apply, fun(B) -> apply(erlang, Name, [B]) end}, |
| 63 | + {qualified_apply, fun(B) -> erlang:apply(erlang, Name, [B]) end}, |
| 64 | + {variable_apply, fun(B) -> apply(Mod, Function, [B]) end}, |
| 65 | + {variable_function, fun(B) -> erlang:Function(B) end}, |
| 66 | + {variable_module_function, fun(B) -> Mod:Function(B) end}, |
| 67 | + {dynamic_fun, Dynamic}, |
| 68 | + {apply_fun, fun(B) -> apply(Dynamic, [B]) end}, |
| 69 | + {apply_local_fun, fun(B) -> apply(local_fun(Name), [B]) end}, |
| 70 | + {apply_remote_fun, fun(B) -> apply(remote_fun(Name), [B]) end}]. |
| 71 | + |
| 72 | +local_fun(bit_size) -> fun bit_size/1; |
| 73 | +local_fun(is_bitstring) -> fun is_bitstring/1. |
| 74 | +remote_fun(bit_size) -> fun erlang:bit_size/1; |
| 75 | +remote_fun(is_bitstring) -> fun erlang:is_bitstring/1. |
| 76 | + |
| 77 | +check_calls(Name, Calls, X, Expected) -> |
| 78 | + Results = [{Form, outcome(F, X)} || {Form, F} <- Calls], |
| 79 | + ExpectedResults = [{Form, Expected} || {Form, _} <- Calls], |
| 80 | + {Name, X, ExpectedResults} = {Name, X, Results}, |
| 81 | + ok. |
| 82 | + |
| 83 | +outcome(F, X) -> try F(X) of V -> {ok, V} catch error:R -> {error, R} end. |
| 84 | +id(X) -> X. |
0 commit comments