ObjFW  Check-in [1dcbea2fb0]

Overview
Comment:Call C++ constructors in correct order.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1dcbea2fb04b588d14a8847ff3b8a27b827d25b881d80d544bc3127bebc09b3a
User & Date: js on 2012-10-20 22:22:36
Other Links: manifest | tags
Context
2012-10-21
22:12
Make GCC 4.4 happy. check-in: fcade8da38 user: js tags: trunk
2012-10-20
22:22
Call C++ constructors in correct order. check-in: 1dcbea2fb0 user: js tags: trunk
2012-10-18
17:33
Fix missing #else. check-in: 7169decdef user: js tags: trunk
Changes

Modified src/instance.m from [8a19fb2a3a] to [31b3780a78].

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

#include "config.h"

#import "OFObject.h"

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;





























id
objc_constructInstance(Class cls, void *bytes)
{
	id obj = (id)bytes;
	BOOL (*last)(id, SEL) = NULL;

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

	if (cxx_construct == NULL)
		cxx_construct = sel_registerName(".cxx_construct");
	if (cxx_destruct == NULL)
		cxx_destruct = sel_registerName(".cxx_destruct");

	object_setClass(obj, cls);

	/* FIXME: Constructors of superclasses should be called first */
	for (; cls != Nil; cls = class_getSuperclass(cls)) {
		BOOL (*ctor)(id, SEL);

		if (class_respondsToSelector(cls, cxx_construct)) {
			if ((ctor = (BOOL(*)(id, SEL))
			    class_getMethodImplementation(cls,
			    cxx_construct)) != last)
				if (!ctor(obj, cxx_construct))
					return nil;

			last = ctor;
		} else
			break;
	}

	return obj;
}

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




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

		if (class_respondsToSelector(cls, cxx_destruct)) {
			if ((dtor = (void(*)(id, SEL))
			    class_getMethodImplementation(cls,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<




<
<
<
<
<


<
<
<
|
<
<
<
<
<
|
<
<
<
<
<










>
>
>







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

#include "config.h"

#import "OFObject.h"

static SEL cxx_construct = NULL;
static SEL cxx_destruct = NULL;

static BOOL
call_ctors(Class cls, id obj)
{
	Class super = class_getSuperclass(cls);
	id (*ctor)(id, SEL);
	id (*last)(id, SEL);

	if (super != nil)
		if (!call_ctors(super, obj))
			return NO;

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

	if (!class_respondsToSelector(cls, cxx_construct))
		return YES;

	ctor = (id(*)(id, SEL))
	    class_getMethodImplementation(cls, cxx_construct);
	last = (id(*)(id, SEL))
	    class_getMethodImplementation(super, cxx_construct);

	if (ctor == last)
		return YES;

	return (ctor(obj, cxx_construct) != nil ? YES : NO);
}

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


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






	object_setClass(obj, cls);




	if (!call_ctors(cls, obj))





		return nil;






	return obj;
}

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

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

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

		if (class_respondsToSelector(cls, cxx_destruct)) {
			if ((dtor = (void(*)(id, SEL))
			    class_getMethodImplementation(cls,