Skip to content

Commit 4473137

Browse files
Aidan63Aidan Lee
andauthored
Implement __GetHandle for AutoCallables (#1293)
* Override getHandle for build in auto callables * Add handle for adapter callables * Recurse __GetHandle * Revert "Recurse __GetHandle" This reverts commit dc2ed5c. * Update GC tests to avoid same thread stack clearing clang now seems to be doing optimisations which breaks it --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com>
1 parent a20c1cc commit 4473137

6 files changed

Lines changed: 60 additions & 10 deletions

File tree

include/Array.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ namespace hx
14111411
template<class ELEM_> \
14121412
::hx::Callable<value(args_list)> Array_obj<ELEM_>::name##_dyn() \
14131413
{ \
1414-
struct _hx_array_##name : public ::hx::AutoCallable_obj<value(args_list)> \
1414+
struct _hx_array_##name final : public ::hx::AutoCallable_obj<value(args_list)> \
14151415
{ \
14161416
Array<ELEM_> mThis; \
14171417
_hx_array_##name(Array<ELEM_> inThis) : mThis(inThis) \
@@ -1422,6 +1422,7 @@ namespace hx
14221422
{ \
14231423
ret mThis->name(args_call); \
14241424
} \
1425+
void *__GetHandle() const override { return mThis.GetPtr(); } \
14251426
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
14261427
ARRAY_VISIT_FUNC \
14271428
int __Compare(const ::hx::Object* inRhs) const override \
@@ -1460,7 +1461,7 @@ template<class ELEM_>
14601461
template<class TO>
14611462
::hx::Callable<Array<TO>(::hx::Callable<TO(ELEM_)>)> Array_obj<ELEM_>::map_dyn()
14621463
{
1463-
struct _hx_array_map : public ::hx::AutoCallable_obj<Array<TO>(::hx::Callable<TO(ELEM_)>)>
1464+
struct _hx_array_map final : public ::hx::AutoCallable_obj<Array<TO>(::hx::Callable<TO(ELEM_)>)>
14641465
{
14651466
Array<ELEM_> mThis;
14661467
_hx_array_map(Array<ELEM_> inThis) : mThis(inThis)
@@ -1483,6 +1484,10 @@ ::hx::Callable<Array<TO>(::hx::Callable<TO(ELEM_)>)> Array_obj<ELEM_>::map_dyn()
14831484
if (mThis != casted->mThis) return -1;
14841485
return 0;
14851486
}
1487+
void* __GetHandle() const override
1488+
{
1489+
return mThis.GetPtr();
1490+
}
14861491
};
14871492

14881493
return new _hx_array_map(this);

include/hx/Functions.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ namespace hx
130130
return wrapped(args...);
131131
}
132132

133+
void* __GetHandle() const override
134+
{
135+
return wrapped.GetPtr();
136+
}
137+
133138
inline void __Mark(hx::MarkContext* __inCtx) override
134139
{
135140
HX_MARK_MEMBER(wrapped);
@@ -165,6 +170,11 @@ namespace hx
165170
return null();
166171
}
167172

173+
void* __GetHandle() const override
174+
{
175+
return wrapped.GetPtr();
176+
}
177+
168178
inline void __Mark(hx::MarkContext* __inCtx) override
169179
{
170180
HX_MARK_MEMBER(wrapped);
@@ -206,6 +216,11 @@ namespace hx
206216
return wrapped(args...);
207217
}
208218

219+
void* __GetHandle() const override
220+
{
221+
return wrapped.GetPtr();
222+
}
223+
209224
inline void __Mark(hx::MarkContext* __inCtx) override
210225
{
211226
HX_MARK_MEMBER(wrapped);
@@ -295,6 +310,11 @@ namespace hx
295310
wrapped(args...);
296311
}
297312

313+
void* __GetHandle() const override
314+
{
315+
return wrapped.GetPtr();
316+
}
317+
298318
inline void __Mark(hx::MarkContext* __inCtx) override
299319
{
300320
HX_MARK_MEMBER(wrapped);
@@ -336,6 +356,11 @@ namespace hx
336356
wrapped(args...);
337357
}
338358

359+
void* __GetHandle() const override
360+
{
361+
return wrapped.GetPtr();
362+
}
363+
339364
inline void __Mark(hx::MarkContext* __inCtx) override
340365
{
341366
HX_MARK_MEMBER(wrapped);

src/Array.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ namespace cpp
690690
#if (HXCPP_API_LEVEL>=500)
691691
::hx::Callable<bool()> IteratorBase::hasNext_dyn()
692692
{
693-
struct _hx_iterator_hasNext : public ::hx::AutoCallable_obj<bool()>
693+
struct _hx_iterator_hasNext final : public ::hx::AutoCallable_obj<bool()>
694694
{
695695
::hx::ObjectPtr<IteratorBase> __this;
696696

@@ -704,6 +704,11 @@ namespace cpp
704704
return __this->hasNext();
705705
}
706706

707+
void* __GetHandle() const override
708+
{
709+
return __this.GetPtr();
710+
}
711+
707712
void __Mark(hx::MarkContext* __inCtx) final override
708713
{
709714
HX_MARK_MEMBER(__this);
@@ -736,6 +741,11 @@ namespace cpp
736741
return __this->_dynamicNext();
737742
}
738743

744+
void* __GetHandle() const override
745+
{
746+
return __this.GetPtr();
747+
}
748+
739749
void __Mark(hx::MarkContext* __inCtx) final override
740750
{
741751
HX_MARK_MEMBER(__this);
@@ -800,7 +810,7 @@ namespace cpp
800810
#define HX_VARRAY_FUNC(ret, value, name, args_list, func_list, args_call) \
801811
::hx::Callable<value(args_list)> VirtualArray_obj::name##_dyn() \
802812
{ \
803-
struct _hx_virtualarray_##name : public ::hx::AutoCallable_obj<value(args_list)> \
813+
struct _hx_virtualarray_##name final : public ::hx::AutoCallable_obj<value(args_list)> \
804814
{ \
805815
VirtualArray mThis; \
806816
_hx_virtualarray_##name(::cpp::VirtualArray inThis) : mThis(inThis) \
@@ -811,6 +821,7 @@ namespace cpp
811821
{ \
812822
ret mThis->name(args_call); \
813823
} \
824+
void* __GetHandle() const override { return mThis.GetPtr(); } \
814825
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
815826
ARRAY_VISIT_FUNC \
816827
int __Compare(const ::hx::Object* inRhs) const override \

src/Math.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ double Math_obj::POSITIVE_INFINITY = std::numeric_limits<double>::infinity();
5757
#define HX_MATHS_FUNC(value, name, args_list, func_list, args_call) \
5858
::hx::Callable<value(args_list)> Math_obj::name##_dyn() \
5959
{ \
60-
struct _hx_maths_##name : public ::hx::AutoCallable_obj<value(args_list)> \
60+
struct _hx_maths_##name final : public ::hx::AutoCallable_obj<value(args_list)> \
6161
{ \
6262
value HX_LOCAL_RUN(func_list) override \
6363
{ \

src/String.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ String &String::operator+=(const String &inRHS)
22752275
#define HX_STRING_FUNC(value, name, args_list, func_list, args_call) \
22762276
::hx::Callable<value(args_list)> String::name##_dyn() \
22772277
{ \
2278-
struct _hx_string_##name : public ::hx::AutoCallable_obj<value(args_list)> \
2278+
struct _hx_string_##name final : public ::hx::AutoCallable_obj<value(args_list)> \
22792279
{ \
22802280
::String mThis; \
22812281
_hx_string_##name(const ::String& inThis) : mThis(inThis) \
@@ -2290,6 +2290,7 @@ String &String::operator+=(const String &inRHS)
22902290
{ \
22912291
mThis = inThis; \
22922292
} \
2293+
void* __GetHandle() const override { return const_cast<char *>(mThis.raw_ptr()); } \
22932294
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
22942295
STRING_VISIT_FUNC \
22952296
int __Compare(const ::hx::Object* inRhs) const override \

test/haxe/gc/TestGC.hx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ class TestGC extends Test {
3434
return clearStack(count-1);
3535
}
3636

37+
// NOTE : previously the objects below were created in the same thread as the assertions and the clear
38+
// stack function above attempted to remove references to it so it was eligable for collection.
39+
// With the callable changes it seems clang can do some more aggressive optimisations which broke these tests,
40+
// so now the objects are created on a separate thread and we sleep for 1s to give time for the threads to exit and unregister from the GC.
3741

3842
function createAbc():Void {
3943
var object = { test: "abc" };
4044
Gc.doNotKill(object);
4145
}
4246
public function testObject():Void {
43-
create(createAbc);
47+
sys.thread.Thread.create(createAbc);
48+
Sys.sleep(1);
4449
var zombie:Dynamic = gc();
4550
Assert.notNull(zombie);
4651
Assert.equals("abc", zombie.test);
@@ -68,7 +73,8 @@ class TestGC extends Test {
6873
Gc.doNotKill(object);
6974
};
7075
public function testFunc():Void {
71-
create(createFunction);
76+
sys.thread.Thread.create(createFunction);
77+
Sys.sleep(1);
7278
var zombie:Dynamic = gc();
7379
Assert.notNull(zombie);
7480
Assert.equals("abc", zombie());
@@ -80,7 +86,8 @@ class TestGC extends Test {
8086
Gc.doNotKill(object);
8187
};
8288
public function testCustomObject():Void {
83-
create(createCustom);
89+
sys.thread.Thread.create(createCustom);
90+
Sys.sleep(1);
8491
var zombie = gc();
8592
Assert.notNull(zombie);
8693
Assert.isOfType(zombie, CustomObject);
@@ -92,7 +99,8 @@ class TestGC extends Test {
9299
Gc.doNotKill(object);
93100
};
94101
public function testBytes():Void {
95-
create(createBytes);
102+
sys.thread.Thread.create(createBytes);
103+
Sys.sleep(1);
96104
var zombie = gc();
97105
Assert.notNull(zombie);
98106
Assert.isOfType(zombie, Bytes);

0 commit comments

Comments
 (0)