Mojoler.POC (mojoler v0.1.0)

Copy Markdown View Source

Checked signed 64-bit addition, loaded from priv/native/libpoc.so.

The entry name is Elixir.Mojoler.POC. nif_init writes that string into ErlNifEntry.name.

Summary

Types

A signed 64-bit integer. Values outside this range are not terms this NIF accepts.

Functions

Returns a + b when both arguments and the sum fit in a signed 64-bit integer.

Loads priv/native/libpoc.so.

Loads the library again.

Types

i64()

@type i64() :: -9_223_372_036_854_775_808..9_223_372_036_854_775_807

A signed 64-bit integer. Values outside this range are not terms this NIF accepts.

Functions

add(a, b)

@spec add(i64(), i64()) :: i64()

Returns a + b when both arguments and the sum fit in a signed 64-bit integer.

a and b are integers. Floats, atoms, and binaries are rejected. There is no optional argument.

Returns

The sum, an integer.

Raises

  • ArgumentError when a or b is not an integer, does not fit in signed 64 bits, or the sum would overflow

A wrong arity never reaches this function. Elixir raises UndefinedFunctionError.

Examples

iex> Mojoler.POC.add(20, 22)
42

iex> Mojoler.POC.add(-5, -7)
-12

iex> Mojoler.POC.add(1, :nope)
** (ArgumentError) argument error

iex> Mojoler.POC.add(9_223_372_036_854_775_807, 1)
** (ArgumentError) argument error

load_nif()

@spec load_nif() :: :ok | {:error, term()}

Loads priv/native/libpoc.so.

Called from this module by @on_load. A second call in the same VM returns an error. The library stays loaded until the OS process exits.

Returns

  • :ok on the first load
  • {:error, {:reload, charlist}} when OTP rejects a second load
  • {:error, reason} when the file is missing or the entry is refused

Examples

iex> Mojoler.POC.load_nif()
{:error, {:reload, ~c"NIF library already loaded (reload disallowed since OTP 20)."}}

reload()

@spec reload() :: :ok | {:error, term()}

Loads the library again.

Same contract as load_nif/0. The suite uses it to observe OTP's reload refusal.

Examples

iex> Mojoler.POC.reload()
{:error, {:reload, ~c"NIF library already loaded (reload disallowed since OTP 20)."}}