Add a pointerp predicate
This is a small feature request, not a bug! I'm experimenting with bindings for whitedb, and it occurred to me that I need some way to ensure that the results I get from the calls to the C code are indeed what I expect them to be. Since it seems like all pointers are saved in the FFI context, there could be a predicate telling whether a particular symbol is indeed a pointer in that context.
Thanks!
Only some pointers are tracked in the context, so that alone is insufficient for testing if an arbitrary symbol is a pointer. Unless that's what you're specifically querying for? As you probably noticed, a pointer value is just symbol whose name contains the content of the printf %p specifier. Since that's a platform-specific format, recognizing a pointer portably is a tricky problem. However, unless I'm mistaken (I'm a little rusty on this), that's the only time you'll ever get a symbol as a value from the FFI, so symbolp should work as your predicate.
I would have used a uintptr_t instead to handle all pointers as plain integers, but on the Emacs side integers are always smaller than the host platform's pointers, and so these values would need to be broken up anyway (a la seconds-to-time).
Nope, that's not the only case, in fact, if I expect a pointer, but got a NULL instead, I get back a symbol, which is not of the same format as a pointer (I don't know where does the string come from). Here's an example:
(defun wg:attach-database (name size)
(assert (integerp name) name "%s must be an integer")
(assert (integerp size) size "%s must be an integer")
(wg:log 2 "attaching database: %d, capacity: %d" name size)
(let ((result
(ffi-call wg:so-name "wg_attach_database"
[:pointer :pointer :uint32] (format "%d" name) size)))
(unless (symbolp result) (error "Couldn't attach database"))
(wg:log 2 "database: %s attached" result)
result))
When this succeeds, I get a symbol which looks like 0x00001234, and if it fails, I get back a symbol bd.
I'll try to investigate the issue further on the weekend, maybe I'll know why is the result what it is.