diff options
author | Steve Bennett <steveb@workware.net.au> | 2020-04-16 13:15:04 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2020-04-17 07:49:56 +1000 |
commit | 67703c4ea7b0ae290818d79beeacf931b08ea37e (patch) | |
tree | 02b57352093cca5263ec4c4e03b45439546b7c45 | |
parent | e799d21972cc06d6758cbd0c397df37cf0de0928 (diff) | |
download | jimtcl-67703c4ea7b0ae290818d79beeacf931b08ea37e.zip jimtcl-67703c4ea7b0ae290818d79beeacf931b08ea37e.tar.gz jimtcl-67703c4ea7b0ae290818d79beeacf931b08ea37e.tar.bz2 |
apply: ignore the current namespace
If a namespace is given, it is relative to the global namespace
not the current namespace.
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r-- | jim.c | 33 | ||||
-rw-r--r-- | tests/applyns.test | 16 |
2 files changed, 25 insertions, 24 deletions
@@ -3787,28 +3787,6 @@ static const Jim_HashTableType JimCommandsHashTableType = { #ifdef jim_ext_namespace /** - * Returns the "unscoped" version of the given namespace. - * That is, the fully qualified name without the leading :: - * The returned value is either nsObj, or an object with a zero ref count. - */ -static Jim_Obj *JimQualifyNameObj(Jim_Interp *interp, Jim_Obj *nsObj) -{ - const char *name = Jim_String(nsObj); - if (name[0] == ':' && name[1] == ':') { - /* This command is being defined in the global namespace */ - while (*++name == ':') { - } - nsObj = Jim_NewStringObj(interp, name, -1); - } - else if (Jim_Length(interp->framePtr->nsObj)) { - /* This command is being defined in a non-global namespace */ - nsObj = Jim_DuplicateObj(interp, interp->framePtr->nsObj); - Jim_AppendStrings(interp, nsObj, "::", name, NULL); - } - return nsObj; -} - -/** * If nameObjPtr starts with "::", returns it. * Otherwise returns a new object with nameObjPtr prefixed with "::". * In this case, decrements the ref count of nameObjPtr. @@ -13199,8 +13177,15 @@ static int Jim_ApplyCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar if (len == 3) { #ifdef jim_ext_namespace - /* Need to canonicalise the given namespace. */ - nsObj = JimQualifyNameObj(interp, Jim_ListGetIndex(interp, argv[1], 2)); + /* Need to canonicalise the given namespaces, but it is always treated as global */ + const char *name; + nsObj = Jim_ListGetIndex(interp, argv[1], 2); + name = Jim_String(nsObj); + if (name[0] == ':' && name[1] == ':') { + while (*++name == ':') { + } + nsObj = Jim_NewStringObj(interp, name, -1); + } #else Jim_SetResultString(interp, "namespaces not enabled", -1); return JIM_ERR; diff --git a/tests/applyns.test b/tests/applyns.test index 0483692..bff7624 100644 --- a/tests/applyns.test +++ b/tests/applyns.test @@ -89,7 +89,23 @@ test apply-7.8 {namespace access} { apply [list args $body testApply] } testApply +# apply ignore the current namespace and runs at global scope +# or the provided namespace (relative to global) +test apply-8.1 {namespace current within apply} { + namespace eval testApply {} + namespace eval testApply2 { + apply {a { return [namespace current]-$a } testApply} 5 + } +} {::testApply-5} + +test apply-8.2 {namespace current within apply} { + namespace eval testApply2 { + apply {a { return [namespace current]-$a }} 5 + } +} {::-5} + namespace delete testApply +namespace delete testApply2 testreport |