Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

info script: return real current source file #269

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions jim.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ static int utf8_tounicode_case(const char *s, int *uc, int upper)
return l;
}

/* A common pattern is to save an object from interp and set a new
* value, and then restore the original. Use this pattern:
*
* Jim_Obj *saveObj = JimPushInterpObj(interp->obj, newobj);
* JimPopInterpObj(interp, interp->obj, saveObj);
*/
static Jim_Obj *JimPushInterpObjImpl(Jim_Obj **iop, Jim_Obj *no)
{
Jim_Obj *io = *iop;
Jim_IncrRefCount(no);
*iop = no;
return io;
}

#define JimPushInterpObj(IO, NO) JimPushInterpObjImpl(&(IO), NO)
#define JimPopInterpObj(I, IO, SO) do { Jim_DecrRefCount(I, IO); IO = SO; } while (0)

/* These can be used in addition to JIM_CASESENS/JIM_NOCASE */
#define JIM_CHARSET_SCAN 2
#define JIM_CHARSET_GLOB 0
Expand Down Expand Up @@ -5741,6 +5758,7 @@ Jim_Interp *Jim_CreateInterp(void)
i->errorProc = i->emptyObj;
i->nullScriptObj = Jim_NewEmptyStringObj(i);
i->evalFrame = &i->topEvalFrame;
i->currentFilenameObj = Jim_NewEmptyStringObj(i);
Jim_IncrRefCount(i->emptyObj);
Jim_IncrRefCount(i->result);
Jim_IncrRefCount(i->stackTrace);
Expand All @@ -5750,6 +5768,7 @@ Jim_Interp *Jim_CreateInterp(void)
Jim_IncrRefCount(i->errorProc);
Jim_IncrRefCount(i->trueObj);
Jim_IncrRefCount(i->falseObj);
Jim_IncrRefCount(i->currentFilenameObj);

/* Initialize key variables every interpreter should contain */
Jim_SetVariableStrWithStr(i, JIM_LIBPATH, TCL_LIBRARY);
Expand Down Expand Up @@ -5794,6 +5813,7 @@ void Jim_FreeInterp(Jim_Interp *i)
Jim_DecrRefCount(i, i->unknown);
Jim_DecrRefCount(i, i->defer);
Jim_DecrRefCount(i, i->nullScriptObj);
Jim_DecrRefCount(i, i->currentFilenameObj);

/* This will disard any cached commands */
Jim_InterpIncrProcEpoch(i);
Expand Down Expand Up @@ -11641,17 +11661,25 @@ static Jim_Obj *JimReadTextFile(Jim_Interp *interp, const char *filename)

int Jim_EvalFile(Jim_Interp *interp, const char *filename)
{
Jim_Obj *filenameObj;
Jim_Obj *oldFilenameObj;
Jim_Obj *scriptObjPtr;
int retcode;

scriptObjPtr = JimReadTextFile(interp, filename);
if (!scriptObjPtr) {
return JIM_ERR;
}
JimSetSourceInfo(interp, scriptObjPtr, Jim_NewStringObj(interp, filename, -1), 1);

filenameObj = Jim_NewStringObj(interp, filename, -1);
JimSetSourceInfo(interp, scriptObjPtr, filenameObj, 1);

oldFilenameObj = JimPushInterpObj(interp->currentFilenameObj, filenameObj);

retcode = Jim_EvalObj(interp, scriptObjPtr);

JimPopInterpObj(interp, interp->currentFilenameObj, oldFilenameObj);

/* Handle the JIM_RETURN return code */
if (retcode == JIM_RETURN) {
if (--interp->returnLevel <= 0) {
Expand Down Expand Up @@ -15565,7 +15593,7 @@ static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
JIM_DEF_SUBCMD("procs", "?pattern?", 0, 1),
JIM_DEF_SUBCMD("references", NULL, 0, 0),
JIM_DEF_SUBCMD("returncodes", "?code?", 0, 1),
JIM_DEF_SUBCMD("script", NULL, 0, 0),
JIM_DEF_SUBCMD("script", "?filename?", 0, 1),
JIM_DEF_SUBCMD("source", "source ?filename line?", 1, 3),
JIM_DEF_SUBCMD("stacktrace", NULL, 0, 0),
JIM_DEF_SUBCMD("statics", "procname", 1, 1),
Expand Down Expand Up @@ -15655,7 +15683,12 @@ static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
return JIM_OK;

case INFO_SCRIPT:
Jim_SetResult(interp, JimGetScript(interp, interp->evalFrame->scriptObj)->fileNameObj);
if (argc == 3) {
Jim_IncrRefCount(argv[2]);
Jim_DecrRefCount(interp, interp->currentFilenameObj);
interp->currentFilenameObj = argv[2];
}
Jim_SetResult(interp, interp->currentFilenameObj);
return JIM_OK;

case INFO_SOURCE:{
Expand Down
2 changes: 1 addition & 1 deletion jim.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ typedef struct Jim_PrngState {
typedef struct Jim_Interp {
Jim_Obj *result; /* object returned by the last command called. */
int unused_errorLine; /* Error line where an error occurred. */
Jim_Obj *unused_errorFileNameObj; /* Error file where an error occurred. */
Jim_Obj *currentFilenameObj; /* filename of current Jim_EvalFile() */
int unused_addStackTrace;
int maxCallFrameDepth; /* Used for infinite loop detection. */
int maxEvalDepth; /* Used for infinite loop detection. */
Expand Down
10 changes: 10 additions & 0 deletions tests/jim.test
Original file line number Diff line number Diff line change
Expand Up @@ -3339,6 +3339,16 @@ test info-7.5 {info vars with temporary variables} {
}
t1
} {a}
test info-8.1 {info script} {
file tail [info script]
} {jim.test}
test info-8.2 {info script - set} {
set save [info script]
list [info script abc] [info script] [file tail [info script $save]]
} {abc abc jim.test}
test info-8.3 {info script - usage} -body {
info script too many args
} -returnCodes error -result {wrong # args: should be "info script ?filename?"}

################################################################################
# RANGE
Expand Down