Skip to content

Commit 54db835

Browse files
committed
[[ LCB Builder ]] Replace direct MCResizeArrays with __extend_array wrapper.
1 parent fb1c240 commit 54db835

1 file changed

Lines changed: 87 additions & 106 deletions

File tree

libscript/src/script-builder.cpp

Lines changed: 87 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ static void __assign_definition_name(MCScriptModuleBuilderRef self, uindex_t p_i
8686
self -> module . definition_names[p_index] = MCValueRetain(p_name);
8787
}
8888

89+
template<typename T> static bool __extend_array(MCScriptModuleBuilderRef self, T*& x_array, uindex_t& x_array_size, uindex_t& r_index)
90+
{
91+
if (!MCMemoryResizeArray(x_array_size + 1, x_array, x_array_size))
92+
{
93+
r_index = 0;
94+
self -> valid = false;
95+
return false;
96+
}
97+
98+
r_index = x_array_size - 1;
99+
100+
return true;
101+
}
102+
89103
void MCScriptBeginModule(MCScriptModuleKind p_kind, MCNameRef p_name, MCScriptModuleBuilderRef& r_builder)
90104
{
91105
MCScriptModuleBuilder *self;
@@ -162,32 +176,24 @@ void MCScriptAddDependencyToModule(MCScriptModuleBuilderRef self, MCNameRef p_de
162176
return;
163177
}
164178

165-
if (!MCMemoryResizeArray(self -> module . dependency_count + 1, self -> module . dependencies, self -> module . dependency_count))
166-
{
167-
r_index = 0;
168-
self -> valid = false;
179+
if (!__extend_array(self, self -> module . dependencies, self -> module . dependency_count, r_index))
169180
return;
170-
}
171-
172-
self -> module . dependencies[self -> module . dependency_count - 1] . name = MCValueRetain(p_dependency);
173-
self -> module . dependencies[self -> module . dependency_count - 1] . version = 0;
174181

175-
r_index = self -> module . dependency_count - 1;
182+
self -> module . dependencies[r_index] . name = MCValueRetain(p_dependency);
183+
self -> module . dependencies[r_index] . version = 0;
176184
}
177185

178186
void MCScriptAddExportToModule(MCScriptModuleBuilderRef self, uindex_t p_definition)
179187
{
180188
if (self == nil || !self -> valid)
181189
return;
182190

183-
if (!MCMemoryResizeArray(self -> module . exported_definition_count + 1, self -> module . exported_definitions, self -> module . exported_definition_count))
184-
{
185-
self -> valid = false;
191+
uindex_t t_index;
192+
if (!__extend_array(self, self -> module . exported_definitions, self -> module . exported_definition_count, t_index))
186193
return;
187-
}
188194

189-
self -> module . exported_definitions[self -> module . exported_definition_count - 1] . name = MCValueRetain(self -> module . definition_names[p_definition]);
190-
self -> module . exported_definitions[self -> module . exported_definition_count - 1] . index = p_definition;
195+
self -> module . exported_definitions[t_index] . name = MCValueRetain(self -> module . definition_names[p_definition]);
196+
self -> module . exported_definitions[t_index] . index = p_definition;
191197

192198
// If the definition is a type, then make sure we make it a 'defined' type otherwise
193199
// it won't get bound globally.
@@ -218,9 +224,10 @@ void MCScriptAddImportToModule(MCScriptModuleBuilderRef self, uindex_t p_index,
218224
}
219225
}
220226

221-
if (!MCMemoryResizeArray(self -> module . imported_definition_count + 1, self -> module . imported_definitions, self -> module . imported_definition_count) ||
222-
!MCMemoryResizeArray(self -> module . definition_count + 1, self -> module . definitions, self -> module . definition_count) ||
223-
!MCMemoryNew((MCScriptExternalDefinition*&)self -> module . definitions[self -> module . definition_count - 1]) ||
227+
uindex_t t_imp_index, t_def_index;
228+
if (!__extend_array(self, self -> module . imported_definitions, self -> module . imported_definition_count, t_imp_index) ||
229+
!__extend_array(self, self -> module . definitions, self -> module . definition_count, t_def_index) ||
230+
!MCMemoryNew((MCScriptExternalDefinition*&)self -> module . definitions[t_def_index]) ||
224231
!__append_definition_name(self, p_name))
225232
{
226233
r_index = 0;
@@ -229,36 +236,34 @@ void MCScriptAddImportToModule(MCScriptModuleBuilderRef self, uindex_t p_index,
229236
}
230237

231238
MCScriptImportedDefinition *t_import;
232-
t_import = &self -> module . imported_definitions[self -> module . imported_definition_count - 1];
239+
t_import = &self -> module . imported_definitions[t_imp_index];
233240

234241
t_import -> module = p_index;
235242
t_import -> kind = p_kind;
236243
t_import -> name = MCValueRetain(p_name);
237244
// t_import -> type = MCValueRetain(p_type);
238245

239246
MCScriptExternalDefinition *t_definition;
240-
t_definition = static_cast<MCScriptExternalDefinition *>(self -> module . definitions[self -> module . definition_count - 1]);
247+
t_definition = static_cast<MCScriptExternalDefinition *>(self -> module . definitions[t_def_index]);
241248

242249
t_definition -> kind = kMCScriptDefinitionKindExternal;
243-
t_definition -> index = self -> module . imported_definition_count - 1;
250+
t_definition -> index = t_imp_index;
244251

245-
r_index = self -> module . definition_count - 1;
252+
r_index = t_def_index;
246253
}
247254

248255
void MCScriptAddDefinitionToModule(MCScriptModuleBuilderRef self, uindex_t& r_index)
249256
{
250257
if (self == nil || !self -> valid)
251258
return;
252259

253-
if (!MCMemoryResizeArray(self -> module . definition_count + 1, self -> module . definitions, self -> module . definition_count) ||
260+
if (!__extend_array(self, self -> module . definitions, self -> module . definition_count, r_index) ||
254261
!__append_definition_name(self, kMCEmptyName))
255262
{
256263
r_index = 0;
257264
self -> valid = false;
258265
return;
259266
}
260-
261-
r_index = self -> module . definition_count - 1;
262267
}
263268

264269
void MCScriptAddTypeToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_type, uindex_t p_index)
@@ -401,14 +406,11 @@ void MCScriptAddEventToModule(MCScriptModuleBuilderRef self, MCNameRef p_name, u
401406

402407
static void __add_script_type(MCScriptModuleBuilderRef self, MCScriptType *p_type, uindex_t& r_index)
403408
{
404-
if (!MCMemoryResizeArray(self -> module . type_count + 1, self -> module . types, self -> module . type_count))
405-
{
406-
self -> valid = false;
409+
if (!__extend_array(self, self -> module . types, self -> module . type_count, r_index))
407410
return;
408-
}
409411

410-
self -> module . types[self -> module . type_count - 1] = p_type;
411-
r_index = self -> module . type_count - 1;
412+
self -> module . types[r_index] = p_type;
413+
r_index = r_index;
412414
}
413415

414416
void MCScriptAddDefinedTypeToModule(MCScriptModuleBuilderRef self, uindex_t p_index, uindex_t& r_type)
@@ -515,17 +517,14 @@ void MCScriptContinueHandlerTypeInModule(MCScriptModuleBuilderRef self, MCScript
515517
MCScriptHandlerType *t_type;
516518
t_type = static_cast<MCScriptHandlerType *>(self -> current_type);
517519

518-
if (!MCMemoryResizeArray(t_type -> parameter_count + 1, t_type -> parameters, t_type -> parameter_count) ||
519-
!MCMemoryResizeArray(t_type -> parameter_name_count + 1, t_type -> parameter_names, t_type -> parameter_name_count))
520-
{
521-
self -> valid = false;
520+
uindex_t t_param_index, t_name_index;
521+
if (!__extend_array(self, t_type -> parameters, t_type -> parameter_count, t_param_index) ||
522+
!__extend_array(self, t_type -> parameter_names, t_type -> parameter_name_count, t_name_index))
522523
return;
523-
}
524524

525-
t_type -> parameters[t_type -> parameter_count - 1] . mode = p_mode;
526-
//t_type -> parameters[t_type -> parameter_count - 1] . name = MCValueRetain(p_name);
527-
t_type -> parameters[t_type -> parameter_count - 1] . type = p_type;
528-
t_type -> parameter_names[t_type -> parameter_name_count - 1] = MCValueRetain(p_name);
525+
t_type -> parameters[t_param_index] . mode = p_mode;
526+
t_type -> parameters[t_param_index] . type = p_type;
527+
t_type -> parameter_names[t_name_index] = MCValueRetain(p_name);
529528
}
530529

531530
void MCScriptEndHandlerTypeInModule(MCScriptModuleBuilderRef self, uindex_t& r_new_type)
@@ -604,14 +603,12 @@ void MCScriptContinueRecordTypeInModule(MCScriptModuleBuilderRef self, MCNameRef
604603
MCScriptRecordType *t_type;
605604
t_type = static_cast<MCScriptRecordType *>(self -> current_type);
606605

607-
if (!MCMemoryResizeArray(t_type -> field_count + 1, t_type -> fields, t_type -> field_count))
608-
{
609-
self -> valid = false;
606+
uindex_t t_index;
607+
if (!__extend_array(self, t_type -> fields, t_type -> field_count, t_index))
610608
return;
611-
}
612609

613-
t_type -> fields[t_type -> field_count - 1] . name = MCValueRetain(p_name);
614-
t_type -> fields[t_type -> field_count - 1] . type = p_type;
610+
t_type -> fields[t_index] . name = MCValueRetain(p_name);
611+
t_type -> fields[t_index] . type = p_type;
615612
}
616613

617614
void MCScriptEndRecordTypeInModule(MCScriptModuleBuilderRef self, uindex_t& r_new_type)
@@ -703,13 +700,14 @@ void MCScriptBeginSyntaxMethodInModule(MCScriptModuleBuilderRef self, uindex_t p
703700
MCScriptSyntaxDefinition *t_syntax;
704701
t_syntax = static_cast<MCScriptSyntaxDefinition *>(self -> module . definitions[self -> current_syntax]);
705702

706-
if (!MCMemoryResizeArray(t_syntax -> method_count + 1, t_syntax -> methods, t_syntax -> method_count))
703+
uindex_t t_index;
704+
if (!__extend_array(self, t_syntax -> methods, t_syntax -> method_count, t_index))
707705
{
708706
self -> valid = false;
709707
return;
710708
}
711709

712-
t_syntax -> methods[t_syntax -> method_count - 1] . handler = p_handler;
710+
t_syntax -> methods[t_index] . handler = p_handler;
713711
}
714712

715713
static void MCScriptAddArgumentToSyntaxMethodInModule(MCScriptModuleBuilderRef self, uindex_t p_index)
@@ -729,13 +727,11 @@ static void MCScriptAddArgumentToSyntaxMethodInModule(MCScriptModuleBuilderRef s
729727
MCScriptSyntaxMethod *t_method;
730728
t_method = &t_syntax -> methods[t_syntax -> method_count - 1];
731729

732-
if (!MCMemoryResizeArray(t_method -> argument_count + 1, t_method -> arguments, t_method -> argument_count))
733-
{
734-
self -> valid = false;
730+
uindex_t t_arg_index;
731+
if (!__extend_array(self, t_method -> arguments, t_method -> argument_count, t_arg_index))
735732
return;
736-
}
737733

738-
t_method -> arguments[t_method -> argument_count - 1] = p_index;
734+
t_method -> arguments[t_arg_index] = p_index;
739735
}
740736

741737
void MCScriptAddBuiltinArgumentToSyntaxMethodInModule(MCScriptModuleBuilderRef self, uindex_t p_index)
@@ -818,13 +814,11 @@ void MCScriptAddHandlerToDefinitionGroupInModule(MCScriptModuleBuilderRef self,
818814
if (self -> current_handler_group[i] == index)
819815
return;
820816

821-
if (!MCMemoryResizeArray(self -> current_handler_group_size + 1, self -> current_handler_group, self -> current_handler_group_size))
822-
{
823-
self -> valid = false;
817+
uindex_t t_gindex;
818+
if (!__extend_array(self, self -> current_handler_group, self -> current_handler_group_size, t_gindex))
824819
return;
825-
}
826820

827-
self -> current_handler_group[self -> current_handler_group_size - 1] = index;
821+
self -> current_handler_group[t_gindex] = index;
828822
}
829823

830824
void MCScriptEndDefinitionGroupInModule(MCScriptModuleBuilderRef self, uindex_t& r_index)
@@ -923,13 +917,11 @@ static void __emit_bytecode_byte(MCScriptModuleBuilderRef self, uint8_t p_byte)
923917
if (self == nil || !self -> valid)
924918
return;
925919

926-
if (!MCMemoryResizeArray(self -> module . bytecode_count + 1, self -> module . bytecode, self -> module . bytecode_count))
927-
{
928-
self -> valid = false;
920+
uindex_t t_index;
921+
if (!__extend_array(self, self -> module . bytecode, self -> module . bytecode_count, t_index))
929922
return;
930-
}
931923

932-
self -> module . bytecode[self -> module . bytecode_count - 1] = p_byte;
924+
self -> module . bytecode[t_index] = p_byte;
933925
}
934926

935927
static void __emit_bytecode_uint(MCScriptModuleBuilderRef self, uindex_t p_value)
@@ -992,41 +984,34 @@ static void __emit_constant(MCScriptModuleBuilderRef self, MCValueRef p_constant
992984
return;
993985
}
994986

995-
if (!MCMemoryResizeArray(self -> module . value_count + 1, self -> module . values, self -> module . value_count))
996-
{
997-
r_index = 0;
998-
self -> valid = false;
987+
uindex_t t_vindex;
988+
if (!__extend_array(self, self -> module . values, self -> module . value_count, t_vindex))
999989
return;
1000-
}
1001990

1002-
self -> module . values[self -> module . value_count - 1] = MCValueRetain(p_constant);
991+
self -> module . values[t_vindex] = MCValueRetain(p_constant);
1003992

1004-
r_index = self -> module . value_count;
993+
r_index = t_vindex + 1;
1005994
}
1006995

1007996
static void __begin_instruction(MCScriptModuleBuilderRef self, MCScriptBytecodeOp p_operation)
1008997
{
1009-
if (!MCMemoryResizeArray(self -> instruction_count + 1, self -> instructions, self -> instruction_count))
1010-
{
1011-
self -> valid = false;
998+
uindex_t t_index;
999+
if (!__extend_array(self, self -> instructions, self -> instruction_count, t_index))
10121000
return;
1013-
}
10141001

1015-
self -> instructions[self -> instruction_count - 1] . file = self -> current_file;
1016-
self -> instructions[self -> instruction_count - 1] . line = self -> current_line;
1002+
self -> instructions[t_index] . file = self -> current_file;
1003+
self -> instructions[t_index] . line = self -> current_line;
10171004

1018-
self -> instructions[self -> instruction_count - 1] . operation = p_operation;
1019-
self -> instructions[self -> instruction_count - 1] . arity = 0;
1020-
self -> instructions[self -> instruction_count - 1] . operands = self -> operand_count;
1005+
self -> instructions[t_index] . operation = p_operation;
1006+
self -> instructions[t_index] . arity = 0;
1007+
self -> instructions[t_index] . operands = self -> operand_count;
10211008
}
10221009

10231010
static void __continue_instruction(MCScriptModuleBuilderRef self, uindex_t p_argument)
10241011
{
1025-
if (!MCMemoryResizeArray(self -> operand_count + 1, self -> operands, self -> operand_count))
1026-
{
1027-
self -> valid = false;
1012+
uindex_t t_op_index;
1013+
if (!__extend_array(self, self -> operands, self -> operand_count, t_op_index))
10281014
return;
1029-
}
10301015

10311016
if (self -> instructions[self -> instruction_count - 1] . arity == 256)
10321017
{
@@ -1035,7 +1020,7 @@ static void __continue_instruction(MCScriptModuleBuilderRef self, uindex_t p_arg
10351020
}
10361021

10371022
self -> instructions[self -> instruction_count - 1] . arity += 1;
1038-
self -> operands[self -> operand_count - 1] = p_argument;
1023+
self -> operands[t_op_index] = p_argument;
10391024
}
10401025

10411026
static void __end_instruction(MCScriptModuleBuilderRef self)
@@ -1069,15 +1054,13 @@ static void __emit_position(MCScriptModuleBuilderRef self, uindex_t p_address, u
10691054
t_last_pos -> line == p_line)
10701055
return;
10711056

1072-
if (!MCMemoryResizeArray(self -> module . position_count + 1, self -> module . positions, self -> module . position_count))
1073-
{
1074-
self -> valid = false;
1057+
uindex_t t_pindex;
1058+
if (!__extend_array(self, self -> module . positions, self -> module . position_count, t_pindex))
10751059
return;
1076-
}
10771060

1078-
self -> module . positions[self -> module . position_count - 1] . address = p_address;
1079-
self -> module . positions[self -> module . position_count - 1] . file = p_file;
1080-
self -> module . positions[self -> module . position_count - 1] . line = p_line;
1061+
self -> module . positions[t_pindex] . address = p_address;
1062+
self -> module . positions[t_pindex] . file = p_file;
1063+
self -> module . positions[t_pindex] . line = p_line;
10811064
}
10821065

10831066
void MCScriptBeginHandlerInModule(MCScriptModuleBuilderRef self, MCNameRef p_name, uindex_t p_type, uindex_t p_index)
@@ -1223,16 +1206,16 @@ void MCScriptAddVariableToHandlerInModule(MCScriptModuleBuilderRef self, MCNameR
12231206
MCScriptHandlerDefinition *t_handler;
12241207
t_handler = static_cast<MCScriptHandlerDefinition *>(self -> module . definitions[self -> current_handler]);
12251208

1226-
if (!MCMemoryResizeArray(t_handler -> local_type_count + 1, t_handler -> local_types, t_handler -> local_type_count) ||
1227-
!MCMemoryResizeArray(t_handler -> local_name_count + 1, t_handler -> local_names, t_handler -> local_name_count))
1209+
uindex_t t_tindex, t_nindex;
1210+
if (!__extend_array(self, t_handler -> local_types, t_handler -> local_type_count, t_tindex) ||
1211+
!__extend_array(self, t_handler -> local_names, t_handler -> local_name_count, t_nindex))
12281212
{
12291213
r_index = 0;
1230-
self -> valid = false;
12311214
return;
12321215
}
12331216

1234-
t_handler -> local_types[t_handler -> local_type_count - 1] = p_type;
1235-
t_handler -> local_names[t_handler -> local_name_count - 1] = MCValueRetain(p_name);
1217+
t_handler -> local_types[t_tindex] = p_type;
1218+
t_handler -> local_names[t_nindex] = MCValueRetain(p_name);
12361219

12371220
r_index = self -> current_param_count + t_handler -> local_type_count - 1;
12381221
}
@@ -1242,16 +1225,16 @@ void MCScriptDeferLabelForBytecodeInModule(MCScriptModuleBuilderRef self, uindex
12421225
if (self == nil || !self -> valid)
12431226
return;
12441227

1245-
if (!MCMemoryResizeArray(self -> label_count + 1, self -> labels, self -> label_count))
1228+
uindex_t t_lindex;
1229+
if (!__extend_array(self, self -> labels, self -> label_count, t_lindex))
12461230
{
12471231
r_label = 0;
1248-
self -> valid = false;
12491232
return;
12501233
}
12511234

1252-
self -> labels[self -> label_count - 1] . instruction = UINDEX_MAX;
1235+
self -> labels[t_lindex] . instruction = UINDEX_MAX;
12531236

1254-
r_label = self -> label_count;
1237+
r_label = t_lindex + 1;
12551238
}
12561239

12571240
void MCScriptResolveLabelForBytecodeInModule(MCScriptModuleBuilderRef self, uindex_t p_label)
@@ -1418,14 +1401,12 @@ void MCScriptEmitPositionInModule(MCScriptModuleBuilderRef self, MCNameRef p_fil
14181401
return;
14191402
}
14201403

1421-
if (!MCMemoryResizeArray(self -> module . source_file_count + 1, self -> module . source_files, self -> module . source_file_count))
1422-
{
1423-
self -> valid = false;
1404+
uindex_t t_findex;
1405+
if (!__extend_array(self, self -> module . source_files, self -> module . source_file_count, t_findex))
14241406
return;
1425-
}
14261407

1427-
self -> module . source_files[self -> module . source_file_count - 1] = MCValueRetain(p_file);
1428-
self -> current_file = self -> module . source_file_count - 1;
1408+
self -> module . source_files[t_findex] = MCValueRetain(p_file);
1409+
self -> current_file = t_findex;
14291410
}
14301411

14311412
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)