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 227
Expand file tree
/
Copy pathscript-module.cpp
More file actions
1640 lines (1384 loc) · 56.5 KB
/
script-module.cpp
File metadata and controls
1640 lines (1384 loc) · 56.5 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 <foundation-system.h>
#include <libscript/script.h>
#include <libscript/script-auto.h>
#include "script-private.h"
#include "script-bytecode.hpp"
#include <stddef.h>
#include <stdarg.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
////////////////////////////////////////////////////////////////////////////////
/* Using offsetof() on a non-POD type (e.g. structs with inheritance
* and classes) is, strictly-speaking, undefined according to the C++
* standard (because C++'s offsetof is only present for C
* compatibility and is only defined for operations on C types).
* Disable GCC's offsetof warnings for these macros */
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#endif
MC_PICKLE_BEGIN_RECORD(MCScriptDefinedType)
MC_PICKLE_UINDEX(index)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptForeignType)
MC_PICKLE_STRINGREF(binding)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptOptionalType)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptHandlerTypeParameter)
MC_PICKLE_INTENUM(MCScriptHandlerTypeParameterMode, mode)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptHandlerType)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptHandlerTypeParameter, parameters, parameter_count)
MC_PICKLE_UINDEX(return_type)
MC_PICKLE_ARRAY_OF_NAMEREF(parameter_names, parameter_name_count)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptRecordTypeField)
MC_PICKLE_NAMEREF(name)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptRecordType)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptRecordTypeField, fields, field_count)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_VARIANT(MCScriptType, kind)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindDefined, MCScriptDefinedType)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindForeign, MCScriptForeignType)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindOptional, MCScriptOptionalType)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindHandler, MCScriptHandlerType)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindRecord, MCScriptRecordType)
MC_PICKLE_VARIANT_CASE(kMCScriptTypeKindForeignHandler, MCScriptHandlerType)
MC_PICKLE_END_VARIANT()
//////////
MC_PICKLE_BEGIN_RECORD(MCScriptDependency)
MC_PICKLE_NAMEREF(name)
MC_PICKLE_UINDEX(version)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptExportedDefinition)
MC_PICKLE_NAMEREF(name)
MC_PICKLE_UINDEX(index)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptImportedDefinition)
MC_PICKLE_UINDEX(module)
MC_PICKLE_INTENUM(MCScriptDefinitionKind, kind)
MC_PICKLE_NAMEREF(name)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptSyntaxMethod)
MC_PICKLE_UINDEX(handler)
MC_PICKLE_ARRAY_OF_UINDEX(arguments, argument_count)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptPosition)
MC_PICKLE_UINDEX(address)
MC_PICKLE_UINDEX(file)
MC_PICKLE_UINDEX(line)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptExternalDefinition)
MC_PICKLE_UINDEX(index)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptTypeDefinition)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptConstantDefinition)
MC_PICKLE_UINDEX(value)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptVariableDefinition)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptHandlerDefinition)
MC_PICKLE_UINDEX(type)
MC_PICKLE_ARRAY_OF_UINDEX(local_types, local_type_count)
MC_PICKLE_ARRAY_OF_NAMEREF(local_names, local_name_count)
MC_PICKLE_UINDEX(start_address)
MC_PICKLE_UINDEX(finish_address)
MC_PICKLE_INTSET(MCScriptHandlerAttributes, attributes)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptForeignHandlerDefinition)
MC_PICKLE_UINDEX(type)
MC_PICKLE_STRINGREF(binding)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptPropertyDefinition)
MC_PICKLE_UINDEX(getter)
MC_PICKLE_UINDEX(setter)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptEventDefinition)
MC_PICKLE_UINDEX(type)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptSyntaxDefinition)
MC_PICKLE_UINDEX(variable_count)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptSyntaxMethod, methods, method_count)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_RECORD(MCScriptDefinitionGroupDefinition)
MC_PICKLE_ARRAY_OF_UINDEX(handlers, handler_count)
MC_PICKLE_END_RECORD()
MC_PICKLE_BEGIN_VARIANT(MCScriptDefinition, kind)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindExternal, MCScriptExternalDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindType, MCScriptTypeDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindConstant, MCScriptConstantDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindVariable, MCScriptVariableDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindHandler, MCScriptHandlerDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindForeignHandler, MCScriptForeignHandlerDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindProperty, MCScriptPropertyDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindEvent, MCScriptEventDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindSyntax, MCScriptSyntaxDefinition)
MC_PICKLE_VARIANT_CASE(kMCScriptDefinitionKindDefinitionGroup, MCScriptDefinitionGroupDefinition)
MC_PICKLE_END_VARIANT()
MC_PICKLE_BEGIN_RECORD(MCScriptModule)
MC_PICKLE_INTENUM(MCScriptModuleKind, module_kind)
MC_PICKLE_NAMEREF(name)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptDependency, dependencies, dependency_count)
MC_PICKLE_ARRAY_OF_VALUEREF(values, value_count)
MC_PICKLE_ARRAY_OF_VARIANT(MCScriptType, types, type_count)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptExportedDefinition, exported_definitions, exported_definition_count)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptImportedDefinition, imported_definitions, imported_definition_count)
MC_PICKLE_ARRAY_OF_VARIANT(MCScriptDefinition, definitions, definition_count)
MC_PICKLE_ARRAY_OF_BYTE(bytecode, bytecode_count)
MC_PICKLE_ARRAY_OF_NAMEREF(definition_names, definition_name_count)
MC_PICKLE_ARRAY_OF_NAMEREF(source_files, source_file_count)
MC_PICKLE_ARRAY_OF_RECORD(MCScriptPosition, positions, position_count)
MC_PICKLE_END_RECORD()
/* Re-enable invalid-offsetof warnings */
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
////////////////////////////////////////////////////////////////////////////////
static MCScriptModule *s_modules = nil;
static MCScriptModule **s_context_slot_owners = nil;
static uindex_t s_context_slot_count;
////////////////////////////////////////////////////////////////////////////////
static MCStringRef __MCScriptDefinitionKindToString(MCScriptDefinitionKind p_kind)
{
switch(p_kind)
{
case kMCScriptDefinitionKindNone: return MCSTR("unknown");
case kMCScriptDefinitionKindExternal: return MCSTR("external");
case kMCScriptDefinitionKindType: return MCSTR("type");
case kMCScriptDefinitionKindConstant: return MCSTR("constant");
case kMCScriptDefinitionKindVariable: return MCSTR("variable");
case kMCScriptDefinitionKindHandler: return MCSTR("handler");
case kMCScriptDefinitionKindForeignHandler: return MCSTR("handler");
case kMCScriptDefinitionKindProperty: return MCSTR("property");
case kMCScriptDefinitionKindEvent: return MCSTR("event");
case kMCScriptDefinitionKindSyntax: return MCSTR("syntax");
case kMCScriptDefinitionKindDefinitionGroup: return MCSTR("group");
default:
return MCSTR("unknown");
}
return kMCEmptyString;
}
////////////////////////////////////////////////////////////////////////////////
void MCScriptDestroyModule(MCScriptModuleRef self)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);
// Call the finalizer, if any.
if (self->finalizer != nullptr)
{
self->finalizer();
}
// Release the bits pickle-release doesn't touch.
for(uindex_t i = 0; i < self -> dependency_count; i++)
if (self -> dependencies[i] . instance != nil)
MCScriptReleaseInstance(self -> dependencies[i] . instance);
for(uindex_t i = 0; i < self -> type_count; i++)
if (self -> types[i] -> typeinfo != nil)
MCValueRelease(self -> types[i] -> typeinfo);
for(uindex_t i = 0; i < self -> definition_count; i++)
if (self -> definitions[i] -> kind == kMCScriptDefinitionKindForeignHandler)
{
MCScriptForeignHandlerDefinition *t_def;
t_def = static_cast<MCScriptForeignHandlerDefinition *>(self -> definitions[i]);
switch(t_def->language)
{
case kMCScriptForeignHandlerLanguageUnknown:
break;
case kMCScriptForeignHandlerLanguageC:
break;
case kMCScriptForeignHandlerLanguageBuiltinC:
break;
case kMCScriptForeignHandlerLanguageObjC:
MCMemoryDelete(t_def->objc.function_cif);
break;
case kMCScriptForeignHandlerLanguageJava:
MCValueRelease(t_def->java.class_name);
break;
}
}
// Remove ourselves from the context slot owners list.
for(uindex_t i = 0; i < s_context_slot_count; i++)
if (s_context_slot_owners[i] == self)
s_context_slot_owners[i] = nil;
// Remove ourselves from the global module list.
if (s_modules == self)
s_modules = self -> next_module;
else
for(MCScriptModule *t_module = s_modules; t_module != nil; t_module = t_module -> next_module)
if (t_module -> next_module == self)
{
t_module -> next_module = self -> next_module;
break;
}
// Free any loaded libraries
if (self->libraries != nullptr)
{
MCValueRelease(self->libraries);
}
// Free the compiled module representation
MCPickleRelease(kMCScriptModulePickleInfo, self);
}
bool MCScriptValidateModule(MCScriptModuleRef self)
{
for(uindex_t i = 0; i < self -> definition_count; i++)
{
if (self -> definitions[i] -> kind == kMCScriptDefinitionKindVariable)
{
MCScriptVariableDefinition *t_variable;
t_variable = static_cast<MCScriptVariableDefinition *>(self -> definitions[i]);
t_variable -> slot_index = self -> slot_count++;
}
else 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;
MCTypeInfoRef t_signature;
t_signature = self -> types[t_handler -> type] -> typeinfo;
t_state.register_limit = MCHandlerTypeInfoGetParameterCount(t_signature) +
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)
goto invalid_bytecode_error;
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)
goto invalid_bytecode_error;
// If we didn't reach the limit, the bytecode is malformed.
if (t_bytecode != t_bytecode_limit)
goto invalid_bytecode_error;
// If the last operation was not return, the bytecode is malformed.
if (t_state.operation != kMCScriptBytecodeOpReturn)
goto invalid_bytecode_error;
// The total number of slots we need is recorded in register_limit.
t_handler -> slot_count = t_state.register_limit;
}
}
return true;
invalid_bytecode_error:
return MCErrorThrowGenericWithMessage(MCSTR("%{name} is not valid - malformed bytecode"),
"name", self -> name,
nil);
}
////////////////////////////////////////////////////////////////////////////////
MCScriptModuleRef MCScriptRetainModule(MCScriptModuleRef self)
{
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);
return (MCScriptModuleRef)MCScriptRetainObject(self);
}
void MCScriptReleaseModule(MCScriptModuleRef self)
{
if (nil == self)
return;
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);
MCScriptReleaseObject(self);
}
uint32_t MCScriptGetRetainCountOfModule(MCScriptModuleRef self)
{
if (nil == self)
return 0;
__MCScriptValidateObjectAndKind__(self, kMCScriptObjectKindModule);
return MCScriptGetRetainCountOfObject(self);
}
////////////////////////////////////////////////////////////////////////////////
bool MCScriptCreateModuleFromStream(MCStreamRef stream, MCScriptModuleRef& r_module)
{
uint8_t t_header[4];
if (!MCStreamRead(stream, t_header, 4))
return false;
// If this fails, then it definitely isn't a LiveCode module.
if (t_header[0] != 'L' ||
t_header[1] != 'C')
return MCErrorThrowGeneric(MCSTR("not a module"));
// If this fails, then it is a potentially a LiveCode module, but in a format
// we do not support.
if (t_header[2] != ((kMCScriptCurrentModuleVersion >> 0) & 0xFF) ||
t_header[3] != ((kMCScriptCurrentModuleVersion >> 8) & 0xFF))
return MCErrorThrowGeneric(MCSTR("module format not supported"));
// If this fails then we've run out of memory (oh well - not much to be done!).
MCScriptModule *t_module;
if (!MCScriptCreateObject(kMCScriptObjectKindModule, t_module))
return false;
// initialize licensed state
t_module->licensed = true;
// If the unpickling fails, there's nothing we can do.
if (!MCPickleRead(stream, kMCScriptModulePickleInfo, t_module))
{
MCScriptDestroyObject(t_module);
// If there is a pending error then its out of memory, otherwise report
// that we haven't been able to unpickle.
if (!MCErrorIsPending())
return MCErrorThrowGeneric(MCSTR("error reading module"));
return false;
}
// If there is already a module with the same name in memory, there's nothing we can do.
for(MCScriptModule *t_other_module = s_modules; t_other_module != nil; t_other_module = t_other_module -> next_module)
if (MCNameIsEqualToCaseless(t_other_module -> name, t_module -> name))
{
MCScriptDestroyObject(t_module);
return MCErrorThrowGeneric(MCSTR("module already loaded"));
}
// Link our module into the global module list.
t_module -> next_module = s_modules;
s_modules = t_module;
// Return the shiny new module.
r_module = t_module;
return true;
}
MC_DLLEXPORT_DEF bool
MCScriptCreateModuleFromData (MCDataRef data,
MCScriptModuleRef & r_module)
{
MCAutoValueRefBase<MCStreamRef> t_stream;
MCAutoScriptModuleRef t_module;
if (!MCMemoryInputStreamCreate (MCDataGetBytePtr (data),
MCDataGetLength (data),
&t_stream))
return false;
if (!MCScriptCreateModuleFromStream (*t_stream, &t_module))
return false;
r_module = MCScriptRetainModule (*t_module);
return true;
}
MC_DLLEXPORT_DEF bool
MCScriptCreateModulesFromStream(MCStreamRef p_stream,
MCAutoScriptModuleRefArray& x_modules)
{
size_t t_available = 0;
do
{
MCAutoScriptModuleRef t_module;
if (!MCScriptCreateModuleFromStream(p_stream, &t_module))
return false;
if (!x_modules.Push(*t_module))
return false;
if (!MCStreamGetAvailableForRead(p_stream, t_available))
return false;
}
while (t_available > 0);
return true;
}
MC_DLLEXPORT_DEF bool
MCScriptCreateModulesFromData(MCDataRef p_data,
MCAutoScriptModuleRefArray& x_modules)
{
MCAutoValueRefBase<MCStreamRef> t_stream;
if (!MCMemoryInputStreamCreate(MCDataGetBytePtr(p_data),
MCDataGetLength(p_data),
&t_stream))
return false;
return MCScriptCreateModulesFromStream(*t_stream, x_modules);
}
void
MCScriptConfigureBuiltinModule(MCScriptModuleRef p_module,
bool (*p_initializer)(void),
void (*p_finalizer)(void),
void **p_builtins)
{
p_module->initializer = p_initializer;
p_module->finalizer = p_finalizer;
p_module->builtins = p_builtins;
}
bool MCScriptLookupModule(MCNameRef p_name, MCScriptModuleRef& r_module)
{
for(MCScriptModule *t_module = s_modules; t_module != nil; t_module = t_module -> next_module)
if (MCNameIsEqualToCaseless(p_name, t_module -> name))
{
r_module = t_module;
return true;
}
return false;
}
MCNameRef MCScriptGetNameOfModule(MCScriptModuleRef self)
{
return self -> name;
}
bool MCScriptIsModuleALibrary(MCScriptModuleRef self)
{
return self -> module_kind == kMCScriptModuleKindLibrary;
}
bool MCScriptIsModuleAWidget(MCScriptModuleRef self)
{
return self -> module_kind == kMCScriptModuleKindWidget;
}
void
MCScriptSetModuleLicensed(MCScriptModuleRef self, bool p_licensed)
{
self->licensed = p_licensed;
}
bool
MCScriptIsModuleLicensed(MCScriptModuleRef self)
{
return self->licensed;
}
bool MCScriptEnsureModuleIsUsable(MCScriptModuleRef self)
{
// If the module has already been ensured as usable, we are done.
if (self -> is_usable)
return true;
/* If the module is already marked as being checked for usability,
* then there must be a cyclic module dependency. */
if (self -> is_in_usable_check)
return MCErrorThrowGeneric(MCSTR("cyclic module dependency"));
self -> is_in_usable_check = true;
// First ensure we can resolve all its external dependencies.
for(uindex_t i = 0; i < self -> dependency_count; i++)
{
MCScriptModuleRef t_module;
if (!MCScriptLookupModule(self -> dependencies[i] . name, t_module))
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - dependent module not found %{dependency}"),
"name", self -> name,
"dependency", self -> dependencies[i] . name,
nil);
goto error_cleanup;
}
// A used module must not be a widget.
if (t_module -> module_kind == kMCScriptModuleKindWidget)
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - depends on widget module %{dependency}"),
"name", self -> name,
"dependency", self -> dependencies[i] . name,
nil);
goto error_cleanup;
}
// A used module must be usable - do this before resolving imports so
// chained imports work.
if (!MCScriptEnsureModuleIsUsable(t_module))
goto error_cleanup;
// Check all the imported definitions from the module, and compute indicies.
for(uindex_t t_import = 0; t_import < self -> imported_definition_count; t_import++)
{
MCScriptImportedDefinition *t_import_def;
t_import_def = &self -> imported_definitions[t_import];
if (t_import_def -> module != i)
continue;
MCScriptDefinition *t_def;
if (!MCScriptLookupDefinitionInModule(t_module, t_import_def -> name, t_def))
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - %{dependency}.%{definition} not found"),
"name", self -> name,
"dependency", self -> dependencies[i] . name,
"definition", t_import_def -> name,
nil);
goto error_cleanup;
}
MCScriptModuleRef t_mod;
if (t_def -> kind == kMCScriptDefinitionKindExternal)
{
MCScriptExternalDefinition *t_ext_def;
t_ext_def = static_cast<MCScriptExternalDefinition *>(t_def);
t_mod = t_module -> imported_definitions[t_ext_def -> index] . resolved_module;
t_def = t_module -> imported_definitions[t_ext_def -> index] . resolved_definition;
}
else
t_mod = t_module;
if (t_def -> kind != t_import_def -> kind)
{
if (t_import_def -> kind != kMCScriptDefinitionKindHandler ||
t_def -> kind != kMCScriptDefinitionKindForeignHandler)
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - %{dependency}.%{definition} expected to be %{expected}, but is %{actual}"),
"name", self -> name,
"dependency", self -> dependencies[i] . name,
"definition", t_import_def -> name,
"expected", __MCScriptDefinitionKindToString(t_import_def -> kind),
"actual", __MCScriptDefinitionKindToString(t_def -> kind),
nil);
goto error_cleanup;
}
}
// Check that signatures match.
t_import_def -> resolved_definition = t_def;
t_import_def -> resolved_module = t_mod;
}
// Now create the instance we need - if this fails its due to out of memory.
if (!MCScriptCreateInstanceOfModule(t_module, self -> dependencies[i] . instance))
goto error_cleanup;
}
// Now call the initializer, if any.
if (self->initializer != nullptr)
{
if (!self->initializer())
goto error_cleanup;
}
// Now build all the typeinfo's
for(uindex_t i = 0; i < self -> type_count; i++)
{
MCAutoTypeInfoRef t_typeinfo;
switch(self -> types[i] -> kind)
{
case kMCScriptTypeKindDefined:
{
MCScriptDefinedType *t_type;
t_type = static_cast<MCScriptDefinedType *>(self -> types[i]);
MCScriptDefinition *t_def;
t_def = self -> definitions[t_type -> index];
if (t_def -> kind == kMCScriptDefinitionKindType)
{
MCNameRef t_public_name;
t_public_name = nil;
for(uindex_t j = 0; j < self -> exported_definition_count; j++)
if (self -> exported_definitions[j] . index == t_type -> index)
{
t_public_name = self -> exported_definitions[j] . name;
break;
}
MCScriptTypeDefinition *t_type_def;
t_type_def = static_cast<MCScriptTypeDefinition *>(t_def);
if (t_public_name != nil)
{
MCAutoStringRef t_name_string;
if (!MCStringFormat(&t_name_string, "%@.%@", self -> name, t_public_name))
goto error_cleanup; // oom
MCNewAutoNameRef t_name;
if (!MCNameCreate(*t_name_string, &t_name))
goto error_cleanup; // oom
// If the target type is an alias, named type or optional type then
// we just create an alias. Otherwise it must be a record, foreign,
// handler type - this means it must be named.
MCTypeInfoRef t_target_type;
t_target_type = self -> types[t_type_def -> type] -> typeinfo;
if (MCTypeInfoIsAlias(t_target_type) ||
MCTypeInfoIsNamed(t_target_type) ||
MCTypeInfoIsOptional(t_target_type))
{
if (!MCAliasTypeInfoCreate(*t_name, t_target_type, &t_typeinfo))
goto error_cleanup; // oom
}
else
{
if (!MCNamedTypeInfoCreate(*t_name, &t_typeinfo))
goto error_cleanup; // oom
if (!MCNamedTypeInfoBind(*t_typeinfo, t_target_type))
goto error_cleanup; // oom
}
}
else
{
if (!MCAliasTypeInfoCreate(kMCEmptyName, self -> types[t_type_def -> type] -> typeinfo, &t_typeinfo))
goto error_cleanup; // oom
}
}
else if (t_def -> kind == kMCScriptDefinitionKindExternal)
{
MCScriptExternalDefinition *t_ext_def;
t_ext_def = static_cast<MCScriptExternalDefinition *>(t_def);
MCScriptImportedDefinition *t_import;
t_import = &self -> imported_definitions[t_ext_def -> index];
MCScriptModuleRef t_module;
t_module = t_import -> resolved_module;
t_typeinfo = t_module -> types[static_cast<MCScriptTypeDefinition *>(t_import -> resolved_definition) -> type] -> typeinfo;
}
else
goto error_cleanup;
}
break;
case kMCScriptTypeKindOptional:
{
MCScriptOptionalType *t_type;
t_type = static_cast<MCScriptOptionalType *>(self -> types[i]);
if (!MCOptionalTypeInfoCreate(self -> types[t_type -> type] -> typeinfo, &t_typeinfo))
goto error_cleanup; // oom
}
break;
case kMCScriptTypeKindForeign:
{
MCScriptForeignType *t_type;
t_type = static_cast<MCScriptForeignType *>(self -> types[i]);
uindex_t t_offset = 0;
if (!MCStringFirstIndexOfChar(t_type->binding, ':', 0, kMCStringOptionCompareExact, t_offset))
{
bool t_is_builtin = false;
void *t_symbol = nullptr;
integer_t t_ordinal = 0;
if (self->builtins != nullptr &&
MCTypeConvertStringToLongInteger(t_type->binding, t_ordinal))
{
t_symbol = self->builtins[t_ordinal];
t_is_builtin = true;
}
else
{
t_symbol = MCSLibraryLookupSymbol(MCScriptGetLibrary(),
t_type->binding);
t_is_builtin = false;
}
if (t_symbol == nullptr)
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - unable to resolve foreign type '%{type}'"),
"name", self -> name,
"type", t_type -> binding,
nil);
goto error_cleanup;
}
/* The symbol is a function that returns a type info reference. */
if (t_is_builtin)
{
MCTypeInfoRef t_typeinfo_bare;
void (*t_type_func_builtin)(void*rv, void**av) = (void(*)(void*, void**))t_symbol;
t_type_func_builtin(&t_typeinfo_bare, nullptr);
t_typeinfo = t_typeinfo_bare;
}
else
{
MCTypeInfoRef (*t_type_func)(void) = (MCTypeInfoRef (*)(void)) t_symbol;
t_typeinfo = t_type_func();
}
}
else
{
#if defined(__EMSCRIPTEN__) // Skip foreign type constructor check on emscripten
t_typeinfo = kMCNullTypeInfo;
#else
MCAutoStringRef t_type_func, t_args;
if (!MCStringDivideAtChar(t_type->binding, ':', kMCStringOptionCompareExact, &t_type_func, &t_args))
{
goto error_cleanup;
}
void *t_symbol =
MCSLibraryLookupSymbol(MCScriptGetLibrary(),
*t_type_func);
if (t_symbol == nullptr)
{
MCErrorThrowGenericWithMessage(MCSTR("%{name} not usable - unable to resolve foreign type constructor '%{type}'"),
"name", self -> name,
"type", *t_type_func,
nil);
goto error_cleanup;
}
bool (*t_type_constructor)(MCStringRef p_binding, MCTypeInfoRef& r_typeinfo) =
(bool(*)(MCStringRef, MCTypeInfoRef&))t_symbol;
if (!t_type_constructor(*t_args, &t_typeinfo))
{
goto error_cleanup;
}
#endif
}
}
break;
case kMCScriptTypeKindRecord:
{
MCScriptRecordType *t_type;
t_type = static_cast<MCScriptRecordType *>(self -> types[i]);
MCAutoArray<MCRecordTypeFieldInfo> t_fields;
for(uindex_t j = 0; j < t_type -> field_count; j++)
{
MCRecordTypeFieldInfo t_field;
t_field . name = t_type -> fields[j] . name;
t_field . type = self -> types[t_type -> fields[j] . type] -> typeinfo;
if (!t_fields . Push(t_field))
goto error_cleanup; // oom
}
if (!MCRecordTypeInfoCreate(t_fields . Ptr(), t_type -> field_count, &t_typeinfo))
goto error_cleanup; // oom
}
break;
case kMCScriptTypeKindHandler:
case kMCScriptTypeKindForeignHandler:
{
MCScriptHandlerType *t_type;
t_type = static_cast<MCScriptHandlerType *>(self -> types[i]);
MCAutoArray<MCHandlerTypeFieldInfo> t_parameters;
for(uindex_t j = 0; j < t_type -> parameter_count; j++)
{
MCHandlerTypeFieldInfo t_parameter;
t_parameter . mode = t_type -> parameters[j] . GetFieldMode();
t_parameter . type = self -> types[t_type -> parameters[j] . type] -> typeinfo;
if (!t_parameters . Push(t_parameter))
goto error_cleanup;
}
if (t_type -> kind == kMCScriptTypeKindHandler)
{
if (!MCHandlerTypeInfoCreate(t_parameters . Ptr(), t_type -> parameter_count, self -> types[t_type -> return_type] -> typeinfo, &t_typeinfo))
goto error_cleanup; // oom
}
else
{
if (!MCForeignHandlerTypeInfoCreate(t_parameters . Ptr(), t_type -> parameter_count, self -> types[t_type -> return_type] -> typeinfo, &t_typeinfo))
goto error_cleanup; // oom
}
}
break;
}
self -> types[i] -> typeinfo = MCValueRetain(*t_typeinfo);
}
// First validate the module - if this fails we do nothing more.
if (!MCScriptValidateModule(self))
goto error_cleanup;
// Now bind all the public types.
self -> is_usable = true;
self -> is_in_usable_check = false;
return true;
error_cleanup:
self -> is_in_usable_check = false;
return false;
}
bool
MCScriptLoadModuleLibrary(MCScriptModuleRef self,
MCStringRef p_library_name_string,
MCSLibraryRef& r_library)
{
/* If the string is empty, then use the library containing libscript (i.e.
* 'builtin'). */
if (MCStringIsEmpty(p_library_name_string))
{
r_library = MCScriptGetLibrary();
return true;
}
/* Create a nameref for the library so we can lookup up in the module's
* library list. */
MCNewAutoNameRef t_library_name;
if (!MCNameCreate(p_library_name_string,
&t_library_name))
{
return false;
}
/* If there is a libraries array, then check whether the library has been
* loaded before, and if so use that library. Otherwise create the libraries
* array. */
if (self->libraries != nullptr)
{
if (MCArrayFetchValue(self->libraries,
true,
*t_library_name,
(MCValueRef&)r_library))
{
return true;
}
}
else
{
if (!MCArrayCreateMutable(self->libraries))
{
return false;
}
}
/* Attempt to load the library */
MCSAutoLibraryRef t_library;
if (!MCScriptLoadLibrary(self,
p_library_name_string,
&t_library))
{
return false;
}
/* Store the library in the libraries array for the module. */
if (!MCArrayStoreValue(self->libraries,
true,
*t_library_name,
*t_library))
{
return false;
}
/* Return the library (unretained - as the module holds a reference through
* its array). */
r_library = *t_library;
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool
__MCScriptCopyDefinitionsOfModule(MCScriptModuleRef self,
MCScriptDefinitionKind p_kind,
/* copy */ MCProperListRef& r_names)
{
MCAutoProperListRef t_names;
if (!MCProperListCreateMutable(&t_names))
{
return false;
}
for(uindex_t i = 0; i < self -> exported_definition_count; i++)