Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 176aa2348643839a84ca2f1491d961a7bd31c9c89b650dd9311a0f0a27cb3315
Ticket: e58d7e19e937496f179fe711345b415f3f3d31b2
Upwards compatibility of TclObjTypeHasProc()
User & Date: apnadkarni on 2023-07-28 09:38:28
Changes

  1. icomment:
    The rationale of TIP 644 was to permit the `Tcl_ObjType` to be extended within **minor** versions of Tcl and allow Tcl **extensions** to work **without needing recompilation**. This is how the Tcl_Channel works as Jan described previously.
    
    Now both models of versioning being debated need to support the above requirement.
    
    Suppose in 9.1 a new method `Tcl_DictObjLookupProc *dictLookup` were added to `Tcl_ObjType` at the end of the struct with version bumped. What would be needed for the following call to work
    
    ```
    TclObjTypeHasProc(objPtr, dictLookup);
    ```
    
    Jan's version needs no modification at all that I can see.
    
    On the other hand, the macro in the trunk will fail in its current form as it does not check for V3 and has to be modified. (I will ignore the small bit of work having to update all the `TCL_OBJTYPE_VX` macros.) A redefinition like
    
    ```
    #define TclObjTypeHasProc(objPtr, proc)		\
        (((objPtr)->typePtr				\
          && (   (objPtr)->typePtr->version == 1	\
    	  || (objPtr)->typePtr->version == 2))	\
    	  || (objPtr)->typePtr->version == 3))	\
         ?	((objPtr)->typePtr)->proc		\
         : NULL)
    ```
    
    will not only begin to look ugly as version 4, 5, etc are added and has performance implications on every call, but the bigger problem is that it is not correct.
    
    Consider an extension **built for 9.0** that has a V2 `Tcl_ObjType` loaded into Tcl 9.1. When the macro `TclObjTypeHasProc(objPtr, dictLookup)` (within Tcl 9.1 core, not the extension) is called, it is going to see version as 2, and thus check `objPtr->typePtr->proc` which is a garbage location (remembering that the size of structure in the extension is the 9.0 V2 size, NOT the 9.1 size and the 9.0 `TCL_OBJTYPE_V2` macro, not the 9.1, would have been used to initialize it). 
    
    The problem is that `proc` values that are defined only in V3 should only be checked if the version is V3 but the preprocessor will not let you do that (remember neither objPtr nor proc are constants) in any way that I can see.
    
    The only way out then is to have a separate macro for each version of `Tcl_ObjType` - `TclObjTypeHasProcV1`, `TclObjTypeHasProcV2` etc. that do checks specific to the methods defined for that version. This is more work and more complicated for the programmer who now has to track which macro needs to be used for which method call. And for no functional benefit.
    
    Note this problem already exists between V0 V1 V2 except that there are no extensions out there built with older versioned structures.
    
    In terms of limiting changes to `Tcl_ObjType` to only adding functions at the end, that is a given for exactly the same reasons as above. Unless one is willing to write separate macros for each version. I don't see adding at the end limiting in any significant fashion anyways for many reasons.
    
    Thus I don't currently see the current trunk definition as being workable.
    
    One final note about Brian's comment "There's no guarantee that the address after the end in the older compiled code is a valid address." - I do not see how that macro accesses a potentially invalid address at all. It is only calculating structure offsets which is perfectly legitimate.
    
    /Ashok
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"