C3nif.Generator (C3nif v0.2.0)

View Source

Generate C3 NIF entry point code.

This module generates the C3 code needed to register NIFs with the Erlang runtime:

  • ErlNifFunc array with function metadata
  • nif_init function that returns the ErlNifEntry

Generated Code Structure

// === AUTO-GENERATED NIF ENTRY ===
ErlNifFunc[N] __c3nif_funcs__ = {
    { .name = "func1", .arity = 1, .fptr = &func1, .flags = 0 },
    { .name = "func2", .arity = 2, .fptr = &func2, .flags = erl_nif::ERL_NIF_DIRTY_JOB_CPU_BOUND },
};

ErlNifEntry __c3nif_entry__;

fn ErlNifEntry* nif_init() @export("nif_init") {
    __c3nif_entry__ = c3nif::make_nif_entry(
        "Elixir.MyModule",
        &__c3nif_funcs__,
        N,
        &on_load,  // or null
        null
    );
    return &__c3nif_entry__;
}

Summary

Functions

Generate complete C3 source by appending entry point to user code.

Generate C3 entry point code for the given NIFs and callbacks.

Functions

generate_complete(user_code, module_name, nifs, callbacks)

@spec generate_complete(
  String.t(),
  String.t(),
  [C3nif.Parser.NifFunction.t()],
  C3nif.Parser.Callbacks.t()
) :: String.t()

Generate complete C3 source by appending entry point to user code.

Parameters

  • user_code - The user's C3 source code
  • module_name - The Elixir module name
  • nifs - List of NifFunction structs
  • callbacks - Callbacks struct

Returns

Complete C3 source code ready for compilation.

generate_entry(module_name, nifs, callbacks)

@spec generate_entry(
  String.t(),
  [C3nif.Parser.NifFunction.t()],
  C3nif.Parser.Callbacks.t()
) ::
  String.t()

Generate C3 entry point code for the given NIFs and callbacks.

Parameters

  • module_name - The Elixir module name (e.g., "Elixir.MyApp.MyNif")
  • nifs - List of NifFunction structs
  • callbacks - Callbacks struct with on_load/on_unload

Returns

C3 source code string to append to the user's C3 code.