diff options
author | Kevin Walzer <kw@codebykevin.com> | 2012-09-12 03:05:29 (GMT) |
---|---|---|
committer | Kevin Walzer <kw@codebykevin.com> | 2012-09-12 03:05:29 (GMT) |
commit | 4d00df90e7901e55bc22ae2b9589a9c005f6d9f7 (patch) | |
tree | 531b9edd57ee78802a71cb6c0fd09a0367f59211 /generic/tkUtil.c | |
parent | 799666382d309825a9bd5d7205fd05662a742391 (diff) | |
download | tk-4d00df90e7901e55bc22ae2b9589a9c005f6d9f7.zip tk-4d00df90e7901e55bc22ae2b9589a9c005f6d9f7.tar.gz tk-4d00df90e7901e55bc22ae2b9589a9c005f6d9f7.tar.bz2 |
Review branch for merge of Tk-Cocoa into Tk 8.5 main branch
Diffstat (limited to 'generic/tkUtil.c')
-rw-r--r-- | generic/tkUtil.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/generic/tkUtil.c b/generic/tkUtil.c index bfa5d5c..bb926c4 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -9,6 +9,8 @@ * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. + * + * RCS: @(#) $Id$ */ #include "tkInt.h" @@ -976,6 +978,89 @@ TkFindStateNumObj( } /* + * ---------------------------------------------------------------------- + * + * TkBackgroundEvalObjv -- + * + * Evaluate a command while ensuring that we do not affect the + * interpreters state. This is important when evaluating script + * during background tasks. + * + * Results: + * A standard Tcl result code. + * + * Side Effects: + * The interpreters variables and code may be modified by the script + * but the result will not be modified. + * + * ---------------------------------------------------------------------- + */ + +int +TkBackgroundEvalObjv( + Tcl_Interp *interp, + int objc, + Tcl_Obj *const *objv, + int flags) +{ + Tcl_DString errorInfo, errorCode; + Tcl_SavedResult state; + int n, r = TCL_OK; + + Tcl_DStringInit(&errorInfo); + Tcl_DStringInit(&errorCode); + + Tcl_Preserve(interp); + + /* + * Record the state of the interpreter + */ + + Tcl_SaveResult(interp, &state); + Tcl_DStringAppend(&errorInfo, + Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY), -1); + Tcl_DStringAppend(&errorCode, + Tcl_GetVar(interp, "errorCode", TCL_GLOBAL_ONLY), -1); + + /* + * Evaluate the command and handle any error. + */ + + for (n = 0; n < objc; ++n) { + Tcl_IncrRefCount(objv[n]); + } + r = Tcl_EvalObjv(interp, objc, objv, flags); + for (n = 0; n < objc; ++n) { + Tcl_DecrRefCount(objv[n]); + } + if (r == TCL_ERROR) { + Tcl_AddErrorInfo(interp, "\n (background event handler)"); + Tcl_BackgroundError(interp); + } + + Tcl_Release(interp); + + /* + * Restore the state of the interpreter + */ + + Tcl_SetVar(interp, "errorInfo", + Tcl_DStringValue(&errorInfo), TCL_GLOBAL_ONLY); + Tcl_SetVar(interp, "errorCode", + Tcl_DStringValue(&errorCode), TCL_GLOBAL_ONLY); + Tcl_RestoreResult(interp, &state); + + /* + * Clean up references. + */ + + Tcl_DStringFree(&errorInfo); + Tcl_DStringFree(&errorCode); + + return r; +} + +/* * Local Variables: * mode: c * c-basic-offset: 4 |