Current section

Files

Jump to
unifex lib unifex loader.ex
Raw

lib/unifex/loader.ex

defmodule Unifex.Loader do
@moduledoc """
This module allows to generate definitions for native functions described in Unifex specs.
To acheive that simply use this module:
use Unifex.Loader
"""
alias Unifex.{Helper, InterfaceIO, Specs}
defmacro __using__(_args) do
specs =
Helper.get_source_dir()
|> InterfaceIO.get_interfaces_specs!()
|> Enum.map(fn {name, _dir, specs} -> Specs.parse(specs, name) end)
|> Enum.find(&(&1.module == __CALLER__.module))
funs =
specs.functions_args
|> Enum.map(fn {name, args} ->
wrapped_name = name |> to_string() |> (&"unifex_#{&1}").() |> String.to_atom()
arg_names = args |> Keyword.keys() |> Enum.map(&Macro.var(&1, nil))
quote do
defnifp unquote(wrapped_name)(unquote_splicing(arg_names))
@compile {:inline, [unquote({name, length(args)})]}
def unquote(name)(unquote_splicing(arg_names)) do
unquote({wrapped_name, [], arg_names})
end
end
end)
overrides =
specs.functions_args
|> Enum.map(fn {name, args} ->
{name, length(args)}
end)
quote do
use Bundlex.Loader, nif: unquote(specs.name)
unquote_splicing(funs)
defoverridable unquote(overrides)
end
end
end