Way to override/overload D’s runtime assertions to use custom handlers?
Mike Franklin
slavo5150 at yahoo.com
Wed Jul 25 16:20:15 UTC 2018
On Wednesday, 25 July 2018 at 15:24:50 UTC, Alexander Nicholi
wrote:
> Is there a way to change this to use our own handlers with the
> D runtime?
You can provide your own implementations of the runtime hooks at
https://github.com/dlang/druntime/blob/cb5efa9854775c5a72acd6870083b16e5ebba369/src/core/exception.d#L628
extern(C) void _d_assertp(immutable(char)* file, uint line)
{
import core.stdc.stdio;
printf("Houston, we have a problem at %s:%u\n", file, line);
}
void main()
{
assert(false);
}
Try it out at https://run.dlang.io/is/QZEO9W
> How does this change without the runtime, e.g. via `-betterC`
> code?
Unfortunately, this doesn't work with -betterC because -betterC
seems to forward runtime assertions to the C implementation. See
https://run.dlang.io/is/QZEO9W
For that you have to provide a new implementation of `__assert`:
extern(C) void __assert(const char *msg, const char *file, int
line)
{
import core.stdc.stdio;
printf("Houston, we have a problem at %s:%u\n", file, line);
}
extern(C) void main()
{
assert(false);
}
https://run.dlang.io/is/D5JxCT
Mike
More information about the Digitalmars-d
mailing list