Skip to content

Commit

Permalink
CGSC 1.1.1: Scr_CreateArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Iswenzz committed Jun 15, 2022
1 parent 0202870 commit eb32542
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions asm/cgsc_export.asm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pexport Scr_AddFunc
pexport Scr_AddVariable
pexport Scr_CallFunction
pexport Scr_CallMethod
pexport Scr_CreateArray
pexport Scr_ExecThreadResult
pexport Scr_FreeArray
pexport Scr_GetThreadReturn
Expand Down
10 changes: 10 additions & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Extensions

#### ``Scr_AllocArray(int length)``
Create a new GSC array with a fixed size.
The array can be freed with Scr_FreeArray.

```c
VariableValueArray array = Scr_AllocArray(5);
Scr_FreeArray(&array);
```
<hr>
#### ``Scr_FreeArray(VariableValueArray *array)``
Free the value obtained by Scr_GetArray.
Expand Down
12 changes: 9 additions & 3 deletions extensions/variables.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "variables.h"
#include <assert.h>

VariableValueArray Scr_CreateArray(int length)
{
VariableValueArray array;
array.length = length;
array.items = (VariableValue *)malloc(length * sizeof(VariableValue));
return array;
}

void Scr_FreeArray(VariableValueArray* array)
{
free(array->items);
Expand All @@ -20,9 +28,7 @@ VariableValueArray Scr_GetArray(unsigned int paramnum)
int index = length - 1;
unsigned int nextSibling;

VariableValueArray array = { 0 };
array.length = length;
array.items = (VariableValue *)malloc(length * sizeof(VariableValue));
VariableValueArray array = Scr_CreateArray(length);
id = IGScrVarGlob[parentId + VARIABLELIST_PARENT_BEGIN].nextSibling;
if (id)
{
Expand Down
8 changes: 8 additions & 0 deletions extensions/variables.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#pragma once
#include "cgsc.h"

/**
* @brief Create a new VariableValueArray with a fixed length.
* The array can be freed with Scr_FreeArray.
*
* @param length - The array length.
*/
Plugin(VariableValueArray, Scr_CreateArray(int length));

/**
* @brief Free the variables of VariableValueArray.
*
Expand Down

0 comments on commit eb32542

Please sign in to comment.