From 611bad40369dd16aeecbc2b7370fc2397f9ae987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D2=9C=E2=84=B4=C5=A3=D8=A3=C4=B7?= <40791562+TheByKotik@users.noreply.github.com> Date: Thu, 9 Jul 2020 08:59:17 +0500 Subject: [PATCH] ArrayStack: add Clone method (#1304) * Provide ArrayStack.Clone method * Clean definition for old syntax. Co-authored-by: Kyle Sanderson --- core/logic/smn_adt_stack.cpp | 28 ++++++++++++++++++++++++++++ plugins/include/adt_stack.inc | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/core/logic/smn_adt_stack.cpp b/core/logic/smn_adt_stack.cpp index 670a5ead..7a48d1d2 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 94af6a7f..0f873af1 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