Add macros to simplify handling of internal structs#1965
Add macros to simplify handling of internal structs#1965alcaeus wants to merge 4 commits intomongodb:v2.xfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a small set of new helper macros to standardize how internal intern structs are fetched/initialized, and refactors many classes to use those macros for $this handling and object initialization.
Changes:
- Added
PHONGO_INTERN_FROM_*,PHONGO_INTERN_OBJECT_ALLOC, andPHONGO_INTERN_INIT_EXmacros to centralize internal struct access/allocation. - Refactored many BSON/Driver/Monitoring classes to use the new macros instead of per-type
Z_*_OBJ_Phelpers and manualobject_init_ex+ fetch patterns. - Simplified several object creation/debug/free paths by replacing repeated boilerplate allocations/fetches.
Reviewed changes
Copilot reviewed 50 out of 50 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/phongo_classes.h | Adds macros for fetching/allocating/init’ing intern structs. |
| src/phongo_bson_encode.c | Replaces direct Z_*_OBJ_P intern fetches with macros during BSON encoding. |
| src/phongo_bson.c | Uses PHONGO_INTERN_INIT_EX for typemap BSON document/array initialization. |
| src/MongoDB/WriteResult.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/WriteError.c | Refactors $this/object/free/debug intern fetch & init to macros (contains a type mismatch bug). |
| src/MongoDB/WriteConcernError.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/MongoDB/WriteConcern.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/MongoDB/TopologyDescription.c | Refactors $this/object/free intern fetch & init to macros. |
| src/MongoDB/Session.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/ServerDescription.c | Refactors $this/object/free intern fetch & init to macros. |
| src/MongoDB/ServerApi.c | Refactors $this/object/free intern fetch & init to macros. |
| src/MongoDB/Server.c | Refactors $this/object/free intern fetch & allocation to macros. |
| src/MongoDB/ReadPreference.c | Refactors $this/object/free intern fetch & init to macros. |
| src/MongoDB/ReadConcern.c | Refactors $this/object/free intern fetch & init to macros. |
| src/MongoDB/Query.c | Refactors object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/TopologyOpeningEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/TopologyClosedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/TopologyChangedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerOpeningEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerHeartbeatStartedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerHeartbeatFailedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerClosedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/ServerChangedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/CommandSucceededEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/CommandStartedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Monitoring/CommandFailedEvent.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Manager.c | Refactors $this/object/free intern fetch & allocation to macros. |
| src/MongoDB/Cursor.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/Command.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/ClientEncryption.c | Refactors $this/object/free intern fetch & allocation to macros (contains a missing intern fetch bug in debug info). |
| src/MongoDB/BulkWriteCommandResult.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/MongoDB/BulkWriteCommand.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/MongoDB/BulkWrite.c | Refactors $this/object/free/debug intern fetch & allocation to macros. |
| src/BSON/Undefined.c | Refactors object/free intern allocation/fetch to macros. |
| src/BSON/UTCDateTime.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Timestamp.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Symbol.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Regex.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/PackedArray.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/ObjectId.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/MinKey.c | Refactors object/free intern allocation/fetch to macros. |
| src/BSON/MaxKey.c | Refactors object/free intern allocation/fetch to macros. |
| src/BSON/Javascript.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Iterator.c | Refactors $this/object/free/debug intern fetch & init to macros (including iterator handlers). |
| src/BSON/Int64.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Document.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Decimal128.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/DBPointer.c | Refactors $this/object/free/debug intern fetch & init to macros. |
| src/BSON/Binary.c | Refactors $this/object/free/debug intern fetch & init to macros, including vector helpers. |
Comments suppressed due to low confidence (1)
src/MongoDB/WriteError.c:1
- This fetches a
phongo_writeconcernerror_t*for aWriteErrorobject. That is a type mismatch and can lead to invalid memory access in the destructor. UsePHONGO_INTERN_FROM_Z_OBJ(writeerror, object);here sointernhas the correct struct type.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| zval retval = ZVAL_STATIC_INIT; | ||
| *is_temp = 1; | ||
|
|
||
| array_init(&retval); | ||
|
|
There was a problem hiding this comment.
phongo_clientencryption_get_debug_info() no longer fetches intern (the previous intern = Z_OBJ_CLIENTENCRYPTION(object); was removed), but the function almost certainly still needs intern to populate debug info. This will either fail to compile (if intern is referenced later) or produce incomplete debug output. Add PHONGO_INTERN_FROM_Z_OBJ(clientencryption, object); near the top of this function and use intern as before.
Following the changes made in #1959, this PR introduces additional macros to handle declarations of the
internvariables used throughout and working with$thisand object initialisation.@paulinevos and @GromNaN, I'll let you review and decide whether you find the code more legible with the macros or if you prefer the current style.