From 47e36cfed75296086257264c045c2b12e7a15b96 Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:05:45 +0800 Subject: [PATCH 1/7] Update kinda_codegen.ex --- kinda_example/lib/kinda_exmaple_nif.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kinda_example/lib/kinda_exmaple_nif.ex b/kinda_example/lib/kinda_exmaple_nif.ex index 3c0e5e1..89d870d 100644 --- a/kinda_example/lib/kinda_exmaple_nif.ex +++ b/kinda_example/lib/kinda_exmaple_nif.ex @@ -1,6 +1,8 @@ defmodule KindaExample.NIF do - use Kinda.CodeGen, with: KindaExample.CodeGen, root: __MODULE__, forward: KindaExample.Native - + @nifs use Kinda.CodeGen, + with: KindaExample.CodeGen, + root: __MODULE__, + forward: KindaExample.Native defmodule CInt do use Kinda.ResourceKind, forward_module: KindaExample.Native From 327f818b54f47e978ad5cc296ea115a9de372f00 Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:05:47 +0800 Subject: [PATCH 2/7] Update kinda_codegen.ex --- lib/kinda_codegen.ex | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/kinda_codegen.ex b/lib/kinda_codegen.ex index 1401ea8..5b6cce9 100644 --- a/lib/kinda_codegen.ex +++ b/lib/kinda_codegen.ex @@ -10,9 +10,9 @@ defmodule Kinda.CodeGen do mod = Keyword.fetch!(unquote(opts), :with) root = Keyword.fetch!(unquote(opts), :root) forward = Keyword.fetch!(unquote(opts), :forward) - - Kinda.CodeGen.nif_ast(mod.kinds(), mod.nifs(), root, forward) - |> then(&Module.eval_quoted(__MODULE__, &1)) + {ast, mf} = Kinda.CodeGen.nif_ast(mod.kinds(), mod.nifs(), root, forward) + ast |> Code.eval_quoted([], __ENV__) + mf end end @@ -27,7 +27,9 @@ defmodule Kinda.CodeGen do |> Enum.map(&NIFDecl.from_resource_kind/1) |> List.flatten() - for nif <- nifs ++ extra_kind_nifs do + nifs = nifs ++ extra_kind_nifs + + for nif <- nifs do nif = case nif do {wrapper_name, arity} when is_atom(wrapper_name) and is_integer(arity) -> @@ -43,7 +45,7 @@ defmodule Kinda.CodeGen do args_ast = Macro.generate_unique_arguments(nif.arity, __MODULE__) - %NIFDecl{wrapper_name: wrapper_name, nif_name: nif_name} = nif + %NIFDecl{wrapper_name: wrapper_name, nif_name: nif_name, arity: arity} = nif wrapper_name = if is_bitstring(wrapper_name) do @@ -52,21 +54,26 @@ defmodule Kinda.CodeGen do wrapper_name end + wrapper_ast = + if nif_name != wrapper_name do + quote do + def unquote(wrapper_name)(unquote_splicing(args_ast)) do + refs = Kinda.unwrap_ref([unquote_splicing(args_ast)]) + ret = apply(__MODULE__, unquote(nif_name), refs) + unquote(forward_module).check!(ret) + end + end + end + quote do @doc false def unquote(nif_name)(unquote_splicing(args_ast)), - do: - raise( - "NIF for resource kind is not implemented, or failed to load NIF library. Function: :\"#{unquote(nif_name)}\"/#{unquote(nif.arity)}" - ) + do: :erlang.nif_error(:not_loaded) - def unquote(wrapper_name)(unquote_splicing(args_ast)) do - refs = Kinda.unwrap_ref([unquote_splicing(args_ast)]) - ret = apply(__MODULE__, unquote(nif_name), refs) - unquote(forward_module).check!(ret) - end + unquote(wrapper_ast) end + |> then(&{&1, {nif_name, arity}}) end - |> List.flatten() + |> Enum.unzip() end end From 008695886bd403888900dc1c7af3d2698bf55c65 Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:06:20 +0800 Subject: [PATCH 3/7] Update elixir.yml --- .github/workflows/elixir.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 524248b..083a196 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -23,7 +23,7 @@ jobs: matrix: runs-on: ["ubuntu-latest"] otp: ["24.2", "25.0"] - elixir: ["1.13.0", "1.16.2"] + elixir: ["1.13.0", "1.16.2", "1.18.0"] steps: - uses: actions/checkout@v2 - uses: erlef/setup-beam@v1 From 13efa4f14436c4f9033e92696bacfef2f86d58cd Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:07:59 +0800 Subject: [PATCH 4/7] add exclude --- .github/workflows/elixir.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 083a196..60c21df 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -24,6 +24,9 @@ jobs: runs-on: ["ubuntu-latest"] otp: ["24.2", "25.0"] elixir: ["1.13.0", "1.16.2", "1.18.0"] + exclude: + - otp: "24.2" + elixir: "1.18.0" steps: - uses: actions/checkout@v2 - uses: erlef/setup-beam@v1 From 6f276e6cc20e859174bb9cb2fa85bdc99c0e4aee Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:13:13 +0800 Subject: [PATCH 5/7] Update elixir.yml --- .github/workflows/elixir.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 60c21df..64fe54a 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -52,12 +52,17 @@ jobs: restore-keys: ${{ runner.os }}-mix-example- - name: Install dependencies run: mix deps.get + - name: Check formatting of Elixir + run: mix format --check-formatted - name: Run tests run: | mix test - name: Install dependencies (example) working-directory: ./kinda_example run: mix deps.get + - name: Check formatting of Elixir + working-directory: ./kinda_example + run: mix format --check-formatted - name: Run tests (example) working-directory: ./kinda_example run: | From 3e01a7a094fb15c15869760253ee0e401d23cfdf Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:21:31 +0800 Subject: [PATCH 6/7] Update mix.lock --- mix.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.lock b/mix.lock index e86e107..099bf47 100644 --- a/mix.lock +++ b/mix.lock @@ -3,7 +3,7 @@ "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "ex_doc": {:hex, :ex_doc, "0.35.1", "de804c590d3df2d9d5b8aec77d758b00c814b356119b3d4455e4b8a8687aecaf", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "2121c6402c8d44b05622677b761371a759143b958c6c19f6558ff64d0aed40df"}, "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, - "makeup_elixir": {:hex, :makeup_elixir, "1.0.0", "74bb8348c9b3a51d5c589bf5aebb0466a84b33274150e3b6ece1da45584afc82", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49159b7d7d999e836bedaf09dcf35ca18b312230cf901b725a64f3f42e407983"}, + "makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"}, "makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, } From d7c59818735def36e9b0843695809c039cee0115 Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 20 Dec 2024 20:24:58 +0800 Subject: [PATCH 7/7] Update mix.exs --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index b01a9a2..a1e6573 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Kinda.MixProject do def project do [ app: :kinda, - version: "0.9.4-dev", + version: "0.9.5-dev", elixir: "~> 1.9", start_permanent: Mix.env() == :prod, deps: deps(),