ObjFW  Diff

Differences From Artifact [64f7431604]:

To Artifact [77311e8f90]:


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
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







-
-
+
+


-
+

-
+




-
+


-
-
+
+

-
+



-
+

-
+




-
+



-
+

-
+

-
+


-
+

-
+


-
+



-
+

-
+


-
+



-
+


-
-
+
+

-
-
+
+


-
+

-
-
-
+
+
+






-
+

 * file.
 */

#include "config.h"

#import "OFObject.h"

static SEL constructSel = NULL;
static SEL destructSel = NULL;
static SEL constructSelector = NULL;
static SEL destructSelector = NULL;

static bool
callConstructors(Class cls, id obj)
callConstructors(Class class, id object)
{
	Class super = class_getSuperclass(cls);
	Class super = class_getSuperclass(class);
	id (*construct)(id, SEL);
	id (*last)(id, SEL);

	if (super != nil)
		if (!callConstructors(super, obj))
		if (!callConstructors(super, object))
			return false;

	if (constructSel == NULL)
		constructSel = sel_registerName(".cxx_construct");
	if (constructSelector == NULL)
		constructSelector = sel_registerName(".cxx_construct");

	if (!class_respondsToSelector(cls, constructSel))
	if (!class_respondsToSelector(class, constructSelector))
		return true;

	construct = (id (*)(id, SEL))
	    class_getMethodImplementation(cls, constructSel);
	    class_getMethodImplementation(class, constructSelector);
	last = (id (*)(id, SEL))
	    class_getMethodImplementation(super, constructSel);
	    class_getMethodImplementation(super, constructSelector);

	if (construct == last)
		return true;

	return (construct(obj, constructSel) != nil);
	return (construct(object, constructSelector) != nil);
}

id
objc_constructInstance(Class cls, void *bytes)
objc_constructInstance(Class class, void *bytes)
{
	id obj = (id)bytes;
	id object = (id)bytes;

	if (cls == Nil || bytes == NULL)
	if (class == Nil || bytes == NULL)
		return nil;

	object_setClass(obj, cls);
	object_setClass(object, class);

	if (!callConstructors(cls, obj))
	if (!callConstructors(class, object))
		return nil;

	return obj;
	return object;
}

void *
objc_destructInstance(id obj)
objc_destructInstance(id object)
{
	Class cls;
	Class class;
	void (*last)(id, SEL) = NULL;

	if (obj == nil)
	if (object == nil)
		return NULL;

#ifdef OF_OBJFW_RUNTIME
	objc_zero_weak_references(obj);
	objc_zero_weak_references(object);
#endif

	if (destructSel == NULL)
		destructSel = sel_registerName(".cxx_destruct");
	if (destructSelector == NULL)
		destructSelector = sel_registerName(".cxx_destruct");

	for (cls = object_getClass(obj); cls != Nil;
	    cls = class_getSuperclass(cls)) {
	for (class = object_getClass(object); class != Nil;
	    class = class_getSuperclass(class)) {
		void (*destruct)(id, SEL);

		if (class_respondsToSelector(cls, destructSel)) {
		if (class_respondsToSelector(class, destructSelector)) {
			if ((destruct = (void (*)(id, SEL))
			    class_getMethodImplementation(cls,
			    destructSel)) != last)
				destruct(obj, destructSel);
			    class_getMethodImplementation(class,
			    destructSelector)) != last)
				destruct(object, destructSelector);

			last = destruct;
		} else
			break;
	}

	return obj;
	return object;
}