ObjCRuntime.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. {
  2. * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
  7. *
  8. * This file contains Original Code and/or Modifications of Original Code
  9. * as defined in and that are subject to the Apple Public Source License
  10. * Version 2.0 (the 'License'). You may not use this file except in
  11. * compliance with the License. Please obtain a copy of the License at
  12. * http://www.opensource.apple.com/apsl/ and read it before using this
  13. * file.
  14. *
  15. * The Original Code and all software distributed under the License are
  16. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  17. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  18. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  20. * Please see the License for the specific language governing rights and
  21. * limitations under the License.
  22. *
  23. * @APPLE_LICENSE_HEADER_END@
  24. }
  25. {
  26. Pascal translation by Adriaan van Os <[email protected]>, April 2008
  27. * objc-class.h
  28. * Copyright 1988-1996, NeXT Software, Inc.
  29. * objc.h
  30. * Copyright 1988-1996, NeXT Software, Inc.
  31. * objc-auto.h
  32. * Copyright 2004 Apple Computer, Inc.
  33. * objc_exception.h
  34. Support for Objective-C language Exceptions
  35. Created by Blaine Garst on Fri Nov 01 2002.
  36. * Copyright (c) 2002-3 Apple Computer, Inc. All rights reserved.
  37. * objc_sync.h
  38. * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
  39. * objc-runtime.h
  40. * Copyright 1988-1996, NeXT Software, Inc.
  41. }
  42. {
  43. Modified for use with Free Pascal
  44. Version 308
  45. Please report any bugs to <[email protected]>
  46. }
  47. {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
  48. {$mode macpas}
  49. {$packenum 1}
  50. {$macro on}
  51. {$inline on}
  52. {$calling mwpascal}
  53. unit ObjCRuntime;
  54. interface
  55. {$setc UNIVERSAL_INTERFACES_VERSION := $0400}
  56. {$setc GAP_INTERFACES_VERSION := $0308}
  57. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  58. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  59. {$endc}
  60. {$ifc defined CPUPOWERPC and defined CPUI386}
  61. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  62. {$endc}
  63. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  64. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  65. {$endc}
  66. {$ifc not defined __ppc__ and defined CPUPOWERPC32}
  67. {$setc __ppc__ := 1}
  68. {$elsec}
  69. {$setc __ppc__ := 0}
  70. {$endc}
  71. {$ifc not defined __ppc64__ and defined CPUPOWERPC64}
  72. {$setc __ppc64__ := 1}
  73. {$elsec}
  74. {$setc __ppc64__ := 0}
  75. {$endc}
  76. {$ifc not defined __i386__ and defined CPUI386}
  77. {$setc __i386__ := 1}
  78. {$elsec}
  79. {$setc __i386__ := 0}
  80. {$endc}
  81. {$ifc not defined __x86_64__ and defined CPUX86_64}
  82. {$setc __x86_64__ := 1}
  83. {$elsec}
  84. {$setc __x86_64__ := 0}
  85. {$endc}
  86. {$ifc not defined __arm__ and defined CPUARM}
  87. {$setc __arm__ := 1}
  88. {$elsec}
  89. {$setc __arm__ := 0}
  90. {$endc}
  91. {$ifc not defined __arm64__ and defined CPUAARCH64}
  92. {$setc __arm64__ := 1}
  93. {$elsec}
  94. {$setc __arm64__ := 0}
  95. {$endc}
  96. {$ifc defined cpu64}
  97. {$setc __LP64__ := 1}
  98. {$elsec}
  99. {$setc __LP64__ := 0}
  100. {$endc}
  101. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  102. {$error Conflicting definitions for __ppc__ and __i386__}
  103. {$endc}
  104. {$ifc defined __ppc__ and __ppc__}
  105. {$setc TARGET_CPU_PPC := TRUE}
  106. {$setc TARGET_CPU_PPC64 := FALSE}
  107. {$setc TARGET_CPU_X86 := FALSE}
  108. {$setc TARGET_CPU_X86_64 := FALSE}
  109. {$setc TARGET_CPU_ARM := FALSE}
  110. {$setc TARGET_CPU_ARM64 := FALSE}
  111. {$setc TARGET_OS_MAC := TRUE}
  112. {$setc TARGET_OS_IPHONE := FALSE}
  113. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  114. {$setc TARGET_OS_EMBEDDED := FALSE}
  115. {$elifc defined __ppc64__ and __ppc64__}
  116. {$setc TARGET_CPU_PPC := FALSE}
  117. {$setc TARGET_CPU_PPC64 := TRUE}
  118. {$setc TARGET_CPU_X86 := FALSE}
  119. {$setc TARGET_CPU_X86_64 := FALSE}
  120. {$setc TARGET_CPU_ARM := FALSE}
  121. {$setc TARGET_CPU_ARM64 := FALSE}
  122. {$setc TARGET_OS_MAC := TRUE}
  123. {$setc TARGET_OS_IPHONE := FALSE}
  124. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  125. {$setc TARGET_OS_EMBEDDED := FALSE}
  126. {$elifc defined __i386__ and __i386__}
  127. {$setc TARGET_CPU_PPC := FALSE}
  128. {$setc TARGET_CPU_PPC64 := FALSE}
  129. {$setc TARGET_CPU_X86 := TRUE}
  130. {$setc TARGET_CPU_X86_64 := FALSE}
  131. {$setc TARGET_CPU_ARM := FALSE}
  132. {$setc TARGET_CPU_ARM64 := FALSE}
  133. {$ifc defined(iphonesim)}
  134. {$setc TARGET_OS_MAC := FALSE}
  135. {$setc TARGET_OS_IPHONE := TRUE}
  136. {$setc TARGET_IPHONE_SIMULATOR := TRUE}
  137. {$elsec}
  138. {$setc TARGET_OS_MAC := TRUE}
  139. {$setc TARGET_OS_IPHONE := FALSE}
  140. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  141. {$endc}
  142. {$setc TARGET_OS_EMBEDDED := FALSE}
  143. {$elifc defined __x86_64__ and __x86_64__}
  144. {$setc TARGET_CPU_PPC := FALSE}
  145. {$setc TARGET_CPU_PPC64 := FALSE}
  146. {$setc TARGET_CPU_X86 := FALSE}
  147. {$setc TARGET_CPU_X86_64 := TRUE}
  148. {$setc TARGET_CPU_ARM := FALSE}
  149. {$setc TARGET_CPU_ARM64 := FALSE}
  150. {$ifc defined(iphonesim)}
  151. {$setc TARGET_OS_MAC := FALSE}
  152. {$setc TARGET_OS_IPHONE := TRUE}
  153. {$setc TARGET_IPHONE_SIMULATOR := TRUE}
  154. {$elsec}
  155. {$setc TARGET_OS_MAC := TRUE}
  156. {$setc TARGET_OS_IPHONE := FALSE}
  157. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  158. {$endc}
  159. {$setc TARGET_OS_EMBEDDED := FALSE}
  160. {$elifc defined __arm__ and __arm__}
  161. {$setc TARGET_CPU_PPC := FALSE}
  162. {$setc TARGET_CPU_PPC64 := FALSE}
  163. {$setc TARGET_CPU_X86 := FALSE}
  164. {$setc TARGET_CPU_X86_64 := FALSE}
  165. {$setc TARGET_CPU_ARM := TRUE}
  166. {$setc TARGET_CPU_ARM64 := FALSE}
  167. { will require compiler define when/if other Apple devices with ARM cpus ship }
  168. {$setc TARGET_OS_MAC := FALSE}
  169. {$setc TARGET_OS_IPHONE := TRUE}
  170. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  171. {$setc TARGET_OS_EMBEDDED := TRUE}
  172. {$elifc defined __arm64__ and __arm64__}
  173. {$setc TARGET_CPU_PPC := FALSE}
  174. {$setc TARGET_CPU_PPC64 := FALSE}
  175. {$setc TARGET_CPU_X86 := FALSE}
  176. {$setc TARGET_CPU_X86_64 := FALSE}
  177. {$setc TARGET_CPU_ARM := FALSE}
  178. {$setc TARGET_CPU_ARM64 := TRUE}
  179. { will require compiler define when/if other Apple devices with ARM cpus ship }
  180. {$setc TARGET_OS_MAC := FALSE}
  181. {$setc TARGET_OS_IPHONE := TRUE}
  182. {$setc TARGET_IPHONE_SIMULATOR := FALSE}
  183. {$setc TARGET_OS_EMBEDDED := TRUE}
  184. {$elsec}
  185. {$error __ppc__ nor __ppc64__ nor __i386__ nor __x86_64__ nor __arm__ nor __arm64__ is defined.}
  186. {$endc}
  187. {$ifc defined __LP64__ and __LP64__ }
  188. {$setc TARGET_CPU_64 := TRUE}
  189. {$elsec}
  190. {$setc TARGET_CPU_64 := FALSE}
  191. {$endc}
  192. {$ifc defined FPC_BIG_ENDIAN}
  193. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  194. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  195. {$elifc defined FPC_LITTLE_ENDIAN}
  196. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  197. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  198. {$elsec}
  199. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  200. {$endc}
  201. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  202. {$setc CALL_NOT_IN_CARBON := FALSE}
  203. {$setc OLDROUTINENAMES := FALSE}
  204. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  205. {$setc OPAQUE_UPP_TYPES := TRUE}
  206. {$setc OTCARBONAPPLICATION := TRUE}
  207. {$setc OTKERNEL := FALSE}
  208. {$setc PM_USE_SESSION_APIS := TRUE}
  209. {$setc TARGET_API_MAC_CARBON := TRUE}
  210. {$setc TARGET_API_MAC_OS8 := FALSE}
  211. {$setc TARGET_API_MAC_OSX := TRUE}
  212. {$setc TARGET_CARBON := TRUE}
  213. {$setc TARGET_CPU_68K := FALSE}
  214. {$setc TARGET_CPU_MIPS := FALSE}
  215. {$setc TARGET_CPU_SPARC := FALSE}
  216. {$setc TARGET_OS_UNIX := FALSE}
  217. {$setc TARGET_OS_WIN32 := FALSE}
  218. {$setc TARGET_RT_MAC_68881 := FALSE}
  219. {$setc TARGET_RT_MAC_CFM := FALSE}
  220. {$setc TARGET_RT_MAC_MACHO := TRUE}
  221. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  222. {$setc TYPE_BOOL := FALSE}
  223. {$setc TYPE_EXTENDED := FALSE}
  224. {$setc TYPE_LONGLONG := TRUE}
  225. uses MacTypes;
  226. {$endc} {not MACOSALLINCLUDE}
  227. {$ifc not TARGET_CPU_64 and TARGET_OS_MAC}
  228. {$ALIGN POWER}
  229. {$ifc TARGET_CPU_64 }
  230. {$errorc 64-bit not supported}
  231. {$endc}
  232. type
  233. objc_ivar_list_Ptr = ^objc_ivar_list;
  234. objc_method_list_PtrPtr = ^objc_method_list_Ptr;
  235. objc_method_list_Ptr = ^objc_method_list;
  236. objc_cache_Ptr = ^objc_cache;
  237. objc_protocol_list_Ptr = ^objc_protocol_list;
  238. {
  239. * Class Template
  240. }
  241. objc_class_Ptr = ^objc_class;
  242. objc_class = record
  243. isa: objc_class_Ptr;
  244. super_class: objc_class_Ptr;
  245. name: CStringPtr;
  246. version: SInt32;
  247. info: SInt32;
  248. instance_size: SInt32;
  249. ivars: objc_ivar_list_Ptr;
  250. methodLists: objc_method_list_PtrPtr;
  251. cache: objc_cache_Ptr;
  252. protocols: objc_protocol_list_Ptr
  253. end;
  254. {
  255. * Category Template
  256. }
  257. objc_category_Ptr = ^objc_category;
  258. objc_category = record
  259. category_name: CStringPtr;
  260. class_name: CStringPtr;
  261. instance_methods: objc_method_list_Ptr;
  262. class_methods: objc_method_list_Ptr;
  263. protocols: objc_protocol_list_Ptr
  264. end;
  265. Category = objc_category_Ptr;
  266. {
  267. * Instance Variable Template
  268. }
  269. objc_ivar_Ptr = ^objc_ivar;
  270. objc_ivar = record
  271. ivar_name: CStringPtr;
  272. ivar_type: CStringPtr;
  273. ivar_offset: SInt32;
  274. {$ifc not undefined __alpha__ }
  275. space: SInt32;
  276. {$endc}
  277. end;
  278. Ivar = objc_ivar_Ptr;
  279. objc_ivar_list = record
  280. ivar_count: SInt32;
  281. {$ifc not undefined __alpha__ }
  282. space: SInt32;
  283. {$endc}
  284. ivar_list: array[ 0..0] of objc_ivar { variable length structure }
  285. end;
  286. { use _ObjC suffix to avoid Object Pascal class an self conflicts }
  287. Class_ObjC_Ptr = ^Class_ObjC;
  288. Class_ObjC = objc_class_Ptr;
  289. objc_object_Ptr = ^objc_object;
  290. objc_object = record
  291. isa: Class_ObjC
  292. end;
  293. objc_id_Ptr = ^objc_id;
  294. objc_id = objc_object_Ptr;
  295. id = objc_id;
  296. id_Ptr = objc_id_Ptr;
  297. objc_selector_Ptr = UnivPtr;
  298. SELPtr = ^SEL;
  299. SEL = objc_selector_Ptr;
  300. IMP = function( self_ObjC: objc_id; param2: SEL; ... ): objc_id;
  301. {
  302. * Method Template
  303. }
  304. objc_method_Ptr = ^objc_method;
  305. objc_method = record
  306. method_name: SEL;
  307. method_types: CStringPtr;
  308. method_imp: IMP
  309. end;
  310. Method = objc_method_Ptr;
  311. objc_method_list = record
  312. obsolete: objc_method_list_Ptr;
  313. method_count: SInt32;
  314. {$ifc not undefined __alpha__ }
  315. space: SInt32;
  316. {$endc}
  317. method_list: array[0..0] of objc_method { variable length structure }
  318. end;
  319. { Protocol support }
  320. Protocol = objc_object;
  321. objc_protocol_list = record
  322. next: objc_protocol_list_Ptr;
  323. count: SInt32;
  324. list: array[0..0] of Protocol
  325. end;
  326. Cache = objc_cache_Ptr;
  327. objc_cache = record
  328. mask: UInt32; { total = mask + 1 }
  329. occupied: UInt32;
  330. buckets: array[0..0] of Method
  331. end;
  332. type
  333. BOOL = boolean; {SInt8}
  334. {
  335. BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
  336. even if -funsigned-char is used.
  337. }
  338. const
  339. YES = True; {BOOL(1)}
  340. NO = False; {BOOL(0)}
  341. {
  342. # define __strong
  343. }
  344. {$ifc not defined(STRICT_OPENSTEP) }
  345. {
  346. type
  347. STR = CStringPtr;
  348. }
  349. function sel_isMapped(_sel: SEL): boolean; external name '_sel_isMapped';
  350. function sel_getName(_sel: SEL): CStringPtr; external name '_sel_getName';
  351. function sel_getUid( str: CStringPtr): SEL; external name '_sel_getUid';
  352. function sel_registerName(str: CStringPtr): SEL; external name '_sel_registerName';
  353. function object_getClassName(obj: objc_id): CStringPtr; external name '_object_getClassName';
  354. function object_getIndexedIvars(obj: objc_id): UnivPtr; external name '_object_getIndexedIvars';
  355. {$ifc defined(__osf__) and defined(__alpha__) }
  356. type
  357. arith_t = SInt32;
  358. uarith_t = UInt32;
  359. const ARITH_SHIFT = 32;
  360. {$elsec}
  361. type
  362. arith_t = SInt32;
  363. uarith_t = UInt32;
  364. const ARITH_SHIFT = 16;
  365. {$endc}
  366. {$endc} { not defined(STRICT_OPENSTEP) }
  367. const
  368. CLS_CLASS = $01;
  369. CLS_META = $02;
  370. CLS_INITIALIZED = $04;
  371. CLS_POSING = $08;
  372. CLS_MAPPED = $010;
  373. CLS_FLUSH_CACHE = $020;
  374. CLS_GROW_CACHE = $040;
  375. CLS_NEED_BIND = $080;
  376. CLS_METHOD_ARRAY = $0100;
  377. { the JavaBridge constructs classes with these markers }
  378. CLS_JAVA_HYBRID = $0200;
  379. CLS_JAVA_CLASS = $0400;
  380. { thread-safe +initialize }
  381. CLS_INITIALIZING = $0800;
  382. { bundle unloading }
  383. CLS_FROM_BUNDLE = $01000;
  384. { C++ ivar support }
  385. CLS_HAS_CXX_STRUCTORS = $02000;
  386. { Lazy method list arrays }
  387. CLS_NO_METHOD_ARRAY = $04000;
  388. { +load implementation }
  389. CLS_HAS_LOAD_METHOD = $08000;
  390. function object_setInstanceVariable(param1: objc_id; name: CStringPtr; param3: UnivPtr): Ivar; external name '_object_setInstanceVariable';
  391. function object_getInstanceVariable(param1: objc_id; name: CStringPtr; var param3: UnivPtr): Ivar; external name '_object_getInstanceVariable';
  392. { Definitions of filer types }
  393. Const
  394. _C_ID = '@';
  395. _C_CLASS = '#';
  396. _C_SEL = ':';
  397. _C_CHR = 'c';
  398. _C_UCHR = 'C';
  399. _C_SHT = 's';
  400. _C_USHT = 'S';
  401. _C_INT = 'i';
  402. _C_UINT = 'I';
  403. _C_LNG = 'l';
  404. _C_ULNG = 'L';
  405. _C_FLT = 'f';
  406. _C_DBL = 'd';
  407. _C_BFLD = 'b';
  408. _C_VOID = 'v';
  409. _C_UNDEF = '?';
  410. _C_PTR = '^';
  411. _C_CHARPTR = '*';
  412. _C_ARY_B = '[';
  413. _C_ARY_E = ']';
  414. _C_UNION_B = '(';
  415. _C_UNION_E = ')';
  416. _C_STRUCT_B = '{';
  417. _C_STRUCT_E = '}';
  418. { Structure for method cache - allocated/sized at runtime }
  419. {todo}
  420. {
  421. #define CACHE_BUCKET_NAME(B) ((B)->method_name)
  422. #define CACHE_BUCKET_IMP(B) ((B)->method_imp)
  423. #define CACHE_BUCKET_VALID(B) (B)
  424. #define CACHE_HASH(sel, mask) (((uarith_t)(sel)>>2) & (mask))
  425. }
  426. { operations }
  427. function class_createInstance( param1: Class_ObjC; idxIvars: UInt32 ): objc_id; external name '_class_createInstance';
  428. function class_createInstanceFromZone( param1: Class_ObjC; idxIvars: UInt32; z: UnivPtr): objc_id; external name '_class_createInstanceFromZone';
  429. procedure class_setVersion( param1: Class_ObjC; param2: SInt32); external name '_class_setVersion';
  430. function class_getVersion( param1: Class_ObjC): SInt32; external name '_class_getVersion';
  431. function class_getInstanceVariable( param1: Class_ObjC; param2: CStringPtr): Ivar; external name '_class_getInstanceVariable';
  432. function class_getInstanceMethod( param1: Class_ObjC; param2: SEL): Method; external name '_class_getInstanceMethod';
  433. function class_getClassMethod( param1: Class_ObjC; param2: SEL): Method; external name '_class_getClassMethod';
  434. procedure class_addMethods( param1: Class_ObjC; param2: objc_method_list_Ptr); external name '_class_addMethods';
  435. procedure class_removeMethods( param1: Class_ObjC; param2: objc_method_list_Ptr); external name '_class_removeMethods';
  436. function class_poseAs( imposter: Class_ObjC; original: Class_ObjC): Class_ObjC; external name '_class_poseAs';
  437. function method_getNumberOfArguments( param1: Method): UInt32; external name '_method_getNumberOfArguments';
  438. function method_getSizeOfArguments( param1: Method): UInt32; external name '_method_getSizeOfArguments';
  439. function method_getArgumentInfo( m: Method; arg: SInt32; var argtype: CStringPtr; var offset: SInt32): UInt32; external name '_method_getArgumentInfo';
  440. {
  441. usage for nextMethodList
  442. void *iterator = 0;
  443. struct objc_method_list *mlist;
  444. while ( mlist = class_nextMethodList( cls, &iterator ) )
  445. }
  446. const
  447. OBJC_NEXT_METHOD_LIST = 1;
  448. function class_nextMethodList( param1: Class_ObjC; var param2: UnivPtr): objc_method_list_Ptr; external name '_class_nextMethodList';
  449. type
  450. marg_list = UnivPtr;
  451. {$ifc TARGET_CPU_PPC}
  452. const
  453. marg_prearg_size = 128;
  454. {$elsec}
  455. const
  456. marg_prearg_size = 0;
  457. {$endc}
  458. {todo}
  459. {
  460. #define marg_malloc(margs, method) \
  461. do ( \
  462. margs = (marg_list *)malloc (marg_prearg_size + ((7 + method_getSizeOfArguments(method)) & ~7)); \
  463. ) while (0)
  464. #define marg_free(margs) \
  465. do ( \
  466. free(margs); \
  467. ) while (0)
  468. #define marg_adjustedOffset(method, offset) \
  469. (marg_prearg_size + offset)
  470. #define marg_getRef(margs, offset, type) \
  471. ( (type *)((char *)margs + marg_adjustedOffset(method,offset) ) )
  472. #define marg_getValue(margs, offset, type) \
  473. ( *marg_getRef(margs, offset, type) )
  474. #define marg_setValue(margs, offset, type, value) \
  475. ( marg_getValue(margs, offset, type) = (value) )
  476. }
  477. { Collection utilities }
  478. const
  479. OBJC_GENERATIONAL = 1 shl 0;
  480. procedure objc_collect_if_needed( options: UInt32); external name '_objc_collect_if_needed';
  481. function objc_numberAllocated: UInt32; external name '_objc_numberAllocated';
  482. function objc_collecting_enabled: boolean; external name '_objc_collecting_enabled';
  483. { Memory management }
  484. function objc_allocate_object(cls: Class_ObjC; extra: SInt32): objc_id; external name '_objc_allocate_object';
  485. { Write barriers }
  486. function objc_assign_strongCast( val: objc_id; dest: objc_id_Ptr): objc_id; external name '_objc_assign_strongCast';
  487. function objc_assign_global( val: objc_id; dest: objc_id_Ptr): objc_id; external name '_objc_assign_global';
  488. function objc_assign_ivar( value: objc_id; dest: objc_id_ptr; offset: UInt32): objc_id; external name '_objc_assign_ivar';
  489. function objc_memmove_collectable( dst: UnivPtr; src: UnivPtr; size: size_t): UnivPtr; external name '_objc_memmove_collectable';
  490. { Testing tools }
  491. function objc_is_finalized( ptr: UnivPtr): boolean; external name '_objc_is_finalized';
  492. { compiler reserves a setjmp buffer + 4 words as localExceptionData}
  493. procedure objc_exception_throw( exception: objc_id); external name '_objc_exception_throw';
  494. procedure objc_exception_try_enter( localExceptionData: UnivPtr); external name '_objc_exception_try_enter';
  495. procedure objc_exception_try_exit( localExceptionData: UnivPtr); external name '_objc_exception_try_exit';
  496. function objc_exception_extract( localExceptionData: UnivPtr): objc_id; external name '_objc_exception_extract';
  497. function objc_exception_match( exceptionClass: Class_ObjC; exception: objc_id): SInt32; external name '_objc_exception_match';
  498. type
  499. throw_exc_t = procedure( param1: objc_id);
  500. try_enter_t = procedure( param1: UnivPtr);
  501. try_exit_t = procedure( param1: UnivPtr);
  502. extract_t = function( param1: UnivPtr): objc_id;
  503. match_t = function( param1: Class_ObjC; param2: objc_id): SInt32;
  504. objc_exception_functions_t_Ptr = ^objc_exception_functions_t;
  505. objc_exception_functions_t = record
  506. version: SInt32;
  507. throw_exc: throw_exc_t; { version 0 }
  508. try_enter: try_enter_t; { version 0 }
  509. try_exit: try_exit_t; { version 0 }
  510. extract: extract_t; { version 0 }
  511. match: match_t; { version 0 }
  512. end;
  513. { get table; version tells how many }
  514. procedure objc_exception_get_functions( table: objc_exception_functions_t_Ptr); external name '_objc_exception_get_functions';
  515. { set table }
  516. procedure objc_exception_set_functions( table: objc_exception_functions_t_Ptr); external name '_objc_exception_set_functions';
  517. {
  518. Begin synchronizing on 'obj'.
  519. Allocates recursive pthread_mutex associated with 'obj' if needed.
  520. Returns OBJC_SYNC_SUCCESS once lock is acquired.
  521. }
  522. function objc_sync_enter( obj: objc_id): SInt32; external name '_objc_sync_enter';
  523. {
  524. End synchronizing on 'obj'.
  525. Returns OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR
  526. }
  527. function objc_sync_exit( obj: objc_id): SInt32; external name '_objc_sync_exit';
  528. {
  529. Temporarily release lock on 'obj' and wait for another thread to notify on 'obj'
  530. Return OBJC_SYNC_SUCCESS, OBJC_SYNC_NOT_OWNING_THREAD_ERROR, OBJC_SYNC_TIMED_OUT
  531. }
  532. function objc_sync_wait( obj: objc_id; milliSecondsMaxWait: SInt64): SInt32; external name '_objc_sync_wait';
  533. {
  534. Wake up another thread waiting on 'obj'
  535. Return OBJC_SYNC_SUCCESS, OBJC_SYNC_NOT_OWNING_THREAD_ERROR
  536. }
  537. function objc_sync_notify( obj: objc_id): SInt32; external name '_objc_sync_notify';
  538. {
  539. Wake up all threads waiting on 'obj'
  540. Return OBJC_SYNC_SUCCESS, OBJC_SYNC_NOT_OWNING_THREAD_ERROR
  541. }
  542. function objc_sync_notifyAll( obj: objc_id): SInt32; external name '_objc_sync_notifyAll';
  543. const
  544. OBJC_SYNC_SUCCESS = 0;
  545. OBJC_SYNC_NOT_OWNING_THREAD_ERROR = -1;
  546. OBJC_SYNC_TIMED_OUT = -2;
  547. OBJC_SYNC_NOT_INITIALIZED = -3;
  548. type
  549. objc_symtab_Ptr = ^objc_symtab;
  550. objc_symtab = record
  551. sel_ref_cnt: UInt32;
  552. refs: SELPtr;
  553. cls_def_cnt: UInt16;
  554. cat_def_cnt: UInt16;
  555. defs: array[ 0..0] of UnivPtr
  556. end;
  557. Symtab = objc_symtab_Ptr;
  558. type
  559. objc_module_Ptr = ^objc_module;
  560. objc_module = record
  561. version: UInt32;
  562. size: UInt32;
  563. name: CStringPtr;
  564. _symtab: Symtab
  565. end;
  566. Module = objc_module_Ptr;
  567. type
  568. objc_super_Ptr = ^objc_super;
  569. objc_super = record
  570. receiver: objc_id;
  571. super_class: Class_ObjC
  572. end;
  573. {
  574. * Messaging Primitives (prototypes)
  575. }
  576. function objc_getClass( name: cStringPtr): objc_id; external name '_objc_getClass';
  577. function objc_getMetaClass( name: CStringPtr): objc_id; external name '_objc_getMetaClass';
  578. function objc_msgSend( self_ObjC: objc_id; op: SEL; ...): objc_id; external name '_objc_msgSend'; (* attribute ignoreable *)
  579. function objc_msgSendSuper( super: objc_super_Ptr; op: SEL; ...): objc_id; external name '_objc_msgSendSuper'; (* attribute ignoreable *)
  580. { Floating-point-returning Messaging Primitives (prototypes)
  581. *
  582. * On some platforms, the ABI for functions returning a floating-point
  583. * value is incompatible with that for functions returning an integral type.
  584. * objc_msgSend_fpret must be used for these.
  585. *
  586. * ppc: objc_msgSend_fpret not used
  587. * ppc64: objc_msgSend_fpret not used
  588. * i386: objc_msgSend_fpret REQUIRED
  589. *
  590. * For `float` or `long double` return types, cast the function
  591. * to an appropriate function pointer type first.
  592. }
  593. {$ifc TARGET_CPU_X86}
  594. function objc_msgSend_fpret(self_ObjC: objc_id; op: SEL; ...): double; external name '_objc_msgSend_fpret';
  595. {$endc}
  596. { Struct-returning Messaging Primitives (prototypes)
  597. *
  598. * For historical reasons, the prototypes for the struct-returning
  599. * messengers are unusual. The portable, correct way to call these functions
  600. * is to cast them to your desired return type first.
  601. *
  602. * For example, `NSRect result = [myNSView frame]` could be written as:
  603. * NSRect (*msgSend_stret_fn)(id, SEL, ...) = (NSRect(*)(id, SEL, ...))objc_msgSend_stret;
  604. * NSRect result = (*msgSend_stret_fn)(myNSView, @selector(frame));
  605. * or, without the function pointer:
  606. * NSRect result = (*(NSRect(*)(id, SEL, ...))objc_msgSend_stret)(myNSView, @selector(frame));
  607. *
  608. * BE WARNED that these prototypes have changed in the past and will change
  609. * in the future. Code that uses a cast like the example above will be
  610. * unaffected.
  611. }
  612. procedure objc_msgSend_stret( stretAddr: UnivPtr; self_ObjC: objc_id; op: SEL; ...); external name '_objc_msgSend_stret';
  613. procedure objc_msgSendSuper_stret( stretAddr: UnivPtr; super: objc_super_Ptr; op: SEL; ...); external name '_objc_msgSendSuper_stret';
  614. { Forwarding }
  615. { Note that objc_msgSendv_stret() does not return a structure type,
  616. * and should not be cast to do so. This is unlike objc_msgSend_stret()
  617. * and objc_msgSendSuper_stret().
  618. }
  619. function objc_msgSendv( self_ObjC: objc_id; op: SEL; arg_size: UInt32; arg_frame: marg_list): objc_id; external name '_objc_msgSendv';
  620. procedure objc_msgSendv_stret( stretAddr: UnivPtr; self_ObjC: objc_id; op: SEL; arg_size: UInt32; arg_frame: marg_list); external name '_objc_msgSendv_stret';
  621. {$ifc TARGET_CPU_X86}
  622. function objc_msgSendv_fpret( seflid: objc_id; op: SEL; arg_size: UInt32; arg_frame: marg_list ): double; external name '_objc_msgSendv_fpret';
  623. {$endc}
  624. {
  625. getting all the classes in the application...
  626. int objc_getClassList(buffer, bufferLen)
  627. classes is an array of Class values (which are pointers)
  628. which will be filled by the function; if this
  629. argument is NULL, no copying is done, only the
  630. return value is returned
  631. bufferLen is the number of Class values the given buffer
  632. can hold; if the buffer is not large enough to
  633. hold all the classes, the buffer is filled to
  634. the indicated capacity with some arbitrary subset
  635. of the known classes, which could be different
  636. from call to call
  637. returns the number of classes, which is the number put
  638. in the buffer if the buffer was large enough,
  639. or the length the buffer should have been
  640. int numClasses = 0, newNumClasses = objc_getClassList(NULL, 0);
  641. Class *classes = NULL;
  642. while (numClasses < newNumClasses) (
  643. numClasses = newNumClasses;
  644. classes = realloc(classes, sizeof(Class) * numClasses);
  645. newNumClasses = objc_getClassList(classes, numClasses);
  646. )
  647. // now, can use the classes list; if NULL, there are no classes
  648. free(classes);
  649. }
  650. function objc_getClassList( buffer: Class_ObjC_Ptr; bufferLen: SInt32): SInt32; external name '_objc_getClassList';
  651. {$setc OBSOLETE_OBJC_GETCLASSES := 1}
  652. {$ifc OBSOLETE_OBJC_GETCLASSES}
  653. function objc_getClasses: UnivPtr; external name '_objc_getClasses';
  654. {$endc}
  655. function objc_lookUpClass( name: CStringPtr): objc_id; external name '_objc_lookUpClass';
  656. function objc_getRequiredClass( name: CStringPtr): objc_id; external name '_objc_getRequiredClass';
  657. procedure objc_addClass( myClass: Class_ObjC ); external name '_objc_addClass';
  658. { customizing the error handling for objc_getClass/objc_getMetaClass }
  659. type
  660. ClassHandlerCallback = function( param1: CStringPtr): SInt32;
  661. procedure objc_setClassHandler( handler: ClassHandlerCallback); external name '_objc_setClassHandler';
  662. { Making the Objective-C runtime thread safe. }
  663. procedure objc_setMultithreaded ( flag: boolean); external name '_objc_setMultithreaded';
  664. { overriding the default object allocation and error handling routines }
  665. {todo}
  666. {
  667. OBJC_EXPORT id (_alloc)(Class, unsigned int);
  668. OBJC_EXPORT id (_copy)(id, unsigned int);
  669. OBJC_EXPORT id (_realloc)(id, unsigned int);
  670. OBJC_EXPORT id (_dealloc)(id);
  671. OBJC_EXPORT id (_zoneAlloc)(Class, unsigned int, void *);
  672. OBJC_EXPORT id (_zoneRealloc)(id, unsigned int, void *);
  673. OBJC_EXPORT id (_zoneCopy)(id, unsigned int, void *);
  674. OBJC_EXPORT void (_error)(id, const char *, va_list);
  675. }
  676. {$endc} {not TARGET_CPU_64 and TARGET_OS_MAC}
  677. {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
  678. end.
  679. {$endc} {not MACOSALLINCLUDE}