-
Notifications
You must be signed in to change notification settings - Fork 3
/
Library.pas
338 lines (294 loc) · 11.4 KB
/
Library.pas
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
namespace RemObjects.Marzipan;
interface
uses
mono.utils,
mono.jit,
mono.metadata,
Foundation;
type
MZString = public class(MZObject)
private
method get_length: Integer;
method get_NSString: NSString;
class var fLength: method(aInstance: ^MonoObject; aEx: ^^MonoException): Integer;
class var fType: MZType := MZMonoRuntime.sharedInstance.getCoreType('System.String');
public
class method getType: MZType; override;
class method stringWithNSString(s: NSString): MZString;
class method MonoStringWithNSString(s: NSString): ^MonoString;
class method NSStringWithMonoString(s: ^MonoString): NSString;
property length: Integer read get_length;
property NSString: NSString read get_NSString;
end;
MZDateTime = public int64_t;
MZArray = public class(MZObject, sequence of id)
private
fNSArray: NSArray;
public
constructor withMonoInstance(aInst: ^MonoObject) elementType(aType: &Class);
constructor withNSArray(aArray: NSArray);
//constructor withArray<T>(aArray: array of T);
constructor withArray(aArray: array of String);
property &type: &Class := typeOf(MZObject);
property elements: ^^MonoObject read ^^MonoObject(mono_array_addr_with_size(^MonoArray(__instance), sizeOf(^MonoObject), 0));
property count: NSUInteger read mono_array_length(^MonoArray(__instance));
property «Count»: NSUInteger read count;
property Length: NSUInteger read count;
method objectAtIndex(aIndex: Integer): id;
method objectAtIndexedSubscript(aIndex: Integer): id;
method setObject(aObject: NSObject) atIndexedSubscript(aValue: Integer);
method countByEnumeratingWithState(state: ^NSFastEnumerationState) objects(buffer: ^id) count(len: NSUInteger): NSUInteger;
method NSArray: NSArray;
end;
MZObjectList = public class(MZObject, INSFastEnumeration)
assembly
fSize: ^Int32;
fItems: ^^MonoArray;
fLastItems: ^MonoArray;
fArray: MZArray;
fNSArray: NSArray;
class var fSizeField: ^MonoClassField;
class var fItemsField: ^MonoClassField;
method get_count: NSUInteger;
public
property &type: &Class := typeOf(MZObject);
constructor withNSArray(aNSArray: NSArray);
constructor withObject(aObject: id);
constructor withMonoInstance(aInst: ^MonoObject) elementType(aType: &Class);
method clear;
property count: NSUInteger read get_count;
property «Count»: NSUInteger read count;
method objectAtIndex(aIndex: Integer): id;
method objectAtIndexedSubscript(aIndex: Integer): id;
method countByEnumeratingWithState(state: ^NSFastEnumerationState) objects(buffer: ^id) count(len: NSUInteger): NSUInteger;
method NSArray: NSArray;
end;
RemObjects.Marzipan.Generic.MZObjectList<T> = public class(INSFastEnumeration<T>) mapped to MZObjectList
where T is class;
public
property count: NSUInteger read mapped.count;
//property «Count»: NSUInteger read count;
method objectAtIndex(aIndex: Integer): T; mapped to objectAtIndex(aIndex);
method objectAtIndexedSubscript(aIndex: Integer): T; mapped to objectAtIndexedSubscript(aIndex);
end;
NSString_Marzipan_Helpers = public extension class(NSString)
public
class method stringwithMonoString(s: ^MonoString): NSString;
method MonoString: ^MonoString;
end;
implementation
{ MZString }
class method MZString.getType: MZType;
begin
exit fType;
end;
method MZString.get_length: Integer;
begin
if fLength = nil then
^^Void(@fLength)^ := fType.getMethodThunk(':get_Length()');
var ex: ^MonoException := nil;
result := fLength(__instance, @ex);
if ex <> nil then raiseException(ex);
end;
class method MZString.stringWithNSString(s: NSString): MZString;
begin
if s = nil then exit nil;
exit new MZString withMonoInstance(^MonoObject(mono_string_from_utf16(^mono_unichar2(s.cStringUsingEncoding(NSStringEncoding.NSUnicodeStringEncoding)))));
end;
method MZString.get_NSString: NSString;
begin
exit Foundation.NSString.stringWithCharacters(^unichar(mono_string_chars(^MonoString(__instance)))) length(mono_string_length(^MonoString(__instance)));
end;
class method MZString.NSStringWithMonoString(s: ^MonoString): NSString;
begin
if s = nil then exit nil;
exit Foundation.NSString.stringWithCharacters(^unichar(mono_string_chars(^MonoString(s)))) length(mono_string_length(^MonoString(s)));
end;
class method MZString.MonoStringWithNSString(s: NSString): ^MonoString;
begin
if s = nil then exit nil;
exit mono_string_new_wrapper(s.cStringUsingEncoding(NSStringEncoding.NSUTF8StringEncoding));
end;
class method NSString_Marzipan_Helpers.stringwithMonoString(s: ^MonoString): NSString;
begin
if s = nil then exit nil;
exit Foundation.NSString.stringWithCharacters(^unichar(mono_string_chars(^MonoString(s)))) length(mono_string_length(^MonoString(s)));
end;
method NSString_Marzipan_Helpers.MonoString: ^MonoString;
begin
if self = nil then exit nil;
exit mono_string_from_utf16(^mono_unichar2(self.cStringUsingEncoding(NSStringEncoding.NSUnicodeStringEncoding)));
end;
{ MZArray }
constructor MZArray withMonoInstance(aInst: ^MonoObject) elementType(aType: &Class);
begin
self := inherited initWithMonoInstance(aInst);
if assigned(self) then begin
&type := aType;
end;
result := self;
end;
constructor MZArray withNSArray(aArray: NSArray);
begin
if aArray.count > 0 then begin
self := inherited initWithMonoInstance(mono_array_new(MZMonoRuntime.sharedInstance.domain, (aArray[0] as MZObject).getClass(), aArray.count) as ^MonoObject);
for i: Integer := 0 to aArray.count-1 do begin
var lInst := MZObject(aArray[i]):__instance;
elements[i] := lInst;
end;
end
else begin
self := inherited initWithMonoInstance(mono_array_new(MZMonoRuntime.sharedInstance.domain, mono_class_from_mono_type(MZMonoRuntime.sharedInstance.getCoreType('System.Object').type), 0) as ^MonoObject);
end;
end;
//constructor MZArray withArray<T>(aArray: array of T);
//begin
//if length(aArray) > 0 then begin
//self := inherited initWithMonoInstance(mono_array_new(MZMonoRuntime.sharedInstance.domain, (aArray[0] as MZObject).getClass(), length(aArray)) as ^MonoObject);
//for i: Integer := 0 to length(aArray)-1 do begin
//var lInst := MZObject(aArray[i]):__instance;
//elements[i] := lInst;
//end;
//end
//else begin
//self := inherited initWithMonoInstance(mono_array_new(MZMonoRuntime.sharedInstance.domain, mono_class_from_mono_type(MZMonoRuntime.sharedInstance.getCoreType('System.Object').type), 0) as ^MonoObject);
//end;
//end;
constructor MZArray withArray(aArray: array of String);
begin
self := inherited initWithMonoInstance(mono_array_new(MZMonoRuntime.sharedInstance.domain, mono_class_from_mono_type(MZString.getType.type), RemObjects.Elements.System.length(aArray)) as ^MonoObject);
for i: Integer := 0 to RemObjects.Elements.System.length(aArray)-1 do begin
var lInst := MZString.MonoStringWithNSString(aArray[i]) as ^MonoObject;
elements[i] := lInst;
end;
end;
method MZArray.objectAtIndex(aIndex: Integer): id;
begin
var lItem := elements[aIndex];
if lItem = nil then exit nil;
if &type = typeOf(NSString) then begin
exit MZString.NSStringWithMonoString(^MonoString(lItem));
end;
var lTmp := &type.alloc();
exit id(lTmp).initWithMonoInstance(lItem);
end;
method MZArray.objectAtIndexedSubscript(aIndex: Integer): id;
begin
var lItem := elements[aIndex];
if lItem = nil then exit nil;
if &type = typeOf(NSString) then begin
exit MZString.NSStringWithMonoString(^MonoString(lItem));
end;
var lTmp := &type.alloc();
exit id(lTmp).initWithMonoInstance(lItem);
end;
method MZArray.setObject(aObject: NSObject) atIndexedSubscript(aValue: Integer);
begin
if &type = typeOf(NSString) then
elements[aValue] := MZString.stringWithNSString(NSString(aObject)):__instance
else begin
var lInst := MZObject(aObject):__instance;
elements[aValue] := lInst;
end;
end;
method MZArray.NSArray: NSArray;
begin
if fNSArray = nil then begin
var lTmp := new NSMutableArray withCapacity(count);
var lElements := elements;
if &type = typeOf(String) then begin
for i: Integer := 0 to count -1 do
lTmp[i] := MZString.NSStringWithMonoString(^MonoString(lElements[i]));
end
else begin
for i: Integer := 0 to count -1 do
lTmp[i] := id(&type.alloc()).initWithMonoInstance(lElements[i]);
end;
fNSArray := lTmp;
end;
result := fNSArray;
end;
method MZArray.countByEnumeratingWithState(state: ^NSFastEnumerationState) objects(buffer: ^id) count(len: NSUInteger): NSUInteger;
begin
result := NSArray.countByEnumeratingWithState(state) objects(buffer) count(len);
end;
{ MZObjectList }
constructor MZObjectList withNSArray(aNSArray: NSArray);
begin
fNSArray := aNSArray;
end;
constructor MZObjectList withObject(aObject: id);
begin
fNSArray := Foundation.NSArray.arrayWithObject(aObject);
end;
constructor MZObjectList withMonoInstance(aInst: ^MonoObject) elementType(aType: &Class);
begin
self := inherited initWithMonoInstance(aInst);
if assigned(self) then begin
&type := aType;
end;
result := self;
end;
method MZObjectList.clear;
begin
if fItems = nil then MZObjectListInitFields(self); // global methods optimize better.
if (fItems^ <> fLastItems) or (fArray = nil) then MZObjectListLoadArray(self);
for i: Integer := count -1 downto 0 do // just unset the objects and release them.
fArray.setObject(nil) atIndexedSubscript(i);
fSize^ := 0;
end;
method MZObjectList.objectAtIndex(aIndex: Integer): id;
begin
if fItems = nil then MZObjectListInitFields(self); // global methods optimize better.
if (fItems^ <> fLastItems) or (fArray = nil) then MZObjectListLoadArray(self);
exit fArray[aIndex];
end;
method MZObjectList.objectAtIndexedSubscript(aIndex: Integer): id;
begin
if fItems = nil then MZObjectListInitFields(self); // global methods optimize better.
if (fItems^ <> fLastItems) or (fArray = nil) then MZObjectListLoadArray(self);
exit fArray[aIndex];
end;
method MZObjectList.countByEnumeratingWithState(state: ^NSFastEnumerationState) objects(buffer: ^id) count(len: NSUInteger): NSUInteger;
begin
result := NSArray.countByEnumeratingWithState(state) objects(buffer) count(len);
end;
method MZObjectList.get_count: NSUInteger;
begin
if fItems = nil then MZObjectListInitFields(self);
exit fSize^;
end;
method MZObjectList.NSArray: NSArray;
begin
if fNSArray = nil then begin
if fItems = nil then MZObjectListInitFields(self);
if (fArray = nil) then MZObjectListLoadArray(self);
var lTmp := new NSMutableArray withCapacity(count);
for i: Integer := 0 to count-1 do
lTmp[i] := fArray[i];
fNSArray := lTmp;
end;
result := fNSArray;
end;
method MZObjectListInitFields(aInst: MZObjectList);
begin
if MZObjectList.fSizeField = nil then begin
var lClass := mono_object_get_class(aInst.__instance);
MZObjectList.fSizeField := mono_class_get_field_from_name(lClass, '_size');
MZObjectList.fItemsField := mono_class_get_field_from_name(lClass, '_items');
end;
aInst.fSize := ^Int32(^Byte(aInst.__instance) + mono_field_get_offset(MZObjectList.fSizeField));
aInst.fItems := ^^MonoArray(^Byte(aInst.__instance) + mono_field_get_offset(MZObjectList.fItemsField));
end;
method MZObjectListLoadArray(aInst: MZObjectList);
begin
var lItems := aInst.fItems^;
aInst.fLastItems := lItems;
if lItems = nil then begin
aInst.fArray := nil;
exit;
end;
aInst.fArray := new MZArray withMonoInstance(^MonoObject(lItems));
aInst.fArray.type := aInst.type;
end;
end.