Initial commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
/_build
|
||||||
|
/deps
|
||||||
|
erl_crash.dump
|
||||||
|
*.ez
|
||||||
24
config/config.exs
Normal file
24
config/config.exs
Normal file
@@ -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"
|
||||||
11
examples/basic.exs
Normal file
11
examples/basic.exs
Normal file
@@ -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")
|
||||||
2
lib/ffi.ex
Normal file
2
lib/ffi.ex
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
defmodule FFI do
|
||||||
|
end
|
||||||
37
lib/ffi/library.ex
Normal file
37
lib/ffi/library.ex
Normal file
@@ -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
|
||||||
30
mix.exs
Normal file
30
mix.exs
Normal file
@@ -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
|
||||||
7
test/ffi_test.exs
Normal file
7
test/ffi_test.exs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
defmodule FFITest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "the truth" do
|
||||||
|
assert 1 + 1 == 2
|
||||||
|
end
|
||||||
|
end
|
||||||
1
test/test_helper.exs
Normal file
1
test/test_helper.exs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ExUnit.start()
|
||||||
Reference in New Issue
Block a user