Skip to content

Commit

Permalink
Use zval_array<N> to pass parameters via method_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerGee committed Dec 12, 2020
1 parent 0d11518 commit a064b8f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
9 changes: 7 additions & 2 deletions php-callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
#include "php-callback.h"
using namespace php_git2;

int php_git2::php_git2_invoke_callback(zval* func,zval* ret,int paramCount,zval params[])
int php_git2::php_git2_invoke_callback(
zval* obj,
zval* func,
zval* ret,
int paramCount,
zval params[])
{
php_bailer bailer;
php_bailout_context ctx(bailer);
Expand All @@ -21,7 +26,7 @@ int php_git2::php_git2_invoke_callback(zval* func,zval* ret,int paramCount,zval
if (BAILOUT_ENTER_REGION(ctx)) {
int retval;

retval = call_user_function(NULL,NULL,func,ret,paramCount,params);
retval = call_user_function(NULL,obj,func,ret,paramCount,params);
if (retval == FAILURE) {
php_git2_giterr_set(GITERR_INVALID,"Failed to invoke userspace callback");
result = GIT_EPHPFATAL;
Expand Down
27 changes: 22 additions & 5 deletions php-callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

namespace php_git2
{
int php_git2_invoke_callback(zval* func,zval* ret,int paramCount,zval params[]);
int php_git2_invoke_callback(
zval* obj,
zval* func,
zval* ret,
int paramCount,
zval params[]);

// Provide a type that contains an array of zvals converted from primative
// values.
Expand Down Expand Up @@ -47,21 +52,28 @@ namespace php_git2
template<unsigned I,typename... Ts>
void assign(long h,Ts&&... ts)
{
ZVAL_LONG(params + I,h);
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}

template<unsigned I,typename... Ts>
void assign(int h,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}

template<unsigned I,typename... Ts>
void assign(unsigned int h,Ts&&... ts)
{
ZVAL_LONG(params + I,static_cast<long>(h));
ZVAL_LONG(params + I,static_cast<zend_long>(h));
assign<I+1>(std::forward<Ts>(ts)...);
}

template<unsigned I,typename... Ts>
void assign(size_t sz,Ts&&... ts)
{
ZVAL_LONG(params + I,sz);
ZVAL_LONG(params + I,static_cast<zend_long>(sz));
assign<I+1>(std::forward<Ts>(ts)...);
}

Expand Down Expand Up @@ -110,7 +122,12 @@ namespace php_git2

int call(zval* func,zval* ret)
{
return php_git2_invoke_callback(func,ret,Count,params);
return php_git2_invoke_callback(nullptr,func,ret,Count,params);
}

int call(zval* obj,zval* func,zval* ret)
{
return php_git2_invoke_callback(obj,func,ret,Count,params);
}

zval* operator [](unsigned index)
Expand Down
53 changes: 13 additions & 40 deletions php-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,47 +185,20 @@ namespace php_git2
zval_dtor(&zretval);
}

int call(int nparams = 0,zval* params[] = nullptr)
{
php_bailer bailer;
php_bailout_context ctx(bailer);
int result = GIT_OK;

if (BAILOUT_ENTER_REGION(ctx)) {
int retval;

retval = call_user_function(
NULL,
object_wrapper::thisobj(),
&zmethod,
&zretval,
nparams,
params);

if (retval == FAILURE) {
php_git2_giterr_set(GITERR_INVALID,"Failed to invoke userspace method");
result = GIT_EPHPFATAL;
}
else {
php_exception_wrapper ex;

// Handle case where PHP userspace threw an exception.
if (ex.has_exception()) {
ex.set_giterr();
result = GIT_EPHPEXPROP;
}
}
}
else {
// Set a libgit2 error for completeness.
php_git2_giterr_set(GITERR_INVALID,"PHP reported a fatal error");

// Allow the fatal error to propogate.
result = GIT_EPHPFATALPROP;
bailer.handled();
}
int call()
{
return php_git2_invoke_callback(
object_wrapper::thisobj(),
&zmethod,
&zretval,
0,
nullptr);
}

return result;
template<unsigned Count>
int call(zval_array<Count>& params)
{
return params.call(object_wrapper::thisobj(),&zmethod,&zretval);
}

zval* retval()
Expand Down

0 comments on commit a064b8f

Please sign in to comment.