ArrayStack: add Clone method ()

* Provide ArrayStack.Clone method

* Clean definition for old syntax.

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
This commit is contained in:
Ҝℴţأķ 2020-07-09 08:59:17 +05:00 committed by GitHub
parent 4a4b9ce7f0
commit 611bad4036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions
core/logic
plugins/include

View File

@ -83,6 +83,32 @@ static cell_t CreateStack(IPluginContext *pContext, const cell_t *params)
return hndl; 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) static cell_t PushStackCell(IPluginContext *pContext, const cell_t *params)
{ {
CellArray *array; CellArray *array;
@ -387,6 +413,7 @@ static cell_t GetStackBlockSize(IPluginContext *pContext, const cell_t *params)
REGISTER_NATIVES(cellStackNatives) REGISTER_NATIVES(cellStackNatives)
{ {
{"CreateStack", CreateStack}, {"CreateStack", CreateStack},
{"CloneStack", CloneStack},
{"IsStackEmpty", IsStackEmpty}, {"IsStackEmpty", IsStackEmpty},
{"PopStackArray", PopStackArray}, {"PopStackArray", PopStackArray},
{"PopStackCell", PopStackCell}, {"PopStackCell", PopStackCell},
@ -398,6 +425,7 @@ REGISTER_NATIVES(cellStackNatives)
// Transitional syntax support. // Transitional syntax support.
{"ArrayStack.ArrayStack", CreateStack}, {"ArrayStack.ArrayStack", CreateStack},
{"ArrayStack.Clone", CloneStack},
{"ArrayStack.Pop", ArrayStack_Pop}, {"ArrayStack.Pop", ArrayStack_Pop},
{"ArrayStack.PopString", ArrayStack_PopString}, {"ArrayStack.PopString", ArrayStack_PopString},
{"ArrayStack.PopArray", ArrayStack_PopArray}, {"ArrayStack.PopArray", ArrayStack_PopArray},

View File

@ -54,6 +54,14 @@ methodmap ArrayStack < Handle
// new Array[X][32] // new Array[X][32]
public native ArrayStack(int blocksize=1); 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. // 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 // This may safely be used even if the stack has a blocksize