Added typemap and type sanitizing
This commit is contained in:
28
lib/ffi.ex
28
lib/ffi.ex
@@ -1,14 +1,34 @@
|
|||||||
defmodule FFI do
|
defmodule FFI do
|
||||||
@on_load :init
|
@on_load :init
|
||||||
|
|
||||||
|
@typemap %{
|
||||||
|
void: 0,
|
||||||
|
string: 1,
|
||||||
|
int: 2
|
||||||
|
}
|
||||||
|
|
||||||
def init do
|
def init do
|
||||||
:erlang.load_nif("./ffi_nif", 0)
|
:ok = :erlang.load_nif("./ffi_nif", 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call({library, name, arguments, return_type}, params),
|
def call({library, function, arguments, return_type}, values) do
|
||||||
do: nif_call(library, name, arguments, return_type, params)
|
nif_call(String.to_atom(library),
|
||||||
|
function,
|
||||||
|
Enum.map(arguments, &map_type/1),
|
||||||
|
map_type(return_type),
|
||||||
|
clean_values(values))
|
||||||
|
end
|
||||||
|
|
||||||
def nif_call(library, name, arguments, return_type, params) do
|
def nif_call(library, function, arguments, return_type, values) do
|
||||||
:badarg
|
:badarg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp map_type(type), do: @typemap[type]
|
||||||
|
|
||||||
|
defp clean_values(values) do
|
||||||
|
Enum.map values, fn
|
||||||
|
str when is_bitstring(str) -> to_char_list(str)
|
||||||
|
other -> other
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user