Skip to content

Commit 611bad4

Browse files
ArrayStack: add Clone method (#1304)
* Provide ArrayStack.Clone method * Clean definition for old syntax. Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
1 parent 4a4b9ce commit 611bad4

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

core/logic/smn_adt_stack.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ static cell_t CreateStack(IPluginContext *pContext, const cell_t *params)
8383
return hndl;
8484
}
8585

86+
static cell_t CloneStack(IPluginContext *pContext, const cell_t *params)
87+
{
88+
CellArray *oldArray;
89+
HandleError err;
90+
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
91+
92+
if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&oldArray)) != HandleError_None)
93+
{
94+
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
95+
}
96+
97+
ICellArray *array = oldArray->clone();
98+
if (!array)
99+
{
100+
return pContext->ThrowNativeError("Failed to clone stack. Out of memory.");
101+
}
102+
103+
Handle_t hndl = handlesys->CreateHandle(htCellStack, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
104+
if (!hndl)
105+
{
106+
delete array;
107+
}
108+
109+
return hndl;
110+
}
111+
86112
static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params)
87113
{
88114
CellArray *array;
@@ -387,6 +413,7 @@ static cell_t GetStackBlockSize(IPluginContext *pContext, const cell_t *params)
387413
REGISTER_NATIVES(cellStackNatives)
388414
{
389415
{"CreateStack", CreateStack},
416+
{"CloneStack", CloneStack},
390417
{"IsStackEmpty", IsStackEmpty},
391418
{"PopStackArray", PopStackArray},
392419
{"PopStackCell", PopStackCell},
@@ -398,6 +425,7 @@ REGISTER_NATIVES(cellStackNatives)
398425

399426
// Transitional syntax support.
400427
{"ArrayStack.ArrayStack", CreateStack},
428+
{"ArrayStack.Clone", CloneStack},
401429
{"ArrayStack.Pop", ArrayStack_Pop},
402430
{"ArrayStack.PopString", ArrayStack_PopString},
403431
{"ArrayStack.PopArray", ArrayStack_PopArray},

plugins/include/adt_stack.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ methodmap ArrayStack < Handle
5454
// new Array[X][32]
5555
public native ArrayStack(int blocksize=1);
5656

57+
// Clones an stack, returning a new handle with the same size and data.
58+
// This should NOT be confused with CloneHandle. This is a completely new
59+
// handle with the same data but no relation to the original. It should
60+
// closed when no longer needed.
61+
//
62+
// @return New handle to the cloned stack object
63+
public native ArrayStack Clone();
64+
5765
// Pushes a value onto the end of the stack, adding a new index.
5866
//
5967
// This may safely be used even if the stack has a blocksize

0 commit comments

Comments
 (0)