forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjcData.mm
More file actions
264 lines (215 loc) · 6.18 KB
/
Copy pathObjcData.mm
File metadata and controls
264 lines (215 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <hxcpp.h>
#include <math.h>
#include <hxMath.h>
#include <stdio.h>
using namespace hx;
@implementation NSHaxeWrapperClass
- (id)init:( hx::Object * ) inHaxe {
self = [super init];
haxeObject = inHaxe;
hx::GCAddRoot(&haxeObject);
if (self!=nil) {
}
return self;
}
- (void)dealloc {
GCRemoveRoot(&haxeObject);
#ifndef OBJC_ARC
[super dealloc];
#endif
}
@end
id _hx_value_to_objc(Dynamic d)
{
if (!d.mPtr)
return nil;
switch(d->__GetType())
{
case vtNull:
case vtUnknown:
return nil;
case vtInt:
case vtBool:
return [ NSNumber numberWithInt: (int)d ];
case vtInt64:
return [ NSNumber numberWithLongLong: (cpp::Int64)d ];
case vtFloat:
return [ NSNumber numberWithDouble: (double)d ];
case vtString:
return (NSString *)String(d);
case vtObject:
return _hx_obj_to_nsdictionary(d);
case vtArray:
{
Array_obj<unsigned char> *bytes = dynamic_cast< Array_obj<unsigned char> * >(d.mPtr);
if (bytes)
{
if (!bytes->length)
return [NSData alloc];
else
return [NSData dataWithBytes:bytes->GetBase() length:bytes->length];
}
else
{
cpp::VirtualArray varray = d;
int len = varray->get_length();
NSMutableArray *array = [NSMutableArray arrayWithCapacity:len];
for(int i=0;i<len;i++)
array[i] = _hx_value_to_objc( varray->__get(i) );
return array;
}
}
case vtClass:
if (d->__GetClass()->mName==HX_CSTRING("haxe.io.Bytes"))
{
return _hx_value_to_objc( d->__Field( HX_CSTRING("b"), hx::paccDynamic ) );
}
// else fallthough...
case vtFunction:
case vtEnum:
case vtAbstractBase:
{
return (NSString *)d->toString();
}
}
return nil;
}
NSDictionary<NSString *, id> *_hx_obj_to_nsdictionary(Dynamic d)
{
if (d==null())
return nil;
Array<String> fields = Array_obj<String>::__new();
d->__GetFields(fields);
NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:fields->length ];
NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:fields->length ];
for(int i=0; i <fields->length; i++)
{
objects[i] = _hx_value_to_objc( d->__Field(fields[i], hx::paccDynamic ) );
keys[i] = (NSString *)fields[i];
}
NSDictionary *result = [NSDictionary<NSString *,id> dictionaryWithObjects:objects forKeys:keys];
return result;
}
Array<unsigned char> _hx_objc_to_bytes(id value)
{
NSData *data = value;
return Array_obj<unsigned char>::fromData((const unsigned char *)data.bytes, data.length);
}
hx::Val _hx_objc_to_value(id value)
{
if (value==nil)
return null();
else if ([value isKindOfClass:[NSNumber class]])
return [value floatValue];
else if ([value isKindOfClass:[NSString class]])
return String( (NSString *)value );
else if ([value isKindOfClass:[NSDictionary class]])
return _hx_nsdictionary_to_obj( (NSDictionary *) value);
else if ([value isKindOfClass:[NSArray class]])
{
NSArray *array = value;
cpp::VirtualArray varray = cpp::VirtualArray_obj::__new();
for(id object in array)
varray->push( _hx_objc_to_value(object) );
return varray;
}
else if ([value isKindOfClass:[NSData class]])
{
return _hx_objc_to_bytes(value);
}
else
return String( [value stringValue] );
}
Dynamic _hx_nsdictionary_to_obj(NSDictionary<NSString *, id> *inDictionary)
{
if (!inDictionary)
return null();
hx::Anon obj = new hx::Anon_obj();
for (NSString *key in inDictionary)
{
id value = inDictionary[key];
obj->__SetField( String(key), _hx_objc_to_value(value), hx::paccDynamic);
}
return obj;
}
Dynamic _hx_objc_to_dynamic(id inValue)
{
return _hx_objc_to_value(inValue);
}
namespace hx {
extern hx::Class __ObjcClass;
class ObjcData : public hx::Object
{
public:
inline void *operator new( size_t inSize, hx::NewObjectType inAlloc=NewObjAlloc,const char *inName="objc.BoxedType")
{ return hx::Object::operator new(inSize,inAlloc,inName); }
ObjcData(const id inValue) : mValue(inValue)
{
#ifndef OBJC_ARC
[ inValue retain ];
#endif
mFinalizer = new hx::InternalFinalizer(this,clean);
};
static void clean(hx::Object *inObj)
{
ObjcData *m = dynamic_cast<ObjcData *>(inObj);
if (m)
{
#ifndef OBJC_ARC
[m->mValue release];
#else
m->mValue = nil;
#endif
}
}
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext *__inCtx) { mFinalizer->Visit(__inCtx); }
#endif
hx::Class __GetClass() const { return hx::__ObjcClass; }
#if (HXCPP_API_LEVEL<331)
bool __Is(hx::Object *inClass) const { return dynamic_cast< ObjcData *>(inClass); }
#endif
// k_cpp_objc
int __GetType() const { return vtAbstractBase + 4; }
#ifdef OBJC_ARC
void * __GetHandle() const { return (__bridge void *) mValue; }
#else
void * __GetHandle() const { return (void *) mValue; }
#endif
String toString()
{
return String(!mValue ? "null" : [[mValue description] UTF8String]);
}
String __ToString() const { return String(!mValue ? "null" : [[mValue description] UTF8String]); }
int __Compare(const hx::Object *inRHS) const
{
if (!inRHS)
return mValue == 0 ? 0 : 1;
const ObjcData *data = dynamic_cast< const ObjcData *>(inRHS);
if (data)
{
return [data->mValue isEqual:mValue] ? 0 : mValue < data->mValue ? -1 : 1;
}
else
{
void *r = inRHS->__GetHandle();
#ifdef OBJC_ARC
void * ptr = (__bridge void *) mValue;
#else
void * ptr = (void *) mValue;
#endif
return ptr < r ? -1 : ptr==r ? 0 : 1;
}
}
#ifdef OBJC_ARC
id mValue;
#else
const id mValue;
#endif
hx::InternalFinalizer *mFinalizer;
};
}
Dynamic::Dynamic(const id inVal)
{
mPtr = inVal ? (hx::Object *)new ObjcData(inVal) : 0;
}