Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/null.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,20 @@ namespace hx
template<typename T>
struct Null
{
inline Null() : isNull(true) { }
// A null leaves value cleared, not unset: a compiler may read it when it
// computes Default(), as GCC at -O2 turns `isNull ? true : value` into
// `isNull | value`, and an omitted Bool argument came out as stack bytes
inline Null() : isNull(true), value() { }
inline Null(const Null<T> &inOther) : isNull(inOther.isNull), value(inOther.value) { }
inline Null(const T& inVal) : isNull(false), value(inVal) { }
inline Null(const null &) : isNull(true) { }
inline Null(const Dynamic &inVal)
inline Null(const null &) : isNull(true), value() { }
inline Null(const Dynamic &inVal) : value()
{
isNull = null() == inVal;
if (!isNull)
value = inVal;
}
inline Null(const cpp::Variant &inVal)
inline Null(const cpp::Variant &inVal) : value()
{
isNull = inVal.isNull();
if (!isNull)
Expand Down
44 changes: 44 additions & 0 deletions test/regression/NullDefaultArgument/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// A Bool argument with a default, left out, comes as an hx::Null<bool> built
// from null. Its value used to be left unset: GCC at -O2 can turn
// `isNull ? true : value` into `isNull | value`, which then read the bytes the
// caller's frame held, and an omitted `compress:Bool = true` arrived as 23.
// Here each kind of Null is built over bytes that are not a bool, so the value
// it leaves shows whatever the optimizer does with it.
@:cppFileCode('
#include <new>

template<typename T, typename F>
static int valueOver(F make)
{
alignas(hx::Null<T>) unsigned char buffer[sizeof(hx::Null<T>)];
// Through volatile, or the compiler drops the stores, as the bytes of a new object are indeterminate
volatile unsigned char *fill = buffer;
for (int i = 0; i < (int)sizeof(buffer); i++)
fill[i] = 0xFE;
hx::Null<T> *built = make(buffer);
const volatile unsigned char *value = (const volatile unsigned char *)&built->value;
int result = 0;
for (int i = 0; i < (int)sizeof(T); i++)
result |= value[i];
return result;
}
')
class Main {
static function report(what:String, value:Int):Void {
Sys.println(what + ": " + (value == 0 ? "cleared" : "left as " + value));
}

static function main() {
report("Null<bool>()", untyped __cpp__("valueOver<bool>([](void *at) { return new (at) hx::Null<bool>(); })"));
report("Null<bool>(null)", untyped __cpp__("valueOver<bool>([](void *at) { return new (at) hx::Null<bool>(null()); })"));
report("Null<bool>(null Dynamic)", untyped __cpp__("valueOver<bool>([](void *at) { return new (at) hx::Null<bool>(Dynamic()); })"));
report("Null<bool>(null Variant)", untyped __cpp__("valueOver<bool>([](void *at) { return new (at) hx::Null<bool>(cpp::Variant()); })"));
report("Null<int>(null)", untyped __cpp__("valueOver<int>([](void *at) { return new (at) hx::Null<int>(null()); })"));
report("Null<double>(null)", untyped __cpp__("valueOver<double>([](void *at) { return new (at) hx::Null<double>(null()); })"));
Sys.println("default true: " + defaulted());
}

static function defaulted(flag:Bool = true):Bool {
return flag;
}
}
2 changes: 2 additions & 0 deletions test/regression/NullDefaultArgument/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
7 changes: 7 additions & 0 deletions test/regression/NullDefaultArgument/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Null<bool>(): cleared
Null<bool>(null): cleared
Null<bool>(null Dynamic): cleared
Null<bool>(null Variant): cleared
Null<int>(null): cleared
Null<double>(null): cleared
default true: true
Loading