Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/beaver-lodge/kinda into fix…
Browse files Browse the repository at this point in the history
…-fox-ex118
  • Loading branch information
jackalcooper committed Dec 20, 2024
2 parents 6f276e6 + 9caa208 commit 8c18a20
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
17 changes: 17 additions & 0 deletions kinda_example/lib/kinda_example_native.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule KindaExample.Native do
def check!({:kind, mod, ref}) when is_atom(mod) and is_reference(ref) do
struct!(mod, %{ref: ref})
end

def check!({:error, e}), do: raise(e)
def check!(ret), do: ret

def to_term(%mod{ref: ref}) do
forward(mod, :primitive, [ref])
end

def forward(element_kind, kind_func_name, args) do
apply(KindaExample.NIF, Module.concat(element_kind, kind_func_name), args)
|> check!()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ defmodule KindaExample.NIF do
root: __MODULE__,
forward: KindaExample.Native
defmodule CInt do
use Kinda.ResourceKind,
forward_module: KindaExample.Native
use Kinda.ResourceKind, forward_module: KindaExample.Native
end

for path <-
Expand All @@ -16,6 +15,10 @@ defmodule KindaExample.NIF do
@external_resource path
end

defmodule StrInt do
use Kinda.ResourceKind, forward_module: KindaExample.Native
end

@on_load :load_nif

def load_nif do
Expand Down
27 changes: 0 additions & 27 deletions kinda_example/lib/kinda_exmaple_native.ex

This file was deleted.

29 changes: 15 additions & 14 deletions kinda_example/test/kinda_example_test.exs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
defmodule KindaExampleTest do
use ExUnit.Case

alias KindaExample.{NIF, Native}

test "add in c" do
assert 3 ==
KindaExample.NIF.kinda_example_add(1, 2)
|> KindaExample.Native.to_term()
NIF.kinda_example_add(1, 2) |> Native.to_term()

assert match?(
%Kinda.CallError{message: "Fail to fetch argument #2", error_return_trace: _},
catch_error(KindaExample.NIF.kinda_example_add(1, "2"))
catch_error(NIF.kinda_example_add(1, "2"))
)
end

test "custom make" do
assert KindaExample.Native.forward(
KindaExample.NIF.CInt,
:make,
[100]
)
|> KindaExample.NIF."Elixir.KindaExample.NIF.CInt.primitive"() == 100
assert 100 ==
Native.forward(NIF.CInt, :make, [100])
|> NIF."Elixir.KindaExample.NIF.CInt.primitive"()

e = catch_error(KindaExample.NIF."Elixir.KindaExample.NIF.StrInt.make"(1))
e = catch_error(NIF."Elixir.KindaExample.NIF.StrInt.make"(1))
assert Exception.message(e) =~ "Function clause error\n#{IO.ANSI.reset()}"

err = catch_error(KindaExample.NIF."Elixir.KindaExample.NIF.StrInt.make"(1))
err = catch_error(NIF."Elixir.KindaExample.NIF.StrInt.make"(1))
# only test this on macOS, it will crash on Linux
txt = Exception.message(err)

Expand All @@ -36,8 +34,11 @@ defmodule KindaExampleTest do

assert match?(%Kinda.CallError{message: "Function clause error", error_return_trace: _}, err)

assert KindaExample.NIF."Elixir.KindaExample.NIF.StrInt.make"("1")
|> KindaExample.NIF."Elixir.KindaExample.NIF.CInt.primitive"() ==
1
assert 1 ==
NIF."Elixir.KindaExample.NIF.StrInt.make"("1")
|> NIF."Elixir.KindaExample.NIF.CInt.primitive"()

%NIF.StrInt{ref: ref} = NIF.StrInt.make("1")
assert 1 == ref |> NIF."Elixir.KindaExample.NIF.CInt.primitive"()
end
end
5 changes: 2 additions & 3 deletions lib/codegen/kind_decl.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Kinda.CodeGen.KindDecl do
require Logger

@primitive_types for t <- ~w{
@primitive_types ~w{
bool
c_int
c_uint
Expand All @@ -17,8 +17,7 @@ defmodule Kinda.CodeGen.KindDecl do
u64
u8
usize
},
do: String.to_atom(t)
}a

@opaque_ptr {:optional_type, {:ref, [:*, :anyopaque]}}
@opaque_array {:optional_type, {:ref, [:*, :const, :anyopaque]}}
Expand Down
2 changes: 1 addition & 1 deletion lib/kinda_codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Kinda.CodeGen do
end
end

@callback kinds() :: KindDecl.t()
@callback kinds() :: [KindDecl.t()]
@callback nifs() :: [{atom(), integer()}]
def kinds(), do: []

Expand Down

0 comments on commit 8c18a20

Please sign in to comment.