From 2414144875906c7e255bf1148bd3f45badbaa647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20R=C3=A9mond?= Date: Sun, 15 Nov 2026 21:57:36 +0100 Subject: [PATCH] fix(runtime): dispatch dynamic bitstring BIFs --- CHANGELOG.md | 10 ++++ FEATURES.md | 4 +- crates/nera-runtime/src/ffi/apply.rs | 2 + tests/README.md | 1 + tests/bif_bitstring_dynamic.erl | 84 ++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/bif_bitstring_dynamic.erl diff --git a/CHANGELOG.md b/CHANGELOG.md index 98967ee..a28939d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable milestones for the nera project are documented in this file. +## Dynamic bitstring BIFs match direct calls + +- `bit_size/1` and `is_bitstring/1` now work through `apply/2,3`, variable + module/function names and fun references instead of raising `undef`. +- Dynamic `bit_size/1` returns the bit count and raises `badarg` for + non-bitstrings; dynamic `is_bitstring/1` returns `true` or `false`, as on BEAM. +- `bif_bitstring_dynamic` runs unchanged natively and on BEAM, covering empty, + partial-byte, large and extracted bitstrings, non-bitstrings and higher-order + fun consumers with explicit completion. + ## Regression coverage for case exports inside maybe bodies - A diagnostic regression protects names bound in every `case` branch and read diff --git a/FEATURES.md b/FEATURES.md index 285c727..a79589e 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -167,7 +167,7 @@ Maintainers with OTP 29.0.4 installed can verify the catalogue with | tuple_size/1 | ✅ | | setelement/3 | ✅ | | 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 | -| bit_size/1 | ✅ | Non-bitstrings raise badarg; guards on a non-bitstring fail | +| bit_size/1 | ✅ | Non-bitstrings raise badarg; guards on a non-bitstring fail; direct, apply, variable module/function and fun reference calls | | map_get/2 | ✅ | | is_map_key/2 | ✅ | Auto-imported; guards, negated guards and bodies; non-maps raise badmap | | 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 | is_binary/1 | ✅ | | is_map/1 | ✅ | | is_function/1,2 | ✅ | -| is_bitstring/1 | ✅ | +| is_bitstring/1 | ✅ | Direct, apply, variable module/function and fun reference calls; false for non-bitstrings | | is_reference/1 | ✅ | | is_boolean/1 | ✅ | | atom_to_list/1 | ✅ | diff --git a/crates/nera-runtime/src/ffi/apply.rs b/crates/nera-runtime/src/ffi/apply.rs index aa9ca94..e60c676 100644 --- a/crates/nera-runtime/src/ffi/apply.rs +++ b/crates/nera-runtime/src/ffi/apply.rs @@ -165,6 +165,7 @@ pub(super) unsafe fn apply_impl( (Some("erlang"), Some("is_tuple"), 1) => Some(nera_rt_is_tuple(ctx, arg_terms[0])), (Some("erlang"), Some("is_pid"), 1) => Some(nera_rt_is_pid(ctx, arg_terms[0])), (Some("erlang"), Some("is_binary"), 1) => Some(nera_rt_is_binary(ctx, arg_terms[0])), + (Some("erlang"), Some("is_bitstring"), 1) => Some(nera_rt_is_bitstring(ctx, arg_terms[0])), (Some("erlang"), Some("is_map"), 1) => Some(nera_rt_is_map(ctx, arg_terms[0])), (Some("erlang"), Some("is_function"), 1) => Some(nera_rt_is_function(ctx, arg_terms[0])), (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( (Some("erlang"), Some("is_number"), 1) => Some(nera_rt_is_number(ctx, arg_terms[0])), (Some("erlang"), Some("is_boolean"), 1) => Some(nera_rt_is_boolean(ctx, arg_terms[0])), (Some("erlang"), Some("byte_size"), 1) => Some(nera_rt_binary_byte_size(ctx, arg_terms[0])), + (Some("erlang"), Some("bit_size"), 1) => Some(nera_rt_binary_size(ctx, arg_terms[0])), (Some("erlang"), Some("map_size"), 1) => Some(nera_rt_map_size(ctx, arg_terms[0])), (Some("erlang"), Some("max"), 2) => Some(nera_rt_max(ctx, arg_terms[0], arg_terms[1])), (Some("erlang"), Some("min"), 2) => Some(nera_rt_min(ctx, arg_terms[0], arg_terms[1])), diff --git a/tests/README.md b/tests/README.md index 4121aa2..d3c11f5 100644 --- a/tests/README.md +++ b/tests/README.md @@ -201,6 +201,7 @@ These are automatically skipped by the runner and show up as SKIP in output. | `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 | | `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 | | `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 | +| `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 | | `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 | | `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 | | `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+ | diff --git a/tests/bif_bitstring_dynamic.erl b/tests/bif_bitstring_dynamic.erl new file mode 100644 index 0000000..8e8b362 --- /dev/null +++ b/tests/bif_bitstring_dynamic.erl @@ -0,0 +1,84 @@ +%% EXPECT: bif_bitstring_dynamic_ok +%% Regression: dynamic bit_size/1 and is_bitstring/1 calls must +%% agree with direct calls, not raise undef. The runtime fallback table +%% used to omit both BIFs. Run this same fixture on native Nera and BEAM. +-module(bif_bitstring_dynamic). +-export([test/0]). + +test() -> + SizeCalls = calls(bit_size), + TypeCalls = calls(is_bitstring), + <<_, Tail/binary>> = id(<<1, 2, 3, 4>>), + <<_:3, Bits/bits>> = id(<<1, 2, 3>>), + Big = id(binary:copy(<<"x">>, 1000)), + <<_:5/binary, Middle:990/binary, _/binary>> = Big, + Bitstrings = id([<<>>, <<"a">>, <<"abc">>, <<1:1>>, <<1:7>>, + <<1, 5:3>>, <<1:9>>, Tail, Bits, Big, Middle, + <>]), + lists:foreach(fun(B) -> + %% Direct size semantics have their own bitstring_size_badarg fixture; + %% this regression owns parity of the dynamic entry points. + check_calls(bit_size, SizeCalls, B, {ok, bit_size(B)}), + check_calls(is_bitstring, TypeCalls, B, {ok, true}) + end, Bitstrings), + + NonBitstrings = id([abc, true, [], [1, 2], "abcd", [<<"ab">>], [a | b], + 0, 42, -1, 1 bsl 70, 1.0, {}, {<<"a">>}, #{}, + #{a => <<"a">>}, self(), make_ref(), + fun() -> ok end, fun id/1]), + lists:foreach(fun(X) -> + check_calls(bit_size, SizeCalls, X, {error, badarg}), + check_calls(is_bitstring, TypeCalls, X, {ok, false}) + end, NonBitstrings), + + %% Real higher-order consumers of auto-imported and remote fun references. + [0, 24, 9, 1] = lists:map(fun bit_size/1, id([<<>>, <<"abc">>, <<1:9>>, <<1:1>>])), + [true, false, true] = lists:map(fun erlang:is_bitstring/1, + id([<<1, 5:3>>, [1, 2], <<>>])), + %% A caught badarg does not prevent a later dynamic call from succeeding. + Remote = fun erlang:bit_size/1, + {error, badarg} = outcome(Remote, id(not_bits)), + 11 = apply(erlang, bit_size, [id(<<1, 5:3>>)]), + io:format("bif_bitstring_dynamic_ok~n"), + ok. + +calls(bit_size) -> + Mod = id(erlang), + [{local_fun, fun bit_size/1}, + {remote_fun, fun erlang:bit_size/1}, + {variable_module, fun(B) -> Mod:bit_size(B) end} + | dynamic_calls(bit_size)]; +calls(is_bitstring) -> + Mod = id(erlang), + [{local_fun, fun is_bitstring/1}, + {remote_fun, fun erlang:is_bitstring/1}, + {variable_module, fun(B) -> Mod:is_bitstring(B) end} + | dynamic_calls(is_bitstring)]. + +dynamic_calls(Name) -> + Mod = id(erlang), + Function = id(Name), + Dynamic = fun Mod:Function/1, + [{apply, fun(B) -> apply(erlang, Name, [B]) end}, + {qualified_apply, fun(B) -> erlang:apply(erlang, Name, [B]) end}, + {variable_apply, fun(B) -> apply(Mod, Function, [B]) end}, + {variable_function, fun(B) -> erlang:Function(B) end}, + {variable_module_function, fun(B) -> Mod:Function(B) end}, + {dynamic_fun, Dynamic}, + {apply_fun, fun(B) -> apply(Dynamic, [B]) end}, + {apply_local_fun, fun(B) -> apply(local_fun(Name), [B]) end}, + {apply_remote_fun, fun(B) -> apply(remote_fun(Name), [B]) end}]. + +local_fun(bit_size) -> fun bit_size/1; +local_fun(is_bitstring) -> fun is_bitstring/1. +remote_fun(bit_size) -> fun erlang:bit_size/1; +remote_fun(is_bitstring) -> fun erlang:is_bitstring/1. + +check_calls(Name, Calls, X, Expected) -> + Results = [{Form, outcome(F, X)} || {Form, F} <- Calls], + ExpectedResults = [{Form, Expected} || {Form, _} <- Calls], + {Name, X, ExpectedResults} = {Name, X, Results}, + ok. + +outcome(F, X) -> try F(X) of V -> {ok, V} catch error:R -> {error, R} end. +id(X) -> X.