Skip to content

Add macros to simplify handling of internal structs#1965

Open
alcaeus wants to merge 4 commits intomongodb:v2.xfrom
alcaeus:simplify-intern-declarations
Open

Add macros to simplify handling of internal structs#1965
alcaeus wants to merge 4 commits intomongodb:v2.xfrom
alcaeus:simplify-intern-declarations

Conversation

@alcaeus
Copy link
Copy Markdown
Member

@alcaeus alcaeus commented Mar 27, 2026

Following the changes made in #1959, this PR introduces additional macros to handle declarations of the intern variables used throughout and working with $this and 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.

@alcaeus alcaeus requested a review from a team as a code owner March 27, 2026 15:15
@alcaeus alcaeus requested review from Copilot and paulinevos and removed request for a team March 27, 2026 15:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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, and PHONGO_INTERN_INIT_EX macros to centralize internal struct access/allocation.
  • Refactored many BSON/Driver/Monitoring classes to use the new macros instead of per-type Z_*_OBJ_P helpers and manual object_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 a WriteError object. That is a type mismatch and can lead to invalid memory access in the destructor. Use PHONGO_INTERN_FROM_Z_OBJ(writeerror, object); here so intern has the correct struct type.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +507 to 511
zval retval = ZVAL_STATIC_INIT;
*is_temp = 1;

array_init(&retval);

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
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.

2 participants