This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for catching D exceptions from C++ on Win32
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |