Skip to content

Commit 3ff5e2f

Browse files
committed
Various small fixes
Allows our project to build with the upgraded zigler
1 parent 642217f commit 3ff5e2f

File tree

13 files changed

+182
-196
lines changed

13 files changed

+182
-196
lines changed

guides/01-nifs.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ end
152152

153153
## Send
154154

155-
An important mechanism for exporting values out of a NIF is to send it to a process
156-
ID using the `send` function. Zigler provides an advanced `beam.send` function which
157-
will perform this operation for you.
155+
An important mechanism for exporting values out of a NIF is to send it to a process ID using the
156+
`send` function. Zigler provides an advanced `beam.send` function which will perform this operation
157+
for you.
158158

159159
```elixir
160160
~Z"""
@@ -174,8 +174,8 @@ function call.
174174

175175
## Optional values
176176

177-
You may use optional values as both input and output terms. Note that the empty optional
178-
value in elixir is `nil` and the empty optional value in zig is `null`.
177+
You may use optional values as both input and output terms. Note that the empty optional value in
178+
elixir is `nil` and the empty optional value in zig is `null`.
179179

180180
```elixir
181181
~Z"""
@@ -198,8 +198,8 @@ end
198198

199199
## Error Returns
200200

201-
You may use functions which return an error, in which case an `ErlangException` will be
202-
thrown with the value being the atom representing the error.
201+
You may use functions which return an error, in which case an `ErlangException` will be thrown with
202+
the value being the atom representing the error.
203203

204204
```elixir
205205
~Z"""
@@ -223,4 +223,5 @@ end
223223
This function *does not fail*.
224224
- for more information on the final options parameter of `get` and `make`, see their respective
225225
documentation.
226-
- zigler will translate the hoisted marshalling failures into detailed BEAM exceptions of type `ArgumentError`.
226+
- zigler will translate the hoisted marshalling failures into detailed BEAM exceptions of type
227+
`ArgumentError`.

guides/03-allocators.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ top* of those allocators.
88

99
## Basic Allocator
1010

11-
The first allocator is `allocator`. This allocator wraps the [nif allocator](https://www.erlang.org/doc/man/erl_nif.html#enif_alloc)
12-
provided by the BEAM in the zig allocator interface. You should generally use this allocator over
13-
`malloc` because it often saves a syscall by using existing preallocated memory pools, because it
14-
allows the VM to track how much memory your NIF is using, and possibly gives better memory placement
15-
to avoid cache misses in your execution thread.
11+
The first allocator is `allocator`. This allocator wraps the [nif
12+
allocator](https://www.erlang.org/doc/man/erl_nif.html#enif_alloc) provided by the BEAM in the zig
13+
allocator interface. You should generally use this allocator over `malloc` because it often saves a
14+
syscall by using existing preallocated memory pools, because it allows the VM to track how much
15+
memory your NIF is using, and possibly gives better memory placement to avoid cache misses in your
16+
execution thread.
1617

1718
```elixir
1819
~Z"""
@@ -94,13 +95,13 @@ end
9495

9596
## Wide Alignment Allocator
9697

97-
Zigler provides a `wide_alignment_allocator` which allows you to allocate memory ranges that have a
98+
Zigler provides a `wide_alignment_allocator` which allows you to allocate memory ranges that have a
9899
higher alignment than the maximum alignment for builtin types.
99100

100-
> ### memory penalty {: .warning }
101+
> ### memory penalty {: .warning}
101102
>
102-
> Note that using this allocator comes with a memory penalty, so use as a general allocator
103-
> is not recommended.
103+
> Note that using this allocator comes with a memory penalty, so use as a general allocator is not
104+
> recommended.
104105
105106
```elixir
106107
~Z"""
@@ -125,8 +126,9 @@ memory layouts for mixed allocation sizes and the ability to track memory leaks.
125126

126127
The state of the global general purpose allocator is accessible using `beam.allocator_.general_purpose_allocator_instance`
127128

128-
You may also create a custom general purpose allocator instance using `beam.make_general_purpose_allocator_instance`,
129-
whcih is what happens on a per-nif basis if the nif is checking for leaks.
129+
You may also create a custom general purpose allocator instance using
130+
`beam.make_general_purpose_allocator_instance`, whcih is what happens on a per-nif basis if the nif
131+
is checking for leaks.
130132

131133
```elixir
132134
~Z"""

guides/04-nif_options.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ end
183183

184184
## Typespec override
185185

186-
Typespecs generation can be suppressed by setting `spec: false`.
187-
If you want typespecs for such functions, specify using `@spec` elsewhere
188-
in your module.
186+
Typespecs generation can be suppressed by setting `spec: false`. If you want typespecs for such
187+
functions, specify using `@spec` elsewhere in your module.
189188

190189
For example:
191190

@@ -222,5 +221,3 @@ defmodule DisableDoc do
222221
end
223222
#module
224223
```
225-
226-

guides/08-module_options.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ end
3535

3636
## attributes
3737

38-
Attributes from your module can be used as compile-time constants communicated
39-
from elixir. All attributes of the following types will be automatically
40-
available through the `attributes` module import:
38+
Attributes from your module can be used as compile-time constants communicated from elixir. All
39+
attributes of the following types will be automatically available through the `attributes` module
40+
import:
4141

4242
- `integer` (as `comptime int` values)
4343
- `float` (as `comptime float` values)
@@ -105,9 +105,8 @@ end
105105

106106
## dump options
107107

108-
Zigler lets you dump various compile-time assets to the console for
109-
debugging purposes, which can be enabled by setting any given one
110-
of the following options to `true`:
108+
Zigler lets you dump various compile-time assets to the console for debugging purposes, which can be
109+
enabled by setting any given one of the following options to `true`:
111110

112111
- `dump`: dumps the rendered elixir code generated by `use Zig`.
113112
- `dump_sema`: dumps the json data emitted by the semantic analysis pass.

guides/09-raw_nifs.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Raw nifs
22

3-
All of the nifs shown to this point involve zigler constructing an adapter function with
4-
term marshalling automatically generated. It is also possible to run a nif without
5-
doing any of those steps.
3+
All of the nifs shown to this point involve zigler constructing an adapter function with term
4+
marshalling automatically generated. It is also possible to run a nif without doing any of those
5+
steps.
66

77
The normal (C) header for a BEAM nif is as follows:
88

@@ -19,7 +19,7 @@ The following two zig function signatures are interpreted as raw nifs:
1919

2020
## nif options setup
2121

22-
A raw nif MUST contain the `arity` option. This can be one of:
22+
A raw nif MUST contain the `arity` option. This can be one of:
2323

2424
- a single integer, representing the desired arity of the nif function
2525
- a single range, representing a range of arities for the nif function

guides/10-callbacks.md

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,65 @@
11
# Module Callbacks
22

3-
The BEAM provides for a module to have several event-callbacks that are fired
4-
when a module is created. Zigler labels these callbacks in the following way:
3+
The BEAM provides for a module to have several event-callbacks that are fired when a module is
4+
created. Zigler labels these callbacks in the following way:
55

66
- [`on_load`](#on_load) - when the module is being loaded
77
- [`on_upgrade`](#on_upgrade) - when the module is being loaded to replace another module
88
- [`on_unload`](#on_unload) - when the module is being purged
99

10-
These labeled are passed as options to the `use Zig` directive with the
11-
name of the function to be used as the callback. All three are optional.
12-
You may use just the atom to stand a function with the same name, e.g:
13-
`callbacks: [:on_load]` is shorthand for `callbacks: [on_load: :on_load]`
10+
These labeled are passed as options to the `use Zig` directive with the name of the function to be
11+
used as the callback. All three are optional. You may use just the atom to stand a function with the
12+
same name, e.g: `callbacks: [:on_load]` is shorthand for `callbacks: [on_load: :on_load]`
1413

15-
> ### Callbacks are pub {: .info }
14+
> ### Callbacks are pub {: .info}
1615
>
17-
> Be sure to make your callback functions `pub`. You do *not* need to
18-
> add them to the module's `ignore` option.
16+
> Be sure to make your callback functions `pub`. You do *not* need to add them to the module's
17+
> `ignore` option.
1918
20-
> ### Context in callbacks {: .info }
21-
>
19+
> ### Context in callbacks {: .info}
20+
>
2221
> For all callbacks, the context is set as follows:
23-
>
24-
> - `env`: the `e.ErlNifEnv` value passed to the callback function;
25-
> - `mode`: `.callback`;
26-
> - `allocator`: `beam.allocator`;
27-
>
28-
> Thus you should be able to use `beam.get`, `beam.make`, or `beam.send`
29-
> with the appropriate context set without extra options.
22+
- `env`: the `e.ErlNifEnv` value passed to the callback function;
23+
- `mode`: `.callback`;
24+
- `allocator`: `beam.allocator`;
25+
>
26+
> Thus you should be able to use `beam.get`, `beam.make`, or `beam.send` with the appropriate context
27+
> set without extra options.
3028
3129
## on_load
3230

3331
The on_load callback may have one of the following function signatures:
3432

3533
- `fn (?*?*T, U) void`: if the on_load function can never fail.
36-
- `fn (?*?*T, U) !void`: if the on_load function can fail with an error.
37-
The module load integer will reflect the integer value of the error.
38-
> #### Zig error integers {: .warning }
39-
>
40-
> Note that the integer representation of a zig error may change between
41-
> compilations. Translating this integer back to a meaningful value may
42-
> be challenging.
43-
- `fn (?*?*T, U) int`: the on_load function will be considered to fail if
44-
the integer value is not `0`.
45-
- `fn (?*?*T, U) E`: for an `enum` type `E`, the on_load function will be
46-
considered to fail if the enum value is not zero. It's recommended that
47-
the enum type `E` be defined as so: `const E = enum{ ok = 0, ...};`
48-
- `fn (beam.env, ?*?*anyopaque, e.ErlNifTerm) c_int`: this is a
49-
"raw" call that corresponds to the expected signature of a `upgrade` callback.
34+
- `fn (?*?*T, U) !void`: if the on_load function can fail with an error. The module load integer will
35+
reflect the integer value of the error.
36+
-
37+
> #### Zig error integers {: .warning}
38+
>
39+
> Note that the integer representation of a zig error may change between compilations. Translating
40+
> this integer back to a meaningful value may be challenging.
41+
- `fn (?*?*T, U) int`: the on_load function will be considered to fail if the integer value is not
42+
`0`.
43+
- `fn (?*?*T, U) E`: for an `enum` type `E`, the on_load function will be considered to fail if the
44+
enum value is not zero. It's recommended that the enum type `E` be defined as so: `const E = enum{ ok = 0, ...};`
45+
- `fn (beam.env, ?*?*anyopaque, e.ErlNifTerm) c_int`: this is a "raw" call that corresponds to the
46+
expected signature of a `upgrade` callback.
5047

5148
### on_load Callback types
5249

53-
`T` may be any type at all, which is considered the *private data* of
54-
the module. Any information may be stored in a `*T`, and the double
55-
pointer is passed to the callback for the pointer to be communicated
56-
to the VM. The `*T` supplied will be accessible via the `enif_priv_data`
57-
function
50+
`T` may be any type at all, which is considered the *private data* of the module. Any information
51+
may be stored in a `*T`, and the double pointer is passed to the callback for the pointer to be
52+
communicated to the VM. The `*T` supplied will be accessible via the `enif_priv_data` function
5853

59-
`U` must be a type that can be passed as the first argument of `beam.make`,
60-
and the value of `U` will be set by the result of an `__on_load__/0` function
61-
defined in the module body. The result of this function call will be passed
62-
to the `on_load`
54+
`U` must be a type that can be passed as the first argument of `beam.make`, and the value of `U`
55+
will be set by the result of an `__on_load__/0` function defined in the module body. The result of
56+
this function call will be passed to the `on_load`
6357

64-
> ### on_load and resources {: .info }
58+
> ### on_load and resources {: .info}
6559
>
66-
> The beam nif guide says that resources must be initialized in the `load`
67-
> callback. The `on_load` callback must NOT initialize resources. This
68-
> is performed in a function that will wrap your `on_load` callback.
60+
> The beam nif guide says that resources must be initialized in the `load` callback. The `on_load`
61+
> callback must NOT initialize resources. This is performed in a function that will wrap your
62+
> `on_load` callback.
6963
7064
#### Example
7165

@@ -105,44 +99,41 @@ end
10599
The on_upgrade callback may have one of the following function signatures:
106100

107101
- `fn (?*?*T, ?*?*U, V) void`: if the on_load function can never fail.
108-
- `fn (?*?*T, ?*?*U, V) !void`: if the on_load function can fail with an error.
109-
The module load integer will reflect the integer value of the error.
110-
> #### Zig error integers {: .warning }
111-
>
112-
> Note that the integer representation of a zig error may change between
113-
> compilations. Translating this integer back to a meaningful value may
114-
> be challenging.
115-
- `fn (?*?*T, ?*?*U, V) int`: the on_load function will be considered to fail if
116-
the integer value is not `0`.
117-
- `fn (?*?*T, ?*?*U, V) E`: for an `enum` type `E`, the on_load function will be
118-
considered to fail if the enum value is not zero. It's recommended that
119-
the enum type `E` be defined as so: `const E = enum{ ok = 0, ...};`
120-
- `fn (beam.env, ?*?*anyopaque, ?*?*anyopaque, e.ErlNifTerm) c_int`: this is a
121-
"raw" call that corresponds to the expected signature of a `upgrade` callback.
102+
- `fn (?*?*T, ?*?*U, V) !void`: if the on_load function can fail with an error. The module load
103+
integer will reflect the integer value of the error.
104+
-
105+
> #### Zig error integers {: .warning}
106+
>
107+
> Note that the integer representation of a zig error may change between compilations. Translating
108+
> this integer back to a meaningful value may be challenging.
109+
- `fn (?*?*T, ?*?*U, V) int`: the on_load function will be considered to fail if the integer value is
110+
not `0`.
111+
- `fn (?*?*T, ?*?*U, V) E`: for an `enum` type `E`, the on_load function will be considered to fail if
112+
the enum value is not zero. It's recommended that the enum type `E` be defined as so: `const E = enum{ ok = 0, ...};`
113+
- `fn (beam.env, ?*?*anyopaque, ?*?*anyopaque, e.ErlNifTerm) c_int`: this is a "raw" call that
114+
corresponds to the expected signature of a `upgrade` callback.
122115

123116
### on_upgrade Callback types
124117

125-
`T` and `U` may be any type at all, which is considered the *private data* of
126-
the module. Any information may be stored in these pointers, and the double
127-
pointer is passed to the callback for the pointer to be communicated
128-
to the VM. The `*U` data supplied will be accessible via the `enif_priv_data`
129-
function
118+
`T` and `U` may be any type at all, which is considered the *private data* of the module. Any
119+
information may be stored in these pointers, and the double pointer is passed to the callback for
120+
the pointer to be communicated to the VM. The `*U` data supplied will be accessible via the
121+
`enif_priv_data` function
130122

131-
`V` must be a type that can be passed as the first argument of `beam.make`,
132-
and the value of `V` will be set by the result of an `__on_load__/0` function
133-
defined in the module body. The result of this function call will be passed
134-
to the `on_load`
123+
`V` must be a type that can be passed as the first argument of `beam.make`, and the value of `V`
124+
will be set by the result of an `__on_load__/0` function defined in the module body. The result of
125+
this function call will be passed to the `on_load`
135126

136-
> ### on_upgrade and resources {: .info }
127+
> ### on_upgrade and resources {: .info}
137128
>
138-
> The beam nif guide says that resources must be initialized in the `upgrade`
139-
> callback. The `on_upgrade` callback must NOT initialize resources. This
140-
> is performed in a function that will wrap your `on_upgrade` callback.
129+
> The beam nif guide says that resources must be initialized in the `upgrade` callback. The
130+
> `on_upgrade` callback must NOT initialize resources. This is performed in a function that will wrap
131+
> your `on_upgrade` callback.
141132
142133
## on_unload
143134

144135
The on_unload callback may have one of the following function signatures:
145136

146137
- `fn (?*T) void`: if you would like your inbound private data to be typed.
147-
- `fn (beam.env, ?*anyopaque) void`: this is a "raw" call that corresponds
148-
to the expected signature of a `upgrade` callback.
138+
- `fn (beam.env, ?*anyopaque) void`: this is a "raw" call that corresponds to the expected signature
139+
of a `upgrade` callback.

lib/zig/_c.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ defmodule Zig.C do
5151
defp solve_relative({:system, _} = system, _), do: system
5252

5353
defp solve_relative(file, module_file) do
54-
module_file
55-
|> Path.dirname()
56-
|> Path.join(file)
57-
|> Path.expand()
54+
Path.expand(file, module_file)
5855
end
5956

6057
defp normalize_src(file, module_file) when is_binary(file) do

lib/zig/compiler.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ defmodule Zig.Compiler do
137137
# from the :zigler entrypoint for erlang parse transforms, as well as the
138138
# __before_compile__ entrypoint for Elixir
139139
def compile(zig_code, code_dir, opts) do
140-
zig_code_path = code_dir
141-
|> Path.join(".#{opts.module}.zig")
142-
|> Path.expand()
140+
zig_code_path =
141+
code_dir
142+
|> Path.join(".#{opts.module}.zig")
143+
|> Path.expand()
143144

144145
opts
145146
|> Map.replace!(:zig_code_path, zig_code_path)

lib/zig/templates/build.zig.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn build(b: *std.Build) void {
7474
<% {:system, lib} -> %>
7575
nif.linkSystemLibrary("<%= lib %>", .{});
7676
<% lib -> %>
77-
nif.addObjectFile("<%= lib %>");
77+
nif.addObjectFile(.{.cwd_relative = "<%= lib %>"});
7878
<% end %>
7979
<% end %>
8080
<% end %>

lib/zig/type/enum.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ defmodule Zig.Type.Enum do
8282

8383
defp accumulate(number, []), do: [number]
8484
defp accumulate(number, [succ | rest]) when succ == number + 1, do: [number..succ | rest]
85-
defp accumulate(number, [succ..last//1 | rest]) when succ == number + 1, do: [number..last | rest]
85+
86+
defp accumulate(number, [succ..last//1 | rest]) when succ == number + 1,
87+
do: [number..last | rest]
88+
8689
defp accumulate(number, noncontiguous), do: [number | noncontiguous]
8790

8891
defp unionize(content) do

0 commit comments

Comments
 (0)