-
Notifications
You must be signed in to change notification settings - Fork 67
/
functionDefinitionPrimitives.c
184 lines (137 loc) · 3.79 KB
/
functionDefinitionPrimitives.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "sq.h"
#include "pharovm/macros.h"
#ifdef FEATURE_FFI
#include "pThreadedFFI.h"
void* defineFunctionWithAnd(ffi_type* parameters[], sqInt count, void* returnType, ffi_abi abi){
ffi_cif* cif;
int returnCode;
cif = malloc(sizeof(ffi_cif));
returnCode = ffi_prep_cif(cif, abi, count, returnType, parameters);
if(returnCode != FFI_OK){
primitiveFailFor(returnCode);
free(cif);
free(parameters);
return NULL;
}
return cif;
}
void* defineVariadicFunction(ffi_type* parameters[], sqInt fixedArgsCount, sqInt totalArguments, void* returnType, ffi_abi abi){
ffi_cif* cif;
int returnCode;
cif = malloc(sizeof(ffi_cif));
returnCode = ffi_prep_cif_var (cif, abi, fixedArgsCount, totalArguments, returnType, parameters);
if(returnCode != FFI_OK){
primitiveFailFor(returnCode);
free(cif);
free(parameters);
return NULL;
}
return cif;
}
#endif
PrimitiveWithDepth(primitiveDefineFunction, 2){
#ifndef FEATURE_FFI
primitiveFail();
#else
sqInt count;
void*handler;
sqInt idx;
sqInt returnTypePosition;
ffi_type** parameters;
sqInt paramsArray;
sqInt receiver;
void*returnType;
ffi_abi abiToUse;
if(methodArgumentCount() == 3){
abiToUse = stackIntegerValue(0);
returnTypePosition = 1;
checkFailed();
}else{
returnTypePosition = 0;
abiToUse = FFI_DEFAULT_ABI;
}
returnType = readAddress(stackValue(returnTypePosition));
checkFailed();
count = stSizeOf(stackValue(returnTypePosition + 1));
checkFailed();
paramsArray = stackValue(returnTypePosition + 1);
checkFailed();
/* The parameters are freed by the primitiveFreeDefinition, if there is an error it is freed by #defineFunction:With:And: */
receiver = stackValue(returnTypePosition + 2);
checkFailed();
parameters = malloc(count*sizeof(void*));
for (idx = 0; idx < count; idx += 1) {
parameters[idx] = (readAddress(stObjectat(paramsArray, idx + 1)));
}
checkFailed()
handler = defineFunctionWithAnd(parameters, count, returnType, abiToUse);
checkFailed();
setHandler(receiver, handler);
checkFailed();
#endif
primitiveEnd();
}
PrimitiveWithDepth(primitiveDefineVariadicFunction, 2){
#ifndef FEATURE_FFI
primitiveFail();
#else
sqInt totalCount;
sqInt fixedArguments;
void*handler;
sqInt idx;
ffi_type** parameters;
sqInt paramsArray;
sqInt receiver;
sqInt fixedArgumentsPosition;
void*returnType;
ffi_abi abiToUse;
if(methodArgumentCount() == 4){
abiToUse = stackIntegerValue(0);
fixedArgumentsPosition = 1;
checkFailed();
}else{
fixedArgumentsPosition = 0;
abiToUse = FFI_DEFAULT_ABI;
}
fixedArguments = stackIntegerValue(fixedArgumentsPosition);
checkFailed();
returnType = readAddress(stackValue(fixedArgumentsPosition + 1));
checkFailed();
totalCount = stSizeOf(stackValue(fixedArgumentsPosition + 2));
checkFailed();
paramsArray = stackValue(fixedArgumentsPosition + 2);
checkFailed();
/* The parameters are freed by the primitiveFreeDefinition, if there is an error it is freed by #defineFunction:with:and:fixedArgumentsCount: */
receiver = stackValue(fixedArgumentsPosition + 3);
checkFailed();
parameters = malloc(totalCount*sizeof(void*));
for (idx = 0; idx < totalCount; idx += 1) {
parameters[idx] = (readAddress(stObjectat(paramsArray, idx + 1)));
}
checkFailed()
handler = defineVariadicFunction(parameters, fixedArguments, totalCount, returnType, abiToUse);
checkFailed();
setHandler(receiver, handler);
checkFailed();
#endif
primitiveEnd();
}
PrimitiveWithDepth(primitiveFreeDefinition, 1){
#ifndef FEATURE_FFI
primitiveFail();
#else
void*handler;
sqInt receiver;
receiver = stackValue(0);
checkFailed();
handler = getHandler(receiver);
checkFailed();
if(!handler){
primitiveFail();
return;
}
free(((ffi_cif*)handler)->arg_types);
free(handler);
setHandler(receiver, 0);
#endif
}