mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-25 02:39:47 +00:00
remove ffiType macro because it is duplicated by ffi macro (#22)
This commit is contained in:
parent
6d31fa30bd
commit
e48c9f9edf
@ -1,4 +1,4 @@
|
||||
import std/[json, macros, options]
|
||||
import std/[json, options]
|
||||
import results
|
||||
import ./codegen/meta
|
||||
|
||||
@ -119,38 +119,3 @@ proc ffiDeserialize*[T: object](s: cstring, _: typedesc[T]): Result[T, string] =
|
||||
ok(parseJson($s).to(T))
|
||||
except Exception as e:
|
||||
err(e.msg)
|
||||
|
||||
macro ffiType*(body: untyped): untyped =
|
||||
## Statement macro applied to a type declaration block.
|
||||
## Registers the type in ffiTypeRegistry for binding generation.
|
||||
## Serialization is handled by the generic ffiSerialize/ffiDeserialize overloads.
|
||||
## Usage:
|
||||
## ffiType:
|
||||
## type Foo = object
|
||||
## field: int
|
||||
let typeSection = body[0]
|
||||
let typeDef = typeSection[0]
|
||||
let typeName =
|
||||
if typeDef[0].kind == nnkPostfix:
|
||||
typeDef[0][1]
|
||||
else:
|
||||
typeDef[0]
|
||||
|
||||
let typeNameStr = $typeName
|
||||
var fieldMetas: seq[FFIFieldMeta] = @[]
|
||||
let objTy = typeDef[2]
|
||||
if objTy.kind == nnkObjectTy and objTy.len >= 3:
|
||||
let recList = objTy[2]
|
||||
if recList.kind == nnkRecList:
|
||||
for identDef in recList:
|
||||
if identDef.kind == nnkIdentDefs:
|
||||
let fieldType = identDef[^2]
|
||||
let fieldTypeName =
|
||||
if fieldType.kind == nnkIdent: $fieldType
|
||||
elif fieldType.kind == nnkPtrTy: "ptr " & $fieldType[0]
|
||||
else: fieldType.repr
|
||||
for i in 0 ..< identDef.len - 2:
|
||||
fieldMetas.add(FFIFieldMeta(name: $identDef[i], typeName: fieldTypeName))
|
||||
|
||||
ffiTypeRegistry.add(FFITypeMeta(name: typeNameStr, fields: fieldMetas))
|
||||
result = body
|
||||
|
||||
@ -5,9 +5,8 @@ import ../ffi
|
||||
|
||||
type TestLib = object
|
||||
|
||||
ffiType:
|
||||
type CtxValidationConfig = object
|
||||
initialValue: int
|
||||
type CtxValidationConfig {.ffi.} = object
|
||||
initialValue: int
|
||||
|
||||
proc ctxval_create*(
|
||||
config: CtxValidationConfig
|
||||
|
||||
@ -412,9 +412,8 @@ suite "sendRequestToFFIThread":
|
||||
type SimpleLib = object
|
||||
value: int
|
||||
|
||||
ffiType:
|
||||
type SimpleConfig = object
|
||||
initialValue: int
|
||||
type SimpleConfig {.ffi.} = object
|
||||
initialValue: int
|
||||
|
||||
proc testlib_create*(
|
||||
config: SimpleConfig
|
||||
@ -454,9 +453,8 @@ suite "ffiCtor macro":
|
||||
# Simplified .ffi. macro integration test
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
ffiType:
|
||||
type SendConfig = object
|
||||
message: string
|
||||
type SendConfig {.ffi.} = object
|
||||
message: string
|
||||
|
||||
proc testlib_send*(
|
||||
lib: SimpleLib, cfg: SendConfig
|
||||
@ -558,9 +556,8 @@ suite "async/sync detection in .ffi.":
|
||||
type Handle = object
|
||||
data: string
|
||||
|
||||
ffiType:
|
||||
type NameParam = object
|
||||
name: string
|
||||
type NameParam {.ffi.} = object
|
||||
name: string
|
||||
|
||||
proc testlib_alloc_handle*(
|
||||
lib: SimpleLib, np: NameParam
|
||||
|
||||
@ -1,16 +1,14 @@
|
||||
import unittest
|
||||
import results
|
||||
import ../ffi/serial
|
||||
import ../ffi
|
||||
|
||||
ffiType:
|
||||
type Point = object
|
||||
x: int
|
||||
y: int
|
||||
type Point {.ffi.} = object
|
||||
x: int
|
||||
y: int
|
||||
|
||||
ffiType:
|
||||
type Nested = object
|
||||
label: string
|
||||
point: Point
|
||||
type Nested {.ffi.} = object
|
||||
label: string
|
||||
point: Point
|
||||
|
||||
suite "ffiSerialize / ffiDeserialize primitives":
|
||||
test "string round-trip":
|
||||
@ -81,7 +79,7 @@ suite "pointer serialization":
|
||||
let serialized = ffiSerialize(p)
|
||||
check serialized == "0"
|
||||
|
||||
suite "ffiType macro — object round-trip":
|
||||
suite "{.ffi.} on type — object round-trip":
|
||||
test "Point round-trip":
|
||||
let pt = Point(x: 10, y: 20)
|
||||
let serialized = ffiSerialize(pt)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user