Skip to content
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

Convert IMAP resource to object #6418

Closed
wants to merge 3 commits into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Nov 10, 2020

Might be sub-optimal, also I'm having an issue with imap_close() on how to propagate the EXPUNGE flag, so if anyone has an idea.

@cmb69
Copy link
Member

cmb69 commented Nov 10, 2020

Maybe require users to call imap_expunge() explicitly?

@Girgias Girgias force-pushed the imap-resource-to-object branch from 8fd905f to 8f66db9 Compare November 10, 2020 12:15
@Girgias
Copy link
Member Author

Girgias commented Nov 10, 2020

Maybe require users to call imap_expunge() explicitly?

I figured it out, was trying to dereference a NULL pointer or something alike. Should be working now.

@Girgias Girgias force-pushed the imap-resource-to-object branch from 8f66db9 to 9dd9f90 Compare November 11, 2020 13:25
@Girgias Girgias force-pushed the imap-resource-to-object branch from 9dd9f90 to ea99c00 Compare November 30, 2020 15:19
{
pils *imap_le_struct = (pils *)rsrc->ptr;
static inline zend_object *imap_object_to_zend_object(php_imap_object *obj) {
return ((zend_object*)(obj + 1)) - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These pointers are supposed to have the same size, so this could be simplified to return (zend_object*)obj. That looks wrong, though. Why not return obj->std?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NGL, I have no idea, I just copy pasted this from Sara's PR converting (IIRC) FTP resources to object and it was doing that.
But if it can be simplified than I'll do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're not the same size. sizeof(php_imap_object) is larger than sizeof(zend_object) by definition because the former contains an instance of the latter.

|-------------- php_imap_object ----------------|
|-- imap specific data --|-- zend_object data --|

obj+1 takes us from the left edge of the above diagram to the right edge, then the cast followed by the -1 brings us into the middle of it (and thus the start of the zend_object struct).

We could do return &(obj->std); but when reversing this there's no such convenient expression, so I like to use the math which is visibly reversible in both helpers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course! Thanks for the explanation. :)

if (!(imap_le_struct->flags & OP_PROTOTYPE)) {
mail_close_full(imap_le_struct->imap_stream, imap_le_struct->flags);
static inline php_imap_object *imap_object_from_zend_object(zend_object *zobj) {
return ((php_imap_object*)(zobj + 1)) - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has basically the same issue as above, but to convert from zend_object* to php_imap_object* one usually uses the XtOffsetOf macro.

Copy link
Contributor

@sgolemon sgolemon Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again, because they're not the same size structs, the same answer applies.

XtOffsetOf() does the same math presented in my boilerplate helpers, but presents the (honestly kinda ugly) hack of intermediately casting to char*. c'mon, that's gross.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's how it's documented. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh. Lots of documentation is wrong about lots of things. ;)

Copy link
Contributor

@sgolemon sgolemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only real change needed is the errantly changed key names, everything else is suggestive.

ext/imap/php_imap.c Outdated Show resolved Hide resolved
RETURN_FALSE;
}

if (imap_le_struct->imap_stream && imap_le_struct->imap_stream->mailbox) {
if (imap_conn_struct->imap_stream && imap_conn_struct->imap_stream->mailbox) {
rfc822_date(date);
object_init(return_value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A subject for a future RFC and not something we should add here, but we should consider making this return value a formal type with declared/typed properties.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, you mean adding an object e.g. IMAPMailboxProperties which would have the values as fixed properties instead of returning stdClass, correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But again, something for a future project and shouldn't be mixed in with this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, wouldn't want to take this longer to land.

ext/imap/php_imap.c Outdated Show resolved Hide resolved
ext/imap/php_imap.c Outdated Show resolved Hide resolved
ext/imap/php_imap.c Outdated Show resolved Hide resolved
ext/imap/php_imap.c Outdated Show resolved Hide resolved
ext/imap/php_imap.c Outdated Show resolved Hide resolved
ext/imap/php_imap.stub.php Show resolved Hide resolved
@Girgias Girgias force-pushed the imap-resource-to-object branch from ea99c00 to fe46b60 Compare December 21, 2020 23:15
Copy link
Contributor

@sgolemon sgolemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@php-pulls php-pulls closed this in 383779e Dec 22, 2020
@Girgias Girgias deleted the imap-resource-to-object branch December 22, 2020 02:07
@Ayesh
Copy link
Member

Ayesh commented Dec 23, 2020

I submitted #6534 as a supplement for the constructor and final flag in the stub. Thanks for the great work @Girgias 🙏🏼.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants