diff --git a/core/logic/smn_adt_stack.cpp b/core/logic/smn_adt_stack.cpp index 670a5ead2..7a48d1d29 100644 --- a/core/logic/smn_adt_stack.cpp +++ b/core/logic/smn_adt_stack.cpp @@ -83,6 +83,32 @@ static cell_t CreateStack(IPluginContext *pContext, const cell_t *params) return hndl; } +static cell_t CloneStack(IPluginContext *pContext, const cell_t *params) +{ + CellArray *oldArray; + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); + + if ((err = handlesys->ReadHandle(params[1], htCellStack, &sec, (void **)&oldArray)) != HandleError_None) + { + return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); + } + + ICellArray *array = oldArray->clone(); + if (!array) + { + return pContext->ThrowNativeError("Failed to clone stack. Out of memory."); + } + + Handle_t hndl = handlesys->CreateHandle(htCellStack, array, pContext->GetIdentity(), g_pCoreIdent, NULL); + if (!hndl) + { + delete array; + } + + return hndl; +} + static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params) { CellArray *array; @@ -387,6 +413,7 @@ static cell_t GetStackBlockSize(IPluginContext *pContext, const cell_t *params) REGISTER_NATIVES(cellStackNatives) { {"CreateStack", CreateStack}, + {"CloneStack", CloneStack}, {"IsStackEmpty", IsStackEmpty}, {"PopStackArray", PopStackArray}, {"PopStackCell", PopStackCell}, @@ -398,6 +425,7 @@ REGISTER_NATIVES(cellStackNatives) // Transitional syntax support. {"ArrayStack.ArrayStack", CreateStack}, + {"ArrayStack.Clone", CloneStack}, {"ArrayStack.Pop", ArrayStack_Pop}, {"ArrayStack.PopString", ArrayStack_PopString}, {"ArrayStack.PopArray", ArrayStack_PopArray}, diff --git a/plugins/include/adt_stack.inc b/plugins/include/adt_stack.inc index 94af6a7f1..0f873af1b 100644 --- a/plugins/include/adt_stack.inc +++ b/plugins/include/adt_stack.inc @@ -54,6 +54,14 @@ methodmap ArrayStack < Handle // new Array[X][32] public native ArrayStack(int blocksize=1); + // Clones an stack, returning a new handle with the same size and data. + // This should NOT be confused with CloneHandle. This is a completely new + // handle with the same data but no relation to the original. It should + // closed when no longer needed. + // + // @return New handle to the cloned stack object + public native ArrayStack Clone(); + // Pushes a value onto the end of the stack, adding a new index. // // This may safely be used even if the stack has a blocksize