forked from gnustep/libobjc2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
statics_loader.c
72 lines (64 loc) · 1.79 KB
/
statics_loader.c
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
#include <string.h>
#include <stdio.h>
#include "objc/runtime.h"
#include "module.h"
#include "constant_string.h"
#include "visibility.h"
#define BUFFER_TYPE struct objc_static_instance_list
#include "buffer.h"
static BOOL try_init_statics(struct objc_static_instance_list *statics)
{
const char *class_name = statics->class_name;
// This is a horrible hack.
//
// Very bad things happen when you have more than one constant string class
// used in a program. Unfortunately, GCC defaults to using
// NXConstantString, and if you forget to specify
// -fconstant-string-class=NSConstantString for some compilation units then
// you will end up with some NSConstantString instances and some
// NXConstantString instances. This is a mess. We hack around this by
// silently assuming that the user meant NSConstantString when they said
// NXConstantString if NSConstantString is set as the constant string class
// in string_class.h or by an external -D flag.
if (strcmp(class_name, "NXConstantString") == 0)
{
class_name = CONSTANT_STRING_CLASS;
}
Class class = (Class)objc_getClass(class_name);
if (Nil == class)
{
return NO;
}
for (id *instance=statics->instances ; nil!=*instance ; instance++)
{
(*instance)->isa = class;
}
return YES;
}
PRIVATE void objc_init_statics(struct objc_static_instance_list *statics)
{
if (!try_init_statics(statics))
{
set_buffered_object_at_index(statics, buffered_objects++);
}
}
PRIVATE void objc_init_buffered_statics(void)
{
BOOL shouldReshuffle = NO;
for (unsigned i=0 ; i<buffered_objects ; i++)
{
struct objc_static_instance_list *c = buffered_object_at_index(i);
if (NULL != c)
{
if (try_init_statics(c))
{
set_buffered_object_at_index(NULL, i);
shouldReshuffle = YES;
}
}
}
if (shouldReshuffle)
{
compact_buffer();
}
}