Skip to content

Commit 9135f24

Browse files
committed
chore: quality polishing
1 parent 967df26 commit 9135f24

6 files changed

Lines changed: 42 additions & 36 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ jobs:
133133
uses: actions/cache@v4
134134
with:
135135
path: tests/e2e/cpp/build/_deps
136-
key: ${{ runner.os }}-cpp-e2e-deps-${{ hashFiles('tests/e2e/cpp/CMakeLists.txt') }}
136+
# The FetchContent subbuild bakes the CMake generator (e.g. the MSVC
137+
# "Visual Studio NN" version) into its CMakeCache.txt. Key on the
138+
# runner image so a toolchain bump can't restore a generator-mismatched
139+
# build dir — that aborts the configure. No restore-keys, for the same reason.
140+
key: ${{ runner.os }}-${{ env.ImageOS }}-${{ env.ImageVersion }}-cpp-e2e-deps-${{ hashFiles('tests/e2e/cpp/CMakeLists.txt') }}
137141

138142
- name: Run C++ e2e tests
139143
shell: bash

ffi/codegen/cddl.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ proc emitObjectFields(t: FFITypeMeta): string =
9292
proc emitReqFields(p: FFIProcMeta): string =
9393
var fields: seq[tuple[name: string, typeName: string, isPtr: bool]] = @[]
9494
for ep in p.extraParams:
95-
fields.add((name: ep.name, typeName: ep.typeName, isPtr: ep.ridesAsPtr()))
95+
fields.add((name: ep.name, typeName: ep.typeName, isPtr: ep.wireIsPtr()))
9696
emitMap(fields)
9797

9898
proc responseRule(p: FFIProcMeta): string =
@@ -106,7 +106,7 @@ proc responseRule(p: FFIProcMeta): string =
106106
# The dtor has no meaningful payload — handleRes sends a CBOR null sentinel.
107107
"nil"
108108
of FFIKind.FFI:
109-
if p.returnRidesAsPtr():
109+
if p.returnWireIsPtr():
110110
"uint"
111111
else:
112112
nimTypeToCddl(p.returnTypeName)

ffi/codegen/cpp.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ proc generateCppHeader*(
299299
lines.add("struct $1 {" % [reqName])
300300
for ep in p.extraParams:
301301
let cppType =
302-
if ep.ridesAsPtr():
302+
if ep.wireIsPtr():
303303
CppPtrType
304304
else:
305305
nimTypeToCpp(ep.typeName)
@@ -308,7 +308,7 @@ proc generateCppHeader*(
308308
var fields: seq[(string, string)] = @[]
309309
for ep in p.extraParams:
310310
let cppType =
311-
if ep.ridesAsPtr():
311+
if ep.wireIsPtr():
312312
CppPtrType
313313
else:
314314
nimTypeToCpp(ep.typeName)
@@ -388,7 +388,7 @@ proc generateCppHeader*(
388388
var epNames: seq[string] = @[]
389389
for ep in ctor.extraParams:
390390
let cppType =
391-
if ep.ridesAsPtr():
391+
if ep.wireIsPtr():
392392
CppPtrType
393393
else:
394394
nimTypeToCpp(ep.typeName)
@@ -492,7 +492,7 @@ proc generateCppHeader*(
492492
for m in methods:
493493
let methodName = stripLibPrefixCpp(m.procName, libName)
494494
let retCppType =
495-
if m.returnRidesAsPtr():
495+
if m.returnWireIsPtr():
496496
CppPtrType
497497
else:
498498
nimTypeToCpp(m.returnTypeName)
@@ -502,7 +502,7 @@ proc generateCppHeader*(
502502
var methParamNames: seq[string] = @[]
503503
for ep in m.extraParams:
504504
let cppType =
505-
if ep.ridesAsPtr():
505+
if ep.wireIsPtr():
506506
CppPtrType
507507
else:
508508
nimTypeToCpp(ep.typeName)

ffi/codegen/meta.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ type
55
FFIParamMeta* = object
66
name*: string # Nim param name, e.g. "req"
77
typeName*: string # Nim type name, e.g. "EchoRequest"
8-
isPtr*: bool # true if the type is `ptr T`
9-
isHandle*: bool # true if the type is an {.ffiHandle.} type (wire form uint64)
8+
isPtr*: bool
9+
isHandle*: bool # wire form uint64
1010

1111
FFIKind* {.pure.} = enum
1212
FFI
@@ -20,8 +20,8 @@ type
2020
libTypeName*: string # e.g. "Timer"
2121
extraParams*: seq[FFIParamMeta] # all params except the lib param
2222
returnTypeName*: string # e.g. "EchoResponse", "string", "pointer"
23-
returnIsPtr*: bool # true if return type is ptr T
24-
returnIsHandle*: bool # true if return type is an {.ffiHandle.} type
23+
returnIsPtr*: bool
24+
returnIsHandle*: bool
2525

2626
FFIFieldMeta* = object
2727
name*: string # e.g. "delayMs"
@@ -57,12 +57,12 @@ var ffiHandleTypeNames* {.compileTime.}: seq[string]
5757
proc isFFIHandleTypeName*(name: string): bool {.compileTime.} =
5858
name in ffiHandleTypeNames
5959

60-
proc ridesAsPtr*(ep: FFIParamMeta): bool =
60+
proc wireIsPtr*(ep: FFIParamMeta): bool =
6161
## True if the param crosses the wire as an opaque uint64 — a raw `ptr` or an
6262
## `{.ffiHandle.}` id. Both share the codegen pointer type.
6363
ep.isPtr or ep.isHandle
6464

65-
proc returnRidesAsPtr*(p: FFIProcMeta): bool =
65+
proc returnWireIsPtr*(p: FFIProcMeta): bool =
6666
## True if the return crosses the wire as an opaque uint64 (raw `ptr` or handle).
6767
p.returnIsPtr or p.returnIsHandle
6868

ffi/codegen/rust.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ proc generateTypesRs*(types: seq[FFITypeMeta], procs: seq[FFIProcMeta]): string
256256
for ep in p.extraParams:
257257
let snake = camelToSnakeCase(ep.name)
258258
let rustType =
259-
if ep.ridesAsPtr():
259+
if ep.wireIsPtr():
260260
RustPtrType
261261
else:
262262
nimTypeToRust(ep.typeName)
@@ -550,7 +550,7 @@ proc generateApiRs*(
550550
for ep in ctor.extraParams:
551551
let snake = camelToSnakeCase(ep.name)
552552
let rustType =
553-
if ep.ridesAsPtr():
553+
if ep.wireIsPtr():
554554
RustPtrType
555555
else:
556556
nimTypeToRust(ep.typeName)
@@ -711,7 +711,7 @@ proc generateApiRs*(
711711
for ep in m.extraParams:
712712
let snake = camelToSnakeCase(ep.name)
713713
let rustType =
714-
if ep.ridesAsPtr():
714+
if ep.wireIsPtr():
715715
RustPtrType
716716
else:
717717
nimTypeToRust(ep.typeName)
@@ -729,7 +729,7 @@ proc generateApiRs*(
729729
else:
730730
reqName & " {}"
731731

732-
let retTypeForApi = if m.returnRidesAsPtr(): RustPtrType else: retRustType
732+
let retTypeForApi = if m.returnWireIsPtr(): RustPtrType else: retRustType
733733

734734
# -- blocking method --
735735
lines.add(

ffi/internal/ffi_macro.nim

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ proc isHandleType(typ: NimNode): bool =
8787
typ.kind == nnkIdent and isFFIHandleTypeName($typ)
8888

8989
proc storageType(typ: NimNode): NimNode =
90-
## In-Req-struct storage type. `cstring` rides as `string`; an {.ffiHandle.}
91-
## type rides as its `uint64` id; everything else as-is.
90+
## In-Req-struct storage type. `cstring` stores as `string`; an {.ffiHandle.}
91+
## type stores as its `uint64` id; everything else as-is.
9292
if typ.kind == nnkIdent and $typ == "cstring":
9393
return ident("string")
9494
if isHandleType(typ):
@@ -631,26 +631,27 @@ macro ffiRaw*(prc: untyped): untyped =
631631
echo stmts.repr
632632
return stmts
633633

634-
macro ffiHandle*(prc: untyped): untyped =
634+
macro ffiHandle*(node: untyped): untyped =
635635
## Marks a `ref object` as an opaque FFI handle. Its wire form is a `uint64`
636636
## id; the live object stays in the per-ctx handle registry and never crosses.
637637
##
638638
## type Kernel {.ffiHandle.} = ref object
639639
## ...
640-
if prc.kind != nnkTypeDef:
640+
if node.kind != nnkTypeDef:
641641
error("`.ffiHandle.` must be applied to a type definition")
642642

643-
var clean = prc.copyNimTree()
644-
if clean[0].kind == nnkPragmaExpr:
645-
clean[0] = clean[0][0]
643+
# Strip the `{.ffiHandle.}` pragma off the type name before re-emitting.
644+
let typeDef = node.copyNimTree()
645+
if typeDef[0].kind == nnkPragmaExpr:
646+
typeDef[0] = typeDef[0][0]
646647

647648
let typeName =
648-
if clean[0].kind == nnkPostfix:
649-
clean[0][1]
649+
if typeDef[0].kind == nnkPostfix:
650+
typeDef[0][1]
650651
else:
651-
clean[0]
652+
typeDef[0]
652653

653-
let refTy = clean[2]
654+
let refTy = typeDef[2]
654655
if refTy.kind != nnkRefTy or refTy[0].kind != nnkObjectTy:
655656
error("`.ffiHandle.` type " & $typeName & " must be a `ref object`")
656657
let objTy = refTy[0]
@@ -662,8 +663,8 @@ macro ffiHandle*(prc: untyped): untyped =
662663
ffiHandleTypeNames.add($typeName)
663664

664665
when defined(ffiDumpMacros):
665-
echo clean.repr
666-
return clean
666+
echo typeDef.repr
667+
return typeDef
667668

668669
macro ffi*(prc: untyped): untyped =
669670
## Simplified FFI macro — applies to procs or types.
@@ -687,10 +688,11 @@ macro ffi*(prc: untyped): untyped =
687688
## return ok("done")
688689

689690
if prc.kind == nnkTypeDef:
690-
var cleanTypeDef = prc.copyNimTree()
691-
if cleanTypeDef[0].kind == nnkPragmaExpr:
692-
cleanTypeDef[0] = cleanTypeDef[0][0]
693-
return registerFFITypeInfo(cleanTypeDef)
691+
# Strip the `{.ffi.}` pragma off the type name before registering it.
692+
let typeDef = prc.copyNimTree()
693+
if typeDef[0].kind == nnkPragmaExpr:
694+
typeDef[0] = typeDef[0][0]
695+
return registerFFITypeInfo(typeDef)
694696

695697
let procName = prc[0]
696698
let formalParams = prc[3]
@@ -735,7 +737,7 @@ macro ffi*(prc: untyped): untyped =
735737
let resultRetType = resultInner[1]
736738
rejectRawPtrType(resultRetType, "`.ffi.` proc " & $procName & " return type")
737739

738-
# A handle receiver rides the wire; a value-type lib receiver binds to ctx.myLib.
740+
# A handle receiver crosses the wire; a value-type lib receiver binds to ctx.myLib.
739741
var extraParamNames: seq[string] = @[]
740742
var extraParamTypes: seq[NimNode] = @[]
741743
let wireStart = if firstIsHandle: 1 else: 2

0 commit comments

Comments
 (0)