Skip to content

Commit

Permalink
Add support for OTP 26 by supporting NIF v2.17 (#526)
Browse files Browse the repository at this point in the history
This is adding support for the new functions that are included in the
latest OTP version 26.

Pre-release notes: https://github.com/erlang/otp/releases/tag/OTP-26.0-rc2

New functions added:
  - enif_set_option
  - enif_get_string_length
  - enif_make_new_atom
  - enif_make_new_atom_len

This version also introduce support for UTF8 atoms and strings in the
NIF interface.
  • Loading branch information
philss authored Mar 28, 2023
1 parent bc8c038 commit 11e6481
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rustler/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::env;
use std::process::Command;

// keep this sorted by version number
const NIF_VERSION: &[&str] = &["2.14", "2.15", "2.16"];
const NIF_VERSION: &[&str] = &["2.14", "2.15", "2.16", "2.17"];

fn main() {
let latest_version = NIF_VERSION.last().unwrap().to_string();
Expand Down
16 changes: 15 additions & 1 deletion rustler_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::process::Command;
use std::{env, fs};

const SNIPPET_NAME: &str = "nif_api.snippet";
const SUPPORTED_VERSIONS: [(u32, u32); 3] = [(2, 14), (2, 15), (2, 16)];
const SUPPORTED_VERSIONS: &[(u32, u32)] = &[(2, 14), (2, 15), (2, 16), (2, 17)];

trait ApiBuilder {
fn func(&mut self, ret: &str, name: &str, args: &str);
Expand Down Expand Up @@ -846,6 +846,7 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
b.func("c_int", "enif_make_map_from_arrays", "env: *mut ErlNifEnv, keys: *const ERL_NIF_TERM, values: *const ERL_NIF_TERM, cnt: usize, map_out: *mut ERL_NIF_TERM");
}

// 2.15 was introduced in OTP 22
if opts.nif_version >= (2, 15) {
b.func(
"ErlNifTermType",
Expand All @@ -862,10 +863,23 @@ fn build_api(b: &mut dyn ApiBuilder, opts: &GenerateOptions) {
);
}

// 2.16 was introduced in OTP 24
if opts.nif_version >= (2, 16) {
b.func("*const ErlNifResourceType", "enif_init_resource_type", "env: *mut ErlNifEnv, name_str: *const c_uchar, init: *const ErlNifResourceTypeInit, flags: ErlNifResourceFlags, tried: *mut ErlNifResourceFlags");
b.func("c_int", "enif_dynamic_resource_call", "env: *mut ErlNifEnv, module: ERL_NIF_TERM, name: ERL_NIF_TERM, rsrc: ERL_NIF_TERM, call_data: *const c_void");
}

// 2.17 was introduced in OTP 26
if opts.nif_version >= (2, 17) {
b.func(
"c_int",
"enif_set_option",
"env: *mut ErlNifEnv, opt: ErlNifOption",
);
b.func("c_int", "enif_get_string_length", "env: *mut ErlNifEnv, list: ERL_NIF_TERM, len: *mut c_uint, encoding: ErlNifCharEncoding");
b.func("c_int", "enif_make_new_atom", "env: *mut ErlNifEnv, name: *const c_uchar, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
b.func("c_int", "enif_make_new_atom_len", "env: *mut ErlNifEnv, name: *const c_uchar, len: size_t, atom: *mut ERL_NIF_TERM, encoding: ErlNifCharEncoding");
}
}

fn parse_nif_version(version: &str) -> (u32, u32) {
Expand Down
10 changes: 10 additions & 0 deletions rustler_sys/src/rustler_sys_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub enum ErlNifResourceFlags {
#[repr(C)]
pub enum ErlNifCharEncoding {
ERL_NIF_LATIN1 = 1,
ERL_NIF_UTF8 = 2,
}

/// See [ErlNifPid](http://www.erlang.org/doc/man/erl_nif.html#ErlNifPid) in the Erlang docs.
Expand Down Expand Up @@ -328,6 +329,15 @@ pub enum ErlNifTermType {
ERL_NIF_TERM_TYPE__MISSING_DEFAULT_CASE__READ_THE_MANUAL = -1,
}

/// See [ErlNifOption](http://www.erlang.org/doc/man/erl_nif.html#ErlNifOption) in the Erlang docs.
#[derive(Copy, Clone)]
#[repr(C)]
pub enum ErlNifOption {
// from https://github.com/erlang/otp/blob/1b3c6214d4bf359f7e5c143bef5c5ad9c90c5536/erts/emulator/beam/erl_nif.h#L333
ERL_NIF_OPT_DELAY_HALT = 1,
ERL_NIF_OPT_ON_HALT = 2,
}

// Include the file generated by `build.rs`.
include!(concat!(env!("OUT_DIR"), "/nif_api.snippet"));
// example of included content:
Expand Down

0 comments on commit 11e6481

Please sign in to comment.