Tcl_Namespace(3tcl)

πŸŽ€ Tcl_Namespace(3tcl) πŸŽ€

okay so bestie, welcome to the Tcl Library Procedures page for namespace manipulation. This is where all the C-level namespace functions live: creating, deleting, searching, importing, exporting, and generally making namespaces behave. πŸ’…


✨ NAME ✨

Tcl_AppendExportList, Tcl_CreateNamespace, Tcl_DeleteNamespace, Tcl_Export, Tcl_FindCommand, Tcl_FindNamespace, Tcl_ForgetImport, Tcl_GetCurrentNamespace, Tcl_GetGlobalNamespace, Tcl_GetNamespaceUnknownHandler, Tcl_Import, Tcl_SetNamespaceUnknownHandler β€” manipulate namespaces.


🌈 SYNOPSIS 🌈

#include <tcl.h>

Tcl_Namespace *
Tcl_CreateNamespace(interp, name, clientData, deleteProc)

Tcl_DeleteNamespace(nsPtr)

int
Tcl_AppendExportList(interp, nsPtr, objPtr)

int
Tcl_Export(interp, nsPtr, pattern, resetListFirst)

int
Tcl_Import(interp, nsPtr, pattern, allowOverwrite)

int
Tcl_ForgetImport(interp, nsPtr, pattern)

Tcl_Namespace *
Tcl_GetCurrentNamespace(interp)

Tcl_Namespace *
Tcl_GetGlobalNamespace(interp)

Tcl_Namespace *
Tcl_FindNamespace(interp, name, contextNsPtr, flags)

Tcl_Command
Tcl_FindCommand(interp, name, contextNsPtr, flags)

Tcl_Obj *
Tcl_GetNamespaceUnknownHandler(interp, nsPtr)

int
Tcl_SetNamespaceUnknownHandler(interp, nsPtr, handlerPtr)

🫢 ARGUMENTS 🫢

  • Tcl_Interp *interp (in/out) β€” the interpreter in which the namespace exists and where name lookups are performed. Also where error result messages are written. πŸ’–
  • const char *name (in) β€” the name of the namespace or command to be created or accessed. ✨
  • void *clientData (in) β€” a context pointer by the creator of the namespace. Tcl does not interpret it at all. πŸ™ˆ
  • Tcl_NamespaceDeleteProc *deleteProc (in) β€” a pointer to function to call when the namespace is deleted, or NULL if no such callback is to be performed. πŸ’€
  • Tcl_Namespace *nsPtr (in) β€” the namespace to be manipulated, or NULL (for other than Tcl_DeleteNamespace) to manipulate the current namespace. πŸ‘‘
  • Tcl_Obj *objPtr (out) β€” a reference to an unshared value to which the function output will be written. πŸŽ€
  • const char *pattern (in) β€” the glob-style pattern (see Tcl_StringMatch) that describes the commands to be imported or exported. πŸ¦‹
  • int resetListFirst (in) β€” whether the list of export patterns should be reset before adding the current pattern to it. 🌸
  • int allowOverwrite (in) β€” whether new commands created by this import action can overwrite existing commands. πŸ”₯
  • Tcl_Namespace *contextNsPtr (in) β€” the location in the namespace hierarchy where the search for a namespace or command should be conducted relative to when the search term is not rooted at the global namespace. NULL indicates the current namespace. πŸ’œ
  • int flags (in) β€” OR-ed combination of bits controlling how the search is to be performed. The following flags are supported:
    • TCL_GLOBAL_ONLY β€” indicates that the search is always to be conducted relative to the global namespace. 🌍
    • TCL_NAMESPACE_ONLY β€” just for Tcl_FindCommand; indicates that the search is always to be conducted relative to the context namespace. 🏠
    • TCL_LEAVE_ERR_MSG β€” indicates that an error message should be left in the interpreter if the search fails. 😭
  • Tcl_Obj *handlerPtr (in) β€” a script fragment to be installed as the unknown command handler for the namespace, or NULL to reset the handler to its default. ⭐

πŸ’– DESCRIPTION πŸ’–

okay so namespaces are hierarchic naming contexts that can contain commands and variables. They also maintain a list of patterns that describes what commands are exported, and can import commands that have been exported by other namespaces. Namespaces can also be manipulated through the Tcl command namespace. 🫢

The Tcl_Namespace structure encapsulates a namespace, and is guaranteed to have the following fields in it: name (the local name of the namespace, with no namespace separator characters in it, with empty denoting the global namespace), fullName (the fully specified name of the namespace), clientData, deleteProc (the values specified in the call to Tcl_CreateNamespace), and parentPtr (a pointer to the containing namespace, or NULL for the global namespace.) πŸ‘‘

Tcl_CreateNamespace creates a new namespace. The deleteProc will have the following type signature:

typedef void Tcl_NamespaceDeleteProc(
        void *clientData);

Tcl_DeleteNamespace deletes a namespace, calling the deleteProc defined for the namespace (if any). πŸ’€

Tcl_AppendExportList retrieves the export patterns for a namespace given namespace and appends them (as list items) to objPtr. πŸ“

Tcl_Export sets and appends to the export patterns for a namespace. Patterns are appended unless the resetListFirst flag is true. ✨

Tcl_Import imports commands matching a pattern into a namespace. Note that the pattern must include the name of the namespace to import from. This function returns TCL_ERROR if an attempt to import a command over an existing command is made, unless the allowOverwrite flag has been set. 🚨

Tcl_ForgetImport removes imports matching a pattern. 🧹

Tcl_GetCurrentNamespace returns the current namespace for an interpreter. πŸ“

Tcl_GetGlobalNamespace returns the global namespace for an interpreter. 🌍

Tcl_FindNamespace searches for a namespace named name within the context of the namespace contextNsPtr. If the namespace cannot be found, NULL is returned. πŸ”

Tcl_FindCommand searches for a command named name within the context of the namespace contextNsPtr. If the command cannot be found, NULL is returned. πŸ”Ž

Tcl_GetNamespaceUnknownHandler returns the unknown command handler for the namespace, or NULL if none is set. πŸ‘»

Tcl_SetNamespaceUnknownHandler sets the unknown command handler for the namespace. If handlerPtr is NULL, then the handler is reset to its default. πŸ’…


πŸ”₯ REFERENCE COUNT MANAGEMENT πŸ”₯

okay bestie this part is IMPORTANT, like, pay attention because refcounts are the vibe check of C extension life πŸ’…

  • the objPtr argument you hand to Tcl_AppendExportList should be an unshared object, because that function is gonna modify it. and the reference count of objPtr? not altered. nope. she stays exactly as she was. πŸ™ˆ
  • Tcl_GetNamespaceUnknownHandler gives you back a value that might be shared. so if you wanna keep that value around, you gotta increment its reference count yourself. no one's doing it for you, bestie 😭
  • the handlerPtr argument to Tcl_SetNamespaceUnknownHandler? if it's a non-empty list</>, its reference count will get incremented. that one's on the function, not you 🫢
fr fr, do not sleep on this section. refcount mistakes are the lowkey villain of every Tcl extension, and they will not hesitate to eat your memory alive πŸ’€

🌈 SEE ALSO 🌈

go read those too, they're literally the squad πŸŽ€


⭐ KEYWORDS ⭐

namespace, command


and that's the whole page, bestie!! πŸ’– you now know namespaces in Tcl better than most people who've been writing C extensions since before you were born, no cap πŸ‘‘

this lil masterpiece lives in Tcl 8.5, and its whole government name is Tcl_Namespace(3tcl) β€” go forth and slay those namespaces, mwah πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from