This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathfoundation-handler.cpp
More file actions
363 lines (296 loc) · 11.7 KB
/
foundation-handler.cpp
File metadata and controls
363 lines (296 loc) · 11.7 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include <ffi.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCHandlerCreate(MCTypeInfoRef p_typeinfo, const MCHandlerCallbacks *p_callbacks, void *p_context, MCHandlerRef& r_handler)
{
MCAssert(MCTypeInfoIsHandler(p_typeinfo));
MCAssert(p_callbacks != nil);
// The context data for an MCHandler is stored after the common elements. The
// start of this is a field 'context' in the struct so we must adjust for its
// length.
__MCHandler *self;
if (!__MCValueCreateExtended(kMCValueTypeCodeHandler,
/* prevent overflow */ MCMin(p_callbacks -> size - sizeof(self->context),
p_callbacks -> size),
self))
return false;
MCMemoryCopy(MCHandlerGetContext(self), p_context, p_callbacks -> size);
self -> typeinfo = MCValueRetain(p_typeinfo);
self -> function_ptr = nil;
self -> callbacks = p_callbacks;
r_handler = self;
return true;
}
MC_DLLEXPORT_DEF
bool MCHandlerInvoke(MCHandlerRef self, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
__MCAssertIsHandler(self);
MCAssert(p_arguments != nil || p_argument_count == 0);
return self -> callbacks -> invoke(MCHandlerGetContext(self), p_arguments, p_argument_count, r_value);
}
#if (defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID)) && !defined(CROSS_COMPILE_HOST)
struct MCHandlerInvokeTrampolineContext
{
MCHandlerRef self;
MCValueRef *p_arguments;
uindex_t p_argument_count;
MCValueRef& r_value;
bool return_value;
};
static void MCHandlerInvokeTrampoline(void *p_context)
{
auto ctxt = static_cast<MCHandlerInvokeTrampolineContext *>(p_context);
ctxt->return_value = MCHandlerInvoke(ctxt->self, ctxt->p_arguments, ctxt->p_argument_count, ctxt->r_value);
}
#endif
MC_DLLEXPORT_DEF
bool MCHandlerExternalInvoke(MCHandlerRef self, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
#if defined(TARGET_SUBPLATFORM_ANDROID) && !defined(CROSS_COMPILE_HOST)
extern bool MCAndroidIsOnEngineThread(void);
if (!MCAndroidIsOnEngineThread())
{
typedef void (*co_yield_callback_t)(void *);
extern void co_yield_to_android_and_call(co_yield_callback_t callback, void *context);
MCHandlerInvokeTrampolineContext t_context = {self, p_arguments, p_argument_count, r_value, true};
co_yield_to_android_and_call(MCHandlerInvokeTrampoline, &t_context);
return t_context.return_value;
}
#elif defined(TARGET_SUBPLATFORM_IPHONE) && !defined(CROSS_COMPILE_HOST)
extern bool MCIPhoneIsOnMainFiber(void);
if (!MCIPhoneIsOnMainFiber())
{
extern void MCIPhoneRunOnMainFiber(void (*)(void *), void *);
MCHandlerInvokeTrampolineContext t_context = {self, p_arguments, p_argument_count, r_value, true};
MCIPhoneRunOnMainFiber(MCHandlerInvokeTrampoline, &t_context);
return t_context.return_value;
}
#endif
return MCHandlerInvoke(self, p_arguments, p_argument_count, r_value);
}
MC_DLLEXPORT_DEF
MCErrorRef MCHandlerTryToInvokeWithList(MCHandlerRef self, MCProperListRef& x_arguments, MCValueRef& r_value)
{
__MCAssertIsHandler(self);
__MCAssertIsProperList(x_arguments);
MCAutoValueRefArray t_args;
MCAutoProperListRef t_out_args;
if (!t_args . New(MCProperListGetLength(x_arguments)))
goto error_exit;
for(uindex_t i = 0; i < MCProperListGetLength(x_arguments); i++)
t_args[i] = MCValueRetain(MCProperListFetchElementAtIndex(x_arguments, i));
if (!MCHandlerInvoke(self, t_args . Ptr(), t_args . Size(), r_value))
goto error_exit;
if (!t_args . TakeAsProperList(Out(t_out_args)))
goto error_exit;
MCValueAssign(x_arguments, t_out_args . Take());
return nil;
error_exit:
r_value = nil;
MCErrorRef t_error;
if (!MCErrorCatch(t_error))
return nil;
return t_error;
}
MC_DLLEXPORT_DEF
MCErrorRef MCHandlerTryToExternalInvokeWithList(MCHandlerRef self, MCProperListRef& x_arguments, MCValueRef& r_value)
{
__MCAssertIsHandler(self);
__MCAssertIsProperList(x_arguments);
MCAutoValueRefArray t_args;
MCAutoProperListRef t_out_args;
if (!t_args . New(MCProperListGetLength(x_arguments)))
goto error_exit;
for(uindex_t i = 0; i < MCProperListGetLength(x_arguments); i++)
t_args[i] = MCValueRetain(MCProperListFetchElementAtIndex(x_arguments, i));
if (!MCHandlerExternalInvoke(self, t_args . Ptr(), t_args . Size(), r_value))
goto error_exit;
if (!t_args . TakeAsProperList(Out(t_out_args)))
goto error_exit;
MCValueAssign(x_arguments, t_out_args . Take());
return nil;
error_exit:
r_value = nil;
MCErrorRef t_error;
if (!MCErrorCatch(t_error))
return nil;
return t_error;
}
MC_DLLEXPORT_DEF
void *MCHandlerGetContext(MCHandlerRef self)
{
__MCAssertIsHandler(self);
return (void *)self -> context;
}
MC_DLLEXPORT_DEF
const MCHandlerCallbacks *MCHandlerGetCallbacks(MCHandlerRef self)
{
__MCAssertIsHandler(self);
return self -> callbacks;
}
////////////////////////////////////////////////////////////////////////////////
static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data)
{
MCHandlerRef t_handler;
t_handler = (MCHandlerRef)user_data;
MCTypeInfoRef t_signature;
t_signature = t_handler -> typeinfo;
// Check the arity of the handler - at the moment we can only handle 16
// arguments.
uindex_t t_arity;
t_arity = MCHandlerTypeInfoGetParameterCount(t_signature);
if (t_arity > 16)
{
MCErrorThrowUnimplemented(MCSTR("closures only supported for handlers with at most 16 parameters"));
return;
}
// Check that we can resolve the return type.
MCTypeInfoRef t_return_type;
t_return_type = MCHandlerTypeInfoGetReturnType(t_signature);
MCResolvedTypeInfo t_resolved_return_type;
if (!MCTypeInfoResolve(t_return_type, t_resolved_return_type))
{
MCErrorThrowUnboundType(t_return_type);
return;
}
// Build the argument list, doing foreign type bridging as necessary.
MCValueRef t_value_result;
MCValueRef t_value_args[16];
uindex_t t_arg_index;
t_arg_index = 0;
t_value_result = nil;
for(t_arg_index = 0; t_arg_index < t_arity; t_arg_index++)
{
MCHandlerTypeFieldMode t_mode;
t_mode = MCHandlerTypeInfoGetParameterMode(t_signature, t_arg_index);
MCTypeInfoRef t_type;
t_type = MCHandlerTypeInfoGetParameterType(t_signature, t_arg_index);
// We don't support anything other than 'in' mode parameters at the moment.
if (t_mode != kMCHandlerTypeFieldModeIn)
{
MCErrorThrowUnimplemented(MCSTR("closures only support in arguments"));
goto cleanup;
}
// Make sure we can resolve the parameter type.
MCResolvedTypeInfo t_resolved_type;
if (!MCTypeInfoResolve(t_type, t_resolved_type))
{
MCErrorThrowUnboundType(t_type);
goto cleanup;
}
// If the parameter type is foreign then we must attempt to bridge it
// from the passed in type; otherwise we just retain the valueref.
if (MCTypeInfoIsForeign(t_resolved_type . type))
{
const MCForeignTypeDescriptor *t_descriptor;
t_descriptor = MCForeignTypeInfoGetDescriptor(t_resolved_type . type);
if (t_descriptor -> defined != nil &&
!t_descriptor -> defined(args[t_arg_index]))
t_value_args[t_arg_index] = MCValueRetain(kMCNull);
else
{
if (t_descriptor -> bridgetype != kMCNullTypeInfo)
{
if (!t_descriptor -> doimport(t_descriptor, args[t_arg_index], false, t_value_args[t_arg_index]))
goto cleanup;
}
else
{
if (!MCForeignValueCreate(t_resolved_type . named_type, args[t_arg_index], (MCForeignValueRef&)t_value_args[t_arg_index]))
goto cleanup;
}
}
}
else
{
t_value_args[t_arg_index] = MCValueRetain(*(MCValueRef*)args[t_arg_index]);
}
}
// Actually call the LCB handler.
if (!MCHandlerInvoke(t_handler, t_value_args, t_arity, t_value_result))
goto cleanup;
// If the return type is not 'void' then we must map the value back
// appropriately.
if (t_resolved_return_type . named_type != kMCNullTypeInfo)
{
if (MCTypeInfoIsForeign(t_resolved_return_type . type))
{
const MCForeignTypeDescriptor *t_descriptor;
t_descriptor = MCForeignTypeInfoGetDescriptor(t_resolved_return_type . type);
if (!t_descriptor -> doexport(t_descriptor, t_value_result, false, ret))
goto cleanup;
}
else
{
*(MCValueRef *)ret = t_value_result;
t_value_result = nil;
}
}
cleanup:
if (t_value_result != nil)
MCValueRelease(t_value_result);
for(uindex_t i = 0; i < t_arg_index; i++)
MCValueRelease(t_value_args[i]);
}
MC_DLLEXPORT_DEF
bool MCHandlerGetFunctionPtr(MCHandlerRef self, void*& r_function_ptr)
{
__MCAssertIsHandler(self);
if (self -> function_ptr != nil)
{
r_function_ptr = self -> function_ptr;
return true;
}
ffi_cif *t_cif;
if (!MCHandlerTypeInfoGetLayoutType(self -> typeinfo, (int)FFI_DEFAULT_ABI, (void*&)t_cif))
return false;
self -> closure = ffi_closure_alloc(sizeof(ffi_closure), &self -> function_ptr);
if (self -> closure == nil)
return MCErrorThrowOutOfMemory();
if (ffi_prep_closure_loc((ffi_closure *)self -> closure, t_cif, __exec_closure, self, self -> function_ptr) != FFI_OK)
{
ffi_closure_free(self -> closure);
self -> closure = nil;
return MCErrorThrowGeneric(MCSTR("unexpected libffi failure"));
}
r_function_ptr = self -> function_ptr;
return true;
}
////////////////////////////////////////////////////////////////////////////////
void __MCHandlerDestroy(__MCHandler *self)
{
if (self -> function_ptr != nil)
ffi_closure_free(self -> closure);
}
hash_t __MCHandlerHash(__MCHandler *self)
{
return MCHashPointer(self);
}
bool __MCHandlerIsEqualTo(__MCHandler *self, __MCHandler *other_self)
{
return self == other_self;
}
bool __MCHandlerCopyDescription(__MCHandler *self, MCStringRef& r_desc)
{
if (NULL != self->callbacks->describe)
return self->callbacks->describe(MCHandlerGetContext (self), r_desc);
/* Default implementation. */
/* FIXME Should include information about arguments and return
* values, extracted from the handler's typeinfo. */
return MCStringCopy(MCSTR("<handler>"), r_desc);
}
////////////////////////////////////////////////////////////////////////////////