1 /* Common operations. */ 2 3 #include <stdlib.h> 4 #include "ops.h" 5 #include "progconsts.h" 6 #include "progtypes.h" 7 8 /* Direct access and manipulation of static objects. */ 9 10 __attr __load_static(__ref parent, __ref obj) 11 { 12 __attr out = {.context=parent, .value=obj}; 13 return out; 14 } 15 16 /* Direct retrieval operations, returning and setting attributes. */ 17 18 __attr __load_via_object(__ref obj, int pos) 19 { 20 return obj->attrs[pos]; 21 } 22 23 __attr __load_via_class(__ref obj, int pos) 24 { 25 return __load_via_object(__get_class(obj), pos); 26 } 27 28 __attr __get_class_and_load(__ref obj, int pos) 29 { 30 if (__is_instance(obj)) 31 return __load_via_class(obj, pos); 32 else 33 return __load_via_object(obj, pos); 34 } 35 36 /* Direct storage operations. */ 37 38 int __store_via_object(__ref obj, int pos, __attr value) 39 { 40 obj->attrs[pos] = value; 41 return 1; 42 } 43 44 /* Introspection. */ 45 46 int __is_instance(__ref obj) 47 { 48 return obj->pos == __INSTANCEPOS; 49 } 50 51 __ref __get_class(__ref obj) 52 { 53 return __load_via_object(obj, __pos___class__).value; 54 } 55 56 __attr __get_class_attr(__ref obj) 57 { 58 return __load_via_object(obj, __pos___class__); 59 } 60 61 /* Attribute testing operations. */ 62 63 __ref __test_specific_instance(__ref obj, __ref type) 64 { 65 return __get_class(obj) == type ? obj : 0; 66 } 67 68 __ref __test_common_instance(__ref obj, int pos, int code) 69 { 70 return __HASATTR(__get_class(obj), pos, code) ? obj : 0; 71 } 72 73 __ref __test_common_object(__ref obj, int pos, int code) 74 { 75 return __test_common_type(obj, pos, code) || __test_common_instance(obj, pos, code) ? obj : 0; 76 } 77 78 __ref __test_common_type(__ref obj, int pos, int code) 79 { 80 return __HASATTR(obj, pos, code) ? obj : 0; 81 } 82 83 /* Attribute testing and retrieval operations. */ 84 85 static __attr __check_and_load_via_object_null(__ref obj, int pos, int code) 86 { 87 if (__HASATTR(obj, pos, code)) 88 return __load_via_object(obj, pos); 89 else 90 return __NULL; 91 } 92 93 __attr __check_and_load_via_class(__ref obj, int pos, int code) 94 { 95 return __check_and_load_via_object(__get_class(obj), pos, code); 96 } 97 98 __attr __check_and_load_via_object(__ref obj, int pos, int code) 99 { 100 if (__HASATTR(obj, pos, code)) 101 return __load_via_object(obj, pos); 102 103 __raise_type_error(); 104 return __NULL; 105 } 106 107 __attr __check_and_load_via_any(__ref obj, int pos, int code) 108 { 109 __attr out = __check_and_load_via_object_null(obj, pos, code); 110 if (out.value == 0) 111 out = __check_and_load_via_class(obj, pos, code); 112 return out; 113 } 114 115 /* Attribute testing and storage operations. */ 116 117 int __check_and_store_via_object(__ref obj, int pos, int code, __attr value) 118 { 119 if (__HASATTR(obj, pos, code)) 120 { 121 __store_via_object(obj, pos, value); 122 return 1; 123 } 124 return 0; 125 } 126 127 int __check_and_store_via_any(__ref obj, int pos, int code, __attr value) 128 { 129 if (__check_and_store_via_object(obj, pos, code, value)) 130 return 1; 131 return __check_and_store_via_object(__get_class(obj), pos, code, value); 132 } 133 134 /* Context-related operations. */ 135 136 __attr __test_context(__ref context, __attr attr) 137 { 138 /* Preserve any existing instance context. */ 139 140 if (__is_instance(attr.context)) 141 return attr; 142 if (__is_instance(context) && __test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) 143 return __replace_context(context, attr); 144 if (!__is_instance(context) && __test_common_type(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) 145 return __update_context(context, attr); 146 147 __raise_type_error(); 148 return __NULL; 149 } 150 151 __attr __replace_context(__ref context, __attr attr) 152 { 153 __attr out; 154 155 /* Set the context. */ 156 157 out.context = context; 158 159 /* Reference a callable version of the attribute by obtaining the bound 160 method reference from the __fn__ special attribute. */ 161 162 out.value = __load_via_object(attr.value, __ATTRPOS(__fn__)).b; 163 return out; 164 } 165 166 __attr __update_context(__ref context, __attr attr) 167 { 168 __attr out = {context, .fn=attr.fn}; 169 return out; 170 } 171 172 /* Basic structure tests. */ 173 174 int __WITHIN(__ref obj, int pos) 175 { 176 return pos < obj->table->size; 177 } 178 179 int __HASATTR(__ref obj, int pos, int code) 180 { 181 return __WITHIN(obj, pos) && (obj->table->attrs[pos] == code); 182 } 183 184 /* Parameter position operations. */ 185 186 int __HASPARAM(const __ptable *ptable, int ppos, int pcode) 187 { 188 __param param; 189 190 if (ppos < ptable->size) 191 { 192 param = ptable->params[ppos]; 193 if (param.code == pcode) 194 return param.pos; 195 } 196 197 return -1; 198 } 199 200 /* Conversions. */ 201 202 __attr __CONTEXT_AS_VALUE(__attr attr) 203 { 204 __attr out; 205 out.context = attr.context; 206 out.value = attr.context; 207 return out; 208 } 209 210 /* Type testing. */ 211 212 __ref __ISFUNC(__ref obj) 213 { 214 return __test_specific_instance(obj, &__FUNCTION_TYPE); 215 } 216 217 int __ISNULL(__attr value) 218 { 219 /* (value.context == __NULL.context) is superfluous */ 220 return (value.value == 0); /* __NULL.value */ 221 } 222 223 /* Attribute codes and positions for type objects. */ 224 225 unsigned int __TYPECODE(__ref obj) 226 { 227 return obj->table->attrs[obj->pos]; 228 } 229 230 unsigned int __TYPEPOS(__ref obj) 231 { 232 return obj->pos; 233 } 234 235 /* Copying of structures. */ 236 237 __ref __COPY(__ref obj, int size) 238 { 239 __ref copy = calloc(1, size); 240 memcpy(copy, obj, size); 241 return copy; 242 }