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

Commit 7cf0d4b

Browse files
committed
add test for catching D exceptions from C++ on Win32
1 parent 9c840a1 commit 7cf0d4b

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ endif
134134

135135
ifeq ($(OS),win32)
136136
DISABLED_FAIL_TESTS += fail13939
137+
else
138+
DISABLED_TESTS += ldc_cpp_eh
137139
endif
138140

139141
####

d_do_test.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ bool collectExtraSources (in string input_dir, in string output_dir, in string[]
373373
{
374374
if (msc)
375375
{
376-
command ~= ` /c /nologo `~curSrc~` /Fo`~curObj;
376+
command ~= ` /c /nologo /EHsc `~curSrc~` /Fo`~curObj;
377377
}
378378
else if (envData.os == "win32")
379379
{

runnable/extra-files/dexcept.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// D exceptions to by caught by C++
2+
3+
#pragma once
4+
5+
namespace D {
6+
7+
struct string
8+
{
9+
size_t length;
10+
const char* ptr;
11+
};
12+
13+
class Object;
14+
15+
extern "C" {
16+
string _d_toString(Object* o);
17+
}
18+
19+
class Object
20+
{
21+
void* __monitor;
22+
23+
virtual void _classinfo_data() = 0;
24+
25+
protected:
26+
// don't call directly, ABI mismatch
27+
virtual string _toString();
28+
virtual size_t _toHash();
29+
virtual int _opCmp(Object* o);
30+
virtual bool _opEquals(Object* o);
31+
32+
public:
33+
string toString() { return _d_toString(this); }
34+
};
35+
36+
class Throwable : public Object
37+
{
38+
public:
39+
string msg; /// A message describing the error.
40+
41+
/**
42+
* The _file name and line number of the D source code corresponding with
43+
* where the error was thrown from.
44+
*/
45+
string file;
46+
size_t line; /// ditto
47+
48+
void* info;
49+
Throwable* next;
50+
51+
virtual string _toString();
52+
};
53+
54+
class Exception : public Throwable
55+
{
56+
};
57+
58+
class Error : public Throwable
59+
{
60+
Throwable* bypassedException;
61+
};
62+
63+
} // namespace D
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "dexcept.h"
2+
#include <string.h>
3+
#include <assert.h>
4+
5+
void throwException();
6+
7+
bool test_eh()
8+
{
9+
try
10+
{
11+
throwException();
12+
}
13+
catch (D::Exception* excpt)
14+
{
15+
D::string s = excpt->toString();
16+
assert(memcmp(s.ptr + s.length - 9, "Hello C++", 9) == 0);
17+
return true;
18+
}
19+
catch(...)
20+
{
21+
assert(false);
22+
}
23+
return false;
24+
}

runnable/ldc_cpp_eh.d

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// EXTRA_CPP_SOURCES: ldc_cpp_eh1.cpp
2+
3+
extern(C++) bool test_eh();
4+
5+
extern(C++)
6+
void throwException()
7+
{
8+
throw new Exception("Hello C++");
9+
}
10+
11+
void main()
12+
{
13+
bool rc = test_eh();
14+
assert(rc);
15+
}

0 commit comments

Comments
 (0)