-
Notifications
You must be signed in to change notification settings - Fork 31
Add support for array of objects in Construct/Destruct #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d7153e9
710a964
a4619ad
bd8cf8d
cf533ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -103,6 +103,7 @@ class JitCall { | |||||||||||
enum Kind : char { | ||||||||||||
kUnknown = 0, | ||||||||||||
kGenericCall, | ||||||||||||
kConstructorCall, | ||||||||||||
kDestructorCall, | ||||||||||||
}; | ||||||||||||
struct ArgList { | ||||||||||||
|
@@ -115,25 +116,32 @@ class JitCall { | |||||||||||
// FIXME: Figure out how to unify the wrapper signatures. | ||||||||||||
// FIXME: Hide these implementation details by moving wrapper generation in | ||||||||||||
// this class. | ||||||||||||
// (self, nargs, args, result, nary) | ||||||||||||
using GenericCall = void (*)(void*, size_t, void**, void*); | ||||||||||||
using DestructorCall = void (*)(void*, unsigned long, int); | ||||||||||||
// (result, nary, nargs, args, is_arena) | ||||||||||||
using ConstructorCall = void (*)(void*, size_t, size_t, void**, void*); | ||||||||||||
// (self, nary, withFree) | ||||||||||||
using DestructorCall = void (*)(void*, size_t, int); | ||||||||||||
|
||||||||||||
private: | ||||||||||||
union { | ||||||||||||
GenericCall m_GenericCall; | ||||||||||||
ConstructorCall m_ConstructorCall; | ||||||||||||
DestructorCall m_DestructorCall; | ||||||||||||
}; | ||||||||||||
Kind m_Kind; | ||||||||||||
TCppConstFunction_t m_FD; | ||||||||||||
JitCall() : m_GenericCall(nullptr), m_Kind(kUnknown), m_FD(nullptr) {} | ||||||||||||
JitCall(Kind K, GenericCall C, TCppConstFunction_t FD) | ||||||||||||
: m_GenericCall(C), m_Kind(K), m_FD(FD) {} | ||||||||||||
JitCall(Kind K, ConstructorCall C, TCppConstFunction_t Ctor) | ||||||||||||
: m_ConstructorCall(C), m_Kind(K), m_FD(Ctor) {} | ||||||||||||
JitCall(Kind K, DestructorCall C, TCppConstFunction_t Dtor) | ||||||||||||
: m_DestructorCall(C), m_Kind(K), m_FD(Dtor) {} | ||||||||||||
|
||||||||||||
/// Checks if the passed arguments are valid for the given function. | ||||||||||||
CPPINTEROP_API bool AreArgumentsValid(void* result, ArgList args, | ||||||||||||
void* self) const; | ||||||||||||
CPPINTEROP_API bool AreArgumentsValid(void* result, ArgList args, void* self, | ||||||||||||
size_t nary = 1UL) const; | ||||||||||||
|
||||||||||||
/// This function is used for debugging, it reports when the function was | ||||||||||||
/// called. | ||||||||||||
|
@@ -169,6 +177,12 @@ class JitCall { | |||||||||||
InvokeDestructor(result, /*nary=*/0UL, /*withFree=*/true); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
// Forward if we intended to call a constructor (nary cannot be inferred, so | ||||||||||||
// we stick to constructing a single object) | ||||||||||||
else if (m_Kind == kConstructorCall && result && !args.m_Args) { | ||||||||||||
InvokeConstructor(result, /*nary=*/1UL, args, self); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'else' after 'return' [llvm-else-after-return]
Suggested change
|
||||||||||||
|
||||||||||||
#ifndef NDEBUG | ||||||||||||
assert(AreArgumentsValid(result, args, self) && "Invalid args!"); | ||||||||||||
|
@@ -192,6 +206,23 @@ class JitCall { | |||||||||||
#endif // NDEBUG | ||||||||||||
m_DestructorCall(object, nary, withFree); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Makes a call to a constructor. | ||||||||||||
///\param[in] result - the memory address at which we construct the object | ||||||||||||
/// (placement new). | ||||||||||||
///\param[in] nary - Use array new if we have to construct an array of | ||||||||||||
/// objects (nary > 1). | ||||||||||||
///\param[in] args - a pointer to a argument list and argument size. | ||||||||||||
// FIXME: Change the type of withFree from int to bool in the wrapper code. | ||||||||||||
void InvokeConstructor(void* result, unsigned long nary = 1, | ||||||||||||
ArgList args = {}, void* is_arena = nullptr) const { | ||||||||||||
assert(m_Kind == kConstructorCall && "Wrong overload!"); | ||||||||||||
#ifndef NDEBUG | ||||||||||||
assert(AreArgumentsValid(result, args, nullptr, nary) && "Invalid args!"); | ||||||||||||
ReportInvokeStart(result, args, nullptr); | ||||||||||||
#endif // NDEBUG | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access] m_ConstructorCall(result, nary, args.m_ArgSize, args.m_Args, (int)is_arena);
^ |
||||||||||||
m_ConstructorCall(result, nary, args.m_ArgSize, args.m_Args, is_arena); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access] m_ConstructorCall(result, nary, args.m_ArgSize, args.m_Args, is_arena);
^ |
||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
||||||||||||
///\returns the version string information of the library. | ||||||||||||
|
@@ -779,19 +810,24 @@ enum : long int { | |||||||||||
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type); | ||||||||||||
|
||||||||||||
/// Allocates memory for a given class. | ||||||||||||
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope); | ||||||||||||
/// \c count is used to indicate the number of objects to allocate for. | ||||||||||||
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: enum '(unnamed enum at /github/workspace/include/CppInterOp/CppInterOp.h:802:1)' uses a larger base type ('long', size: 8 bytes) than necessary for its value set, consider using 'std::int8_t' (1 byte) as the base type to reduce its size [performance-enum-size] enum : long int {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should adjust the types to match the signature types to match the generated code types and disable the clang-tidy complaints if we have to. |
||||||||||||
TCppIndex_t count = 1UL); | ||||||||||||
|
||||||||||||
/// Deallocates memory for a given class. | ||||||||||||
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address); | ||||||||||||
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address, | ||||||||||||
TCppIndex_t count = 1UL); | ||||||||||||
|
||||||||||||
/// Creates an object of class \c scope and calls its default constructor. If | ||||||||||||
/// \c arena is set it uses placement new. | ||||||||||||
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr); | ||||||||||||
/// \c count is used to indicate the number of objects to construct. | ||||||||||||
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr, | ||||||||||||
TCppIndex_t count = 1UL); | ||||||||||||
|
||||||||||||
/// Calls the destructor of object of type \c type. When withFree is true it | ||||||||||||
/// calls operator delete/free. | ||||||||||||
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type, | ||||||||||||
bool withFree = true); | ||||||||||||
bool withFree = true, TCppIndex_t count = 0UL); | ||||||||||||
|
||||||||||||
/// @name Stream Redirection | ||||||||||||
/// | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: do not use 'else' after 'return' [llvm-else-after-return]