forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-instance.cpp
More file actions
executable file
·2587 lines (2124 loc) · 104 KB
/
script-instance.cpp
File metadata and controls
executable file
·2587 lines (2124 loc) · 104 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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2013 Runtime Revolution 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
////////////////////////////////////////////////////////////////////////////////
// This context struct, callbacks and associated functions provide an implementation
// of MCHandler for handlers coming from libscript. It enables an MCHandlerRef
// created from within LCB to be used 'out-of-frame' - i.e. not just from direct
// invocation within the LCB VM.
struct __MCScriptHandlerContext
{
MCScriptInstanceRef instance;
MCScriptCommonHandlerDefinition *definition;
};
extern MCHandlerCallbacks __kMCScriptHandlerCallbacks;
bool __MCScriptHandlerInvoke(void *context, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value);
void __MCScriptHandlerRelease(void *context);
bool __MCScriptHandlerDescribe(void *context, MCStringRef &r_desc);
////////////////////////////////////////////////////////////////////////////////
// This is the module of the most recent LCB stack frame on the current thread's
// stack. It is set before and after a foreign handler call so that the native
// code can get some element of context.
static MCScriptModuleRef s_current_module = nil;
////////////////////////////////////////////////////////////////////////////////
bool MCScriptCreateInstanceOfModule(MCScriptModuleRef p_module, MCScriptInstanceRef& r_instance)
{
bool t_success;
t_success = true;
MCScriptInstanceRef t_instance;
t_instance = nil;
// If the module is not usable, then we cannot create an instance.
if (t_success)
if (!p_module -> is_usable)
return false;
// If this is a module which shares a single instance, then return that if we have
// one.
if (p_module -> module_kind != kMCScriptModuleKindWidget &&
p_module -> shared_instance != nil)
{
r_instance = MCScriptRetainInstance(p_module -> shared_instance);
return true;
}
// Attempt to create a script object.
if (t_success)
t_success = MCScriptCreateObject(kMCScriptObjectKindInstance, sizeof(MCScriptInstance), (MCScriptObject*&)t_instance);
// Now associate the script object with the module (so the 'slots' field make sense).
if (t_success)
{
t_instance -> module = MCScriptRetainModule(p_module);
}
// Now allocate the slots field.
if (t_success)
t_success = MCMemoryNewArray(p_module -> slot_count, t_instance -> slots);
if (t_success)
{
for(uindex_t i = 0; i < p_module -> slot_count; i++)
t_instance -> slots[i] = MCValueRetain(kMCNull);
// If this is a module which shares its instance, then add a link to it.
// (Note this is weak reference - we don't retain, otherwise we would have
// a cycle!).
if (p_module -> module_kind != kMCScriptModuleKindWidget)
p_module -> shared_instance = t_instance;
r_instance = t_instance;
}
else
MCScriptDestroyObject(t_instance);
return t_success;
}
void MCScriptDestroyInstance(MCScriptInstanceRef self)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
// If the object had slots initialized, then free them.
if (self -> slots != nil)
{
for(uindex_t i = 0; i < self -> module -> slot_count; i++)
MCValueRelease(self -> slots[i]);
MCMemoryDeleteArray(self -> slots);
}
// If the instance has handler-refs, then free them.
for(uindex_t i = 0; i < self -> handler_count; i++)
MCValueRelease(self -> handlers[i] . value);
MCMemoryDeleteArray(self -> handlers);
// If the instance has a module, and this is the shared instance then set
// the shared instance field to nil.
if (self -> module != nil &&
self -> module -> shared_instance == self)
self -> module -> shared_instance = nil;
// If the instance was associated with its module, then release it.
if (self -> module != nil)
MCScriptReleaseModule(self -> module);
// The rest of the deallocation is handled by MCScriptDestroyObject.
}
////////////////////////////////////////////////////////////////////////////////
MCScriptInstanceRef MCScriptRetainInstance(MCScriptInstanceRef self)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
return (MCScriptInstanceRef)MCScriptRetainObject(self);
}
void MCScriptReleaseInstance(MCScriptInstanceRef self)
{
if (nil == self)
return;
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
MCScriptReleaseObject(self);
}
////////////////////////////////////////////////////////////////////////////////
MCScriptModuleRef MCScriptGetModuleOfInstance(MCScriptInstanceRef self)
{
return self -> module;
}
////////////////////////////////////////////////////////////////////////////////
bool MCHandlerTypeInfoConformsToPropertyGetter(MCTypeInfoRef typeinfo)
{
return MCHandlerTypeInfoGetParameterCount(typeinfo) == 0 &&
MCHandlerTypeInfoGetReturnType(typeinfo) != kMCNullTypeInfo;
}
bool MCHandlerTypeInfoConformsToPropertySetter(MCTypeInfoRef typeinfo)
{
return MCHandlerTypeInfoGetParameterCount(typeinfo) == 1 &&
MCHandlerTypeInfoGetParameterMode(typeinfo, 0) == kMCHandlerTypeFieldModeIn;
}
///////////
bool MCScriptThrowAttemptToSetReadOnlyPropertyError(MCScriptModuleRef p_module, MCNameRef p_property)
{
return MCErrorCreateAndThrow(kMCScriptCannotSetReadOnlyPropertyErrorTypeInfo, "module", p_module -> name, "property", p_property, nil);
}
bool MCScriptThrowInvalidValueForPropertyError(MCScriptModuleRef p_module, MCNameRef p_property, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidPropertyValueErrorTypeInfo, "module", p_module -> name, "property", p_property, "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowInvalidValueForResultError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidReturnValueErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowInParameterNotDefinedError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler, uindex_t p_index)
{
return MCErrorCreateAndThrow(kMCScriptInParameterNotDefinedErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), "parameter", MCScriptGetNameOfParameterInModule(p_module, p_handler, p_index), nil);
}
bool MCScriptThrowOutParameterNotDefinedError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler, uindex_t p_index)
{
return MCErrorCreateAndThrow(kMCScriptOutParameterNotDefinedErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), "parameter", MCScriptGetNameOfParameterInModule(p_module, p_handler, p_index), nil);
}
bool MCScriptThrowLocalVariableUsedBeforeDefinedError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler, uindex_t p_index)
{
return MCErrorCreateAndThrow(kMCScriptVariableUsedBeforeDefinedErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), "variable", MCScriptGetNameOfLocalVariableInModule(p_module, p_handler, p_index), nil);
}
bool MCScriptThrowGlobalVariableUsedBeforeDefinedError(MCScriptModuleRef p_module, uindex_t p_index)
{
return MCErrorCreateAndThrow(kMCScriptVariableUsedBeforeDefinedErrorTypeInfo, "module", p_module -> name, "variable", MCScriptGetNameOfGlobalVariableInModule(p_module, p_index), nil);
}
bool MCScriptThrowInvalidValueForLocalVariableError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler, uindex_t p_index, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidVariableValueErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), "variable", MCScriptGetNameOfLocalVariableInModule(p_module, p_handler, p_index), "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowInvalidValueForGlobalVariableError(MCScriptModuleRef p_module, uindex_t p_index, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidVariableValueErrorTypeInfo, "module", p_module -> name, "variable", MCScriptGetNameOfGlobalVariableInModule(p_module, p_index), "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowInvalidValueForContextVariableError(MCScriptModuleRef p_module, uindex_t p_index, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidVariableValueErrorTypeInfo, "module", p_module -> name, "variable", MCScriptGetNameOfContextVariableInModule(p_module, p_index), "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowNotABooleanError(MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptNotABooleanValueErrorTypeInfo, "value", p_value, nil);
}
bool MCScriptThrowWrongNumberOfArgumentsForInvokeError(MCScriptModuleRef p_module, MCScriptDefinition *p_definition, uindex_t p_provided)
{
// TODO: Encode provided / expected.
return MCErrorCreateAndThrow(kMCScriptWrongNumberOfArgumentsErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_definition), nil);
}
bool MCScriptThrowInvalidValueForArgumentError(MCScriptModuleRef p_module, MCScriptDefinition *p_definition, uindex_t p_arg_index, MCTypeInfoRef p_expected_type, MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptInvalidArgumentValueErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_definition), "parameter", MCScriptGetNameOfParameterInModule(p_module, p_definition, p_arg_index), "type", MCNamedTypeInfoGetName(p_expected_type), "value", p_value, nil);
}
bool MCScriptThrowUnableToResolveForeignHandlerError(MCScriptModuleRef p_module, MCScriptDefinition *p_definition)
{
return MCErrorCreateAndThrow(kMCScriptForeignHandlerBindingErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_definition), nil);
}
bool MCScriptThrowUnableToResolveTypeError(MCTypeInfoRef p_type)
{
return MCErrorThrowUnboundType(p_type);
}
bool MCScriptThrowUnableToResolveMultiInvoke(MCScriptModuleRef p_module, MCScriptDefinition *p_definition, MCProperListRef p_arguments)
{
MCAutoListRef t_handlers;
if (!MCListCreateMutable(',', &t_handlers))
return false;
MCScriptDefinitionGroupDefinition *t_group;
t_group = static_cast<MCScriptDefinitionGroupDefinition *>(p_definition);
for(uindex_t i = 0; i < t_group -> handler_count; i++)
{
MCNameRef t_name;
t_name = MCScriptGetNameOfDefinitionInModule(p_module, p_module -> definitions[t_group -> handlers[i]]);
if (!MCListAppend(*t_handlers, t_name))
return false;
}
MCAutoListRef t_types;
if (!MCListCreateMutable(',', &t_types))
return false;
for(uindex_t i = 0; i < MCProperListGetLength(p_arguments); i++)
{
MCTypeInfoRef t_type;
t_type = MCValueGetTypeInfo(MCProperListFetchElementAtIndex(p_arguments, i));
MCNewAutoNameRef t_type_name;
if (MCTypeInfoIsNamed(t_type))
t_type_name = MCNamedTypeInfoGetName(t_type);
else
t_type_name = MCNAME("unknown");
if (!MCListAppend(*t_types, *t_type_name))
return false;
}
MCAutoStringRef t_handler_list, t_type_list;
if (!MCListCopyAsString(*t_handlers, &t_handler_list) ||
!MCListCopyAsString(*t_types, &t_type_list))
return false;
return MCErrorCreateAndThrow(kMCScriptNoMatchingHandlerErrorTypeInfo, "handlers", *t_handler_list, "types", *t_type_list, nil);
}
bool MCScriptThrowNotAHandlerValueError(MCValueRef p_value)
{
return MCErrorCreateAndThrow(kMCScriptNotAHandlerValueErrorTypeInfo, "value", p_value, nil);
}
bool MCScriptThrowCannotCallContextHandlerError(MCScriptModuleRef p_module, MCScriptDefinition *p_handler)
{
return MCErrorCreateAndThrow(kMCScriptCannotCallContextHandlerErrorTypeInfo, "module", p_module -> name, "handler", MCScriptGetNameOfDefinitionInModule(p_module, p_handler), nil);
}
bool MCScriptThrowHandlerNotFoundError(MCScriptModuleRef p_module, MCNameRef p_handler)
{
return MCErrorCreateAndThrow(kMCScriptHandlerNotFoundErrorTypeInfo, "module", p_module -> name, "handler", p_handler, nil);
}
bool MCScriptThrowPropertyNotFoundError(MCScriptModuleRef p_module, MCNameRef p_property)
{
return MCErrorCreateAndThrow(kMCScriptPropertyNotFoundErrorTypeInfo, "module", p_module -> name, "property", p_property, nil);
}
///////////
MCScriptVariableDefinition *MCScriptDefinitionAsVariable(MCScriptDefinition *self)
{
__MCScriptAssert__(self -> kind == kMCScriptDefinitionKindVariable,
"definition not a variable");
return static_cast<MCScriptVariableDefinition *>(self);
}
MCScriptHandlerDefinition *MCScriptDefinitionAsHandler(MCScriptDefinition *self)
{
__MCScriptAssert__(self -> kind == kMCScriptDefinitionKindHandler,
"definition not a handler");
return static_cast<MCScriptHandlerDefinition *>(self);
}
MCScriptForeignHandlerDefinition *MCScriptDefinitionAsForeignHandler(MCScriptDefinition *self)
{
__MCScriptAssert__(self -> kind == kMCScriptDefinitionKindForeignHandler,
"definition not a foreign handler");
return static_cast<MCScriptForeignHandlerDefinition *>(self);
}
MCScriptDefinitionGroupDefinition *MCScriptDefinitionAsDefinitionGroup(MCScriptDefinition *self)
{
__MCScriptAssert__(self -> kind == kMCScriptDefinitionKindDefinitionGroup,
"definition not a definition group");
return static_cast<MCScriptDefinitionGroupDefinition *>(self);
}
///////////
bool MCScriptGetPropertyOfInstance(MCScriptInstanceRef self, MCNameRef p_property, MCValueRef& r_value)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
// Lookup the definition (throws if not found).
MCScriptPropertyDefinition *t_definition;
if (!MCScriptLookupPropertyDefinitionInModule(self -> module, p_property, t_definition))
return MCScriptThrowPropertyNotFoundError(self -> module, p_property);
MCScriptDefinition *t_getter;
t_getter = t_definition -> getter != 0 ? self -> module -> definitions[t_definition -> getter - 1] : nil;
/* LOAD CHECK */ __MCScriptAssert__(t_getter != nil,
"property has no getter");
/* LOAD CHECK */ __MCScriptAssert__(t_getter -> kind == kMCScriptDefinitionKindVariable ||
t_getter -> kind == kMCScriptDefinitionKindHandler,
"property getter is not a variable or handler");
if (t_getter -> kind == kMCScriptDefinitionKindVariable)
{
// The easy case - fetching a variable-based property.
MCScriptVariableDefinition *t_variable_def;
t_variable_def = MCScriptDefinitionAsVariable(t_getter);
// Variables are backed by an slot in the instance.
uindex_t t_slot_index;
t_slot_index = t_variable_def -> slot_index;
/* COMPUTE CHECK */ __MCScriptAssert__(t_slot_index < self -> module -> slot_count,
"computed variable slot out of range");
// Slot based properties are easy, we just copy the value out of the slot.
r_value = MCValueRetain(self -> slots[t_slot_index]);
}
else if (t_getter -> kind == kMCScriptDefinitionKindHandler)
{
// The more difficult case - we have to execute a handler.
MCScriptHandlerDefinition *t_handler_def;
t_handler_def = MCScriptDefinitionAsHandler(t_getter);
/* LOAD CHECK */ __MCScriptAssert__(MCHandlerTypeInfoConformsToPropertyGetter(self -> module -> types[t_handler_def -> type] -> typeinfo),
"incorrect signature for property getter");
if (!MCScriptCallHandlerOfInstanceInternal(self, t_handler_def, nil, 0, r_value))
return false;
}
else
{
__MCScriptUnreachable__("inconsistency with definition kind in property fetching");
}
return true;
}
bool MCScriptSetPropertyOfInstance(MCScriptInstanceRef self, MCNameRef p_property, MCValueRef p_value)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
// Lookup the definition.
MCScriptPropertyDefinition *t_definition;
if (!MCScriptLookupPropertyDefinitionInModule(self -> module, p_property, t_definition))
return MCScriptThrowPropertyNotFoundError(self -> module, p_property);
MCScriptDefinition *t_setter;
t_setter = t_definition -> setter != 0 ? self -> module -> definitions[t_definition -> setter - 1] : nil;
// If there is no setter for the property then this is an error.
if (t_setter == nil)
return MCScriptThrowAttemptToSetReadOnlyPropertyError(self -> module, p_property);
/* LOAD CHECK */ __MCScriptAssert__(t_setter != nil,
"property has no setter");
/* LOAD CHECK */ __MCScriptAssert__(t_setter -> kind == kMCScriptDefinitionKindVariable ||
t_setter -> kind == kMCScriptDefinitionKindHandler,
"property setter is not a variable or handler");
if (t_setter -> kind == kMCScriptDefinitionKindVariable)
{
// The easy case - storing a variable-based property.
MCScriptVariableDefinition *t_variable_def;
t_variable_def = MCScriptDefinitionAsVariable(t_setter);
MCTypeInfoRef t_type;
t_type = self -> module -> types[t_variable_def -> type] -> typeinfo;
// Make sure the value is of the correct type - if not it is an error.
// (The caller has to ensure things are converted as appropriate).
if (!MCTypeInfoConforms(MCValueGetTypeInfo(p_value), t_type))
return MCScriptThrowInvalidValueForPropertyError(self -> module, p_property, t_type, p_value);
// Variables are backed by an slot in the instance.
uindex_t t_slot_index;
t_slot_index = t_variable_def -> slot_index;
/* COMPUTE CHECK */ __MCScriptAssert__(t_slot_index < self -> module -> slot_count,
"computed variable slot out of range");
// If the value of the slot isn't changing, assign our new value.
if (p_value != self -> slots[t_slot_index])
{
MCValueRelease(self -> slots[t_slot_index]);
self -> slots[t_slot_index] = MCValueRetain(p_value);
}
}
else if (t_setter -> kind == kMCScriptDefinitionKindHandler)
{
// The more difficult case - we have to execute a handler.
MCScriptHandlerDefinition *t_handler_def;
t_handler_def = MCScriptDefinitionAsHandler(t_setter);
/* LOAD CHECK */ __MCScriptAssert__(MCHandlerTypeInfoConformsToPropertySetter(self -> module -> types[t_handler_def -> type] -> typeinfo),
"incorrect signature for property setter");
// Get the required type of the parameter.
MCTypeInfoRef t_property_type;
t_property_type = MCHandlerTypeInfoGetParameterType(self -> module -> types[t_handler_def -> type] -> typeinfo, 0);
// Make sure the value if of the correct type - if not it is an error.
// (The caller has to ensure things are converted as appropriate).
if (!MCTypeInfoConforms(MCValueGetTypeInfo(p_value), t_property_type))
return MCScriptThrowInvalidValueForPropertyError(self -> module, p_property, t_property_type, p_value);
MCValueRef t_result;
if (!MCScriptCallHandlerOfInstanceInternal(self, t_handler_def, &p_value, 1, t_result))
return false;
MCValueRelease(t_result);
}
else
{
__MCScriptUnreachable__("inconsistency with definition kind in property fetching");
}
return true;
}
static bool MCScriptCallHandlerOfInstanceDirect(MCScriptInstanceRef self, MCScriptHandlerDefinition *p_handler, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
// Get the signature of the handler.
MCTypeInfoRef t_signature;
t_signature = self -> module -> types[p_handler -> type] -> typeinfo;
// If the handler is of context scope, then we cannot call it directly - only
// from a LCB frame.
if (p_handler -> scope == kMCScriptHandlerScopeContext)
return MCScriptThrowCannotCallContextHandlerError(self -> module, p_handler);
// Check the number of arguments.
uindex_t t_required_param_count;
t_required_param_count = MCHandlerTypeInfoGetParameterCount(t_signature);
if (t_required_param_count != p_argument_count)
return MCScriptThrowWrongNumberOfArgumentsForInvokeError(self -> module, p_handler, p_argument_count);
// Check the types of the arguments.
for(uindex_t i = 0; i < t_required_param_count; i++)
{
MCHandlerTypeFieldMode t_mode;
t_mode = MCHandlerTypeInfoGetParameterMode(t_signature, i);
if (t_mode != kMCHandlerTypeFieldModeOut)
{
if (p_arguments[i] == nil)
return MCScriptThrowInParameterNotDefinedError(self -> module, p_handler, i);
MCTypeInfoRef t_type;
t_type = MCHandlerTypeInfoGetParameterType(t_signature, i);
if (!MCTypeInfoConforms(MCValueGetTypeInfo(p_arguments[i]), t_type))
return MCScriptThrowInvalidValueForArgumentError(self -> module, p_handler, i, t_type, p_arguments[i]);
}
}
// Now the input argument array is appropriate, we can just call the handler.
if (!MCScriptCallHandlerOfInstanceInternal(self, p_handler, p_arguments, p_argument_count, r_value))
return false;
return true;
}
bool MCScriptCallHandlerOfInstance(MCScriptInstanceRef self, MCNameRef p_handler, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
// Lookup the definition (throws if not found).
MCScriptHandlerDefinition *t_definition;
if (!MCScriptLookupHandlerDefinitionInModule(self -> module, p_handler, t_definition))
return MCScriptThrowHandlerNotFoundError(self -> module, p_handler);
return MCScriptCallHandlerOfInstanceDirect(self, t_definition, p_arguments, p_argument_count, r_value);
}
bool MCScriptCallHandlerOfInstanceIfFound(MCScriptInstanceRef self, MCNameRef p_handler, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindInstance);
// Lookup the definition (throws if not found).
MCScriptHandlerDefinition *t_definition;
if (!MCScriptLookupHandlerDefinitionInModule(self -> module, p_handler, t_definition))
{
r_value = MCValueRetain(kMCNull);
return true;
}
return MCScriptCallHandlerOfInstanceDirect(self, t_definition, p_arguments, p_argument_count, r_value);
}
////////////////////////////////////////////////////////////////////////////////
struct MCScriptFrameContext
{
uindex_t count;
MCValueRef slots[1];
};
// This structure is a single frame on the execution stack.
struct MCScriptFrame
{
// The calling frame.
MCScriptFrame *caller;
// The instance we are executing within.
MCScriptInstanceRef instance;
// The handler of the instance's module being run right now.
MCScriptHandlerDefinition *handler;
// The address (instruction pointer) into the instance's module bytecode.
uindex_t address;
// The slots for the current handler invocation. The slots are in this order:
// <parameters>
// <locals>
// <registers>
MCValueRef *slots;
// The context slots for the current handler invocation (if initialized)
MCScriptFrameContext *context;
// The result register in the caller.
uindex_t result;
// The mapping array - this lists the mapping from parameters to registers
// in the callers frame, for handling inout/out parameters.
uindex_t *mapping;
};
static inline bool MCScriptIsRegisterValidInFrame(MCScriptFrame *p_frame, int p_register)
{
return p_register >= 0 && p_register < p_frame -> handler -> slot_count;
}
static inline bool MCScriptIsConstantValidInFrame(MCScriptFrame *p_frame, int p_constant)
{
return p_constant >= 0 && p_constant < p_frame -> instance -> module -> value_count;
}
static bool MCScriptCreateFrame(MCScriptFrame *p_caller, MCScriptInstanceRef p_instance, MCScriptHandlerDefinition *p_handler, MCScriptFrame*& r_frame)
{
MCScriptFrame *self;
self = nil;
if (!MCMemoryNew(self) ||
!MCMemoryNewArray(p_handler -> slot_count, self -> slots))
{
MCMemoryDelete(self);
return false;
}
for(uindex_t i = 0; i < p_handler -> slot_count; i++)
self -> slots[i] = MCValueRetain(kMCNull);
self -> caller = p_caller;
self -> instance = MCScriptRetainInstance(p_instance);
self -> handler = p_handler;
self -> address = p_handler -> start_address;
self -> mapping = nil;
r_frame = self;
return true;
}
static MCScriptFrame *MCScriptDestroyFrame(MCScriptFrame *self)
{
MCScriptFrame *t_caller;
t_caller = self -> caller;
for(int i = 0; i < self -> handler -> slot_count; i++)
MCValueRelease(self -> slots[i]);
MCScriptReleaseInstance(self -> instance);
MCMemoryDeleteArray(self -> slots);
MCMemoryDeleteArray(self -> mapping);
MCMemoryDelete(self);
return t_caller;
}
static inline void MCScriptBytecodeDecodeOp(byte_t*& x_bytecode_ptr, MCScriptBytecodeOp& r_op, uindex_t& r_arity)
{
byte_t t_op_byte;
t_op_byte = *x_bytecode_ptr++;
// The lower nibble is the bytecode operation.
MCScriptBytecodeOp t_op;
t_op = (MCScriptBytecodeOp)(t_op_byte & 0xf);
// The upper nibble is the arity.
uindex_t t_arity;
t_arity = (t_op_byte >> 4);
// If the arity is 15, then overflow to a subsequent byte.
if (t_arity == 15)
t_arity += *x_bytecode_ptr++;
r_op = t_op;
r_arity = t_arity;
}
// TODO: Make this better for negative numbers.
static inline uindex_t MCScriptBytecodeDecodeArgument(byte_t*& x_bytecode_ptr)
{
uindex_t t_value;
t_value = 0;
int t_shift;
t_shift = 0;
for(;;)
{
byte_t t_next;
t_next = *x_bytecode_ptr++;
t_value |= (t_next & 0x7f) << t_shift;
if ((t_next & 0x80) == 0)
break;
t_shift += 7;
}
return t_value;
}
static inline void MCScriptResolveDefinitionInFrame(MCScriptFrame *p_frame, uindex_t p_index, MCScriptInstanceRef& r_instance, MCScriptDefinition*& r_definition)
{
MCScriptInstanceRef t_instance;
t_instance = p_frame -> instance;
MCScriptDefinition *t_definition;
t_definition = t_instance -> module -> definitions[p_index];
if (t_definition -> kind == kMCScriptDefinitionKindExternal)
{
MCScriptExternalDefinition *t_ext_def;
t_ext_def = static_cast<MCScriptExternalDefinition *>(t_definition);
MCScriptImportedDefinition *t_import_def;
t_import_def = &t_instance -> module -> imported_definitions[t_ext_def -> index];
t_instance = t_import_def -> resolved_module -> shared_instance;
t_definition = t_import_def -> resolved_definition;
}
r_instance = t_instance;
r_definition = t_definition;
}
static inline MCTypeInfoRef MCScriptGetRegisterTypeInFrame(MCScriptFrame *p_frame, int p_register)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsRegisterValidInFrame(p_frame, p_register),
"register out of range on fetch register type");
MCTypeInfoRef t_handler_type;
t_handler_type = p_frame -> instance -> module -> types[p_frame -> handler -> type] -> typeinfo;
uindex_t t_parameter_count;
t_parameter_count = MCHandlerTypeInfoGetParameterCount(t_handler_type);
if (p_register < t_parameter_count)
return MCHandlerTypeInfoGetParameterType(t_handler_type, p_register);
if (p_register < t_parameter_count + p_frame -> handler -> local_type_count)
return p_frame -> instance -> module -> types[p_frame -> handler -> local_types[p_register - t_parameter_count]] -> typeinfo;
return nil;
}
static bool MCScriptGetRegisterTypeIsOptionalInFrame(MCScriptFrame *p_frame, int p_register)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsRegisterValidInFrame(p_frame, p_register),
"register out of range on fetch register type is optional");
MCTypeInfoRef t_type;
t_type = MCScriptGetRegisterTypeInFrame(p_frame, p_register);
if (t_type == nil)
return true;
MCResolvedTypeInfo t_resolved_type;
if (!MCTypeInfoResolve(t_type, t_resolved_type))
return true; /* RESOLVE UNCHECKED */
return t_resolved_type . is_optional;
}
static inline MCValueRef MCScriptFetchFromRegisterInFrame(MCScriptFrame *p_frame, int p_register)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsRegisterValidInFrame(p_frame, p_register),
"register out of range on fetch");
return p_frame -> slots[p_register];
}
static inline void MCScriptStoreToRegisterInFrameAndRelease(MCScriptFrame *p_frame, int p_register, MCValueRef p_value)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsRegisterValidInFrame(p_frame, p_register),
"register out of range on store");
if (p_frame -> slots[p_register] != p_value)
{
MCValueRelease(p_frame -> slots[p_register]);
p_frame -> slots[p_register] = p_value;
}
}
static inline void MCScriptStoreToRegisterInFrame(MCScriptFrame *p_frame, int p_register, MCValueRef p_value)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsRegisterValidInFrame(p_frame, p_register),
"register out of range on store");
if (p_frame -> slots[p_register] != p_value)
{
MCValueRelease(p_frame -> slots[p_register]);
p_frame -> slots[p_register] = MCValueRetain(p_value);
}
}
static bool MCScriptCheckedFetchFromRegisterInFrame(MCScriptFrame *p_frame, int p_register, MCValueRef& r_value)
{
MCValueRef t_value;
t_value = MCScriptFetchFromRegisterInFrame(p_frame, p_register);
if (t_value == kMCNull &&
!MCScriptGetRegisterTypeIsOptionalInFrame(p_frame, p_register))
return MCScriptThrowLocalVariableUsedBeforeDefinedError(p_frame -> instance -> module, p_frame -> handler, p_register);
r_value = t_value;
return true;
}
static bool MCScriptCheckedStoreToRegisterInFrame(MCScriptFrame *p_frame, int p_register, MCValueRef p_value)
{
MCTypeInfoRef t_type;
t_type = MCScriptGetRegisterTypeInFrame(p_frame, p_register);
if (t_type != nil &&
!MCTypeInfoConforms(MCValueGetTypeInfo(p_value), t_type))
return MCScriptThrowInvalidValueForLocalVariableError(p_frame -> instance -> module, p_frame -> handler, p_register, t_type, p_value);
MCScriptStoreToRegisterInFrame(p_frame, p_register, p_value);
return true;
}
static inline MCValueRef MCScriptFetchConstantInFrame(MCScriptFrame *p_frame, int p_index)
{
/* LOAD CHECK */ __MCScriptAssert__(MCScriptIsConstantValidInFrame(p_frame, p_index),
"constant out of range on fetch");
return p_frame -> instance -> module -> values[p_index];
}
static bool MCScriptPerformScriptInvoke(MCScriptFrame*& x_frame, byte_t*& x_next_bytecode, MCScriptInstanceRef p_instance, MCScriptHandlerDefinition *p_handler, uindex_t *p_arguments, uindex_t p_arity)
{
MCTypeInfoRef t_signature;
t_signature = p_instance -> module -> types[p_handler -> type] -> typeinfo;
// If the signature of the handler includes a return value, then the first
// argument will be the register to place it in.
uindex_t t_result_reg;
t_result_reg = p_arguments[0];
p_arity -= 1;
p_arguments += 1;
// If the number of remaining arguments is not the same as the parameter count then
// it is an error.
if (MCHandlerTypeInfoGetParameterCount(t_signature) != p_arity)
return MCScriptThrowWrongNumberOfArgumentsForInvokeError(p_instance -> module, p_handler, p_arity);
// Check that the arguments all conform to the required types and that we aren't
// using any local variables before they have been assigned.
for(uindex_t i = 0; i < p_arity; i++)
{
// Out parameters are initialized to undefined, so we don't care about
// their values on input.
if (MCHandlerTypeInfoGetParameterMode(t_signature, i) == kMCHandlerTypeFieldModeOut)
continue;
MCValueRef t_value;
if (!MCScriptCheckedFetchFromRegisterInFrame(x_frame, p_arguments[i], t_value))
return false;
MCTypeInfoRef t_type;
t_type = MCHandlerTypeInfoGetParameterType(t_signature, i);
if (!MCTypeInfoConforms(MCValueGetTypeInfo(t_value), t_type))
return MCScriptThrowInvalidValueForArgumentError(p_instance -> module, p_handler, i, t_type, t_value);
}
// Create a (stack) frame for the handler.
MCScriptFrame *t_callee;
if (!MCScriptCreateFrame(x_frame, p_instance, p_handler, t_callee))
return false;
// We need to record a mapping vector if we have any out parameters.
bool t_needs_mapping;
t_needs_mapping = false;
// Fetch the parameter values and store them in the appropriate slots. The
// parameters are always the first 'arity' slots in the frame.
for(int i = 0; i < MCHandlerTypeInfoGetParameterCount(t_signature); i++)
{
MCHandlerTypeFieldMode t_mode;
t_mode = MCHandlerTypeInfoGetParameterMode(t_signature, i);
MCValueRef t_value;
if (t_mode != kMCHandlerTypeFieldModeOut)
t_value = MCScriptFetchFromRegisterInFrame(x_frame, p_arguments[i]);
else
t_value = kMCNull;
if (t_mode != kMCHandlerTypeFieldModeIn)
t_needs_mapping = true;
MCValueAssign(t_callee -> slots[i], t_value);
}
if (t_needs_mapping)
{
if (!MCMemoryNewArray(p_arity, t_callee -> mapping))
{
MCScriptDestroyFrame (t_callee);
return false;
}
MCMemoryCopy(t_callee -> mapping, p_arguments, sizeof(int) * p_arity);
}
t_callee -> result = t_result_reg;
x_frame = t_callee;
x_next_bytecode = x_frame -> instance -> module -> bytecode + x_frame -> address;
return true;
}
// This method resolves the binding string in the foreign function. The format is:
// [lang:][library>][class.]function[!calling]
//
// lang - one of c, cpp, objc or java. If not present, it is taken to be c.
// library - the library to load the symbol from. If not present, it is taken to be the
// main executable.
// class - only valid for cpp, objc, or java. If not present, it is taken to be no class.
// function - the name of the function or method.
// calling - the calling convention, if not present it is taken to be the platform default.
//
// The calling conventions are:
// default (sysv on 32-bit unix intel, unix64 on 64-bit unix intel, win64 on 64-bit windows, cdelc on 32-bit windows)
// stdcall (windows 32-bit)
// thiscall (windows 32-bit)
// fastcall (windows 32-bit)
// cdecl (windows 32-bit)
// pascal (windows 32-bit)
// register (windows 32-bit)
// If a windows calling convention is specified, it is taken to be 'default' on
// other platforms.
//
static bool __split_binding(MCStringRef& x_string, codepoint_t p_char, MCStringRef& r_field)
{
MCStringRef t_head, t_tail;
if (!MCStringDivideAtChar(x_string, p_char, kMCStringOptionCompareExact, t_head, t_tail))
return false;
if (!MCStringIsEmpty(t_tail))
{
r_field = t_head;
MCValueRelease(x_string);
x_string = t_tail;
}
else
{
MCValueRelease(x_string);
MCValueRelease(t_tail);
x_string = t_head;
}
return true;
}
static bool MCScriptPlatformLoadSharedLibrary(MCStringRef p_path, void*& r_handle)
{
#if defined(_WIN32)
HMODULE t_module;
MCAutoStringRefAsWString t_library_wstr;
if (!t_library_wstr.Lock(p_path))
return false;
t_module = LoadLibraryW(*t_library_wstr);
if (t_module == NULL)
return false;
r_handle = (void *)t_module;
#else
MCAutoStringRefAsUTF8String t_utf8_library;
if (!t_utf8_library.Lock(p_path))
return false;
void *t_module;
t_module = dlopen(*t_utf8_library, RTLD_LAZY);
if (t_module == NULL)
return false;
r_handle = (void *)t_module;
#endif
return true;
}
static bool MCScriptPlatformLoadSharedLibraryFunction(void *p_module, MCStringRef p_function, void*& r_pointer)
{
MCAutoStringRefAsCString t_function_name;
if (!t_function_name.Lock(p_function))
return false;
void *t_pointer;
#if defined(_WIN32)
t_pointer = GetProcAddress((HMODULE)p_module, *t_function_name);
#else
t_pointer = dlsym(p_module, *t_function_name);
#endif
r_pointer = t_pointer;
return true;
}
static bool MCScriptLoadSharedLibrary(MCScriptModuleRef p_module, MCStringRef p_library, void*& r_handle)
{
// If there is no library name then we resolve to the executable module.
if (MCStringIsEmpty(p_library))