Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
add test for catching D exceptions from C++ on Win32
Browse files Browse the repository at this point in the history
  • Loading branch information
rainers committed Jan 29, 2016
1 parent 9c840a1 commit 7cf0d4b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ endif

ifeq ($(OS),win32)
DISABLED_FAIL_TESTS += fail13939
else
DISABLED_TESTS += ldc_cpp_eh
endif

####
Expand Down
2 changes: 1 addition & 1 deletion d_do_test.d
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ bool collectExtraSources (in string input_dir, in string output_dir, in string[]
{
if (msc)
{
command ~= ` /c /nologo `~curSrc~` /Fo`~curObj;
command ~= ` /c /nologo /EHsc `~curSrc~` /Fo`~curObj;
}
else if (envData.os == "win32")
{
Expand Down
63 changes: 63 additions & 0 deletions runnable/extra-files/dexcept.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// D exceptions to by caught by C++

#pragma once

namespace D {

struct string
{
size_t length;
const char* ptr;
};

class Object;

extern "C" {
string _d_toString(Object* o);
}

class Object
{
void* __monitor;

virtual void _classinfo_data() = 0;

protected:
// don't call directly, ABI mismatch
virtual string _toString();
virtual size_t _toHash();
virtual int _opCmp(Object* o);
virtual bool _opEquals(Object* o);

public:
string toString() { return _d_toString(this); }
};

class Throwable : public Object
{
public:
string msg; /// A message describing the error.

/**
* The _file name and line number of the D source code corresponding with
* where the error was thrown from.
*/
string file;
size_t line; /// ditto

void* info;
Throwable* next;

virtual string _toString();
};

class Exception : public Throwable
{
};

class Error : public Throwable
{
Throwable* bypassedException;
};

} // namespace D
24 changes: 24 additions & 0 deletions runnable/extra-files/ldc_cpp_eh1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "dexcept.h"
#include <string.h>
#include <assert.h>

void throwException();

bool test_eh()
{
try
{
throwException();
}
catch (D::Exception* excpt)
{
D::string s = excpt->toString();
assert(memcmp(s.ptr + s.length - 9, "Hello C++", 9) == 0);
return true;
}
catch(...)
{
assert(false);
}
return false;
}
15 changes: 15 additions & 0 deletions runnable/ldc_cpp_eh.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// EXTRA_CPP_SOURCES: ldc_cpp_eh1.cpp

extern(C++) bool test_eh();

extern(C++)
void throwException()
{
throw new Exception("Hello C++");
}

void main()
{
bool rc = test_eh();
assert(rc);
}

0 comments on commit 7cf0d4b

Please sign in to comment.