Skip to content

Commit

Permalink
Array: allow getting empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Iswenzz committed Jun 15, 2022
1 parent eb32542 commit 793e1d6
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions extensions/variables.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "variables.h"
#include <assert.h>

VariableValueArray Scr_CreateArray(int length)
{
Expand All @@ -11,32 +10,34 @@ VariableValueArray Scr_CreateArray(int length)

void Scr_FreeArray(VariableValueArray* array)
{
free(array->items);
if (array && array->items)
free(array->items);
}

VariableValueArray Scr_GetArray(unsigned int paramnum)
{
VariableValueArray array = { 0 };
int parentId = Scr_GetObject(paramnum);
assert(parentId != 0);
assert(Scr_GetObjectType(parentId) == VAR_ARRAY);

if (Scr_GetObjectType(parentId) != VAR_ARRAY)
return array;

int length = GetArraySize(parentId);
assert(length > 0);
int index = length - 1;

if (!length)
return array;

VariableValueInternal *entryValue;
unsigned int id;
int index = length - 1;
array = Scr_CreateArray(length);
unsigned int id = IGScrVarGlob[parentId + VARIABLELIST_PARENT_BEGIN].nextSibling;
unsigned int nextSibling;

VariableValueArray array = Scr_CreateArray(length);
id = IGScrVarGlob[parentId + VARIABLELIST_PARENT_BEGIN].nextSibling;
if (id)
{
while (qtrue)
{
entryValue = &IGScrVarGlob[id + VARIABLELIST_CHILD_BEGIN];
assert((entryValue->w.status & VAR_STAT_MASK) != VAR_STAT_FREE);
assert(!IsObject(entryValue));

array.items[index].type = entryValue->w.type & VAR_MASK;
array.items[index].u = entryValue->u.u;
Expand All @@ -46,7 +47,6 @@ VariableValueArray Scr_GetArray(unsigned int paramnum)
break;

id = IGScrVarGlob[nextSibling + VARIABLELIST_CHILD_BEGIN].hash.id;
assert(id != 0);
index--;
}
}
Expand Down

0 comments on commit 793e1d6

Please sign in to comment.