commit 7f443aa263fc20ef4666edd4966b76c1b44441a3 Author: Josh Nussbaum Date: Sun Sep 20 19:30:19 2015 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9607671 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/_build +/deps +erl_crash.dump +*.ez diff --git a/README.md b/README.md new file mode 100644 index 0000000..c8df627 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Ffi +=== + +** TODO: Add description ** diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..6dfa82f --- /dev/null +++ b/config/config.exs @@ -0,0 +1,24 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for third- +# party users, it should be done in your mix.exs file. + +# Sample configuration: +# +# config :logger, :console, +# level: :info, +# format: "$date $time [$level] $metadata$message\n", +# metadata: [:user_id] + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/examples/basic.exs b/examples/basic.exs new file mode 100644 index 0000000..58df07d --- /dev/null +++ b/examples/basic.exs @@ -0,0 +1,11 @@ +defmodule MyLib do + use FFI.Library + + ffi_lib "c" + + attach_function :puts, [:string], :int +end + +{:ok, pid} = MyLib.start_link + +MyLib.puts(pid, "Hello world") diff --git a/lib/ffi.ex b/lib/ffi.ex new file mode 100644 index 0000000..6c0395f --- /dev/null +++ b/lib/ffi.ex @@ -0,0 +1,2 @@ +defmodule FFI do +end diff --git a/lib/ffi/library.ex b/lib/ffi/library.ex new file mode 100644 index 0000000..77902fd --- /dev/null +++ b/lib/ffi/library.ex @@ -0,0 +1,37 @@ +defmodule FFI.Library do + defmacro __using__(_x) do + quote do + import FFI.Library + + def start_link(args \\ []) do + GenServer.start_link(__MODULE__, :init, []) + end + + def init(state), + do: {:ok, state} + end + end + + defmacro ffi_lib(name) do + quote do + def ffi_lib, + do: unquote(name) + end + end + + defmacro attach_function(name, arguments, return_type) do + quote do + def unquote(name)(pid, x) do + args = {unquote(name), x} + + GenServer.call(pid, args) + end + + def handle_call({unquote(name), x}, _from, state) do + IO.puts ffi_lib + a = IO.puts(x) + {:reply, a, state} + end + end + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..eaa37f5 --- /dev/null +++ b/mix.exs @@ -0,0 +1,30 @@ +defmodule FFI.Mixfile do + use Mix.Project + + def project do + [app: :ffi, + version: "0.0.1", + elixir: "~> 1.0", + deps: deps] + end + + # Configuration for the OTP application + # + # Type `mix help compile.app` for more information + def application do + [applications: [:logger]] + end + + # Dependencies can be Hex packages: + # + # {:mydep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} + # + # Type `mix help deps` for more examples and options + defp deps do + [] + end +end diff --git a/test/ffi_test.exs b/test/ffi_test.exs new file mode 100644 index 0000000..f82bea4 --- /dev/null +++ b/test/ffi_test.exs @@ -0,0 +1,7 @@ +defmodule FFITest do + use ExUnit.Case + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()