-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.h
More file actions
387 lines (334 loc) · 12.4 KB
/
message.h
File metadata and controls
387 lines (334 loc) · 12.4 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifndef _OBJC_MESSAGE_H
#define _OBJC_MESSAGE_H
#include <objc/objc.h>
#include <objc/runtime.h>
#ifndef OBJC_SUPER
#define OBJC_SUPER
/// WGRunTimeSourceCode 源码阅读
/*
简化版
struct objc_super {
id receiver; //消息接收者
Class super_class; //父类
}
*/
/// Specifies the superclass of an instance.
struct objc_super {
/// Specifies an instance of a class.
__unsafe_unretained _Nonnull id receiver;
/// Specifies the particular superclass of the instance to message.
#if !defined(__cplusplus) && !__OBJC2__
/* For compatibility with old objc-runtime.h header */
__unsafe_unretained _Nonnull Class class;
#else
__unsafe_unretained _Nonnull Class super_class;
#endif
/* super_class is the first class to search */
};
#endif
/* Basic Messaging Primitives
*
* On some architectures, use objc_msgSend_stret for some struct return types.
* On some architectures, use objc_msgSend_fpret for some float return types.
* On some architectures, use objc_msgSend_fp2ret for some float return types.
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void
objc_msgSend(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
#else
/**
* Sends a message with a simple return value to an instance of a class.
*
* @param self A pointer to the instance of the class that is to receive the message.
* @param op The selector of the method that handles the message.
* @param ...
* A variable argument list containing the arguments to the method.
*
* @return The return value of the method.
*
* @note When it encounters a method call, the compiler generates a call to one of the
* functions \c objc_msgSend, \c objc_msgSend_stret, \c objc_msgSendSuper, or \c objc_msgSendSuper_stret.
* Messages sent to an object’s superclass (using the \c super keyword) are sent using \c objc_msgSendSuper;
* other messages are sent using \c objc_msgSend. Methods that have data structures as return values
* are sent using \c objc_msgSendSuper_stret and \c objc_msgSend_stret.
*/
OBJC_EXPORT id _Nullable
objc_msgSend(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
/**
* Sends a message with a simple return value to the superclass of an instance of a class.
*
* @param super A pointer to an \c objc_super data structure. Pass values identifying the
* context the message was sent to, including the instance of the class that is to receive the
* message and the superclass at which to start searching for the method implementation.
* @param op A pointer of type SEL. Pass the selector of the method that will handle the message.
* @param ...
* A variable argument list containing the arguments to the method.
*
* @return The return value of the method identified by \e op.
*
* @see objc_msgSend
*/
OBJC_EXPORT id _Nullable
/// WGRunTimeSourceCode 源码阅读
/*
struct objc_super {
id receiver; //消息接收者
Class super_class; //父类
}
objc_msgSendSuper({消息接收者,消息接收者的父类}, @selector())
代表消息查找是从消息接收者的父类里面开始查找的
*/
objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
#endif
/* Struct-returning Messaging Primitives
*
* Use these functions to call methods that return structs on the stack.
* On some architectures, some structures are returned in registers.
* Consult your local function call ABI documentation for details.
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void
objc_msgSend_stret(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
OBJC_EXPORT void
objc_msgSendSuper_stret(void /* struct objc_super *super, SEL op, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#else
/**
* Sends a message with a data-structure return value to an instance of a class.
*
* @see objc_msgSend
*/
OBJC_EXPORT void
objc_msgSend_stret(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
/**
* Sends a message with a data-structure return value to the superclass of an instance of a class.
*
* @see objc_msgSendSuper
*/
OBJC_EXPORT void
objc_msgSendSuper_stret(struct objc_super * _Nonnull super,
SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#endif
/* Floating-point-returning Messaging Primitives
*
* Use these functions to call methods that return floating-point values
* on the stack.
* Consult your local function call ABI documentation for details.
*
* arm: objc_msgSend_fpret not used
* i386: objc_msgSend_fpret used for `float`, `double`, `long double`.
* x86-64: objc_msgSend_fpret used for `long double`.
*
* arm: objc_msgSend_fp2ret not used
* i386: objc_msgSend_fp2ret not used
* x86-64: objc_msgSend_fp2ret used for `_Complex long double`.
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
#if !OBJC_OLD_DISPATCH_PROTOTYPES
# if defined(__i386__)
OBJC_EXPORT void
objc_msgSend_fpret(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
# elif defined(__x86_64__)
OBJC_EXPORT void
objc_msgSend_fpret(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
objc_msgSend_fp2ret(void /* id self, SEL op, ... */ )
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
# endif
// !OBJC_OLD_DISPATCH_PROTOTYPES
#else
// OBJC_OLD_DISPATCH_PROTOTYPES
# if defined(__i386__)
/**
* Sends a message with a floating-point return value to an instance of a class.
*
* @see objc_msgSend
* @note On the i386 platform, the ABI for functions returning a floating-point value is
* incompatible with that for functions returning an integral type. On the i386 platform, therefore,
* you must use \c objc_msgSend_fpret for functions returning non-integral type. For \c float or
* \c long \c double return types, cast the function to an appropriate function pointer type first.
*/
OBJC_EXPORT double
objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
/* Use objc_msgSendSuper() for fp-returning messages to super. */
/* See also objc_msgSendv_fpret() below. */
# elif defined(__x86_64__)
/**
* Sends a message with a floating-point return value to an instance of a class.
*
* @see objc_msgSend
*/
OBJC_EXPORT long double
objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
# if __STDC_VERSION__ >= 199901L
OBJC_EXPORT _Complex long double
objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
# else
OBJC_EXPORT void objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
# endif
/* Use objc_msgSendSuper() for fp-returning messages to super. */
/* See also objc_msgSendv_fpret() below. */
# endif
// OBJC_OLD_DISPATCH_PROTOTYPES
#endif
/* Direct Method Invocation Primitives
* Use these functions to call the implementation of a given Method.
* This is faster than calling method_getImplementation() and method_getName().
*
* The receiver must not be nil.
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void
method_invoke(void /* id receiver, Method m, ... */ )
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
method_invoke_stret(void /* id receiver, Method m, ... */ )
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#else
OBJC_EXPORT id _Nullable
method_invoke(id _Nullable receiver, Method _Nonnull m, ...)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
method_invoke_stret(id _Nullable receiver, Method _Nonnull m, ...)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#endif
/* Message Forwarding Primitives
* Use these functions to forward a message as if the receiver did not
* respond to it.
*
* The receiver must not be nil.
*
* class_getMethodImplementation() may return (IMP)_objc_msgForward.
* class_getMethodImplementation_stret() may return (IMP)_objc_msgForward_stret
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*
* Before Mac OS X 10.6, _objc_msgForward must not be called directly
* but may be compared to other IMP values.
*/
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void
_objc_msgForward(void /* id receiver, SEL sel, ... */ )
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
_objc_msgForward_stret(void /* id receiver, SEL sel, ... */ )
OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#else
OBJC_EXPORT id _Nullable
_objc_msgForward(id _Nonnull receiver, SEL _Nonnull sel, ...)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
OBJC_EXPORT void
_objc_msgForward_stret(id _Nonnull receiver, SEL _Nonnull sel, ...)
OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
#endif
/* Variable-argument Messaging Primitives
*
* Use these functions to call methods with a list of arguments, such
* as the one passed to forward:: .
*
* The contents of the argument list are architecture-specific.
* Consult your local function call ABI documentation for details.
*
* These functions must be cast to an appropriate function pointer type
* before being called, except for objc_msgSendv_stret() which must not
* be cast to a struct-returning type.
*/
typedef void* marg_list;
OBJC_EXPORT id _Nullable
objc_msgSendv(id _Nullable self, SEL _Nonnull op, size_t arg_size,
marg_list _Nonnull arg_frame)
OBJC2_UNAVAILABLE;
OBJC_EXPORT void
objc_msgSendv_stret(void * _Nonnull stretAddr, id _Nullable self,
SEL _Nonnull op, size_t arg_size,
marg_list _Nullable arg_frame)
OBJC2_UNAVAILABLE;
/* Note that objc_msgSendv_stret() does not return a structure type,
* and should not be cast to do so. This is unlike objc_msgSend_stret()
* and objc_msgSendSuper_stret().
*/
#if defined(__i386__)
OBJC_EXPORT double
objc_msgSendv_fpret(id _Nullable self, SEL _Nonnull op,
unsigned arg_size, marg_list _Nullable arg_frame)
OBJC2_UNAVAILABLE;
#endif
/* The following marg_list macros are of marginal utility. They
* are included for compatibility with the old objc-class.h header. */
#if !__OBJC2__
#define marg_prearg_size 0
#define marg_malloc(margs, method) \
do { \
margs = (marg_list *)malloc (marg_prearg_size + ((7 + method_getSizeOfArguments(method)) & ~7)); \
} while (0)
#define marg_free(margs) \
do { \
free(margs); \
} while (0)
#define marg_adjustedOffset(method, offset) \
(marg_prearg_size + offset)
#define marg_getRef(margs, offset, type) \
( (type *)((char *)margs + marg_adjustedOffset(method,offset) ) )
#define marg_getValue(margs, offset, type) \
( *marg_getRef(margs, offset, type) )
#define marg_setValue(margs, offset, type, value) \
( marg_getValue(margs, offset, type) = (value) )
#endif
#endif