From 0e24bd44bfdeacaed97aa9b1292be5689fca79f1 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 18 Jun 2017 14:01:00 +0000 Subject: Factor out chunk of non-obvious code in the guts of [oo::define] into one place. --- generic/tclOODefineCmds.c | 200 +++++++++++++++++----------------------------- 1 file changed, 74 insertions(+), 126 deletions(-) diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 8747ff5..e953dc0 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -47,8 +47,11 @@ struct DeclaredSlot { static inline void BumpGlobalEpoch(Tcl_Interp *interp, Class *classPtr); static Tcl_Command FindCommand(Tcl_Interp *interp, Tcl_Obj *stringObj, Tcl_Namespace *const namespacePtr); -static void GenerateErrorInfo(Tcl_Interp *interp, Object *oPtr, +static inline void GenerateErrorInfo(Tcl_Interp *interp, Object *oPtr, Tcl_Obj *savedNameObj, const char *typeOfSubject); +static inline int MagicDefinitionInvoke(Tcl_Interp *interp, + Tcl_Namespace *nsPtr, int cmdIndex, + int objc, Tcl_Obj *const *objv); static inline Class * GetClassInOuterContext(Tcl_Interp *interp, Tcl_Obj *className, const char *errMsg); static inline int InitDefineContext(Tcl_Interp *interp, @@ -755,7 +758,7 @@ GetClassInOuterContext( * ---------------------------------------------------------------------- */ -static void +static inline void GenerateErrorInfo( Tcl_Interp *interp, /* Where to store the error info trace. */ Object *oPtr, /* What object (or class) was being configured @@ -787,6 +790,69 @@ GenerateErrorInfo( /* * ---------------------------------------------------------------------- * + * MagicDefinitionInvoke -- + * Part of the implementation of the "oo::define" and "oo::objdefine" + * commands that is used to implement the more-than-one-argument case, + * applying ensemble-like tricks with dispatch so that error messages are + * clearer. Doesn't handle the management of the stack frame. + * + * ---------------------------------------------------------------------- + */ + +static inline int +MagicDefinitionInvoke( + Tcl_Interp *interp, + Tcl_Namespace *nsPtr, + int cmdIndex, + int objc, + Tcl_Obj *const *objv) +{ + Tcl_Obj *objPtr, *obj2Ptr, **objs; + Tcl_Command cmd; + int isRoot, dummy, result, offset = cmdIndex + 1; + + /* + * More than one argument: fire them through the ensemble processing + * engine so that everything appears to be good and proper in error + * messages. Note that we cannot just concatenate and send through + * Tcl_EvalObjEx, as that doesn't do ensemble processing, and we cannot go + * through Tcl_EvalObjv without the extra work to pre-find the command, as + * that finds command names in the wrong namespace at the moment. Ugly! + */ + + isRoot = TclInitRewriteEnsemble(interp, offset, 1, objv); + + /* + * Build the list of arguments using a Tcl_Obj as a workspace. See + * comments above for why these contortions are necessary. + */ + + objPtr = Tcl_NewObj(); + obj2Ptr = Tcl_NewObj(); + cmd = FindCommand(interp, objv[cmdIndex], nsPtr); + if (cmd == NULL) { + /* punt this case! */ + Tcl_AppendObjToObj(obj2Ptr, objv[cmdIndex]); + } else { + Tcl_GetCommandFullName(interp, cmd, obj2Ptr); + } + Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr); + /* TODO: overflow? */ + Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc-offset, objv+offset); + Tcl_ListObjGetElements(NULL, objPtr, &dummy, &objs); + + result = Tcl_EvalObjv(interp, objc-cmdIndex, objs, TCL_EVAL_INVOKE); + if (isRoot) { + TclResetRewriteEnsemble(interp, 1); + } + Tcl_DecrRefCount(objPtr); + + return result; +} + +/* + * ---------------------------------------------------------------------- + * * TclOODefineObjCmd -- * Implementation of the "oo::define" command. Works by effectively doing * the same as 'namespace eval', but with extra magic applied so that the @@ -805,8 +871,8 @@ TclOODefineObjCmd( Tcl_Obj *const *objv) { Foundation *fPtr = TclOOGetFoundation(interp); - int result; Object *oPtr; + int result; if (objc < 3) { Tcl_WrongNumArgs(interp, 1, objv, "className arg ?arg ...?"); @@ -846,46 +912,7 @@ TclOODefineObjCmd( } TclDecrRefCount(objNameObj); } else { - Tcl_Obj *objPtr, *obj2Ptr, **objs; - Tcl_Command cmd; - int isRoot, dummy; - - /* - * More than one argument: fire them through the ensemble processing - * engine so that everything appears to be good and proper in error - * messages. Note that we cannot just concatenate and send through - * Tcl_EvalObjEx, as that doesn't do ensemble processing, and we - * cannot go through Tcl_EvalObjv without the extra work to pre-find - * the command, as that finds command names in the wrong namespace at - * the moment. Ugly! - */ - - isRoot = TclInitRewriteEnsemble(interp, 3, 1, objv); - - /* - * Build the list of arguments using a Tcl_Obj as a workspace. See - * comments above for why these contortions are necessary. - */ - - objPtr = Tcl_NewObj(); - obj2Ptr = Tcl_NewObj(); - cmd = FindCommand(interp, objv[2], fPtr->defineNs); - if (cmd == NULL) { - /* punt this case! */ - Tcl_AppendObjToObj(obj2Ptr, objv[2]); - } else { - Tcl_GetCommandFullName(interp, cmd, obj2Ptr); - } - Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr); - /* TODO: overflow? */ - Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc-3, objv+3); - Tcl_ListObjGetElements(NULL, objPtr, &dummy, &objs); - - result = Tcl_EvalObjv(interp, objc-2, objs, TCL_EVAL_INVOKE); - if (isRoot) { - TclResetRewriteEnsemble(interp, 1); - } - Tcl_DecrRefCount(objPtr); + result = MagicDefinitionInvoke(interp, fPtr->defineNs, 2, objc, objv); } DelRef(oPtr); @@ -918,8 +945,8 @@ TclOOObjDefObjCmd( Tcl_Obj *const *objv) { Foundation *fPtr = TclOOGetFoundation(interp); - int isRoot, result; Object *oPtr; + int result; if (objc < 3) { Tcl_WrongNumArgs(interp, 1, objv, "objectName arg ?arg ...?"); @@ -952,47 +979,7 @@ TclOOObjDefObjCmd( } TclDecrRefCount(objNameObj); } else { - Tcl_Obj *objPtr, *obj2Ptr, **objs; - Tcl_Command cmd; - int dummy; - - /* - * More than one argument: fire them through the ensemble processing - * engine so that everything appears to be good and proper in error - * messages. Note that we cannot just concatenate and send through - * Tcl_EvalObjEx, as that doesn't do ensemble processing, and we - * cannot go through Tcl_EvalObjv without the extra work to pre-find - * the command, as that finds command names in the wrong namespace at - * the moment. Ugly! - */ - - isRoot = TclInitRewriteEnsemble(interp, 3, 1, objv); - - /* - * Build the list of arguments using a Tcl_Obj as a workspace. See - * comments above for why these contortions are necessary. - */ - - objPtr = Tcl_NewObj(); - obj2Ptr = Tcl_NewObj(); - cmd = FindCommand(interp, objv[2], fPtr->objdefNs); - if (cmd == NULL) { - /* punt this case! */ - Tcl_AppendObjToObj(obj2Ptr, objv[2]); - } else { - Tcl_GetCommandFullName(interp, cmd, obj2Ptr); - } - Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr); - /* TODO: overflow? */ - Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc-3, objv+3); - Tcl_ListObjGetElements(NULL, objPtr, &dummy, &objs); - - result = Tcl_EvalObjv(interp, objc-2, objs, TCL_EVAL_INVOKE); - - if (isRoot) { - TclResetRewriteEnsemble(interp, 1); - } - Tcl_DecrRefCount(objPtr); + result = MagicDefinitionInvoke(interp, fPtr->objdefNs, 2, objc, objv); } DelRef(oPtr); @@ -1025,8 +1012,8 @@ TclOODefineSelfObjCmd( Tcl_Obj *const *objv) { Foundation *fPtr = TclOOGetFoundation(interp); - int result; Object *oPtr; + int result; if (objc < 2) { Tcl_WrongNumArgs(interp, 1, objv, "arg ?arg ...?"); @@ -1059,46 +1046,7 @@ TclOODefineSelfObjCmd( } TclDecrRefCount(objNameObj); } else { - Tcl_Obj *objPtr, *obj2Ptr, **objs; - Tcl_Command cmd; - int isRoot, dummy; - - /* - * More than one argument: fire them through the ensemble processing - * engine so that everything appears to be good and proper in error - * messages. Note that we cannot just concatenate and send through - * Tcl_EvalObjEx, as that doesn't do ensemble processing, and we - * cannot go through Tcl_EvalObjv without the extra work to pre-find - * the command, as that finds command names in the wrong namespace at - * the moment. Ugly! - */ - - isRoot = TclInitRewriteEnsemble(interp, 2, 1, objv); - - /* - * Build the list of arguments using a Tcl_Obj as a workspace. See - * comments above for why these contortions are necessary. - */ - - objPtr = Tcl_NewObj(); - obj2Ptr = Tcl_NewObj(); - cmd = FindCommand(interp, objv[1], fPtr->objdefNs); - if (cmd == NULL) { - /* punt this case! */ - Tcl_AppendObjToObj(obj2Ptr, objv[1]); - } else { - Tcl_GetCommandFullName(interp, cmd, obj2Ptr); - } - Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr); - /* TODO: overflow? */ - Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc-2, objv+2); - Tcl_ListObjGetElements(NULL, objPtr, &dummy, &objs); - - result = Tcl_EvalObjv(interp, objc-1, objs, TCL_EVAL_INVOKE); - if (isRoot) { - TclResetRewriteEnsemble(interp, 1); - } - Tcl_DecrRefCount(objPtr); + result = MagicDefinitionInvoke(interp, fPtr->objdefNs, 1, objc, objv); } DelRef(oPtr); -- cgit v0.12 From 0d73e7f26411020d1345814bcdb9fe78272335ff Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 22 Jun 2017 08:15:13 +0000 Subject: Add test-cases, testing the legacy behavior of "format %#d" --- doc/GetInt.3 | 2 +- tests/format.test | 19 +++++++++++++++++++ tests/util.test | 6 +++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/GetInt.3 b/doc/GetInt.3 index 3e7204c..5a3304a 100644 --- a/doc/GetInt.3 +++ b/doc/GetInt.3 @@ -4,7 +4,7 @@ '\" '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. -'\" +'\" .TH Tcl_GetInt 3 "" Tcl "Tcl Library Procedures" .so man.macros .BS diff --git a/tests/format.test b/tests/format.test index 9afedd9..2795ac2 100644 --- a/tests/format.test +++ b/tests/format.test @@ -78,6 +78,25 @@ test format-1.11.1 {integer formatting} longIs64bit { test format-1.12 {integer formatting} { format "%b %#b %#b %llb" 5 0 5 [expr {2**100}] } {101 0b0 0b101 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000} +test format-1.13 {integer formatting} longIs32bit { + format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1 +} {0 6 34 16923 -12} +test format-1.13.1 {integer formatting} longIs64bit { + format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1 +} {0 6 34 16923 -12} +test format-1.14 {integer formatting} longIs32bit { + format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 +} { 0 6 34 16923 -12} +test format-1.14.1 {integer formatting} longIs64bit { + format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 +} { 0 6 34 16923 -12} +test format-1.15 {integer formatting} longIs32bit { + format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1 +} {0 6 34 16923 -12 } +test format-1.15.1 {integer formatting} longIs64bit { + format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1 +} {0 6 34 16923 -12 } + test format-2.1 {string formatting} { format "%s %s %c %s" abcd {This is a very long test string.} 120 x diff --git a/tests/util.test b/tests/util.test index 7782f35..2ac11bf 100644 --- a/tests/util.test +++ b/tests/util.test @@ -208,7 +208,7 @@ test util-4.6 {Tcl_ConcatObj - utf-8 sequence with "whitespace" char} { } \xe0 test util-4.7 {Tcl_ConcatObj - refCount safety} testconcatobj { # Check for Bug #1447328 (actually, bugs in its original "fix"). One of the - # symptoms was Bug #2055782. + # symptoms was Bug #2055782. testconcatobj } {} @@ -566,7 +566,7 @@ test util-9.1.3 {TclGetIntForIndex} { } k test util-9.2.0 {TclGetIntForIndex} { string index abcd end -} d +} d test util-9.2.1 {TclGetIntForIndex} -body { string index abcd { end} } -returnCodes error -match glob -result * @@ -4007,7 +4007,7 @@ test util-17.1 {bankers' rounding [Bug 3349507]} {ieeeFloatingPoint} { } set r } [list {*}{ - 0x43fffffffffffffc 0xc3fffffffffffffc + 0x43fffffffffffffc 0xc3fffffffffffffc 0x43fffffffffffffc 0xc3fffffffffffffc 0x43fffffffffffffd 0xc3fffffffffffffd 0x43fffffffffffffe 0xc3fffffffffffffe -- cgit v0.12