forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-execute.cpp
More file actions
771 lines (673 loc) · 19.2 KB
/
Copy pathscript-execute.cpp
File metadata and controls
771 lines (673 loc) · 19.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/* Copyright (C) 2003-2016 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 "libscript/script.h"
#include "script-private.h"
#include "ffi.h"
#include "foundation-auto.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include "script-execute.hpp"
typedef void (*__MCScriptValueDrop)(void *);
static void __MCScriptDropValueRef(void *p_value)
{
MCValueRelease(*(MCValueRef *)p_value);
}
class MCScriptForeignInvocation
{
public:
MCScriptForeignInvocation(void)
: m_argument_count(0),
m_storage_frontier(0),
m_storage_limit(sizeof(m_stack_storage))
{
}
~MCScriptForeignInvocation(void)
{
// Drop any slots which have a drop method (those whose
// value have been taken will have had their drop reset
// to nil).
for(uindex_t i = 0; i < m_argument_count; i++)
{
if (m_argument_drops[i] != nil)
{
m_argument_drops[i](m_argument_slots[i]);
}
}
}
// Allocate temporary storage for the invocation
bool Allocate(size_t p_amount,
size_t p_align,
void*& r_ptr)
{
// Compute any padding needed for alignment (t_align_delta will
// be the number of pad bytes).
size_t t_align_delta;
t_align_delta = p_align - (m_storage_frontier % p_align);
// If there is not enough space then throw.
if (m_storage_limit - m_storage_frontier < p_amount + t_align_delta)
{
return MCErrorThrowOutOfMemory();
}
// Round the frontier up to the required alignment.
m_storage_frontier += t_align_delta;
// Compute the returned pointer into the actual storage array
r_ptr = m_stack_storage + m_storage_frontier;
// Bump the frontier by the amount allocated.
m_storage_frontier += p_amount;
return true;
}
// Append a non-reference argument - this takes contents_ptr
bool Argument(void *p_slot_ptr,
__MCScriptValueDrop p_slot_drop)
{
if (m_argument_count == kMaxArguments)
{
return MCErrorThrowOutOfMemory();
}
// For normal arguments, the slot is passed directly.
m_argument_values[m_argument_count] = p_slot_ptr;
// Store the slot ptr and its drop.
m_argument_slots[m_argument_count] = p_slot_ptr;
m_argument_drops[m_argument_count] = p_slot_drop;
// Bump the number of arguments
m_argument_count++;
return true;
}
// Append a reference argument - this takes contents_ptr
bool ReferenceArgument(void *p_slot_ptr,
__MCScriptValueDrop p_slot_drop)
{
if (m_argument_count == kMaxArguments ||
!Allocate(sizeof(void *),
alignof(void *),
m_argument_values[m_argument_count]))
{
return MCErrorThrowOutOfMemory();
}
// Store the slot into the reference slot we just created.
*(void **)m_argument_values[m_argument_count] = p_slot_ptr;
// Store the slot ptr and its drop.
m_argument_slots[m_argument_count] = p_slot_ptr;
m_argument_drops[m_argument_count] = p_slot_drop;
// Bump the number of arguments
m_argument_count++;
return true;
}
// Call
bool Call(void *p_function_cif,
void *p_function,
void *p_result_slot_ptr)
{
ffi_call((ffi_cif *)p_function_cif,
(void(*)())p_function,
p_result_slot_ptr,
m_argument_values);
return true;
}
// Take the contents of a slot - this stops the invocation
// cleaning up the taken slot.
void *TakeArgument(uindex_t p_index)
{
void *t_slot_ptr;
t_slot_ptr = m_argument_slots[p_index];
m_argument_slots[p_index] = nil;
m_argument_drops[p_index] = nil;
return t_slot_ptr;
}
private:
enum
{
kMaxArguments = 32,
kMaxStorage = 4096,
};
// The number of arguments currently accumulated.
uindex_t m_argument_count;
// The array of pointers which will be passed to libffi - this will
// be the pointers in m_argument_slots for normal parameters and
// a pointer to the points in m_argument_slots for reference
// parameters.
void *m_argument_values[kMaxArguments];
// The drops for the slot values in m_argument_slots.
__MCScriptValueDrop m_argument_drops[kMaxArguments];
// The actual values in the slots (not indirected for references).
void *m_argument_slots[kMaxArguments];
size_t m_storage_frontier;
size_t m_storage_limit;
char m_stack_storage[kMaxStorage];
};
inline void
__MCScriptComputeSlotAttributes(const MCResolvedTypeInfo& p_slot_type,
size_t& r_slot_size,
size_t& r_slot_align,
__MCScriptValueDrop& r_slot_drop)
{
// If the slot is foreign then we interrogate the foreign descriptor.
if (MCTypeInfoIsForeign(p_slot_type.type))
{
const MCForeignTypeDescriptor *t_desc =
MCForeignTypeInfoGetDescriptor(p_slot_type.type);
r_slot_size = t_desc->size;
r_slot_align = t_desc->size;
r_slot_drop = t_desc->finalize;
return;
}
// If the slot is a foreign handler then it is pointer sized.
if (MCTypeInfoIsHandler(p_slot_type.type) &&
MCHandlerTypeInfoIsForeign(p_slot_type.type))
{
r_slot_size = sizeof(void*);
r_slot_align = alignof(void*);
r_slot_drop = nil;
return;
}
r_slot_size = sizeof(void*);
r_slot_align = alignof(void*);
r_slot_drop = __MCScriptDropValueRef;
}
void
MCScriptExecuteContext::InvokeForeign(MCScriptInstanceRef p_instance,
MCScriptForeignHandlerDefinition *p_handler_def,
uindex_t p_result_reg,
const uindex_t *p_argument_regs,
uindex_t p_argument_count)
{
if (m_error)
{
return;
}
if (p_handler_def->function == nil)
{
if (!MCScriptBindForeignHandlerInInstanceInternal(p_instance,
p_handler_def))
{
Rethrow();
return;
}
}
// Fetch the handler signature.
MCTypeInfoRef t_signature =
GetSignatureOfHandler(p_instance,
p_handler_def);
// Check the parameter count.
if (MCHandlerTypeInfoGetParameterCount(t_signature) != p_argument_count)
{
ThrowWrongNumberOfArguments(p_instance,
p_handler_def,
p_argument_count);
return;
}
MCScriptForeignInvocation t_invocation;
for(uindex_t i = 0; i < MCHandlerTypeInfoGetParameterCount(t_signature); ++i)
{
// Fetch the parameter mode and type
MCHandlerTypeFieldMode t_param_mode =
MCHandlerTypeInfoGetParameterMode(t_signature,
i);
MCTypeInfoRef t_param_type =
MCHandlerTypeInfoGetParameterType(t_signature,
i);
// Compute the parameter's slot size and allocate storage for it
MCResolvedTypeInfo t_resolved_param_type;
if (!ResolveTypeInfo(t_param_type,
t_resolved_param_type))
{
return;
}
size_t t_slot_size = 0;
size_t t_slot_align = 0;
__MCScriptValueDrop t_slot_drop = nil;
__MCScriptComputeSlotAttributes(t_resolved_param_type,
t_slot_size,
t_slot_align,
t_slot_drop);
void *t_slot_ptr = nil;
if (!t_invocation.Allocate(t_slot_size,
t_slot_align,
t_slot_ptr))
{
Rethrow();
return;
}
// If the mode is not out, then we have an initial value to initialize
// it with; otherwise we must initialize it directly.
MCValueRef t_arg_value = nil;
if (t_param_mode != kMCHandlerTypeFieldModeOut)
{
t_arg_value = CheckedFetchRegister(p_argument_regs[i]);
if (t_arg_value == nil)
{
return;
}
}
// Convert the value to an unboxed one.
if (!UnboxingConvert(t_arg_value,
t_resolved_param_type,
t_slot_ptr))
{
return;
}
if (t_slot_ptr == nil)
{
ThrowInvalidValueForArgument(p_instance,
p_handler_def,
i,
t_arg_value);
return;
}
if (t_param_mode == kMCHandlerTypeFieldModeIn)
{
if (!t_invocation.Argument(t_slot_ptr,
t_slot_drop))
{
Rethrow();
return;
}
}
else
{
if (!t_invocation.ReferenceArgument(t_slot_ptr,
t_slot_drop))
{
Rethrow();
return;
}
}
}
// Fetch the return value type.
MCResolvedTypeInfo t_return_value_type;
if (!ResolveTypeInfo(MCHandlerTypeInfoGetReturnType(t_signature),
t_return_value_type))
{
return;
}
// Compute the return value slot size, but only if the return type is not
// nothing.
size_t t_return_value_slot_size = 0;
size_t t_return_value_slot_align = 0;
__MCScriptValueDrop t_return_value_slot_drop = nil;
void *t_return_value_slot_ptr = nil;
if (t_return_value_type.named_type != kMCNullTypeInfo)
{
__MCScriptComputeSlotAttributes(t_return_value_type,
t_return_value_slot_size,
t_return_value_slot_align,
t_return_value_slot_drop);
// Alloate the return value slot storage (which will do nothing for
// zero size).
if (!t_invocation.Allocate(t_return_value_slot_size,
t_return_value_slot_align,
t_return_value_slot_ptr))
{
Rethrow();
return;
}
}
// Call the function - we assume here that if the invocation succeeds
// then the result slot will be valid unless an LC error has been
// raised.
if (!t_invocation.Call(p_handler_def->function_cif,
p_handler_def->function,
t_return_value_slot_ptr) ||
MCErrorIsPending())
{
Rethrow();
return;
}
// Box the return value - this operation 'releases' the contents of
// the slot (i.e. the value is taken by the box).
MCAutoValueRef t_return_value;
if (!BoxingConvert(t_return_value_type,
t_return_value_slot_ptr,
&t_return_value))
{
return;
}
// Store the boxed return value into the result reg (if required).
if (p_result_reg != UINDEX_MAX)
{
if (!CheckedStoreRegister(p_result_reg,
*t_return_value))
{
return;
}
}
for(uindex_t i = 0; i < MCHandlerTypeInfoGetParameterCount(t_signature); ++i)
{
// Fetch the parameter mode and type
MCHandlerTypeFieldMode t_param_mode =
MCHandlerTypeInfoGetParameterMode(t_signature,
i);
MCTypeInfoRef t_param_type =
MCHandlerTypeInfoGetParameterType(t_signature,
i);
// If the mode is in, there is nothing to do for this parameter.
if (t_param_mode == kMCHandlerTypeFieldModeIn)
{
continue;
}
MCResolvedTypeInfo t_resolved_param_type;
if (!ResolveTypeInfo(t_param_type,
t_resolved_param_type))
{
return;
}
// Do a boxing convert - note that we take the slot value from the
// invocation as BoxingConvert will release regardless of successs.
MCAutoValueRef t_arg_value;
if (!BoxingConvert(t_resolved_param_type,
t_invocation.TakeArgument(i),
&t_arg_value))
{
return;
}
// Finally do a checked store to register.
if (!CheckedStoreRegister(p_argument_regs[i],
*t_arg_value))
{
return;
}
}
}
bool
MCScriptExecuteContext::Convert(MCValueRef p_value,
MCTypeInfoRef p_to_type,
MCValueRef& r_new_value)
{
if (p_to_type == nil)
{
r_new_value = MCValueRetain(p_value);
return true;
}
MCResolvedTypeInfo t_resolved_to_type;
if (!ResolveTypeInfo(p_to_type,
t_resolved_to_type))
{
return false;
}
return ConvertToResolvedType(p_value,
t_resolved_to_type,
r_new_value);
}
bool
MCScriptExecuteContext::ConvertToResolvedType(MCValueRef p_value,
const MCResolvedTypeInfo& p_to_type,
MCValueRef& r_new_value)
{
// Get resolved typeinfos.
MCResolvedTypeInfo t_resolved_from_type;
if (!ResolveTypeInfo(MCValueGetTypeInfo(p_value),
t_resolved_from_type))
{
return false;
}
// Check conformance - if this fails, then there is no conversion.
if (!MCResolvedTypeInfoConforms(t_resolved_from_type,
p_to_type))
{
r_new_value = nil;
return true;
}
// The only case which requires a value conversion is when
// one side is foreign - which is a bridging operation. In
// other cases, it is a cast towards root which doesn't require
// a value conversion.
const MCForeignTypeDescriptor *t_from_desc = nil;
if (MCTypeInfoIsForeign(t_resolved_from_type.type))
{
t_from_desc = MCForeignTypeInfoGetDescriptor(t_resolved_from_type.type);
}
const MCForeignTypeDescriptor *t_to_desc = nil;
if (MCTypeInfoIsForeign(p_to_type.type))
{
t_to_desc = MCForeignTypeInfoGetDescriptor(p_to_type.type);
}
if (t_from_desc == t_to_desc)
{
// The two types are the same or are both non-foreign - no conversion is
// needed at this stage.
r_new_value = MCValueRetain(p_value);
}
else if (t_from_desc != nil)
{
// Import the contents of the foreign value as its bridge type. Note
// that not all types can be imported (i.e there is no bridging type)
if (t_from_desc->doimport == nil ||
!t_from_desc->doimport(MCForeignValueGetContentsPtr(p_value),
false,
r_new_value))
{
Rethrow();
return false;
}
}
else if (t_to_desc != nil)
{
// If the foreign-typed slot is optional and the value is undefined,
// don't try to export. This allows optional foreign slots to be set to
// undefined when the type has no bridging type; without this, it is not
// possible to set these slots to undefined!
if (p_to_type.is_optional &&
p_value == kMCNull)
{
r_new_value = MCValueRetain(kMCNull);
}
else
{
// Export the foreign value
if (!MCForeignValueExport(p_to_type.named_type,
p_value,
(MCForeignValueRef&)r_new_value))
{
Rethrow();
return false;
}
}
}
return true;
}
bool
MCScriptExecuteContext::BoxingConvert(const MCResolvedTypeInfo& p_slot_type,
void *p_slot_ptr,
MCValueRef& r_new_value)
{
if (MCTypeInfoIsForeign(p_slot_type.type))
{
// This is a foreign slot type. If the foreign value has the notion
// of 'defined', then we map to Null if it is not defined.
const MCForeignTypeDescriptor *t_desc =
MCForeignTypeInfoGetDescriptor(p_slot_type.type);
if (t_desc->defined != nil &&
!t_desc->defined(p_slot_ptr))
{
r_new_value = MCValueRetain(kMCNull);
}
else if (!MCForeignValueCreateAndRelease(p_slot_type.named_type,
p_slot_ptr,
(MCForeignValueRef&)r_new_value))
{
Rethrow();
return false;
}
}
else if (p_slot_type.named_type == kMCNullTypeInfo)
{
r_new_value = MCValueRetain(kMCNull);
}
else
{
// This is not a foreign slot type, so we just take the valueref value.
// Since we bridge null to nil, we need to check that here.
if (*(MCValueRef *)p_slot_ptr != nil)
{
r_new_value = *(MCValueRef *)p_slot_ptr;
}
else
{
r_new_value = MCValueRetain(kMCNull);
}
}
return true;
}
bool
MCScriptExecuteContext::UnboxingConvert(MCValueRef p_value,
const MCResolvedTypeInfo& p_slot_type,
void*& x_slot_ptr)
{
// If the input value is nil then this is a slot init.
if (p_value == nil)
{
if (MCTypeInfoIsForeign(p_slot_type.type))
{
// The value is foreign, so if it has an initializer - use it.
const MCForeignTypeDescriptor *t_desc =
MCForeignTypeInfoGetDescriptor(p_slot_type.type);
if (t_desc->initialize != nil)
{
if (!t_desc->initialize(x_slot_ptr))
{
Rethrow();
return false;
}
}
}
else
{
// If it is not foreign, then it is a valueref which always
// initialize to unassigned.
*(MCValueRef *)x_slot_ptr = nil;
}
return true;
}
MCResolvedTypeInfo t_resolved_from_type;
if (!ResolveTypeInfo(MCValueGetTypeInfo(p_value),
t_resolved_from_type))
{
return false;
}
// Check conformance - if this fails, then there is no conversion.
if (!MCResolvedTypeInfoConforms(t_resolved_from_type,
p_slot_type))
{
x_slot_ptr = nil;
return true;
}
if (MCTypeInfoIsForeign(p_slot_type.type))
{
const MCForeignTypeDescriptor *t_slot_desc =
MCForeignTypeInfoGetDescriptor(p_slot_type.type);
// If the source is foreign then we just copy the contents, otherwise
// it is a bridging conversion so we must export.
if (MCTypeInfoIsForeign(t_resolved_from_type.type))
{
const MCForeignTypeDescriptor *t_from_desc =
MCForeignTypeInfoGetDescriptor(t_resolved_from_type.type);
// If the two foreign types are the same, copy the contents
if (t_slot_desc == t_from_desc)
{
if (!t_slot_desc->copy(MCForeignValueGetContentsPtr(p_value),
x_slot_ptr))
{
Rethrow();
return false;
}
}
else
{
// Bridging to the common bridging type and back again is required
// Note that we can only get here if the bridging type is common
// (otherwise the types wouldn't conform).
MCAssert(t_slot_desc->bridgetype == t_from_desc->bridgetype);
MCAutoValueRef t_bridged_value;
if (!t_from_desc->doimport(MCForeignValueGetContentsPtr(p_value), false, &t_bridged_value) ||
!t_slot_desc->doexport(*t_bridged_value, false, x_slot_ptr))
{
Rethrow();
return false;
}
}
}
else
{
// If the source value is null, then it must mean the target type
// is nullable.
if (p_value == kMCNull)
{
if (!t_slot_desc->initialize(x_slot_ptr))
{
Rethrow();
return false;
}
}
else if (!t_slot_desc->doexport(p_value,
false,
x_slot_ptr))
{
Rethrow();
return false;
}
}
}
else if (MCTypeInfoIsForeign(t_resolved_from_type.type))
{
const MCForeignTypeDescriptor *t_from_desc =
MCForeignTypeInfoGetDescriptor(t_resolved_from_type.type);
// If the type of the destination slot is not exactly the foreign type,
// import as the bridge type.
if (t_from_desc->bridgetype != kMCNullTypeInfo &&
t_resolved_from_type.type != p_slot_type.type)
{
MCValueRef t_bridged_value;
if (!t_from_desc->doimport(MCForeignValueGetContentsPtr(p_value),
false,
t_bridged_value))
{
Rethrow();
return false;
}
*(MCValueRef *)x_slot_ptr = t_bridged_value;
}
else
{
*(MCValueRef *)x_slot_ptr = MCValueRetain(p_value);
}
}
else if (MCTypeInfoIsHandler(p_slot_type.type) &&
MCHandlerTypeInfoIsForeign(p_slot_type.type))
{
// If the slot type is a foreign handler, then we fetch a closure
// from the handler value.
void *t_function_ptr;
if (!MCHandlerGetFunctionPtr((MCHandlerRef)p_value,
t_function_ptr))
{
Rethrow();
return false;
}
*(void **)x_slot_ptr = t_function_ptr;
}
else
{
// If the valueref is Null, then we map to nil.
if (p_value != kMCNull)
{
*(MCValueRef *)x_slot_ptr = MCValueRetain(p_value);
}
else
{
*(MCValueRef *)x_slot_ptr = nil;
}
}
return true;
}