Skip to content

Commit

Permalink
Merge pull request #17 from lynnporu/dev
Browse files Browse the repository at this point in the history
Compile function, process function pointers
  • Loading branch information
lynnporu authored Jan 14, 2022
2 parents ce9abb1 + d584780 commit e46c6d3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions annotatec/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def unwrap(self, wrapped):
else:
return wrapped

def is_function(self, name: str):
"""Checks if passed name is a function.
"""
return hasattr(self.lib, name)

def compile(self, name: str, unwrap: bool = False):
"""
Arguments:
Expand Down Expand Up @@ -96,6 +101,9 @@ def compile(self, name: str, unwrap: bool = False):
if name in BASE_C_TYPES:
return BASE_C_TYPES[name]

if self.is_function(name):
return name

if name not in self:
raise NamespaceError(
f"Trying to get `{name}` object, but there's no objects with "
Expand Down Expand Up @@ -193,10 +201,17 @@ def compile(self):

if not self.compiled:

prototype = ctypes.CFUNCTYPE(*list(map(
self.namespace.compile_unwrap,
[self.return_type] + self.argument_types
)))
prototype = ctypes.CFUNCTYPE(
self.namespace.compile_unwrap(self.return_type),
*[
# compile the name
self.namespace.compile_unwrap(argument_type)
if not self.namespace.is_function(argument_type)
# or give (void*) if the name defines a function
else ctypes.c_void_p
for argument_type in self.argument_types
]
)

self.compilation_result = prototype(
(self.name, self.namespace.lib))
Expand Down

0 comments on commit e46c6d3

Please sign in to comment.