www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When to call setAssertHandler?

reply Benjamin Thaut <code benjamin-thaut.de> writes:
So I want to install my own assertHandler. The problem is, that even if 
I call "setAssetHandler" in a shared module constructor, and that module 
does not import any other modules, it is still not initialized first. Is 
there a way to set the assert before any module constructors run without 
hacking druntime?

Kind Regards
Benjamin Thaut
Mar 22 2013
next sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
On Mar 22, 2013, at 2:58 AM, Benjamin Thaut <code benjamin-thaut.de> =
wrote:

 So I want to install my own assertHandler. The problem is, that even =
if I call "setAssetHandler" in a shared module constructor, and that = module does not import any other modules, it is still not initialized = first. Is there a way to set the assert before any module constructors = run without hacking druntime? I'm afraid not. I suppose this is a case I should have handled, but it = didn't occur to me at the time.=
Mar 25 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 25.03.2013 23:49, schrieb Sean Kelly:
 On Mar 22, 2013, at 2:58 AM, Benjamin Thaut <code benjamin-thaut.de> wrote:

 So I want to install my own assertHandler. The problem is, that even if I call
"setAssetHandler" in a shared module constructor, and that module does not
import any other modules, it is still not initialized first. Is there a way to
set the assert before any module constructors run without hacking druntime?
I'm afraid not. I suppose this is a case I should have handled, but it didn't occur to me at the time.
Do you have some idea how to solve this in a generic way? Is this even possible without adding another feature to the language? Kind Regards Benjamin Thaut
Mar 26 2013
parent reply Sean Kelly <sean invisibleduck.org> writes:
On Mar 26, 2013, at 11:37 AM, Benjamin Thaut <code benjamin-thaut.de> =
wrote:

 Am 25.03.2013 23:49, schrieb Sean Kelly:
 On Mar 22, 2013, at 2:58 AM, Benjamin Thaut <code benjamin-thaut.de> =
wrote:
=20
 So I want to install my own assertHandler. The problem is, that even =
if I call "setAssetHandler" in a shared module constructor, and that = module does not import any other modules, it is still not initialized = first. Is there a way to set the assert before any module constructors = run without hacking druntime?
=20
 I'm afraid not.  I suppose this is a case I should have handled, but =
it didn't occur to me at the time.
=20
 Do you have some idea how to solve this in a generic way? Is this even =
possible without adding another feature to the language? I'd have to give it some thought. My first idea for solving the problem = (looking for a specific named function using dlsym) should work but it = seems like a hack. The easiest fix without any library changes would be = to have all the modules in your app that you want to run after the hook = is set import the module that initializes the hook.=
Mar 27 2013
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 27.03.2013 20:04, schrieb Sean Kelly:
 On Mar 26, 2013, at 11:37 AM, Benjamin Thaut <code benjamin-thaut.de> wrote:

 Am 25.03.2013 23:49, schrieb Sean Kelly:
 On Mar 22, 2013, at 2:58 AM, Benjamin Thaut <code benjamin-thaut.de> wrote:

 So I want to install my own assertHandler. The problem is, that even if I call
"setAssetHandler" in a shared module constructor, and that module does not
import any other modules, it is still not initialized first. Is there a way to
set the assert before any module constructors run without hacking druntime?
I'm afraid not. I suppose this is a case I should have handled, but it didn't occur to me at the time.
Do you have some idea how to solve this in a generic way? Is this even possible without adding another feature to the language?
I'd have to give it some thought. My first idea for solving the problem (looking for a specific named function using dlsym) should work but it seems like a hack. The easiest fix without any library changes would be to have all the modules in your app that you want to run after the hook is set import the module that initializes the hook.
Well I'm not afraid of library changes, I already have a heavily modified version of druntime and phobos. But whenever I find a problem within D I strive to find a solution which works for everyone ;-) -- Kind Regards Benjamin Thaut
Mar 28 2013
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-03-22 10:58, Benjamin Thaut wrote:
 So I want to install my own assertHandler. The problem is, that even if
 I call "setAssetHandler" in a shared module constructor, and that module
 does not import any other modules, it is still not initialized first. Is
 there a way to set the assert before any module constructors run without
 hacking druntime?
As a workaround you could try overriding "onAssertError" and/or "onAssertErrorMsg". https://github.com/D-Programming-Language/druntime/blob/master/src/core/exception.d#L393 -- /Jacob Carlborg
Mar 26 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 26.03.2013 10:09, schrieb Jacob Carlborg:
 On 2013-03-22 10:58, Benjamin Thaut wrote:
 So I want to install my own assertHandler. The problem is, that even if
 I call "setAssetHandler" in a shared module constructor, and that module
 does not import any other modules, it is still not initialized first. Is
 there a way to set the assert before any module constructors run without
 hacking druntime?
As a workaround you could try overriding "onAssertError" and/or "onAssertErrorMsg". https://github.com/D-Programming-Language/druntime/blob/master/src/core/exception.d#L393
Yes, but this workaround does not work for windows 64 bit as the microsoft linker complains about duplicate symbols. Also I think it will only work with dmd because all other compiles (I tested with gdc) also complain about duplicate symbols. Kind Regards Benjamin Thaut
Mar 26 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-03-26 19:35, Benjamin Thaut wrote:

 Yes, but this workaround does not work for windows 64 bit as the
 microsoft linker complains about duplicate symbols. Also I think it will
 only work with dmd because all other compiles (I tested with gdc) also
 complain about duplicate symbols.
I suspected that. -- /Jacob Carlborg
Mar 26 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-03-22 10:58, Benjamin Thaut wrote:
 So I want to install my own assertHandler. The problem is, that even if
 I call "setAssetHandler" in a shared module constructor, and that module
 does not import any other modules, it is still not initialized first. Is
 there a way to set the assert before any module constructors run without
 hacking druntime?
What is your use case? Do you want to use assert in a module constructor? Or is it when running unit tests. I mean, why can't use set it in "main". If it's for unit testing, could you setup your own unit test runner that calls "setAssetHandler" before running the tests? -- /Jacob Carlborg
Mar 28 2013
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 28.03.2013 08:15, schrieb Jacob Carlborg:
 On 2013-03-22 10:58, Benjamin Thaut wrote:
 So I want to install my own assertHandler. The problem is, that even if
 I call "setAssetHandler" in a shared module constructor, and that module
 does not import any other modules, it is still not initialized first. Is
 there a way to set the assert before any module constructors run without
 hacking druntime?
What is your use case? Do you want to use assert in a module constructor? Or is it when running unit tests. I mean, why can't use set it in "main". If it's for unit testing, could you setup your own unit test runner that calls "setAssetHandler" before running the tests?
It's because I'm want to use assert in a module constructor. (Well not directly, but functions I call in a module constructor use assert). On Windows x64 I want to use the CrtDebugReport to display any failing assert and I want this to happen for all asserts and not only for asserts that happen after main started. -- Kind Regards Benjamin Thaut
Mar 28 2013