tomazzlender

tomazzlender

Compiling and loading NIFs on runtime

I tried out Rustler and it is amazing when I need to define NIFs at the compile time.

I’m experimenting with a use case where it would be useful to define a NIF at a run time.

Imagine being in an IEx shell where you are able to define a function at the runtime and use it right after.

I wonder how I could do something similar for nifs. Let’s imagine that I have the following Rust code that I want to compile and load at the runtime of BEAM

  fn add(a: i64, b: i64) -> i64 {
    a + b
  }

and use it like

defmodule AdditionWithRust do
  def add(a, b), do
     add_function = """
     fn add(a: i64, b: i64) -> i64 {
       a + b
     }
    """

    RustOnRuntime.apply(add_function, a, b)
end
"""

Where RustOnRuntime is a fictional module that does some magic.

I would copy paste this to IEx and I would be able to use the function normally.

It’s OK to assume I have a fixed number of Rust libraries that are defined at the compile time, and only those library functions can be used, no new can be loaded.

I suppose this is quite a complex question. I suppose that at the bottom of it there is probably a question if it is even possible to compile and load erlang nifs (of any kind not just Rust) at the run time…

Any pointers what things to consider or where to start would be much appreciated.

Marked As Solved

arcadian

arcadian

I’m not a Rust fan - prefer C++ - but I’ll tell you what I know. If you update the shared library, and the NIF supports reloading (see ERL_NIF_INIT), then there is no reason that you shouldn’t be able to update your NIF whenever you want with new functionality. Updating the shared library for my setup is as easy as just running gmake in the right directory.

Imagine being in an IEx shell where you are able to define a function at the runtime and use it right after.

Yeah, this is what I should be doing, but I haven’t set the appropriate reload functions. Still, though my development cycle is pretty close - edit the c++ code in one window, then restart iex in another (which compiles my nif via gmake) and then run my test at the iex command line.

BTW I proposed a dedicated Nif subforum here but couldn’t get any momentum so my plan is to maintain some pages of Nif-related resources somewhere else, either github or my own site.

Also Liked

tomazzlender

tomazzlender

That’s a great pointer @arcadian, thanks! I’ll look into ERL_NIF_INIT. Your answer also helped me find the Rust equivalent.

lkuty

lkuty

That would be great :slight_smile:

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement