1 /* Operations depending on program specifics. 2 */ 3 4 #include <stdlib.h> 5 #include "types.h" 6 #include "ops.h" 7 #include "progconsts.h" 8 #include "progops.h" 9 #include "progtypes.h" 10 #include "main.h" 11 #include "exceptions.h" 12 13 /* Generic instantiation operations, defining common members. */ 14 15 __attr __new(const __table * table, __ref cls, size_t size) 16 { 17 __ref obj = (__ref) calloc(1, size); 18 __attr self = {obj, obj}; 19 __attr tmp = {0, cls}; 20 obj->table = table; 21 __store_via_object(obj, __pos___class__, tmp); 22 return self; 23 } 24 25 /* Generic internal data allocation. */ 26 27 __attr __newdata(__attr args[], unsigned int number) 28 { 29 /* Calculate the size of the fragment. */ 30 31 __fragment *data = (__fragment *) calloc(1, __FRAGMENT_SIZE(number)); 32 __attr attr = {0, .data=data}; 33 unsigned int i, j; 34 35 /* Copy the given number of values, starting from the second element. */ 36 37 for (i = 1, j = 0; i <= number; i++, j++) 38 data->attrs[j] = args[i]; 39 40 data->size = number; 41 data->capacity = number; 42 return attr; 43 } 44 45 /* A helper for raising type errors within common operations. */ 46 47 void __raise_type_error() 48 { 49 __attr args[1]; 50 __attr exc = __TYPE_ERROR_INSTANTIATOR(args); 51 __Raise(exc); 52 } 53 54 /* Generic invocation operations. */ 55 56 /* Invoke the given callable, supplying keyword argument details in the given 57 codes and arguments arrays, indicating the number of arguments described. 58 The number of positional arguments is specified, and such arguments then 59 follow as conventional function arguments. Typically, at least one argument 60 is specified, starting with any context argument. 61 */ 62 63 __attr __invoke(__attr callable, int always_callable, 64 unsigned int nkwargs, __param kwcodes[], __attr kwargs[], 65 unsigned int nargs, __attr args[]) 66 { 67 /* Obtain the __args__ special member, referencing the parameter table. */ 68 69 __attr minparams = __check_and_load_via_object(callable.value, __pos___args__, __code___args__); 70 71 /* Refer to the table and minimum/maximum. */ 72 73 const __ptable *ptable = minparams.ptable; 74 const unsigned int min = minparams.min, max = ptable->size; 75 76 /* Reserve enough space for the arguments. */ 77 78 __attr allargs[max]; 79 80 /* Traverse the arguments. */ 81 82 unsigned int pos, kwpos; 83 84 /* Check the number of arguments. */ 85 86 if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) 87 return __NULL; 88 89 /* Copy the arguments. */ 90 91 for (pos = 0; pos < nargs; pos++) 92 allargs[pos] = args[pos]; 93 94 /* Erase the remaining arguments. */ 95 96 for (pos = nargs; pos < max; pos++) 97 { 98 allargs[pos].value = 0; 99 } 100 101 /* Fill keyword arguments. */ 102 103 for (kwpos = 0; kwpos < nkwargs; kwpos++) 104 { 105 pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); 106 107 /* Check the table entry against the supplied argument details. 108 Set the argument but only if it does not overwrite positional 109 arguments. */ 110 111 if ((pos == -1) || (pos < nargs)) 112 return __NULL; 113 114 /* Set the argument using the appropriate position. */ 115 116 allargs[pos] = kwargs[kwpos]; 117 } 118 119 /* Fill the defaults. */ 120 121 for (pos = nargs; pos < max; pos++) 122 { 123 if (allargs[pos].value == 0) 124 allargs[pos] = __GETDEFAULT(callable.value, pos - nargs); 125 } 126 127 /* Call with the prepared arguments. */ 128 129 return (always_callable ? __load_via_object(callable.value, __pos___fn__) 130 : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) 131 ).fn(allargs); 132 } 133 134 /* Error routines. */ 135 136 __attr __unbound_method(__attr args[]) 137 { 138 __attr excargs[1]; 139 __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); 140 __Raise(exc); 141 return __builtins___none_None; /* superfluous */ 142 } 143 144 /* Generic operations depending on specific program details. */ 145 146 void __SETDEFAULT(__ref obj, int pos, __attr value) 147 { 148 __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); 149 } 150 151 __attr __GETDEFAULT(__ref obj, int pos) 152 { 153 return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); 154 } 155 156 int __BOOL(__attr attr) 157 { 158 __attr args[2] = {{0, 0}, attr}; 159 160 /* Invoke the bool function with the object and test against True. */ 161 162 return __fn___builtins___boolean_bool(args).value == __builtins___boolean_True.value; 163 }