Skip to content

Commit 70bd43b

Browse files
committed
Simplify .xnif.
1 parent 9ace437 commit 70bd43b

File tree

9 files changed

+35
-35
lines changed

9 files changed

+35
-35
lines changed

nimler.nim

+19-27
Original file line numberDiff line numberDiff line change
@@ -126,36 +126,28 @@ proc genNifWrapper(nifName: NimNode, fn: NimNode): NimNode {.compileTime.} =
126126
let rbody = newTree(nnkStmtList, fn)
127127
let rcall = newCall(fn.name, ident("env"))
128128

129+
template tNifFn(n, rbody) {.dirty.} =
130+
func n(env: ptr ErlNifEnv; argc: cint; argv: ErlNifArgs): ErlNifTerm =
131+
rbody
132+
template tReturnTerm(v) =
133+
return toTerm(env, v)
134+
template tFromTerm(a, b, c) =
135+
let a = fromTerm(env, argv[b], c)
136+
template tCheckTerm(p) =
137+
if unlikely(isNone(p)):
138+
return enif_make_badarg(env)
139+
template tGetFromTerm(p) =
140+
unsafeGet(p)
141+
129142
for i in 2 ..< len(fn.params):
130143
let p = fn.params[i]
131-
let arg = newTree(nnkBracketExpr, ident("argv"), newLit(i-2))
132-
rbody.add(newTree(nnkLetSection, newTree(nnkIdentDefs,
133-
p[0],
134-
newNimNode(nnkEmpty),
135-
newCall("fromTerm", ident("env"), arg, p[1]))))
136-
rbody.add(newTree(nnkIfStmt, newTree(nnkElifBranch,
137-
newCall("unlikely", newCall("isNone", p[0])),
138-
newTree(nnkReturnStmt, newCall("enif_make_badarg", ident("env"))))))
139-
rcall.add(newCall("unsafeGet", p[0]))
140-
141-
rbody.add(newTree(nnkReturnStmt, newCall("toTerm", ident("env"), rcall)))
142-
143-
let rfn = newProc(rname, [], rbody, fn.kind)
144-
rfn.params = newTree(nnkFormalParams,
145-
ident("ErlTerm"),
146-
newTree(nnkIdentDefs,
147-
ident("env"),
148-
newNimNode(nnkPtrTy).add(ident("ErlNifEnv")),
149-
newNimNode(nnkEmpty)),
150-
newTree(nnkIdentDefs,
151-
ident("argc"),
152-
ident("cint"),
153-
newNimNode(nnkEmpty)),
154-
newTree(nnkIdentDefs,
155-
ident("argv"),
156-
ident("ErlNifArgs"),
157-
newNimNode(nnkEmpty)))
144+
rbody.add(getAst(tFromTerm(p[0], newLit(i-2), p[1])))
145+
rbody.add(getAst(tCheckTerm(p[0])))
146+
rcall.add(getAst(tGetFromTerm(p[0])))
147+
148+
rbody.add(getAst(tReturnTerm(rcall)))
158149

150+
let rfn = getAst(tNifFn(rname, rbody))
159151
rfn.pragma = copyPragmaWithout(fn.pragma, "raises")
160152
rfn.pragma.add(ident("nif"))
161153
rfn.pragma.add(newTree(nnkExprColonExpr,

tests/codec/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerCodec do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def codec_options(_, _), do: exit(:nif_library_not_loaded)
78
def codec_int(_, _), do: exit(:nif_library_not_loaded)

tests/dirty_nif/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerDirtyNif do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def dirty_cpu(), do: exit(:nif_library_not_loaded)
78
def dirty_io(), do: exit(:nif_library_not_loaded)

tests/init_api/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerInitApi do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 123)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 123)
56

67
def test(), do: exit(:nif_library_not_loaded)
78
def test_priv(), do: exit(:nif_library_not_loaded)

tests/init_resource/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerInitResource do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def new(), do: exit(:nif_library_not_loaded)
78
def set(_, _), do: exit(:nif_library_not_loaded)

tests/integration/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerIntegration do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def is_atom(_), do: exit(:nif_library_not_loaded)
78
def is_binary(_), do: exit(:nif_library_not_loaded)

tests/message/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerMessage do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def send_message(_, _), do: exit(:nif_library_not_loaded)
78
def send_message_caller(_), do: exit(:nif_library_not_loaded)

tests/resource/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerWrapper do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def create_resource(), do: exit(:nif_library_not_loaded)
78
def update_resource(_), do: exit(:nif_library_not_loaded)

tests/timeslice/NimlerWrapper.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule NimlerTimeslice do
22
@on_load :init
33

4-
def init(), do: :erlang.load_nif(to_charlist(Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
4+
def init(), do: :erlang.load_nif(to_charlist(
5+
Path.join(Path.dirname(__ENV__.file), 'nif')), 0)
56

67
def test_consume_timeslice(_, _), do: exit(:nif_library_not_loaded)
78

0 commit comments

Comments
 (0)