-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/com_dotnet: Use modern HashTable APIs #16208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Girgias
wants to merge
2
commits into
php:master
Choose a base branch
from
Girgias:com-hash-foreach
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,73 +24,54 @@ | |
#include "php_com_dotnet.h" | ||
#include "php_com_dotnet_internal.h" | ||
|
||
/* create an automation SafeArray from a PHP array. | ||
/* create an automation SafeArray from a PHP array (HashTable). | ||
* Only creates a single-dimensional array of variants. | ||
* The keys of the PHP hash MUST be numeric. */ | ||
static void safe_array_from_zval(VARIANT *v, zval *z, int codepage) | ||
static void safe_array_from_hashtable(VARIANT *v, HashTable *ht, int codepage) | ||
{ | ||
SAFEARRAY *sa = NULL; | ||
SAFEARRAYBOUND bound; | ||
HashPosition pos; | ||
int keytype; | ||
zend_string *strindex; | ||
zend_ulong intindex = 0; | ||
VARIANT *va; | ||
zval *item; | ||
|
||
/* find the largest array index, and assert that all keys are integers */ | ||
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(z), &pos); | ||
for (;; zend_hash_move_forward_ex(Z_ARRVAL_P(z), &pos)) { | ||
|
||
keytype = zend_hash_get_current_key_ex(Z_ARRVAL_P(z), &strindex, &intindex, &pos); | ||
if (!zend_array_is_list(ht)) { | ||
// TODO: Make this a ValueError | ||
php_error_docref(NULL, E_WARNING, "COM: converting from PHP array to VARIANT array; only arrays with numeric keys are allowed"); | ||
V_VT(v) = VT_NULL; | ||
return; | ||
} | ||
// TODO: Check not empty? | ||
|
||
if (HASH_KEY_IS_STRING == keytype) { | ||
goto bogus; | ||
} else if (HASH_KEY_NON_EXISTENT == keytype) { | ||
break; | ||
} else if (intindex > UINT_MAX) { | ||
php_error_docref(NULL, E_WARNING, "COM: max number %u of elements in safe array exceeded", UINT_MAX); | ||
break; | ||
} | ||
uint32_t nb_values = zend_hash_num_elements(ht); | ||
if (nb_values > UINT_MAX) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears to be superfluous, since |
||
// TODO: Make this a ValueError? | ||
php_error_docref(NULL, E_WARNING, "COM: max number %u of elements in safe array exceeded", UINT_MAX); | ||
V_VT(v) = VT_NULL; | ||
return; | ||
} | ||
|
||
/* allocate the structure */ | ||
bound.lLbound = 0; | ||
bound.cElements = zend_hash_num_elements(Z_ARRVAL_P(z)); | ||
bound.cElements = nb_values; | ||
sa = SafeArrayCreate(VT_VARIANT, 1, &bound); | ||
|
||
/* get a lock on the array itself */ | ||
SafeArrayAccessData(sa, (void **) &va); | ||
va = (VARIANT*)sa->pvData; | ||
|
||
/* now fill it in */ | ||
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(z), &pos); | ||
for (;; zend_hash_move_forward_ex(Z_ARRVAL_P(z), &pos)) { | ||
if (NULL == (item = zend_hash_get_current_data_ex(Z_ARRVAL_P(z), &pos))) { | ||
break; | ||
zend_ulong index = 0; | ||
zval *value; | ||
ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, index, value) { | ||
if (index < bound.cElements) { | ||
php_com_variant_from_zval(&va[index], value, codepage); | ||
} | ||
zend_hash_get_current_key_ex(Z_ARRVAL_P(z), &strindex, &intindex, &pos); | ||
if (intindex < bound.cElements) { | ||
php_com_variant_from_zval(&va[intindex], item, codepage); | ||
} | ||
} | ||
} ZEND_HASH_FOREACH_END(); | ||
|
||
/* Unlock it and stuff it into our variant */ | ||
SafeArrayUnaccessData(sa); | ||
V_VT(v) = VT_ARRAY|VT_VARIANT; | ||
V_ARRAY(v) = sa; | ||
|
||
return; | ||
|
||
bogus: | ||
php_error_docref(NULL, E_WARNING, "COM: converting from PHP array to VARIANT array; only arrays with numeric keys are allowed"); | ||
|
||
V_VT(v) = VT_NULL; | ||
|
||
if (sa) { | ||
SafeArrayUnlock(sa); | ||
SafeArrayDestroy(sa); | ||
} | ||
} | ||
|
||
static void php_com_variant_from_zval_ex(VARIANT *v, zval *z, int codepage, VARTYPE vt) | ||
|
@@ -142,7 +123,7 @@ static void php_com_variant_from_zval_ex(VARIANT *v, zval *z, int codepage, VART | |
|
||
case IS_ARRAY: | ||
/* map as safe array */ | ||
safe_array_from_zval(v, z, codepage); | ||
safe_array_from_hashtable(v, Z_ARRVAL_P(z), codepage); | ||
break; | ||
|
||
case IS_LONG: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now stricter as before, where arrays with holes could be passed. Since the old behavior doesn't make much sense (would then skip elements at the end), the change might be okay, but the test case would need to be updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll copy the similar function from f90323c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would solve the BC problem, but I'm not sure that's the best way forward.
php-src/ext/com_dotnet/tests/variant_variation.phpt
Line 9 in 6deaaf6
php-src/ext/com_dotnet/tests/variant_variation.phpt
Lines 26 to 28 in 6deaaf6
I mean, that doesn't make sense. Might be better to require users to pass a proper list (can be checked upfront, or just pass
array_values($my_arr)
). At least for PHP 9, this should be done.