-
Notifications
You must be signed in to change notification settings - Fork 71
/
callbackPrimitives.c
224 lines (178 loc) · 5.93 KB
/
callbackPrimitives.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "callbacks.h"
#include "pharovm/macros.h"
/* primitiveReadNextWorkerCallback
* answers next pending callback
*/
PrimitiveWithDepth(primitiveReadNextCallback, 1){
CallbackInvocation *address;
sqInt externalAddress;
address = queue_next_pending_callback();
if(address) {
externalAddress = instantiateClassindexableSize(classExternalAddress(), sizeof(void*));
checkFailed();
writeAddress(externalAddress, address);
checkFailed();
} else {
externalAddress = nilObject();
}
primitiveEndReturn(externalAddress);
}
PrimitiveWithDepth(primitiveGetCallbackInvocationUserData, 2){
CallbackInvocation *callbackInvocation;
sqInt receiver = getReceiver();
checkFailed();
callbackInvocation = (CallbackInvocation *)getHandler(receiver);
checkFailed();
sqInt returnValue = stringForCString((char*)callbackInvocation->callback->userData);
if(!returnValue){
primitiveFailFor(PrimErrNoMemory);
return;
}
primitiveEndReturn(returnValue);
}
/*
* Initialize the callback queue with the semaphore index
*
* Arguments:
*
* - 0 semaphoreIndex <SmallInteger>
*/
Primitive(primitiveInitilizeCallbacks){
int semaphoreIndex = stackIntegerValue(0);
checkFailed();
initilizeCallbacks(semaphoreIndex);
primitiveEnd();
}
/* primitiveUnregisterWorkerCallback
* unregisters callback (taking a handle as parameter)
* arguments:
* - callbackHandle <ExternalAddress>
*/
PrimitiveWithDepth(primitiveUnregisterCallback, 1){
Callback *callback;
callback = (Callback *)readAddress(stackValue(0));
checkFailed();
if(callback != NULL)
callback_release(callback);
primitiveEnd();
}
/*
* This primitive register a callback in libFFI.
* This primitive generates the pointer to the function to be passed as the callback.
* To do so, it generates all the structures expected by libFfi.
*
* It uses two objects, the receiver and a optional parameter ByteString object.
*
* The receiver is a TFCallback.
*
* It should at least have the following instance variables
*
* 0: handler: The pointer to the C callback function. This is the pointer passed to the C libraries using the callback.
* 1: callbackData: A pointer to the plugin internal data structure.
* 2: parameterHandlers
* 3: returnTypeHandler
* 4: runner
*
* The parameter is a ByteString that will be stored in the internal callback structure as a way of debugging.
* The parameter can be nil.
*/
PrimitiveWithDepth(primitiveRegisterCallback, 3){
sqInt callbackHandle;
Callback *callback;
sqInt count;
sqInt idx;
sqInt paramArray;
sqInt runnerInstance;
ffi_type **parameters;
sqInt receiver;
ffi_type *returnType;
sqInt debugString;
receiver = getReceiver();
checkFailed();
//As the parameter is optional, the primitive invocation can came without it
if(methodArgumentCount() == 1){
debugString = stackObjectValue(0);
checkFailed();
}else{
debugString = nilObject();
}
callbackHandle = getAttributeOf(receiver, 1);
checkFailed();
paramArray = getAttributeOf(receiver, 2);
checkFailed();
returnType = getHandler(getAttributeOf(receiver, 3));
checkFailed();
runnerInstance = getAttributeOf(receiver, 4);
checkFailed();
Runner *runner = (Runner *)getHandler(runnerInstance);
checkFailed();
if(runner == NULL){
primitiveFail();
return;
}
count = stSizeOf(paramArray);
checkFailed();
//This array is freed when the callback is released.
//If there is an error during the creation of the callback.
//callback_new() frees it.
parameters = malloc(count*sizeof(void*));
for (idx = 0; idx < count; idx += 1) {
parameters[idx] = (getHandler(stObjectat(paramArray, idx + 1)));
}
checkFailed();
callback = callback_new(runner, parameters, count, returnType);
checkFailed();
if(debugString == nilObject()){
callback->userData = NULL;
}else{
callback->userData = malloc(strlen(readString(debugString)) + 1);
strcpy(callback->userData, readString(debugString));
}
setHandler(receiver, callback->functionAddress);
checkFailed();
writeAddress(callbackHandle, callback);
checkFailed();
primitiveEnd();
}
/* primitiveWorkerCallbackReturn
* returns from a callback
* receiver <TFCallbackInvocation>
* It returns true if the callback can return, and false if the order is not correct and should
* retry later.
*/
PrimitiveWithDepth(primitiveCallbackReturn, 2) {
CallbackInvocation *callbackInvocation;
sqInt receiver, callbackInstance, runnerInstance;
Runner *runner;
// A callback invocation
receiver = getReceiver();
checkFailed();
callbackInstance = getAttributeOf(receiver, 1);
checkFailed();
runnerInstance = getAttributeOf(callbackInstance, 4);
checkFailed();
runner = (Runner *)getHandler(runnerInstance);
checkFailed();
if(!runner){
primitiveFail();
return;
}
callbackInvocation = (CallbackInvocation*)getHandler(receiver);
checkFailed();
if(!callbackInvocation){
primitiveFail();
return;
}
// If the returning callback is not the last callback that entered, we cannot return
// Otherwise this would produce a stack corruption (returning to an older callback erasing/overriding the stack of newer ones)
if (callbackInvocation != runner->callbackStack){
primitiveEndReturn(falseObject());
return;
}
//We have to do this here. Because the callbackExitFunction may not return
//If we are in the samethread runner it does not return, as it uses a sig_longjmp to return to the caller of the callback!
primitiveEndReturn(trueObject());
// If the callback was the last one, we need to pop it from the callback stack
runner->callbackStack = runner->callbackStack->previous;
runner->callbackExitFunction(runner, callbackInvocation);
}