Skip to content

Commit

Permalink
working for first set of types
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Apr 22, 2024
1 parent 5948731 commit 6303cee
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 376 deletions.
26 changes: 14 additions & 12 deletions lib/zig/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@ defmodule Zig.Command do
# libc locations for statically linking it.
System.delete_env("CC")

sema_command = "run #{sema_file} #{deps} #{mods} -lc #{link_opts(module)}"

s = sema_command(
sema: sema_file,
mods: [
erl_nif: %{path: erl_nif_file},
beam: %{deps: [:erl_nif], path: beam_file},
analyte: %{deps: [:beam, :erl_nif], path: file}
])
|> IO.iodata_to_binary
|> String.split
sema_command = "run #{sema_file} #{deps} #{mods} -lc #{link_opts(module)}"

Check warning on line 95 in lib/zig/command.ex

View workflow job for this annotation

GitHub Actions / Linux OTP 25.0 / Elixir 1.13.4

variable "sema_command" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 95 in lib/zig/command.ex

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.14.5

variable "sema_command" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 95 in lib/zig/command.ex

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.15.2

variable "sema_command" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 95 in lib/zig/command.ex

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.15.2

variable "sema_command" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 95 in lib/zig/command.ex

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.15.2

variable "sema_command" is unused (if the variable is not meant to be used, prefix it with an underscore)

s =
sema_command(
sema: sema_file,
mods: [
erl_nif: %{path: erl_nif_file},
beam: %{deps: [:erl_nif], path: beam_file},
analyte: %{deps: [:beam, :erl_nif], path: file}
]
)
|> IO.iodata_to_binary()
|> String.split()
|> Enum.join(" ")

run_zig(s, stderr_to_stdout: true)
Expand Down Expand Up @@ -180,7 +182,7 @@ defmodule Zig.Command do

true ->
raise CompileError, description: "zig executable not found"
end
end
end

defp find_from_env do
Expand Down
1 change: 1 addition & 0 deletions lib/zig/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule Zig.Compiler do
# into `{:auto, <other_options>}`. This function will reverse the list, but
# since order doesn't matter for this option, it is okay.
defp replace_nif_dots({:auto, _} = auto), do: auto

defp replace_nif_dots(opts) do
Enum.reduce(opts, [], fn
{:..., _, _}, {:auto, list} -> {:auto, list}
Expand Down
12 changes: 7 additions & 5 deletions lib/zig/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ defmodule Zig.Nif do
# }
end

#defp extract_raw(raw_opt, %{return: return}) do
# defp extract_raw(raw_opt, %{return: return}) do
# case {raw_opt, return} do
# {nil, _} -> nil
# {{:c, arity}, _} when is_integer(arity) -> :c
# {arity, :term} when is_integer(arity) -> :beam
# {arity, :erl_nif_term} when is_integer(arity) -> :erl_nif
# end
#end
# end

def render_elixir(%{concurrency: concurrency} = nif) do
doc =
Expand All @@ -92,10 +92,12 @@ defmodule Zig.Nif do
end

_ ->
quote do end
#quote do
quote do
end

# quote do
# @spec unquote(Function.render_elixir_spec(nif.spec, nif.name))
#end
# end
end

functions = concurrency.render_elixir(nif)
Expand Down
12 changes: 6 additions & 6 deletions lib/zig/nif/_basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Zig.Nif.Basic do
|> List.flatten()
end

defp marshal_name(nif), do: :"marshalled-#{nif.type.name}"
defp marshal_name(nif), do: :"marshalled-#{nif.name}"

def entrypoint(nif) do
if needs_marshal?(nif), do: marshal_name(nif), else: nif.name
Expand Down Expand Up @@ -68,7 +68,7 @@ defmodule Zig.Nif.Basic do
end

defp render_elixir_marshalled(
%{type: type} = nif,
%{signature: signature} = nif,
def_or_defp,
empty_params,
used_params,
Expand All @@ -82,7 +82,7 @@ defmodule Zig.Nif.Basic do
|> Enum.flat_map(&apply(ErrorProng, &1, [:elixir, []]))

marshal_params =
type.params
signature.params
|> Enum.zip(used_params)
|> Enum.with_index()
|> Enum.flat_map(fn {{param_type, param}, index} ->
Expand All @@ -99,14 +99,14 @@ defmodule Zig.Nif.Basic do
end

marshal_return =
if Type.marshals_return?(type.return) do
Type.marshal_return(type.return, return, :elixir)
if Type.marshals_return?(signature.return) do
Type.marshal_return(signature.return, return, :elixir)
else
return
end

quote do
unquote(def_or_defp)(unquote(type.name)(unquote_splicing(used_params))) do
unquote(def_or_defp)(unquote(nif.name)(unquote_splicing(used_params))) do
unquote_splicing(marshal_params)
return = unquote(marshal_name)(unquote_splicing(used_params))
unquote(marshal_return)
Expand Down
7 changes: 4 additions & 3 deletions lib/zigler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ defmodule :zigler do
mod_line: line,
render: :render_erlang
)
#|> Options.erlang_normalize!()
#|> Options.normalize!()

# |> Options.erlang_normalize!()
# |> Options.normalize!()
end

otp_app =
Expand All @@ -99,7 +100,7 @@ defmodule :zigler do
|> Path.dirname()
|> Path.absname()

#module =
# module =
# case Enum.find(ast, &match?({:attribute, _, :module, _}, &1)) do
# nil -> raise "No module definition found"
# {:attribute, _, :module, module} -> module
Expand Down
6 changes: 3 additions & 3 deletions priv/beam/get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn get_int(comptime T: type, src: beam.term, opts: anytype) GetError!T {

var buf: Bigger = 0;
const buf_ptr: [*]u8 = @ptrCast(&buf);
std.mem.copy(u8, buf_ptr[0..bytes], result.data[0..bytes]);
@memcpy(buf_ptr[0..bytes], result.data[0..bytes]);
// check to make sure that the top bits are all zeros.
const top_bit_count = (bytes * 8 - int.bits);
if (@clz(buf) < top_bit_count) return GetError.argument_error;
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn get_int(comptime T: type, src: beam.term, opts: anytype) GetError!T {
if (e.enif_inspect_binary(options.env(opts), src.v, &result) == 0) return GetError.unreachable_error;

var buf: Bigger = 0;
std.mem.copy(u8, @as([*]u8, @ptrCast(&buf))[0..bytes], result.data[0..bytes]);
@memcpy(@as([*]u8, @ptrCast(&buf))[0..bytes], result.data[0..bytes]);
// check to make sure that the top bits are all zeros.
const top_bit_count = (bytes * 8 - int.bits);
if (@clz(buf) < top_bit_count) return GetError.argument_error;
Expand Down Expand Up @@ -411,7 +411,7 @@ pub fn get_slice_binary(comptime T: type, src: beam.term, opts: anytype) !T {
return err;
};

std.mem.copy(Child, result, result_ptr[0..item_count]);
@memcpy(result, result_ptr[0..item_count]);

if (slice_info.sentinel) |sentinel| {
result[item_count] = @as(*const Child, @ptrCast(@alignCast(sentinel))).*;
Expand Down
4 changes: 2 additions & 2 deletions priv/beam/make.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn make_int(value: anytype, opts: anytype) beam.term {
var buf = e.enif_make_new_binary(options.env(opts), buf_size, &result);

// transfer content.
std.mem.copy(u8, buf[0..buf_size], @as([*]u8, @ptrCast(&intermediate))[0..buf_size]);
@memcpy(buf[0..buf_size], @as([*]u8, @ptrCast(&intermediate))[0..buf_size]);

return .{ .v = result };
},
Expand Down Expand Up @@ -373,7 +373,7 @@ fn make_binary(content: anytype, opts: anytype) beam.term {
fn make_binary_from_u8_slice(slice: []const u8, opts: anytype) beam.term {
var result: beam.term = undefined;
var buf = e.enif_make_new_binary(options.env(opts), slice.len, &result.v);
std.mem.copy(u8, buf[0..slice.len], slice);
@memcpy(buf[0..slice.len], slice);
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions test/_support/tests/documentation.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#defmodule ZiglerTest.Documentation do
# defmodule ZiglerTest.Documentation do
# @moduledoc false
# use Zig, otp_app: :zigler, nifs: [..., no_docs: [docs: false]]
#
Expand All @@ -16,5 +16,5 @@
# return term;
# }
# """
#end
#
# end
#
6 changes: 3 additions & 3 deletions test/_support/tests/override_typespec.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#defmodule ZiglerTest.OverrideTypespec do
# defmodule ZiglerTest.OverrideTypespec do
# @moduledoc false
# use Zig, otp_app: :zigler, nifs: [do_something: [spec: (integer -> integer)]]
#
Expand All @@ -9,5 +9,5 @@
# return beam.make(value + 47, .{});
# }
# """
#end
#
# end
#
6 changes: 3 additions & 3 deletions test/integration/resource/as_binary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ defmodule ZiglerTest.Resource.AsBinaryTest do
pub fn output_auto(src_string: []u8) StringResource {
const new_string = beam.allocator.alloc(u8, src_string.len) catch unreachable;
std.mem.copy(u8, new_string, src_string);
@memcpy(new_string, src_string);
return StringResource.create(new_string, .{}) catch unreachable;
}
pub const OutputModes = enum {binary, reference, static};
pub fn output_manual(src_string: []u8, mode: OutputModes) beam.term {
const new_string = beam.allocator.alloc(u8, src_string.len) catch unreachable;
std.mem.copy(u8, new_string, src_string);
@memcpy(new_string, src_string);
const resource = StringResource.create(new_string, .{}) catch unreachable;
return switch (mode) {
.binary => beam.make(resource, .{.output = .binary}),
Expand All @@ -43,7 +43,7 @@ defmodule ZiglerTest.Resource.AsBinaryTest do
pub fn output_as_binary(src_string: []u8) StringResource {
const new_string = beam.allocator.alloc(u8, src_string.len) catch unreachable;
std.mem.copy(u8, new_string, src_string);
@memcpy(new_string, src_string);
return StringResource.create(new_string, .{}) catch unreachable;
}
Expand Down
8 changes: 4 additions & 4 deletions test/integration/types/array_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ defmodule ZiglerTest.Types.ArrayTest do
pub fn fastlane_beam_term_test(passed: [3]beam.term) [3]beam.term {
var result: [3]beam.term = undefined;
for (&result, 0..) |*item, index| {
var value: f64 = beam.get(f64, passed[index], .{}) catch unreachable;
const value: f64 = beam.get(f64, passed[index], .{}) catch unreachable;
item.* = beam.make(value + 1.0, .{});
}
return result;
Expand All @@ -173,23 +173,23 @@ defmodule ZiglerTest.Types.ArrayTest do
pub fn fastlane_erl_nif_term_test(passed: [3]e.ErlNifTerm) [3]e.ErlNifTerm {
var result: [3]e.ErlNifTerm = undefined;
for (&result, 0..) |*item, index| {
var value: f64 = beam.get(f64, .{.v = passed[index]}, .{}) catch unreachable;
const value: f64 = beam.get(f64, .{.v = passed[index]}, .{}) catch unreachable;
item.* = beam.make(value + 1.0, .{}).v;
}
return result;
}
pub fn fastlane_beam_term_ptr_test(passed: *[3]beam.term) *[3]beam.term {
for (passed) |*item| {
var value: f64 = beam.get(f64, item.*, .{}) catch unreachable;
const value: f64 = beam.get(f64, item.*, .{}) catch unreachable;
item.* = beam.make(value + 1.0, .{});
}
return passed;
}
pub fn fastlane_erl_nif_term_ptr_test(passed: *[3]e.ErlNifTerm) *[3]e.ErlNifTerm {
for (passed) |*item| {
var value: f64 = beam.get(f64, .{.v = item.*}, .{}) catch unreachable;
const value: f64 = beam.get(f64, .{.v = item.*}, .{}) catch unreachable;
item.* = beam.make(value + 1.0, .{}).v;
}
return passed;
Expand Down
Loading

0 comments on commit 6303cee

Please sign in to comment.