Tcl_Namespace(3)

Tcl_Namespace(3) โ€” Tcl Library Procedures โ€” manipulate namespaces ๐Ÿ’…โœจ

okay bestie, welcome to the namespace corner of the Tcl C API. This is Tcl_Namespace(3), and it is literally your group chat for making, finding, importing, exporting, and generally slaying with namespaces. No cap, every function below is here to help you boss around namespaces from C. ๐Ÿซถ


๐Ÿ’– 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 where the namespace exists and where name lookups happen. It's also where error result messages get written. main character interpreter energy.
  • const char *name (in) โ€” the name of the namespace or command you're creating or accessing.
  • void *clientData (in) โ€” a context pointer from whoever created the namespace. Tcl does not interpret it at all.
  • Tcl_NamespaceDeleteProc *deleteProc (in) โ€” a pointer to the function to call when the namespace is deleted, or NULL if you don't want any such callback.
  • Tcl_Namespace *nsPtr (in) โ€” the namespace to be manipulated, or NULL (for everything except Tcl_DeleteNamespace) to manipulate the current namespace.
  • Tcl_Obj *objPtr (out) โ€” a reference to an unshared value where 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 spot in the namespace hierarchy where the search for a namespace or command should happen relative to, when the search term is not rooted at the global namespace. NULL means the current namespace.
  • int flags (in) โ€” an OR-ed combination of bits controlling how the search is performed. The supported flags are:
    • TCL_GLOBAL_ONLY โ€” the search is always conducted relative to the global namespace.
    • TCL_NAMESPACE_ONLY โ€” just for Tcl_FindCommand; the search is always conducted relative to the context namespace.
    • TCL_LEAVE_ERR_MSG โ€” 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 ๐ŸŒธ

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 they can import commands that have been exported by other namespaces. Namespaces can also be manipulated through the Tcl command namespace. iykyk.

The Tcl_Namespace structure encapsulates a namespace, and it is guaranteed to have these fields in it:

  • name โ€” the local name of the namespace, with no namespace separator characters in it, and with empty denoting the global namespace.
  • fullName โ€” the fully specified name of the namespace.
  • clientData โ€” the value specified in the call to Tcl_CreateNamespace.
  • deleteProc โ€” the value specified in the call to Tcl_CreateNamespace.
  • parentPtr โ€” a pointer to the containing namespace, or NULL for the global namespace.

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

typedef void Tcl_NamespaceDeleteProc(
        void *clientData);

Tcl_DeleteNamespace deletes a namespace, calling the deleteProc defined for the namespace (if any). so real, so final.

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. boom, handled.

๐Ÿงฎ REFERENCE COUNT MANAGEMENT โœจ

okay bestie, gather round, this is the part where the refcounts get spicy and if you fumble it your program will simply pass away ๐Ÿ’€ so listen up fr fr

  • the objPtr argument to Tcl_AppendExportList โ€” you gotta hand it an unshared object, okay? not shared, not "it's fine nobody else is looking at it". because this function is gonna modify it. and heads up: the reference count of objPtr will not be altered. like, at all. so don't be counting on it changing, because it won't ๐Ÿ™ˆ
  • Tcl_GetNamespaceUnknownHandler โ€” this one hands you back a value that might be shared. so if you're planning to keep it around (retain it, hold onto it, make it yours ๐Ÿ‘‘), you need to increment its reference count yourself. it's giving "you break it, you bought it" energy but for refcounts ๐Ÿซถ
  • the handlerPtr argument to Tcl_SetNamespaceUnknownHandler โ€” here's the tea: its reference count will get incremented, but only if it's a non-empty list. empty list? no bump. non-empty list? bump. that's the rule, no cap โญ
๐Ÿšจ lowkey important: these aren't suggestions, they're the rules. get them wrong and you're in undefined-behaviour territory and nobody wants that for you ๐Ÿ’–

๐Ÿ” SEE ALSO ๐ŸŒˆ

go read these next, they're the supporting cast and they ate:

  • Tcl_CreateCommand(3)
  • Tcl_ListObjAppendList(3)
  • Tcl_SetVar(3)

๐Ÿ”‘ KEYWORDS ๐Ÿ’œ

the tags, the vibe check, the search terms:

  • namespace
  • command

and that's the whole page, bestie!! this one came from the Tcl crew, page Tcl_Namespace(3), version 8.5 โ€” iconic behaviour honestly, she's been serving since forever and she's still slaying ๐ŸŽ€๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from