Change soft-fail to use the config, rather than env
[rbx.git] / shotgun / lib / object.h
blobb430f53664af93663939a3e124d4022738185b8e
1 #ifndef RBS_OBJECT_H
2 #define RBS_OBJECT_H
4 #include <assert.h>
6 OBJECT object_create_metaclass(STATE, OBJECT cls, OBJECT sup);
7 OBJECT object_metaclass(STATE, OBJECT obj);
8 int object_kind_of_p(STATE, OBJECT self, OBJECT cls);
9 int object_has_ivars(STATE, OBJECT self);
10 void object_set_has_ivars(STATE, OBJECT self);
11 OBJECT object_get_ivar(STATE, OBJECT self, OBJECT sym);
12 OBJECT object_set_ivar(STATE, OBJECT self, OBJECT sym, OBJECT val);
13 OBJECT object_get_ivars(STATE, OBJECT self);
14 OBJECT object_new(STATE);
15 int object_copy_bytes_into(STATE, OBJECT self, OBJECT dest, int count, int offset);
16 unsigned int object_hash_int(STATE, OBJECT self);
17 int object_stores_bytes_p(STATE, OBJECT self);
18 void object_make_byte_storage(STATE, OBJECT self);
19 void object_initialize_bytes(STATE, OBJECT self);
20 void object_set_tainted(STATE, OBJECT self);
21 void object_set_untainted(STATE, OBJECT self);
22 int object_tainted_p(STATE, OBJECT self);
23 void object_set_frozen(STATE, OBJECT self);
24 int object_frozen_p(STATE, OBJECT self);
25 void object_copy_ivars(STATE, OBJECT self, OBJECT dest);
26 void object_copy_metaclass(STATE, OBJECT self, OBJECT dest);
28 OBJECT object_make_weak_ref(STATE, OBJECT self);
29 void object_cleanup_weak_refs(STATE, OBJECT self);
31 static inline OBJECT object_class(STATE, OBJECT self) {
32 if(REFERENCE_P(self)) {
33 OBJECT cls = self->klass;
34 while(REFERENCE_P(cls) && (metaclass_s_metaclass_p(state, cls) || cls->obj_type != ClassType)) {
35 cls = class_get_superclass(cls);
38 return cls;
41 return state->global->special_classes[((uintptr_t)self) & SPECIAL_CLASS_MASK];
44 #define object_byte_start(st, self) ((char*)BYTES_OF(self))
45 #define object_size(st, self) SIZE_OF_BODY(self)
47 static inline void object_copy_body(STATE, OBJECT self, OBJECT dest) {
48 size_t s1 = object_size(state, self);
49 size_t s2 = object_size(state, dest);
50 assert(s1 <= s2);
52 memcpy(object_byte_start(state, dest), object_byte_start(state, self), s1);
55 /* Ruby's is_a? */
56 #define ISA(o, c) object_kind_of_p(state, o, c)
58 static inline uintptr_t object_get_id(STATE, OBJECT self) {
59 if(REFERENCE_P(self)) {
60 OBJECT meta, id;
62 meta = object_metaclass(state, self);
63 id = object_get_ivar(state, meta, state->global->sym_object_id);
65 /* Lazy allocate object's ids, since most don't need them. */
66 if(NIL_P(id)) {
67 /* All references have an even object_id. last_object_id starts out at 0
68 * but we don't want to use 0 as an object_id, so we just add before using */
69 id = I2N(state->om->last_object_id += 2);
70 object_set_ivar(state, meta, state->global->sym_object_id, id);
73 return (uintptr_t)id;
74 } else {
75 /* All non-references have an odd object_id */
76 return (((uintptr_t)self << 1) | 1);
80 static inline int object_copy_fields_shifted(STATE, OBJECT self, OBJECT dest, int dist) {
81 int count;
82 int i;
84 count = NUM_FIELDS(self);
86 for(i = 0; i < count; i++) {
87 SET_FIELD(dest, dist + i, NTH_FIELD(self, i));
89 return TRUE;
92 static inline int object_copy_fields_from(STATE, OBJECT self, OBJECT dest, int first, int count) {
93 int i, j;
94 int max = NUM_FIELDS(self);
96 for(i = first, j = 0; j < count && i < max; i++, j++) {
97 SET_FIELD(dest, j, NTH_FIELD(self, i));
99 return TRUE;
102 static inline void object_copy_fields(STATE, OBJECT self, OBJECT dest) {
103 int i, max, j;
105 max = NUM_FIELDS(self);
106 j = NUM_FIELDS(dest);
107 if(max < j) max = j;
109 for(i = 0; i < max; i++) {
110 SET_FIELD(dest, i, NTH_FIELD(self, i));
113 #endif