IvanR

IvanR

How to read the BEAM bytecode compiled into memory and that is not on the disk?

Hi,
I’m the author of the Domo library that validates struct’s data against its @type spec and would like to upgrade it to support in-memory modules.

In the library, I read struct’s types by Code.Typespec.fetch_types(module_atom) that works for the module that has the compiled BEAM file on the disk. At the same time, for an in-memory module that you can make in iex, it returns :error like the following:

iex(1)> defmodule Ms do
...(1)>   defstruct [:id]
...(1)>   @type t :: %__MODULE__{id: atom}
...(1)> end
{:module, Ms, ...}

iex(2)> Code.Typespec.fetch_types(Ms)
:error

The Code.Typespec.fetch_types/1 delegates to Erlang’s :code.get_object_code/1 that, from my understanding searches bytecode on the disk. That fails for modules that exist only in memory.

How can I get the bytecode for the module that is in memory only by its name in iex/Livebook?

Marked As Solved

garazdawi

garazdawi

Erlang Core Team

It is not possible to do because during loading the original module is destroyed and it is not possible to reverse-load the module from what is loaded. So to implement such functionality we would be to keep the original module which would double the memory usage of the code for each loaded module.

Also Liked

IvanR

IvanR

Types can be read from the module’s bytecode like the following at compile-time:

defmodule MyModule do
  @type one_type :: atom()
  @opaque another_type :: integer()

  @after_compile {MyModule, :_collect_types}

  def _collect_types(_env, bytecode) do
    {:ok, types} = Code.Typespec.fetch_types(bytecode)

    Enum.map(types, fn {kind, type} ->
      type_ast = Code.Typespec.type_to_quoted(type)

      type_ast
      |> Macro.to_string()
      |> IO.inspect(label: kind)
    end)
  end
end

it prints:

opaque: "another_type() :: integer()"
type: "one_type() :: atom()"
hauleth

hauleth

You cannot. I have asked for such thing on the Erlang Issue Tracker and IRC and there is no way to achieve what you want. The only solution is to manually store 3rd element of the tuple returned by defmodule and use that as a BEAM data.

IvanR

IvanR

Good to know. It seems OTP has no public interface to get in-memory modules yet.

Where Next?

Popular in Questions Top

_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
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
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...
3271 126479 1222
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement