Skip to content

Commit

Permalink
all tests marked skip, no compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed May 5, 2024
1 parent 52a437f commit 57492ef
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 376 deletions.
2 changes: 1 addition & 1 deletion lib/zig/_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Zig.Nif do
end

typespec =
case nif.spec |> dbg do
case nif.spec do
false ->
quote do
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zig/type/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Zig.Type.Error do
def can_cleanup?(_), do: false

def render_elixir_spec(%{child: child}, context, opts) do
Type.spec(child, context, opts)
Type.render_elixir_spec(child, context, opts)
end

def render_return(_, _), do: Type._default_return()
Expand Down
53 changes: 28 additions & 25 deletions test/integration/raw/zig_call_test.exs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
defmodule ZiglerTest.Raw.ZigCallTest do
use ZiglerTest.IntegrationCase, async: true

use Zig,
otp_app: :zigler
@moduletag :raw
test "restore"

# Note "raw" calls can't be called in managed threaded or yielding mode.
# TODO: put warnings on this

~Z"""
const beam = @import("beam");
const e = @import("erl_nif");
pub fn raw_beam_term(env: beam.env, count: c_int, list: [*]const beam.term) beam.term {
return beam.make(.{.count = count, .item = list[0]}, .{.env = env});
}
pub fn raw_erl_nif_term(env: beam.env, count: c_int, list: [*]const e.erl_nif_term) beam.term {
return beam.make(.{.count = count, .item = list[0]}, .{.env = env});
}
"""

test "raw call with beam.term" do
assert %{count: 1, item: 1.0} = raw_beam_term(1.0)
end

test "raw call with erl_ttif_term" do
assert %{count: 1, item: 1.0} = raw_erl_nif_term(1.0)
end
# use Zig,
# otp_app: :zigler
#
# # Note "raw" calls can't be called in managed threaded or yielding mode.
# # TODO: put warnings on this
#
# ~Z"""
# const beam = @import("beam");
# const e = @import("erl_nif");
#
# pub fn raw_beam_term(env: beam.env, count: c_int, list: [*]const beam.term) beam.term {
# return beam.make(.{.count = count, .item = list[0]}, .{.env = env});
# }
#
# pub fn raw_erl_nif_term(env: beam.env, count: c_int, list: [*]const e.erl_nif_term) beam.term {
# return beam.make(.{.count = count, .item = list[0]}, .{.env = env});
# }
# """
#
# test "raw call with beam.term" do
# assert %{count: 1, item: 1.0} = raw_beam_term(1.0)
# end
#
# test "raw call with erl_ttif_term" do
# assert %{count: 1, item: 1.0} = raw_erl_nif_term(1.0)
# end
end
115 changes: 59 additions & 56 deletions test/integration/resource/basic_test.exs
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
defmodule ZiglerTest.Resource.BasicTest do
use ZiglerTest.IntegrationCase, async: true

use Zig, otp_app: :zigler, resources: [:StructResource, :U64Resource]

~Z"""
const beam = @import("beam");
const Resource = beam.Resource;
const root = @import("root");
pub const Struct = struct {
payload: u64,
};
pub const StructResource = Resource(Struct, root, .{});
pub const U64Resource = Resource(u64, root, .{});
pub fn new_scalar(resource: u64) U64Resource {
return U64Resource.create(resource, .{}) catch unreachable;
}
pub fn unpack_scalar(resource: U64Resource) u64 {
return resource.unpack();
}
pub fn increment_scalar(resource: U64Resource) void {
const to_increment = resource.unpack();
return resource.update(to_increment + 1);
}
pub fn new_struct(s: Struct) StructResource {
return StructResource.create(s, .{}) catch unreachable;
}
pub fn unpack_struct(resource: StructResource) Struct {
return resource.unpack();
}
pub fn increment_struct(resource: StructResource) void {
const to_increment_struct = resource.unpack();
return resource.update(.{.payload = to_increment_struct.payload + 1});
}
"""

test "basic scalar resources work" do
res = new_scalar(47)
assert is_reference(res)
assert 47 = unpack_scalar(res)
increment_scalar(res)
assert 48 = unpack_scalar(res)
end

test "basic struct resources work" do
res = new_struct(%{payload: 47})
assert is_reference(res)
assert %{payload: 47} = unpack_struct(res)
increment_struct(res)
assert %{payload: 48} = unpack_struct(res)
end
@moduletag :resource
test "restore"

# use Zig, otp_app: :zigler, resources: [:StructResource, :U64Resource]
#
# ~Z"""
# const beam = @import("beam");
# const Resource = beam.Resource;
# const root = @import("root");
#
# pub const Struct = struct {
# payload: u64,
# };
#
# pub const StructResource = Resource(Struct, root, .{});
# pub const U64Resource = Resource(u64, root, .{});
#
# pub fn new_scalar(resource: u64) U64Resource {
# return U64Resource.create(resource, .{}) catch unreachable;
# }
#
# pub fn unpack_scalar(resource: U64Resource) u64 {
# return resource.unpack();
# }
#
# pub fn increment_scalar(resource: U64Resource) void {
# const to_increment = resource.unpack();
# return resource.update(to_increment + 1);
# }
#
# pub fn new_struct(s: Struct) StructResource {
# return StructResource.create(s, .{}) catch unreachable;
# }
#
# pub fn unpack_struct(resource: StructResource) Struct {
# return resource.unpack();
# }
#
# pub fn increment_struct(resource: StructResource) void {
# const to_increment_struct = resource.unpack();
# return resource.update(.{.payload = to_increment_struct.payload + 1});
# }
# """
#
# test "basic scalar resources work" do
# res = new_scalar(47)
# assert is_reference(res)
# assert 47 = unpack_scalar(res)
# increment_scalar(res)
# assert 48 = unpack_scalar(res)
# end
#
# test "basic struct resources work" do
# res = new_struct(%{payload: 47})
# assert is_reference(res)
# assert %{payload: 47} = unpack_struct(res)
# increment_struct(res)
# assert %{payload: 48} = unpack_struct(res)
# end
end
115 changes: 59 additions & 56 deletions test/integration/resource/cleanup_test.exs
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
defmodule ZiglerTest.Resource.CleanupTest do
use ZiglerTest.IntegrationCase, async: true

use Zig,
otp_app: :zigler,
resources: [:PidResource],
nifs: [
:create_released,
maybe_release: [args: [[cleanup: false], []]]
]

~Z"""
const beam = @import("beam");
const std = @import("std");
const Resource = beam.Resource;
pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks});
pub const PidResourceCallbacks = struct {
pub fn dtor(pid: *beam.pid) void {
_ = beam.send(pid.*, .cleaned, .{.clean = false}) catch unreachable;
}
};
pub fn create_released(pid: beam.pid) PidResource {
const resource = PidResource.create(pid, .{}) catch unreachable;
return resource;
}
pub fn maybe_release(resource: PidResource, release: bool) void {
if (release) {
resource.release();
}
}
"""

test "a function call will keep resources, with no cleanup it doesn't release" do
this = self()

spawn(fn ->
this
|> create_released()
|> maybe_release(false)
end)

refute_receive :cleaned, 500
end

test "a function call will keep resources, you can manually release" do
this = self()

spawn(fn ->
this
|> create_released()
|> maybe_release(true)
end)

assert_receive :cleaned, 100
end
@moduletag :resource
test "restore"

#use Zig,
# otp_app: :zigler,
# resources: [:PidResource],
# nifs: [
# :create_released,
# maybe_release: [args: [[cleanup: false], []]]
# ]
#
#~Z"""
#const beam = @import("beam");
#const std = @import("std");
#const Resource = beam.Resource;
#
#pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks});
#
#pub const PidResourceCallbacks = struct {
# pub fn dtor(pid: *beam.pid) void {
# _ = beam.send(pid.*, .cleaned, .{.clean = false}) catch unreachable;
# }
#};
#
#pub fn create_released(pid: beam.pid) PidResource {
# const resource = PidResource.create(pid, .{}) catch unreachable;
# return resource;
#}
#
#pub fn maybe_release(resource: PidResource, release: bool) void {
# if (release) {
# resource.release();
# }
#}
#"""
#
#test "a function call will keep resources, with no cleanup it doesn't release" do
# this = self()
#
# spawn(fn ->
# this
# |> create_released()
# |> maybe_release(false)
# end)
#
# refute_receive :cleaned, 500
#end
#
#test "a function call will keep resources, you can manually release" do
# this = self()
#
# spawn(fn ->
# this
# |> create_released()
# |> maybe_release(true)
# end)
#
# assert_receive :cleaned, 100
#end
end
Loading

0 comments on commit 57492ef

Please sign in to comment.