Skip to content

Commit

Permalink
gets zig_doc working
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Nov 7, 2023
1 parent 92d59fa commit 5ab51c5
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 80 deletions.
6 changes: 6 additions & 0 deletions guides/4-nif_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ If you tag your nif as `leak_check`, it will check that `beam.allocator` has
cleared all of its contents at the end of the function call, and if that hasn't
happened, it raises.

> ## leak check warning {: .warning }
>
> leak check doesn't seem to be working in 0.11.0 and will return in 0.11.1
```elixir
defmodule LeakCheckTest do
use ExUnit.Case, async: true
Expand All @@ -198,6 +202,7 @@ defmodule LeakCheckTest do
}
"""

@tag :skip
test "leak check" do
assert_raise RuntimeError, "memory leak detected in function `check_me/0`", fn ->
check_me()
Expand All @@ -223,6 +228,7 @@ defmodule LeakCheckAllTest do
}
"""

@tag :skip
test "leak check" do
assert_raise RuntimeError, "memory leak detected in function `check_me/0`", fn ->
check_me()
Expand Down
34 changes: 17 additions & 17 deletions guides/6-c_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ int plus_one(int value) {
```

```elixir
#defmodule CompilingC do
# use ExUnit.Case, async: true
# use Zig,
# otp_app: :zigler,
# include_dir: "include",
# c_src: "src/*"
#
# ~Z"""
# const c = @cImport(@cInclude("included.h"));
#
# pub const plus_one = c.plus_one;
# """
#
# test "c plus one" do
# assert 48 = plus_one(47)
# end
#end
defmodule CompilingC do
use ExUnit.Case, async: true
use Zig,
otp_app: :zigler,
include_dir: "include",
c_src: "src/*"

~Z"""
const c = @cImport(@cInclude("included.h"));
pub const plus_one = c.plus_one;
"""

test "c plus one" do
assert 48 = plus_one(47)
end
end
```

## linking against a C abi library
Expand Down
30 changes: 15 additions & 15 deletions guides/8-module_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ pub const value = 47;
```elixir
defmodule PackageFile do
use ExUnit.Case, async: true
# use Zig,
# otp_app: :zigler,
# packages: [extra: {"test/_support/package/extra.zig", [:beam]}]
#
# ~Z"""
# const extra = @import("extra");
#
# pub fn extra_value() u64 {
# return extra.value;
# }
# """
#
test "package file" #do
# assert 47 = extra_value()
# end
use Zig,
otp_app: :zigler,
packages: [extra: {"test/_support/package/extra.zig", [:beam]}]

~Z"""
const extra = @import("extra");
pub fn extra_value() u64 {
return extra.value;
}
"""

test "package file" do
assert 47 = extra_value()
end
end
#module
```
7 changes: 4 additions & 3 deletions lib/zig/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ defmodule Zig.Command do

def run_sema(file, opts) do
priv_dir = :code.priv_dir(:zigler)
sema_file = Path.join(priv_dir, "beam/sema.zig")
sema_file = Keyword.get(opts, :sema_context, Path.join(priv_dir, "beam/sema.zig"))
beam_file = Path.join(priv_dir, "beam/beam.zig")
erl_nif_file = Path.join(priv_dir, "beam/stub_erl_nif.zig")

package_opts =
opts
|> Keyword.get(:packages)
|> List.wrap()

erl_nif_pkg = {:erl_nif, erl_nif_file}

package_files =
Enum.map(package_opts, fn {name, {path, _}} -> {name, path} end) ++
[beam: beam_file, erl_nif: erl_nif_file]
[beam: {beam_file, [erl_nif_pkg]}, erl_nif: erl_nif_file]

packages =
Enum.map(package_opts, fn
Expand All @@ -58,7 +60,6 @@ defmodule Zig.Command do
{name, {path, deps_keyword}}
end)

erl_nif_pkg = {:erl_nif, erl_nif_file}
beam_pkg = {:beam, {beam_file, [erl_nif_pkg]}}

packages =
Expand Down
13 changes: 11 additions & 2 deletions lib/zig/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Zig.Options do
"""

alias Zig.EasyC
alias Zig.Module
alias Zig.Nif

@spec elixir_normalize!(keyword) :: keyword
Expand All @@ -35,7 +34,7 @@ defmodule Zig.Options do
[]

function when is_atom(function) ->
[{:function, []}]
[{function, []}]

{nif, nif_opts} ->
[{nif, escape_spec(nif_opts)}]
Expand All @@ -49,6 +48,16 @@ defmodule Zig.Options do
end)
end

def erlang_normalize!(opts) do
opts
|> Keyword.replace_lazy(:nifs, fn
:auto -> []
[:auto | rest] -> rest
explicit -> explicit
end)
|> set_auto(opts)
end

def set_auto(new_opts, old_opts) do
explicit_auto = old_opts
|> Keyword.get(:nifs)
Expand Down
3 changes: 2 additions & 1 deletion lib/zig/sema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ defmodule Zig.Sema do
reraise Zig.CompileError.to_error(e, opts), __STACKTRACE__
end

def run_sema(file, module \\ nil, opts \\ [include_dir: []]) do
def run_sema(file, module \\ nil, opts \\ []) do
opts = Keyword.put_new(opts, :include_dir, [])
# TODO: integrate error handling here, and make this a common nexus for
# file compilation
with {:ok, sema_str} <- Zig.Command.run_sema(file, opts),
Expand Down
8 changes: 4 additions & 4 deletions lib/zig/templates/build.zig.eex
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ pub fn build(b: *std.Build) void {
});

<%= for {name, path, deps} <- @packages do %>
const <%= name %>_mod = b.createModule(
const <%= name %> = b.createModule(
.{
.source_file = .{.cwd_relative = "<%= path %>"},
.dependencies = &[_]std.Build.ModuleDependency{
<%= for dep <- deps do %>
.{
.name = "<%= dep %>",
.module = <%= dep %>_mod
.module = <%= dep %>
}
<% end %>
}
});
<% end %>

<% module_list = Enum.map(@packages, fn {name, _, _} -> ", #{name}_mod" end) %>
<% module_list = Enum.map(@packages, fn {name, _, _} -> "#{name}" end) %>

const nif = b.createModule(.{
.source_file = .{.cwd_relative = "<%= @nif_path %>"},
Expand All @@ -48,7 +48,7 @@ pub fn build(b: *std.Build) void {
<%= for module <- module_list do %>
, .{
.name = "<%= module %>",
.module = <%= module %>_mod
.module = <%= module %>
}
<% end %>
}
Expand Down
14 changes: 13 additions & 1 deletion lib/zig/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ defprotocol Zig.Type do
|> __MODULE__.from_json(module)
|> Map.replace!(:mutable, true)

%{"type" => "pointer", "child" => child = %{"type" => "unusable:anyopaque"}} ->
%{"type" => "pointer", "child" => %{"type" => "unusable:anyopaque"}} ->
:anyopaque_pointer

%{"type" => "manypointer"} ->
Expand Down Expand Up @@ -206,6 +206,18 @@ defprotocol Zig.Type do

%{"type" => "term"} ->
:term

%{"type" => "e.ErlNifBinary"} ->
:erl_nif_binary

%{"type" => "e.ErlNifEvent"} ->
:erl_nif_event

%{"type" => "pointer", "child" => %{"type" => "e.ErlNifBinary"}} ->
:erl_nif_binary_pointer

%{"type" => "pointer", "child" => %{"type" => "builtin.StackTrace"}} ->
:stacktrace
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/zigler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule :zigler do
|> elem(0)

opts =
case Enum.find(ast, &match?({:attribute, _, :zig_opts, _}, &1)) do
case Enum.find(ast, &match?({:attribute, _, :zig_opts, _}, &1)) do
nil ->
raise "No zig opts found"

Expand All @@ -78,6 +78,7 @@ defmodule :zigler do
mod_line: line,
render: :render_erlang
)
|> Options.erlang_normalize!()
|> Options.normalize!()
end

Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ defmodule Zigler.MixProject do
{:jason, "~> 1.4"},
# zig parser is pinned to a version of zig parser because versions of zig parser
# are pinned to zig versions
{:zig_parser, "~> 0.2.2"},
{:zig_parser, "~> 0.3.1"},
# documentation
{:ex_doc, "~> 0.30.0", only: :dev, runtime: false}
# {:zig_doc, "~> 0.1.3", only: :dev, runtime: false}
{:ex_doc, "~> 0.30.0", only: :dev, runtime: false},
{:zig_doc, path: "../zig_doc", only: :dev, runtime: false}
]
end
end
10 changes: 5 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
%{
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"},
"credo": {:hex, :credo, "1.7.1", "6e26bbcc9e22eefbff7e43188e69924e78818e2fe6282487d0703652bc20fd62", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e9871c6095a4c0381c89b6aa98bc6260a8ba6addccf7f6a53da8849c748a58a2"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
"earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"},
"ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"},
"earmark_parser": {:hex, :earmark_parser, "1.4.37", "2ad73550e27c8946648b06905a57e4d454e4d7229c2dafa72a0348c99d8be5f7", [:mix], [], "hexpm", "6b19783f2802f039806f375610faa22da130b8edc21209d0bff47918bb48360e"},
"ex_doc": {:hex, :ex_doc, "0.30.9", "d691453495c47434c0f2052b08dd91cc32bc4e1a218f86884563448ee2502dd2", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d7aaaf21e95dc5cddabf89063327e96867d00013963eadf2c6ad135506a8bc10"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [: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", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"pegasus": {:hex, :pegasus, "0.2.4", "3d8d5a2c89552face9c7ca14f959cc6c6d2cd645db1df85940db4c77c3b21a24", [:mix], [{:nimble_parsec, "~> 1.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "2d21e2b6b946fe3cd441544bf9856e7772f29050332f0255e166a13cdbe65bb4"},
"zig_doc": {:hex, :zig_doc, "0.1.3", "5e50c82c93051ef7d538aae830f8e3a442b4b376f020169c5979031e4545b15f", [:mix], [{:ex_doc, "~> 0.30.0", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:zig_parser, "0.1.8", [hex: :zig_parser, repo: "hexpm", optional: false]}], "hexpm", "8a342804c26f37cbb631e02846a2972758ea3892f74c91a0e78c1047ca135ddf"},
"zig_parser": {:hex, :zig_parser, "0.2.2", "a633c4209f77d50c6092d14de74fde6f30e0c5ee2d67172425a7a453650578ad", [:mix], [{:pegasus, "~> 0.2.4", [hex: :pegasus, repo: "hexpm", optional: false]}], "hexpm", "d2eda4b6cb387e4799a07b49357a37404ff2507faaec19308e50d08b90456f54"},
"zig_doc": {:hex, :zig_doc, "0.3.0", "41b83ff0d1a75c052218db366ced499aef46423edfe3c40a1f9d12640c6a44f1", [:mix], [{:ex_doc, "~> 0.30.9", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:zig_parser, "~> 0.3.0", [hex: :zig_parser, repo: "hexpm", optional: false]}], "hexpm", "f62e8151fa87362dfc12f9450dd556b053271b6ada4861487db81a2bd596f024"},
"zig_parser": {:hex, :zig_parser, "0.3.0", "2a597ae6990447e70e46691d9ca7073afb3c3e13ab143dce1df0865a764c48f4", [:mix], [{:pegasus, "~> 0.2.4", [hex: :pegasus, repo: "hexpm", optional: false]}], "hexpm", "6b90ee53cf23e53824dbdad2d2ca597a1cb8d882b0748638921bddda1563a736"},
}
3 changes: 2 additions & 1 deletion priv/beam/beam.zig
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ pub const payload = @import("payload.zig");
/// - `error_info`: pointer to a [`term`](#term) that can be populated with error
/// information that gets propagated on failure to convert. If omitted, the code
/// to produce these errors will get optimized out.

pub const get = get_.get;

/// <!-- topic: Term Management; args: _, value, options -->
Expand Down Expand Up @@ -917,7 +918,7 @@ pub const term_to_binary = binaries.term_to_binary;

/// <!-- topic: Term Management; args: _, string -->
///
/// converts a `[]u8` to a `t:term/0`. The string must be encoded using erlang term format.
/// converts a `[]u8` to a `t:term/0`. The binary must be encoded using erlang term format.
///
/// This is a thin wrapper over [`e.enif_binary_to_term`](https://www.erlang.org/doc/man/erl_nif.html#enif_binary_to_term).
pub const binary_to_term = binaries.binary_to_term;
Expand Down
Loading

0 comments on commit 5ab51c5

Please sign in to comment.