From 39b04810e4980a76d4e08bd510cf281b4ad0d3b2 Mon Sep 17 00:00:00 2001 From: Josh Nussbaum Date: Mon, 13 Jun 2016 02:05:15 -0400 Subject: [PATCH] Updated attach_function macro to use dynamic number of arguments --- lib/ffi/library.ex | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ffi/library.ex b/lib/ffi/library.ex index 40ee9a1..a5f9567 100644 --- a/lib/ffi/library.ex +++ b/lib/ffi/library.ex @@ -8,15 +8,31 @@ defmodule FFI.Library do end defmacro attach_function(name, arguments, return_type) do + argument_names = make_argument_names(arguments) + quote do - def unquote(name)(params) do + def unquote(name)(unquote_splicing(argument_names)) do definition = {ffi_lib, unquote(name), unquote(arguments), unquote(return_type)} - FFI.call(definition, params) + FFI.call(definition, [unquote_splicing(argument_names)]) end end end + + defp make_argument_names(arguments) do + arguments + |> Enum.with_index + |> Enum.map(&make_argument_name/1) + end + + defp make_argument_name({_type, index}) do + variable_name = [?a + index] + |> to_string + |> String.to_atom + + Macro.var(variable_name, nil) + end end