Skip to content

Commit

Permalink
Fix PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Oct 30, 2024
1 parent 2266a61 commit 0e5f500
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion php/src/Operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ IcePHP::SyncTypedInvocation::invoke(INTERNAL_FUNCTION_PARAMETERS)
Ice::Context ctx;
if (ZEND_NUM_ARGS() == static_cast<uint32_t>(_op->numParams) + 1)
{
if (!extractStringMap(&args[ZEND_NUM_ARGS() - 1], ctx))
if (!extractContext(&args[ZEND_NUM_ARGS() - 1], ctx))
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions php/src/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ ZEND_METHOD(Ice_ObjectPrx, ice_getContext)
ProxyPtr _this = Wrapper<ProxyPtr>::value(getThis());
assert(_this);

if (!createStringMap(return_value, _this->proxy->ice_getContext()))
if (!createContext(return_value, _this->proxy->ice_getContext()))
{
RETURN_NULL();
}
Expand All @@ -195,7 +195,7 @@ ZEND_METHOD(Ice_ObjectPrx, ice_context)

// Populate the context.
Ice::Context ctx;
if (arr && !extractStringMap(arr, ctx))
if (arr && !extractContext(arr, ctx))
{
RETURN_NULL();
}
Expand Down Expand Up @@ -1474,7 +1474,7 @@ do_cast(INTERNAL_FUNCTION_PARAMETERS, bool check)

// Populate the context.
Ice::Context ctx;
if (arr && !extractStringMap(arr, ctx))
if (arr && !extractContext(arr, ctx))
{
RETURN_NULL();
}
Expand Down
61 changes: 58 additions & 3 deletions php/src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ IcePHP::createStringMap(zval* zv, const map<string, string>& ctx)
{
array_init(zv);

for (map<string, string>::const_iterator p = ctx.begin(); p != ctx.end(); ++p)
for (auto p = ctx.begin(); p != ctx.end(); ++p)
{
add_assoc_stringl_ex(
zv,
Expand All @@ -318,7 +318,63 @@ IcePHP::extractStringMap(zval* zv, map<string, string>& ctx)
}

HashTable* arr = Z_ARRVAL_P(zv);
zend_ulong num_key;
[[maybe_unused]] zend_ulong num_key;
zend_string* key;
zval* val;
ZEND_HASH_FOREACH_KEY_VAL(arr, num_key, key, val)
{
if (!key)
{
invalidArgument("array key must be a string");
return false;
}

if (Z_TYPE_P(val) != IS_STRING)
{
invalidArgument("array value must be a string");
return false;
}

ctx[key->val] = Z_STRVAL_P(val);
}
ZEND_HASH_FOREACH_END();

return true;
}

// TODO: avoid duplication with code above.

bool
IcePHP::createContext(zval* zv, const Ice::Context& ctx)
{
array_init(zv);

for (auto p = ctx.begin(); p != ctx.end(); ++p)
{
add_assoc_stringl_ex(
zv,
const_cast<char*>(p->first.c_str()),
static_cast<uint32_t>(p->first.length()),
const_cast<char*>(p->second.c_str()),
static_cast<uint32_t>(p->second.length()));
}

return true;
}

bool
IcePHP::extractContext(zval* zv, Ice::Context& ctx)
{
if (Z_TYPE_P(zv) != IS_ARRAY)
{
ostringstream os;
os << "expected an associative array but received " << zendTypeToString(Z_TYPE_P(zv));
invalidArgument(os.str());
return false;
}

HashTable* arr = Z_ARRVAL_P(zv);
[[maybe_unused]] zend_ulong num_key;
zend_string* key;
zval* val;
ZEND_HASH_FOREACH_KEY_VAL(arr, num_key, key, val)
Expand All @@ -336,7 +392,6 @@ IcePHP::extractStringMap(zval* zv, map<string, string>& ctx)
}

ctx[key->val] = Z_STRVAL_P(val);
(void)num_key; // Avoids error from older versions of GCC about unused variable num_key.
}
ZEND_HASH_FOREACH_END();

Expand Down
3 changes: 3 additions & 0 deletions php/src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ namespace IcePHP
bool createStringMap(zval*, const std::map<std::string, std::string>&);
bool extractStringMap(zval*, std::map<std::string, std::string>&);

bool createContext(zval*, const Ice::Context&);
bool extractContext(zval*, Ice::Context&);

bool createStringArray(zval*, const Ice::StringSeq&);
bool extractStringArray(zval*, Ice::StringSeq&);

Expand Down

0 comments on commit 0e5f500

Please sign in to comment.