File tree Expand file tree Collapse file tree 5 files changed +98
-0
lines changed
Expand file tree Collapse file tree 5 files changed +98
-0
lines changed Original file line number Diff line number Diff line change @@ -213,6 +213,21 @@ namespace cpp
213213 T& operator[] (int64_t index) const;
214214 };
215215
216+ class RootHandle
217+ {
218+ ::hx ::Object** object;
219+
220+ RootHandle (hx ::Object** obj);
221+
222+ public :
223+ static RootHandle create (Dynamic obj);
224+ static RootHandle fromVoidPointer (::cpp ::Pointer< void> ptr);
225+
226+ ::cpp ::Pointer< void> toVoidPointer ();
227+ Dynamic getObject ();
228+ void close ();
229+ };
230+
216231 struct Marshal final
217232 {
218233# ifdef HXCPP_BIG_ENDIAN
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include " Definitions.inc"
4+
5+ inline cpp::marshal::RootHandle::RootHandle (::hx::Object** obj) : object(obj) {}
6+
7+ inline cpp::marshal::RootHandle cpp::marshal::RootHandle::create (::Dynamic obj)
8+ {
9+ if (null () == obj)
10+ {
11+ hx::NullReference (" Dynamic" , false );
12+ }
13+
14+ auto root = new hx::Object* { obj.mPtr };
15+
16+ hx::GCAddRoot (root);
17+
18+ return RootHandle (root);
19+ }
20+
21+ inline cpp::marshal::RootHandle cpp::marshal::RootHandle::fromVoidPointer (cpp::Pointer<void > ptr)
22+ {
23+ if (nullptr == ptr.ptr )
24+ {
25+ hx::NullReference (" Dynamic" , false );
26+ }
27+
28+ return RootHandle (static_cast <hx::Object**>(ptr.ptr ));
29+ }
30+
31+ inline void cpp::marshal::RootHandle::close ()
32+ {
33+ hx::GCRemoveRoot (object);
34+
35+ delete object;
36+ }
37+
38+ inline ::Dynamic cpp::marshal::RootHandle::getObject ()
39+ {
40+ return ::Dynamic{ *object };
41+ }
42+
43+ inline cpp::Pointer<void > cpp::marshal::RootHandle::toVoidPointer ()
44+ {
45+ return object;
46+ }
Original file line number Diff line number Diff line change @@ -358,6 +358,7 @@ typedef PropertyAccessMode PropertyAccess;
358358#include < cpp/marshal/PointerReference.hpp>
359359#include < cpp/marshal/View.hpp>
360360#include < cpp/marshal/Marshal.hpp>
361+ #include < cpp/marshal/RootHandle.hpp>
361362#include < cpp/encoding/Ascii.hpp>
362363#include < cpp/encoding/Utf8.hpp>
363364#include < cpp/encoding/Utf16.hpp>
Original file line number Diff line number Diff line change @@ -47,6 +47,8 @@ class Native
4747 new tests.marshalling.view. TestMarshal (),
4848 new tests.marshalling.view. TestViewExtensions (),
4949
50+ new tests.marshalling.root. TestRoot (),
51+
5052 new tests.encoding. TestAscii (),
5153 new tests.encoding. TestUtf8 (),
5254 new tests.encoding. TestUtf16 (),
Original file line number Diff line number Diff line change 1+ package tests .marshalling .root ;
2+
3+ import cpp .marshal .RootHandle ;
4+ import utest .Assert ;
5+ import utest .Test ;
6+
7+ class TestRoot extends Test {
8+ function test_null_object () {
9+ Assert .raises (() -> RootHandle .create (null ));
10+ }
11+
12+ function test_null_void_pointer () {
13+ Assert .raises (() -> RootHandle .fromVoidPointer (null ));
14+ }
15+
16+ function test_get_object () {
17+ final obj = " Hello, World!" ;
18+ final handle = RootHandle .create (obj );
19+
20+ Assert .equals (obj , handle .getObject ());
21+
22+ handle .close ();
23+ }
24+
25+ function test_void_pointer_roundtrip () {
26+ final obj = " Hello, World!" ;
27+ final ptr = RootHandle .create (obj ).toVoidPointer ();
28+ final handle = RootHandle .fromVoidPointer (ptr );
29+
30+ Assert .equals (obj , handle .getObject ());
31+
32+ handle .close ();
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments