-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for multi-arity functions in raw mode
- Loading branch information
Showing
5 changed files
with
109 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule ZiglerTest.Raw.MultiArityTest do | ||
use ZiglerTest.IntegrationCase, async: true | ||
|
||
@moduletag :raw | ||
|
||
use Zig, | ||
otp_app: :zigler, | ||
nifs: [ | ||
range_arity: [arity: 1..3], | ||
list_arity: [arity: [1, 3..4]] | ||
] | ||
|
||
~Z""" | ||
const beam = @import("beam"); | ||
const e = @import("erl_nif"); | ||
pub fn range_arity(env: beam.env, arity: c_int, params: [*]const beam.term) beam.term { | ||
return beam.make(params[0..@intCast(arity)], .{.env = env}); | ||
} | ||
pub fn list_arity(env: beam.env, arity: c_int, params: [*]const beam.term) beam.term { | ||
return beam.make(params[0..@intCast(arity)], .{.env = env}); | ||
} | ||
""" | ||
|
||
test "raw call with range arity" do | ||
for arity <- 1..3 do | ||
foos = List.duplicate(:foo, arity) | ||
assert foos == apply(__MODULE__, :range_arity, foos) | ||
end | ||
|
||
assert_raise UndefinedFunctionError, fn -> | ||
apply(__MODULE__, :range_arity, []) | ||
end | ||
|
||
assert_raise UndefinedFunctionError, fn -> | ||
apply(__MODULE__, :range_arity, [:foo, :foo, :foo, :foo]) | ||
end | ||
end | ||
|
||
test "raw call with list of arity mixtures" do | ||
for arity <- 3..4 do | ||
assert List.duplicate(:foo, arity) == apply(__MODULE__, :list_arity, List.duplicate(:foo, arity)) | ||
end | ||
|
||
assert_raise UndefinedFunctionError, fn -> | ||
apply(__MODULE__, :list_arity, []) | ||
end | ||
|
||
assert_raise UndefinedFunctionError, fn -> | ||
apply(__MODULE__, :list_arity, [:foo, :foo]) | ||
end | ||
|
||
assert_raise UndefinedFunctionError, fn -> | ||
apply(__MODULE__, :list_arity, [:foo, :foo, :foo, :foo, :foo]) | ||
end | ||
end | ||
end |