Skip to content

Commit 92a2afa

Browse files
committed
Improve Cpp::Construct pointer logic for memory arena flags
Also handle both ctors and class scopes
1 parent 180869d commit 92a2afa

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
849849

850850
/// Creates one or more objects of class \c scope by calling its default
851851
/// constructor.
852-
/// \param[in] scope Class to construct
852+
/// \param[in] scope Class to construct, or handle to Constructor
853853
/// \param[in] arena If set, this API uses placement new to construct at this
854854
/// address.
855855
/// \param[in] is used to indicate the number of objects to construct.

lib/CppInterOp/CppInterOp.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,22 +3783,26 @@ void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
37833783
// FIXME: Add optional arguments to the operator new.
37843784
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
37853785
void* arena /*=nullptr*/, TCppIndex_t count /*=1UL*/) {
3786-
auto* Class = (Decl*)scope;
3787-
// FIXME: Diagnose.
3788-
if (!HasDefaultConstructor(Class))
3789-
return nullptr;
37903786

3791-
auto* const Ctor = GetDefaultConstructor(interp, Class);
3792-
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
3793-
if (arena) {
3794-
JC.InvokeConstructor(&arena, count, {},
3795-
(void*)~0); // Tell Invoke to use placement new.
3796-
return arena;
3797-
}
3787+
TCppFunction_t ctor;
3788+
if (Cpp::IsConstructor(scope))
3789+
ctor = scope;
3790+
else if (Cpp::IsClass(scope)) {
3791+
if (!HasDefaultConstructor(scope))
3792+
return nullptr;
3793+
ctor = Cpp::GetDefaultConstructor(scope);
3794+
} else
3795+
return nullptr;
37983796

3799-
void* obj = nullptr;
3800-
JC.InvokeConstructor(&obj, count, {}, nullptr);
3801-
return obj;
3797+
if (JitCall JC = MakeFunctionCallable(&interp, ctor)) {
3798+
if (JC.getKind() != Cpp::JitCall::kConstructorCall)
3799+
return nullptr;
3800+
// invoke the constructor (placement/heap) in one shot
3801+
// flag is non-null for placement new, null for normal new
3802+
void* flag = arena ? reinterpret_cast<void*>(1) : nullptr;
3803+
void* result = arena;
3804+
JC.InvokeConstructor(&result, count, {}, flag);
3805+
return result;
38023806
}
38033807
return nullptr;
38043808
}

0 commit comments

Comments
 (0)