Skip to content

Commit 14133db

Browse files
Aidan63Aidan Lee
andauthored
Haxe friendly root api (#1292)
* Root api * Null checking * Some tests --------- Co-authored-by: Aidan Lee <[email protected]>
1 parent 0d23f85 commit 14133db

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

include/cpp/marshal/Definitions.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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

include/cpp/marshal/RootHandle.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

include/hxcpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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>

test/native/Native.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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(),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)