Open andr1972 opened 1 week ago
Hi, I'm not sure if external_call
is only for statically linked libraries (can be the case, I just don't know). I see 2 little problems with your example:
handle = ffi.DLHandle("libpq.so")
is at runtime ffi.external_call[handle, ...]
can't be passed at compile time (AFAIK).[..., UnsafePointer[Int8]](conninfo)
You don't need to specify the type of args anymore, and Bytes are UInt8 in Mojo.Other than that, until we have a nice interface like what you're asking (which should be the goal), you can have a look at how things for Python interop are currently done. An example:
self.lib = DLHandle(python_lib)
self.total_ref_count = UnsafePointer[Int].alloc(1)
self.dict_type = PyObjectPtr()
self.logging_enabled = logging_enabled
if not self.init_error:
if not self.lib.check_symbol("Py_Initialize"):
self.init_error = "compatible Python library not found"
self.lib.get_function[fn () -> None]("Py_Initialize")()
self.version = PythonVersion(_py_get_version(self.lib))
You can define some more complicated function signatures:
fn PyDict_SetItem(
inout self, dict_obj: PyObjectPtr, key: PyObjectPtr, value: PyObjectPtr
) -> c_int:
"""See https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem."""
var r = self.lib.get_function[
fn (PyObjectPtr, PyObjectPtr, PyObjectPtr) -> c_int
](StringRef("PyDict_SetItem"))(dict_obj, key, value)
But I definitely agree that this should be able to be handled by external_call
and/or have nicer ergonomics
Review Mojo's priorities
What is your request?
I need method like ffi.external_call with parameter "handle" to enable calling methods from any C libraries, not only libc.
What is your motivation for this change?
I want write PostgreSQL library for Mojo - libpq.so wrapper.
Any other details?
Some example of using: