Skip to content

Commit 59e9f1f

Browse files
committed
fix: pr comments
1 parent 65afe33 commit 59e9f1f

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

ffi/codegen/c.nim

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,33 @@ proc emitStructType(reg: var CTypeReg, t: FFITypeMeta) =
258258
proc ensureCType(reg: var CTypeReg, t: FFIType): tuple[cType: string, owns: bool] =
259259
## Walks the shared type IR into a C type, monomorphising each distinct
260260
## `seq[T]` / `Option[T]` into its own struct + codec triple on first sight.
261+
## `owns` marks a C type that carries heap-allocated payload the caller must
262+
## release with its generated free function (strings, byte buffers, and any
263+
## seq/opt/struct transitively containing one); plain scalars and pointers own
264+
## nothing and need no cleanup.
261265
case t.kind
262266
of ftPtr:
263-
(CPtrType, false)
267+
return (CPtrType, false)
264268
of ftScalar:
265-
(scalarCInfoTable[t.scalar].cType, false)
269+
return (scalarCInfoTable[t.scalar].cType, false)
266270
of ftStr:
267-
("NimFfiStr", true)
271+
return ("NimFfiStr", true)
268272
of ftBytes:
269-
("NimFfiBytes", true)
273+
return ("NimFfiBytes", true)
270274
of ftSeq:
271275
let (elemC, _) = ensureCType(reg, t.elem)
272276
let name = reg.libType & "Seq_" & cToken(elemC)
273277
if name notin reg.emitted:
274278
reg.emitted.incl(name)
275279
emitSeqType(reg, name, elemC)
276-
(name, true)
280+
return (name, true)
277281
of ftOpt:
278282
let (elemC, elemOwns) = ensureCType(reg, t.elem)
279283
let name = reg.libType & "Opt_" & cToken(elemC)
280284
if name notin reg.emitted:
281285
reg.emitted.incl(name)
282286
emitOptType(reg, name, elemC, elemOwns)
283-
(name, reg.owns.getOrDefault(name, false))
287+
return (name, reg.owns.getOrDefault(name, false))
284288
of ftStruct:
285289
let name = t.name
286290
if name notin reg.emitted:
@@ -289,7 +293,7 @@ proc ensureCType(reg: var CTypeReg, t: FFIType): tuple[cType: string, owns: bool
289293
emitStructType(reg, reg.typeTable[name])
290294
else:
291295
reg.decls.add("/* unknown type referenced: " & name & " */")
292-
(name, reg.owns.getOrDefault(name, false))
296+
return (name, reg.owns.getOrDefault(name, false))
293297

294298
proc ensureCType(reg: var CTypeReg, nimType: string): tuple[cType: string, owns: bool] =
295299
ensureCType(reg, parseFFIType(nimType))
@@ -306,7 +310,9 @@ proc reqTypeMeta(p: FFIProcMeta): FFITypeMeta =
306310

307311
func paramByValue(nimType: string, ridesAsPtr: bool): bool =
308312
## Scalars / opaque pointers / string views pass by value; composite
309-
## aggregates (seq, Option, user structs) pass by const pointer.
313+
## aggregates (seq, Option, user structs) pass by const pointer. Note `ptr T`
314+
## rides by value as the 64-bit wire int (like `pointer`); production params
315+
## reach here as `pointer` since handles are pre-converted upstream.
310316
if ridesAsPtr:
311317
return true
312318
parseFFIType(nimType).kind in {ftScalar, ftStr, ftPtr}

ffi/codegen/rust.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Rust binding generator for the nim-ffi framework.
22
## Generates a complete Rust crate that uses CBOR (ciborium) on the wire.
33

4-
import std/[os, strutils, sequtils]
4+
import std/[os, strutils]
55
import ./meta, ./string_helpers, ./types_ir
66

77
## Wire-format Rust type used for any Nim `ptr T` / `pointer`. Fixed 64-bit so

ffi/codegen/types_ir.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
## Structured type IR shared by the C / C++ / Rust binding generators: one
1+
## Structured type model shared by the C / C++ / Rust binding generators: one
22
## parser (`parseFFIType`) for the Nim type strings each backend used to slice
33
## by hand, plus `renderNative` to walk the result into a backend's type string.
44

55
import std/[strutils, options]
66

77
type
8-
ScalarKind* = enum
8+
ScalarKind* {.pure.} = enum
99
skBool
1010
skI8
1111
skI16
@@ -18,7 +18,7 @@ type
1818
skF32
1919
skF64
2020

21-
FFITypeKind* = enum
21+
FFITypeKind* {.pure.} = enum
2222
ftScalar
2323
ftStr
2424
ftBytes
@@ -55,7 +55,7 @@ func genericInnerType(typeName, prefix: string): string =
5555
## `genericInnerType("seq[int]", "seq[")` → `"int"`; "" if not that shape.
5656
if typeName.startsWith(prefix) and typeName.endsWith("]"):
5757
return typeName[prefix.len .. ^2]
58-
""
58+
return ""
5959

6060
func scalarKind(t: string): Option[ScalarKind] =
6161
## Single source of truth for the scalar leaf set every backend shares.
@@ -106,7 +106,7 @@ func parseFFIType*(typeName: string): FFIType =
106106
return FFIType(kind: ftOpt, elem: parseFFIType(optInner.strip()))
107107

108108
let sc = scalarKind(t)
109-
if sc.isSome:
109+
if sc.isSome():
110110
return FFIType(kind: ftScalar, scalar: sc.get())
111111
if t == "string" or t == "cstring":
112112
return FFIType(kind: ftStr)

0 commit comments

Comments
 (0)