Skip to content

Commit ec70e5b

Browse files
authored
Merge branch 'main' into out-of-process
2 parents e46d9ce + 98e07e7 commit ec70e5b

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
namespace Cpp {
3737
using TCppIndex_t = size_t;
3838
using TCppScope_t = void*;
39+
using TCppConstScope_t = const void*;
3940
using TCppType_t = void*;
4041
using TCppFunction_t = void*;
4142
using TCppConstFunction_t = const void*;
@@ -839,14 +840,14 @@ enum : long int {
839840
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
840841

841842
/// Allocates memory required by an object of a given class
842-
/// \c scope Given class for which to allocate memory for
843-
/// \c count is used to indicate the number of objects to allocate for.
843+
/// \param[in] scope Given class for which to allocate memory for
844+
/// \param[in] count is used to indicate the number of objects to allocate for.
844845
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
845846
TCppIndex_t count = 1UL);
846847

847848
/// Deallocates memory for a given class.
848-
/// \c scope Class to indicate size of memory to deallocate
849-
/// \c count is used to indicate the number of objects to dallocate for
849+
/// \param[in] scope Class to indicate size of memory to deallocate
850+
/// \param[in] count is used to indicate the number of objects to dallocate for
850851
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
851852
TCppIndex_t count = 1UL);
852853

@@ -856,6 +857,8 @@ CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
856857
/// \param[in] arena If set, this API uses placement new to construct at this
857858
/// address.
858859
/// \param[in] is used to indicate the number of objects to construct.
860+
/// \returns a pointer to the constructed object, which is arena if placement
861+
/// new is used.
859862
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
860863
TCppIndex_t count = 1UL);
861864

@@ -866,7 +869,8 @@ CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
866869
/// destructor
867870
/// \param[in] count indicate the number of objects to destruct, if \c This
868871
/// points to an array of objects
869-
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
872+
/// \returns true if wrapper generation and invocation succeeded.
873+
CPPINTEROP_API bool Destruct(TCppObject_t This, TCppConstScope_t type,
870874
bool withFree = true, TCppIndex_t count = 0UL);
871875

872876
/// @name Stream Redirection

include/clang-c/CXCppInterOp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
361361
* \param type The type of the object.
362362
*
363363
* \param withFree Whether to call operator delete/free or not.
364+
*
365+
* \returns true if wrapper generation and invocation succeeded.
364366
*/
365-
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S,
367+
CINDEX_LINKAGE bool clang_destruct(CXObject This, CXScope S,
366368
bool withFree = true, size_t nary = 0UL);
367369

368370
/**

lib/CppInterOp/CXCppInterOp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,10 @@ void clang_invoke(CXScope func, void* result, void** args, size_t n,
613613
}
614614

615615
namespace Cpp {
616-
void Destruct(compat::Interpreter& interp, TCppObject_t This,
617-
clang::Decl* Class, bool withFree, size_t nary);
616+
bool Destruct(compat::Interpreter& interp, TCppObject_t This,
617+
const clang::Decl* Class, bool withFree, size_t nary);
618618
} // namespace Cpp
619619

620-
void clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
621-
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
620+
bool clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
621+
return Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
622622
}

lib/CppInterOp/CppInterOp.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,19 +3817,20 @@ TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
38173817
return Construct(getInterp(), scope, arena, count);
38183818
}
38193819

3820-
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3820+
bool Destruct(compat::Interpreter& interp, TCppObject_t This, const Decl* Class,
38213821
bool withFree, TCppIndex_t nary) {
38223822
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
38233823
(*wrapper)(This, nary, withFree);
3824-
return;
3824+
return true;
38253825
}
3826-
// FIXME: Diagnose.
3826+
return false;
3827+
// FIXME: Enable stronger diagnostics
38273828
}
38283829

3829-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3830-
TCppIndex_t count /*=0UL*/) {
3831-
auto* Class = static_cast<Decl*>(scope);
3832-
Destruct(getInterp(), This, Class, withFree, count);
3830+
bool Destruct(TCppObject_t This, TCppConstScope_t scope,
3831+
bool withFree /*=true*/, TCppIndex_t count /*=0UL*/) {
3832+
const auto* Class = static_cast<const Decl*>(scope);
3833+
return Destruct(getInterp(), This, Class, withFree, count);
38333834
}
38343835

38353836
class StreamCaptureInfo {

unittests/CppInterOp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ target_link_libraries(DynamicLibraryManagerTests
9898
)
9999

100100
if(EMSCRIPTEN)
101-
set(TEST_SHARED_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/Release/")
101+
set(TEST_SHARED_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/")
102102
string(REPLACE "@" "@@" ESCAPED_TEST_SHARED_LIBRARY_PATH "${TEST_SHARED_LIBRARY_PATH}")
103103
# Check explanation of Emscripten-specific link flags for CppInterOpTests above for DynamicLibraryManagerTests as well.
104104
target_link_options(DynamicLibraryManagerTests

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14811481
Ctor.Invoke(&object);
14821482
EXPECT_TRUE(object) << "Failed to call the ctor.";
14831483
// Building a wrapper with a typedef decl must be possible.
1484-
Cpp::Destruct(object, Decls[1]);
1484+
EXPECT_TRUE(Cpp::Destruct(object, Decls[1]));
14851485

14861486
// C API
14871487
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
@@ -2365,7 +2365,7 @@ TEST(FunctionReflectionTest, ConstructArray) {
23652365
obj = reinterpret_cast<int*>(reinterpret_cast<char*>(where) +
23662366
(Cpp::SizeOf(scope) * 4));
23672367
EXPECT_TRUE(*obj == 42);
2368-
Cpp::Destruct(where, scope, /*withFree=*/false, 5);
2368+
EXPECT_TRUE(Cpp::Destruct(where, scope, /*withFree=*/false, 5));
23692369
Cpp::Deallocate(scope, where, 5);
23702370
output = testing::internal::GetCapturedStdout();
23712371
EXPECT_EQ(output,
@@ -2405,7 +2405,7 @@ TEST(FunctionReflectionTest, Destruct) {
24052405
testing::internal::CaptureStdout();
24062406
Cpp::TCppScope_t scope = Cpp::GetNamed("C");
24072407
Cpp::TCppObject_t object = Cpp::Construct(scope);
2408-
Cpp::Destruct(object, scope);
2408+
EXPECT_TRUE(Cpp::Destruct(object, scope));
24092409
std::string output = testing::internal::GetCapturedStdout();
24102410

24112411
EXPECT_EQ(output, "Destructor Executed");
@@ -2415,7 +2415,7 @@ TEST(FunctionReflectionTest, Destruct) {
24152415
object = Cpp::Construct(scope);
24162416
// Make sure we do not call delete by adding an explicit Deallocate. If we
24172417
// called delete the Deallocate will cause a double deletion error.
2418-
Cpp::Destruct(object, scope, /*withFree=*/false);
2418+
EXPECT_TRUE(Cpp::Destruct(object, scope, /*withFree=*/false));
24192419
Cpp::Deallocate(scope, object);
24202420
output = testing::internal::GetCapturedStdout();
24212421
EXPECT_EQ(output, "Destructor Executed");
@@ -2432,6 +2432,20 @@ TEST(FunctionReflectionTest, Destruct) {
24322432
// Clean up resources
24332433
clang_Interpreter_takeInterpreterAsPtr(I);
24342434
clang_Interpreter_dispose(I);
2435+
2436+
// Failure test, this wrapper should not compile since we explicitly delete
2437+
// the destructor
2438+
Interp->declare(R"(
2439+
class D {
2440+
public:
2441+
D() {}
2442+
~D() = delete;
2443+
};
2444+
)");
2445+
2446+
scope = Cpp::GetNamed("D");
2447+
object = Cpp::Construct(scope);
2448+
EXPECT_FALSE(Cpp::Destruct(object, scope));
24352449
}
24362450

24372451
TEST(FunctionReflectionTest, DestructArray) {
@@ -2483,7 +2497,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24832497

24842498
testing::internal::CaptureStdout();
24852499
// destruct 3 out of 5 objects
2486-
Cpp::Destruct(where, scope, false, 3);
2500+
EXPECT_TRUE(Cpp::Destruct(where, scope, false, 3));
24872501
output = testing::internal::GetCapturedStdout();
24882502

24892503
EXPECT_EQ(
@@ -2495,7 +2509,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24952509
// destruct the rest
24962510
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
24972511
(Cpp::SizeOf(scope) * 3));
2498-
Cpp::Destruct(new_head, scope, false, 2);
2512+
EXPECT_TRUE(Cpp::Destruct(new_head, scope, false, 2));
24992513

25002514
output = testing::internal::GetCapturedStdout();
25012515
EXPECT_EQ(output, "\nDestructor Executed\n\nDestructor Executed\n");
@@ -2510,7 +2524,7 @@ TEST(FunctionReflectionTest, DestructArray) {
25102524
testing::internal::CaptureStdout();
25112525
// FIXME : This should work with the array of objects as well
25122526
// Cpp::Destruct(where, scope, true, 5);
2513-
Cpp::Destruct(where, scope, true);
2527+
EXPECT_TRUE(Cpp::Destruct(where, scope, true));
25142528
output = testing::internal::GetCapturedStdout();
25152529
EXPECT_EQ(output, "\nDestructor Executed\n");
25162530
output.clear();

0 commit comments

Comments
 (0)