Skip to content

Commit 1a47427

Browse files
authored
fix(runtime): dispatch dynamic bitstring BIFs
Apply, variable-module/function and fun-reference calls now use the existing bitstring routines. Dynamic bit_size/1 returns the bit count or raises badarg; is_bitstring/1 returns true or false. One regression runs unchanged natively and on BEAM, with updated feature and test documentation.
1 parent 50d5bde commit 1a47427

5 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Dynamic bitstring BIFs match direct calls
6+
7+
- `bit_size/1` and `is_bitstring/1` now work through `apply/2,3`, variable
8+
module/function names and fun references instead of raising `undef`.
9+
- Dynamic `bit_size/1` returns the bit count and raises `badarg` for
10+
non-bitstrings; dynamic `is_bitstring/1` returns `true` or `false`, as on BEAM.
11+
- `bif_bitstring_dynamic` runs unchanged natively and on BEAM, covering empty,
12+
partial-byte, large and extracted bitstrings, non-bitstrings and higher-order
13+
fun consumers with explicit completion.
14+
515
## Regression coverage for case exports inside maybe bodies
616

717
- A diagnostic regression protects names bound in every `case` branch and read

‎FEATURES.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Maintainers with OTP 29.0.4 installed can verify the catalogue with
167167
| tuple_size/1 | ✅ |
168168
| setelement/3 | ✅ |
169169
| byte_size/1 | ✅ | Rounds partial final bytes up, including deferred pattern segments and guards; non-bitstrings raise badarg and fail guards; direct, apply, variable module/function and fun reference calls |
170-
| bit_size/1 | ✅ | Non-bitstrings raise badarg; guards on a non-bitstring fail |
170+
| bit_size/1 | ✅ | Non-bitstrings raise badarg; guards on a non-bitstring fail; direct, apply, variable module/function and fun reference calls |
171171
| map_get/2 | ✅ |
172172
| is_map_key/2 | ✅ | Auto-imported; guards, negated guards and bodies; non-maps raise badmap |
173173
| map_size/1 | ✅ | Non-maps raise badmap; guards on a non-map fail |
@@ -181,7 +181,7 @@ Maintainers with OTP 29.0.4 installed can verify the catalogue with
181181
| is_binary/1 | ✅ |
182182
| is_map/1 | ✅ |
183183
| is_function/1,2 | ✅ |
184-
| is_bitstring/1 | ✅ |
184+
| is_bitstring/1 | ✅ | Direct, apply, variable module/function and fun reference calls; false for non-bitstrings |
185185
| is_reference/1 | ✅ |
186186
| is_boolean/1 | ✅ |
187187
| atom_to_list/1 | ✅ |

‎crates/nera-runtime/src/ffi/apply.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pub(super) unsafe fn apply_impl(
165165
(Some("erlang"), Some("is_tuple"), 1) => Some(nera_rt_is_tuple(ctx, arg_terms[0])),
166166
(Some("erlang"), Some("is_pid"), 1) => Some(nera_rt_is_pid(ctx, arg_terms[0])),
167167
(Some("erlang"), Some("is_binary"), 1) => Some(nera_rt_is_binary(ctx, arg_terms[0])),
168+
(Some("erlang"), Some("is_bitstring"), 1) => Some(nera_rt_is_bitstring(ctx, arg_terms[0])),
168169
(Some("erlang"), Some("is_map"), 1) => Some(nera_rt_is_map(ctx, arg_terms[0])),
169170
(Some("erlang"), Some("is_function"), 1) => Some(nera_rt_is_function(ctx, arg_terms[0])),
170171
(Some("erlang"), Some("is_function"), 2) => Some(nera_rt_is_function2(ctx, arg_terms[0], arg_terms[1])),
@@ -177,6 +178,7 @@ pub(super) unsafe fn apply_impl(
177178
(Some("erlang"), Some("is_number"), 1) => Some(nera_rt_is_number(ctx, arg_terms[0])),
178179
(Some("erlang"), Some("is_boolean"), 1) => Some(nera_rt_is_boolean(ctx, arg_terms[0])),
179180
(Some("erlang"), Some("byte_size"), 1) => Some(nera_rt_binary_byte_size(ctx, arg_terms[0])),
181+
(Some("erlang"), Some("bit_size"), 1) => Some(nera_rt_binary_size(ctx, arg_terms[0])),
180182
(Some("erlang"), Some("map_size"), 1) => Some(nera_rt_map_size(ctx, arg_terms[0])),
181183
(Some("erlang"), Some("max"), 2) => Some(nera_rt_max(ctx, arg_terms[0], arg_terms[1])),
182184
(Some("erlang"), Some("min"), 2) => Some(nera_rt_min(ctx, arg_terms[0], arg_terms[1])),

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
201201
| `binary_rest_guard_sequence.erl` | Guard sequences `G1; G2` whose first test raises before it reads a deferred binary tail: `if`, `case`, function head and `receive` guards, three-test sequences, the clause body and a read after the expression; also run on BEAM | 15 |
202202
| `bitstring_size_badarg.erl` | byte_size/1, bit_size/1 and their erlang:-qualified forms: sizes of binaries, sub-binaries, bitstrings and a 100-byte binary, `badarg` for 20 terms that are not bitstrings, guards in heads, `case` and `if`, negated, `orelse` and comma guards, guard alternatives, an iolist walk shaped like Cowlib's `cow_iolists:split/2`; also run on BEAM | Table-driven |
203203
| `bif_byte_size_dynamic.erl` | byte_size/1 through apply/2,3, variable module and function names and fun references (auto-imported, qualified, variable), compared with direct calls: binaries, sub-binaries, match tails, partial-byte bitstrings, 1000-byte binaries; `badarg` for 20 terms that are not bitstrings through each form; also run on BEAM | Table-driven |
204+
| `bif_bitstring_dynamic.erl` | bit_size/1 and is_bitstring/1 through apply/2,3, variable module/function names and local/remote/dynamic fun references: parity with direct size calls for binaries, extracted tails, partial-byte and large bitstrings; badarg or false for 20 non-bitstrings; higher-order consumers and caught-error recovery; also run on BEAM | Table-driven |
204205
| `binary_tail_byte_size.erl` | Rounded-up deferred /bits, /bitstring and /binary-unit:N byte sizes in expressions/guards, shifted/recursive contexts and materialized controls; also run on BEAM | Table-driven |
205206
| `binary_rest_unit.erl` | Unsized /binary remainder units in case, heads, matches, shifted/recursive contexts, bound/wildcard tails, receive, maybe and list generators; also run on BEAM | Table-driven |
206207
| `binary_generator.erl` | Pattern-sized <= generators: widths, units, scaled float widths, bitstrings, UTF, nonfinite floats, mismatches, nesting, shadowing and cursor lifetime; same fixture on BEAM | 90+ |

‎tests/bif_bitstring_dynamic.erl‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)