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 228
Expand file tree
/
Copy pathscript-builder.cpp
More file actions
2092 lines (1673 loc) · 64.7 KB
/
script-builder.cpp
File metadata and controls
2092 lines (1673 loc) · 64.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
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-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 <libscript/script.h>
#include "script-private.h"
#include "script-bytecode.hpp"
////////////////////////////////////////////////////////////////////////////////
// Utility functor, used to fetch the bytecode info struct for the given
// opcode.
struct MCScriptDescribeBytecodeOp_Impl
{
public:
template<typename OpStruct>
bool Visit(const MCScriptBytecodeOpInfo*& r_info) const
{
r_info = &OpStruct::Describe();
return true;
}
};
static bool
MCScriptDescribeBytecodeOp(uindex_t p_opcode,
const MCScriptBytecodeOpInfo*& r_info)
{
if (p_opcode > kMCScriptBytecodeOp__Last ||
!MCScriptBytecodeDispatchR((MCScriptBytecodeOp)p_opcode,
MCScriptDescribeBytecodeOp_Impl(),
r_info))
{
return false;
}
return true;
}
//////////
class MCScriptCopyBytecodeNames_Impl
{
public:
template<typename OpStruct>
bool Visit(MCProperListRef x_bytecode_names) const
{
if (!MCProperListPushElementOntoBack(x_bytecode_names,
MCSTR(OpStruct::Describe().name)))
return false;
return true;
}
};
bool MCScriptCopyBytecodeNames(MCProperListRef& r_bytecode_names)
{
MCAutoProperListRef t_bytecode_names;
if (!MCProperListCreateMutable(&t_bytecode_names))
return false;
MCProperListRef t_bytecode_names_arg = *t_bytecode_names;
if (!MCScriptBytecodeForEach(MCScriptCopyBytecodeNames_Impl(),
t_bytecode_names_arg))
return false;
r_bytecode_names = t_bytecode_names.Take();
return true;
}
//////////
struct MCScriptLookupBytecode_Impl
{
public:
struct Args
{
const char *name;
uindex_t& _opcode;
Args(const char *p_name,
uindex_t& r_opcode)
: name(p_name),
_opcode(r_opcode)
{
}
};
template<typename OpStruct>
bool Visit(Args p_args) const
{
if (0 != strcmp(p_args.name, OpStruct::Describe().name))
return true;
p_args._opcode = (uindex_t)OpStruct::Describe().code;
return false;
}
};
bool MCScriptLookupBytecode(const char *p_name, uindex_t& r_opcode)
{
// If we get all the way through the bytecodes without returning false, then
// it means it wasn't found.
MCScriptLookupBytecode_Impl::Args t_args(p_name,
r_opcode);
if (MCScriptBytecodeForEach(MCScriptLookupBytecode_Impl(),
t_args))
return false;
return true;
}
//////////
const char *MCScriptDescribeBytecode(uindex_t p_opcode)
{
const MCScriptBytecodeOpInfo *t_info;
if (!MCScriptDescribeBytecodeOp(p_opcode, t_info))
return "<unknown>";
return t_info->name;
}
//////////
MCScriptBytecodeParameterType MCScriptDescribeBytecodeParameter(uindex_t p_opcode, uindex_t p_index)
{
const MCScriptBytecodeOpInfo *t_info;
if (!MCScriptDescribeBytecodeOp(p_opcode, t_info))
return kMCScriptBytecodeParameterTypeUnknown;
const char *t_desc;
t_desc = t_info->format;
size_t t_desc_length;
t_desc_length = strlen(t_desc);
char t_param_type;
switch(t_desc[t_desc_length - 1])
{
case '?':
if (p_index >= t_desc_length - 1)
return kMCScriptBytecodeParameterTypeUnknown;
t_param_type = t_desc[p_index];
break;
case '*':
case '%':
if (p_index >= t_desc_length - 1)
t_param_type = t_desc[t_desc_length - 2];
else
t_param_type = t_desc[p_index];
break;
default:
if (p_index >= t_desc_length)
return kMCScriptBytecodeParameterTypeUnknown;
t_param_type = t_desc[p_index];
break;
}
switch(t_param_type)
{
case 'l':
return kMCScriptBytecodeParameterTypeLabel;
case 'r':
return kMCScriptBytecodeParameterTypeRegister;
case 'c':
return kMCScriptBytecodeParameterTypeConstant;
case 'd':
return kMCScriptBytecodeParameterTypeDefinition;
case 'v':
return kMCScriptBytecodeParameterTypeVariable;
case 'h':
return kMCScriptBytecodeParameterTypeHandler;
default:
break;
}
return kMCScriptBytecodeParameterTypeUnknown;
}
bool MCScriptCheckBytecodeParameterCount(uindex_t p_opcode, uindex_t p_proposed_count)
{
const MCScriptBytecodeOpInfo *t_info;
if (!MCScriptDescribeBytecodeOp(p_opcode, t_info))
return kMCScriptBytecodeParameterTypeUnknown;
const char *t_desc;
t_desc = t_info->format;
size_t t_desc_length;
t_desc_length = strlen(t_desc);
switch(t_desc[t_desc_length - 1])
{
case '?':
if (p_proposed_count == t_desc_length - 2 ||
p_proposed_count == t_desc_length - 1)
return true;
break;
case '*':
if (p_proposed_count >= t_desc_length - 2)
return true;
break;
case '%':
if (p_proposed_count >= t_desc_length - 2 &&
((p_proposed_count - (t_desc_length - 2)) % 2 == 0))
return true;
break;
default:
if (p_proposed_count == t_desc_length)
return true;
break;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
struct MCScriptBytecodeLabel
{
uindex_t instruction;
};
struct MCScriptBytecodeInstruction
{
MCScriptBytecodeOp operation;
uindex_t arity;
uindex_t operands;
uindex_t address;
uindex_t file;
uindex_t line;
};
struct MCScriptModuleBuilder
{
bool valid;
MCScriptModule module;
uindex_t current_handler;
uindex_t current_param_count;
uindex_t current_local_count;
uindex_t *current_handler_group;
uindex_t current_handler_group_size;
uindex_t current_syntax;
MCScriptType *current_type;
MCScriptBytecodeLabel *labels;
uindex_t label_count;
MCScriptBytecodeInstruction *instructions;
uindex_t instruction_count;
uindex_t *operands;
uindex_t operand_count;
uindex_t current_file;
uindex_t current_line;
MCProperListRef current_list_value;
MCArrayRef current_array_value;
MCScriptDefinitionKind *definition_kinds;
uindex_t definition_kind_count;
};
////////////////////////////////////////////////////////////////////////////////
static void __emit_constant(MCScriptModuleBuilderRef self, MCValueRef p_constant, uindex_t& r_index);
////////////////////////////////////////////////////////////////////////////////
static bool __append_definition_name(MCScriptModuleBuilderRef self, MCNameRef p_name)
{
if (!MCMemoryResizeArray(self -> module . definition_name_count + 1, self -> module . definition_names, self -> module . definition_name_count))
return false;
self -> module . definition_names[self -> module . definition_name_count - 1] = MCValueRetain(p_name);
return true;
}
static void __assign_definition_name(MCScriptModuleBuilderRef self, uindex_t p_index, MCNameRef p_name)
{
MCValueRelease(self -> module . definition_names[p_index]);
self -> module . definition_names[p_index] = MCValueRetain(p_name);
}
template<typename T> static bool __extend_array(MCScriptModuleBuilderRef self, T*& x_array, uindex_t& x_array_size, uindex_t& r_index)
{
if (!MCMemoryResizeArray(x_array_size + 1, x_array, x_array_size))
{
r_index = 0;
self -> valid = false;
return false;
}
r_index = x_array_size - 1;
return true;
}
void MCScriptBeginModule(MCScriptModuleKind p_kind, MCNameRef p_name, MCScriptModuleBuilderRef& r_builder)
{
MCScriptModuleBuilder *self;
if (!MCMemoryNew(self))
{
r_builder = nil;
return;
}
self -> valid = true;
self -> module . module_kind = p_kind;
self -> module . name = MCValueRetain(p_name);
self -> current_handler = UINDEX_MAX;
self -> current_handler_group = nil;
self -> current_handler_group_size = 0;
self -> current_syntax = UINDEX_MAX;
self -> current_file = 0;
self -> current_line = 0;
self -> current_list_value = nil;
self -> current_array_value = nil;
self -> definition_kind_count = 0;
self -> definition_kinds = nil;
r_builder = self;
}
static bool
MCScriptValidateModuleBytecode(MCScriptModule *self)
{
for(uindex_t i = 0; i < self -> definition_count; i++)
{
if (self -> definitions[i] -> kind == kMCScriptDefinitionKindHandler)
{
MCScriptHandlerDefinition *t_handler;
t_handler = static_cast<MCScriptHandlerDefinition *>(self -> definitions[i]);
MCScriptValidateState t_state;
t_state.error = false;
t_state.module = self;
t_state.handler = t_handler;
MCScriptHandlerType *t_signature;
t_signature = static_cast<MCScriptHandlerType *>(self -> types[t_handler -> type]);
t_state.register_limit = t_signature->parameter_count +
t_handler -> local_type_count;
const byte_t *t_bytecode;
const byte_t *t_bytecode_limit;
t_bytecode = self -> bytecode + t_handler -> start_address;
t_bytecode_limit = self -> bytecode + t_handler -> finish_address;
// If there is no bytecode, the bytecode is malformed.
if (t_bytecode == t_bytecode_limit)
return false;
while(t_bytecode != t_bytecode_limit)
{
t_state.current_address = uindex_t(t_bytecode - self-> bytecode);
if (!MCScriptBytecodeIterate(t_bytecode,
t_bytecode_limit,
t_state.operation,
t_state.argument_count,
t_state.arguments))
{
t_state.error = true;
break;
}
if (t_state.operation > kMCScriptBytecodeOp__Last)
{
t_state.error = true;
break;
}
MCScriptBytecodeValidate(t_state);
if (t_state.error)
{
break;
}
}
// If validation failed for a single operation, the bytecode is
// malformed.
if (t_state.error)
return false;
// If we didn't reach the limit, the bytecode is malformed.
if (t_bytecode != t_bytecode_limit)
return false;
// If the last operation was not return, the bytecode is malformed.
if (t_state.operation != kMCScriptBytecodeOpReturn)
return false;
}
}
return true;
}
bool MCScriptEndModule(MCScriptModuleBuilderRef self, MCStreamRef p_stream)
{
if (self == nil)
return false;
for(uindex_t i = 0; self -> valid && i < self -> module . definition_count; i++)
{
if (self -> module . definitions[i] != nil)
continue;
self -> valid = false;
}
if (self->valid)
{
if (!MCScriptValidateModuleBytecode(&self->module))
{
self->valid = false;
}
}
bool t_success;
if (self -> valid)
{
byte_t t_header[4];
t_header[0] = 'L';
t_header[1] = 'C';
t_header[2] = ((kMCScriptCurrentModuleVersion >> 0) & 0xFF);
t_header[3] = ((kMCScriptCurrentModuleVersion >> 8) & 0xFF);
t_success = MCStreamWrite(p_stream, t_header, 4) &&
MCScriptWriteRawModule(p_stream, &self -> module);
}
else
t_success = false;
MCScriptReleaseRawModule(&self -> module);
MCMemoryDeleteArray(self -> labels);
MCMemoryDeleteArray(self -> instructions);
MCMemoryDeleteArray(self -> operands);
MCValueRelease(self -> current_list_value);
MCValueRelease(self -> current_array_value);
MCMemoryDeleteArray(self -> definition_kinds);
MCMemoryDelete(self);
return t_success;
}
void MCScriptAddDependencyToModule(MCScriptModuleBuilderRef self, MCNameRef p_dependency, uindex_t& r_index)
{
if (self == nil || !self -> valid)
return;
for(uindex_t i = 0; i < self -> module . dependency_count; i++)
if (MCNameIsEqualToCaseless(p_dependency, self -> module . dependencies[i] . name))
{
r_index = i;
return;
}
if (!__extend_array(self, self -> module . dependencies, self -> module . dependency_count, r_index))
return;
self -> module . dependencies[r_index] . name = MCValueRetain(p_dependency);
self -> module . dependencies[r_index] . version = 0;
}
void MCScriptAddExportToModule(MCScriptModuleBuilderRef self, uindex_t p_definition)
{
if (self == nil || !self -> valid)
return;
uindex_t t_index;
if (!__extend_array(self, self -> module . exported_definitions, self -> module . exported_definition_count, t_index))
return;
self -> module . exported_definitions[t_index] . name = MCValueRetain(self -> module . definition_names[p_definition]);
self -> module . exported_definitions[t_index] . index = p_definition;
// If the definition is a type, then make sure we make it a 'defined' type otherwise
// it won't get bound globally.
if (self -> module . definitions[p_definition] -> kind == kMCScriptDefinitionKindType)
{
uindex_t t_type_index;
MCScriptAddDefinedTypeToModule(self, p_definition, t_type_index);
}
}
void MCScriptAddImportToModuleWithIndex(MCScriptModuleBuilderRef self, uindex_t p_module_index, MCNameRef p_name, MCScriptDefinitionKind p_kind, uindex_t p_type_index, uindex_t p_def_index)
{
uindex_t t_imp_index;
if (!__extend_array(self, self -> module . imported_definitions, self -> module . imported_definition_count, t_imp_index) ||
!MCMemoryNew((MCScriptExternalDefinition*&)self -> module . definitions[p_def_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_def_index, p_name);
MCScriptImportedDefinition *t_import;
t_import = &self -> module . imported_definitions[t_imp_index];
t_import -> module = p_module_index;
t_import -> kind = p_kind;
t_import -> name = MCValueRetain(p_name);
MCScriptExternalDefinition *t_definition;
t_definition = static_cast<MCScriptExternalDefinition *>(self -> module . definitions[p_def_index]);
t_definition -> kind = kMCScriptDefinitionKindExternal;
t_definition -> index = t_imp_index;
}
void MCScriptAddImportToModule(MCScriptModuleBuilderRef self, uindex_t p_index, MCNameRef p_name, MCScriptDefinitionKind p_kind, uindex_t p_type_index, uindex_t& r_index)
{
if (self == nil || !self -> valid)
{
r_index = 0;
return;
}
for(uindex_t i = 0; i < self -> module . imported_definition_count; i++)
if (MCNameIsEqualToCaseless(p_name, self -> module . imported_definitions[i] . name) &&
p_kind == self -> module . imported_definitions[i] . kind &&
p_index == self -> module . imported_definitions[i] . module)
{
for(uindex_t j = 0; j < self -> module . definition_count; j++)
if (self -> module . definitions[j] != nil &&
self -> module . definitions[j] -> kind == kMCScriptDefinitionKindExternal &&
static_cast<MCScriptExternalDefinition *>(self -> module . definitions[j]) -> index == i)
{
r_index = j;
return;
}
}
MCScriptAddDefinitionToModule(self, p_kind, r_index);
MCScriptAddImportToModuleWithIndex(self, p_index, p_name, p_kind, p_type_index, r_index);
if (!self -> valid)
r_index = 0;
}
void MCScriptAddDefinitionToModule(MCScriptModuleBuilderRef self, MCScriptDefinitionKind p_kind, uindex_t& r_index)
{
if (self == nil || !self -> valid)
return;
uindex_t t_dindex;
if (!__extend_array(self, self -> module . definitions, self -> module . definition_count, r_index) ||
!__extend_array(self, self -> definition_kinds, self -> definition_kind_count, t_dindex) ||
!__append_definition_name(self, kMCEmptyName))
{
r_index = 0;
self -> valid = false;
return;
}
self -> definition_kinds[t_dindex] = p_kind;
}
void MCScriptAddValueToModule(MCScriptModuleBuilderRef self, MCValueRef p_value, uindex_t& r_index)
{
if (self == nil || !self -> valid)
return;
__emit_constant(self, p_value, r_index);
}
void MCScriptBeginListValueInModule(MCScriptModuleBuilderRef self)
{
if (self == nil || !self -> valid)
return;
if (self -> current_list_value != nil ||
!MCProperListCreateMutable(self -> current_list_value))
{
self -> valid = false;
return;
}
}
void MCScriptContinueListValueInModule(MCScriptModuleBuilderRef self, uindex_t p_const_idx)
{
if (self == nil || !self -> valid)
return;
if (self -> current_list_value == nil ||
!MCProperListPushElementOntoBack(self -> current_list_value, self -> module . values[p_const_idx]))
{
self -> valid = false;
return;
}
}
void MCScriptEndListValueInModule(MCScriptModuleBuilderRef self, uindex_t& r_index)
{
if (self == nil || !self -> valid)
return;
MCScriptAddValueToModule(self, self -> current_list_value, r_index);
MCValueRelease(self -> current_list_value);
self -> current_list_value = nil;
}
void MCScriptBeginArrayValueInModule(MCScriptModuleBuilderRef self)
{
if (self == nil || !self -> valid)
return;
if (self -> current_array_value != nil ||
!MCArrayCreateMutable(self -> current_array_value))
{
self -> valid = false;
return;
}
}
void MCScriptContinueArrayValueInModule(MCScriptModuleBuilderRef self,
uindex_t p_key_idx,
uindex_t p_value_idx)
{
MCNewAutoNameRef t_key;
if (self == nil || !self -> valid)
return;
if (self -> current_array_value == nil ||
!MCNameCreate(reinterpret_cast<MCStringRef>(self->module.values[p_key_idx]), &t_key) ||
!MCArrayStoreValue(self -> current_array_value, false,
*t_key, self -> module . values[p_value_idx]))
{
self -> valid = false;
return;
}
}
void MCScriptEndArrayValueInModule(MCScriptModuleBuilderRef self, uindex_t& r_index)
{
if (self == nil || !self -> valid)
return;
MCScriptAddValueToModule(self, self -> current_array_value, r_index);
MCValueRelease(self -> current_array_value);
self -> current_array_value = nil;
}
void MCScriptAddTypeToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_type, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptTypeDefinition*&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptTypeDefinition *t_definition;
t_definition = static_cast<MCScriptTypeDefinition *>(self -> module . definitions[p_index]);
t_definition -> kind = kMCScriptDefinitionKindType;
t_definition -> type = p_type;
}
void MCScriptAddConstantToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_const_idx, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptConstantDefinition*&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptConstantDefinition *t_definition;
t_definition = static_cast<MCScriptConstantDefinition *>(self -> module . definitions[p_index]);
t_definition -> kind = kMCScriptDefinitionKindConstant;
t_definition -> value = p_const_idx;
}
void MCScriptAddVariableToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_type, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptVariableDefinition*&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptVariableDefinition *t_definition;
t_definition = static_cast<MCScriptVariableDefinition *>(self -> module . definitions[p_index]);
t_definition -> kind = kMCScriptDefinitionKindVariable;
t_definition -> type = p_type;
}
void MCScriptAddForeignHandlerToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_signature, MCStringRef p_binding, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptForeignHandlerDefinition*&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptForeignHandlerDefinition *t_definition;
t_definition = static_cast<MCScriptForeignHandlerDefinition *>(self -> module . definitions[p_index]);
t_definition -> kind = kMCScriptDefinitionKindForeignHandler;
t_definition -> type = p_signature;
t_definition -> binding = MCValueRetain(p_binding);
}
void MCScriptAddPropertyToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_getter, uindex_t p_setter, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptPropertyDefinition *&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptPropertyDefinition *t_definition;
t_definition = static_cast<MCScriptPropertyDefinition *>(self -> module . definitions[p_index]);
// The property getters and setter references are stored with +1 indices, 0 meaning
// no getter/setter.
t_definition -> kind = kMCScriptDefinitionKindProperty;
t_definition -> getter = p_getter + 1;
t_definition -> setter = p_setter == UINDEX_MAX ? 0 : p_setter + 1;
}
void MCScriptAddEventToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_type, uindex_t p_index)
{
if (self == nil || !self -> valid)
return;
if (p_index >= self -> module . definition_count ||
self -> module . definitions[p_index] != nil ||
!MCMemoryNew((MCScriptEventDefinition*&)self -> module . definitions[p_index]))
{
self -> valid = false;
return;
}
__assign_definition_name(self, p_index, p_name);
MCScriptEventDefinition *t_definition;
t_definition = static_cast<MCScriptEventDefinition *>(self -> module . definitions[p_index]);
t_definition -> kind = kMCScriptDefinitionKindEvent;
t_definition -> type = p_type;
}
///////////
static void __add_script_type(MCScriptModuleBuilderRef self, MCScriptType *p_type, uindex_t& r_index)
{
if (!__extend_array(self, self -> module . types, self -> module . type_count, r_index))
return;
self -> module . types[r_index] = p_type;
}
void MCScriptAddDefinedTypeToModule(MCScriptModuleBuilderRef self, uindex_t p_index, uindex_t& r_type)
{
for(uindex_t i = 0; i < self -> module . type_count; i++)
if (self -> module . types[i] -> kind == kMCScriptTypeKindDefined)
if (static_cast<MCScriptDefinedType *>(self -> module . types[i]) -> index == p_index)
{
r_type = i;
return;
}
MCScriptDefinedType *t_type;
if (!MCMemoryNew(t_type))
{
self -> valid = false;
return;
}
t_type -> kind = kMCScriptTypeKindDefined;
t_type -> index = p_index;
__add_script_type(self, t_type, r_type);
}
void MCScriptAddForeignTypeToModule(MCScriptModuleBuilderRef self, MCStringRef p_binding, uindex_t& r_type)
{
for(uindex_t i = 0; i < self -> module . type_count; i++)
if (self -> module . types[i] -> kind == kMCScriptTypeKindForeign)
if (MCStringIsEqualTo(static_cast<MCScriptForeignType *>(self -> module . types[i]) -> binding, p_binding, kMCStringOptionCompareExact))
{
r_type = i;
return;
}
MCScriptForeignType *t_type;
if (!MCMemoryNew(t_type))
{
self -> valid = false;
return;
}
t_type -> kind = kMCScriptTypeKindForeign;
t_type -> binding = MCValueRetain(p_binding);
__add_script_type(self, t_type, r_type);
}
void MCScriptAddOptionalTypeToModule(MCScriptModuleBuilderRef self, uindex_t p_type, uindex_t& r_type)
{
if (self -> module . types[p_type] -> kind == kMCScriptTypeKindOptional)
{
r_type = p_type;
return;
}
for(uindex_t i = 0; i < self -> module . type_count; i++)
if (self -> module . types[i] -> kind == kMCScriptTypeKindOptional)
if (static_cast<MCScriptOptionalType *>(self -> module . types[i]) -> type == p_type)
{
r_type = i;
return;
}
MCScriptOptionalType *t_type;
if (!MCMemoryNew(t_type))
{
self -> valid = false;
return;
}
t_type -> kind = kMCScriptTypeKindOptional;
t_type -> type = p_type;
__add_script_type(self, t_type, r_type);
}
static void MCScriptBeginCommonHandlerTypeInModule(MCScriptModuleBuilderRef self, bool p_is_foreign, uindex_t p_return_type)
{
if (self == nil ||
!self -> valid)
return;
MCScriptHandlerType *t_type;
if (!MCMemoryNew(t_type))
{
self -> valid = false;
return;
}
t_type -> kind = p_is_foreign ? kMCScriptTypeKindForeignHandler : kMCScriptTypeKindHandler;
t_type -> return_type = p_return_type;
self -> current_type = t_type;
}
void MCScriptBeginHandlerTypeInModule(MCScriptModuleBuilderRef self, uindex_t p_return_type)
{
MCScriptBeginCommonHandlerTypeInModule(self, false, p_return_type);
}
void MCScriptBeginForeignHandlerTypeInModule(MCScriptModuleBuilderRef self, uindex_t p_return_type)
{
MCScriptBeginCommonHandlerTypeInModule(self, true, p_return_type);
}
void MCScriptContinueHandlerTypeInModule(MCScriptModuleBuilderRef self, MCScriptHandlerTypeParameterMode p_mode, MCNameRef p_name, uindex_t p_type)
{
if (self == nil ||
!self -> valid ||
self -> current_type == nil)
return;
MCScriptHandlerType *t_type;
t_type = static_cast<MCScriptHandlerType *>(self -> current_type);
uindex_t t_param_index, t_name_index;
if (!__extend_array(self, t_type -> parameters, t_type -> parameter_count, t_param_index) ||
!__extend_array(self, t_type -> parameter_names, t_type -> parameter_name_count, t_name_index))
return;
t_type -> parameters[t_param_index] . mode = p_mode;
t_type -> parameters[t_param_index] . type = p_type;
t_type -> parameter_names[t_name_index] = MCValueRetain(p_name);
}
void MCScriptEndHandlerTypeInModule(MCScriptModuleBuilderRef self, uindex_t& r_new_type)
{
if (self == nil ||
!self -> valid ||
self -> current_type == nil)
return;
MCScriptHandlerType *t_type;
t_type = static_cast<MCScriptHandlerType *>(self -> current_type);
for(uindex_t i = 0; i < self -> module . type_count; i++)
{
if (self -> module . types[i] -> kind != t_type -> kind)
continue;
MCScriptHandlerType *t_other_type;
t_other_type = static_cast<MCScriptHandlerType *>(self -> module . types[i]);
if (t_type -> parameter_count != t_other_type -> parameter_count)
continue;
if (t_type -> return_type != t_other_type -> return_type)
continue;
bool t_equal;
t_equal = true;
for(uindex_t j = 0; j < t_type -> parameter_count; j++)
if (t_type -> parameters[j] . mode != t_other_type -> parameters[j] . mode ||
t_type -> parameters[j] . type != t_other_type -> parameters[j] . type)
{
t_equal = false;
break;
}
if (!t_equal)
continue;
MCMemoryDeleteArray(t_type -> parameters);
MCMemoryDelete(t_type);
r_new_type = i;
return;
}
__add_script_type(self, t_type, r_new_type);