| 1 |
/* |
| 2 |
* tclOOBasic.c -- |
| 3 |
* |
| 4 |
* This file contains implementations of the "simple" commands and |
| 5 |
* methods from the object-system core. |
| 6 |
* |
| 7 |
* Copyright (c) 2005-2013 by Donal K. Fellows |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#ifdef HAVE_CONFIG_H |
| 14 |
#include "config.h" |
| 15 |
#endif |
| 16 |
#include "tclInt.h" |
| 17 |
#include "tclOOInt.h" |
| 18 |
|
| 19 |
static inline Tcl_Object *AddConstructionFinalizer(Tcl_Interp *interp); |
| 20 |
static Tcl_NRPostProc AfterNRDestructor; |
| 21 |
static Tcl_NRPostProc DecrRefsPostClassConstructor; |
| 22 |
static Tcl_NRPostProc FinalizeConstruction; |
| 23 |
static Tcl_NRPostProc FinalizeEval; |
| 24 |
static Tcl_NRPostProc NextRestoreFrame; |
| 25 |
|
| 26 |
/* |
| 27 |
* ---------------------------------------------------------------------- |
| 28 |
* |
| 29 |
* AddCreateCallback, FinalizeConstruction -- |
| 30 |
* |
| 31 |
* Special version of TclNRAddCallback that allows the caller to splice |
| 32 |
* the object created later on. Always calls FinalizeConstruction, which |
| 33 |
* converts the object into its name and stores that in the interpreter |
| 34 |
* result. This is shared by all the construction methods (create, |
| 35 |
* createWithNamespace, new). |
| 36 |
* |
| 37 |
* Note that this is the only code in this file (or, indeed, the whole of |
| 38 |
* TclOO) that uses NRE internals; it is the only code that does |
| 39 |
* non-standard poking in the NRE guts. |
| 40 |
* |
| 41 |
* ---------------------------------------------------------------------- |
| 42 |
*/ |
| 43 |
|
| 44 |
static inline Tcl_Object * |
| 45 |
AddConstructionFinalizer( |
| 46 |
Tcl_Interp *interp) |
| 47 |
{ |
| 48 |
TclNRAddCallback(interp, FinalizeConstruction, NULL, NULL, NULL, NULL); |
| 49 |
return (Tcl_Object *) &(TOP_CB(interp)->data[0]); |
| 50 |
} |
| 51 |
|
| 52 |
static int |
| 53 |
FinalizeConstruction( |
| 54 |
ClientData data[], |
| 55 |
Tcl_Interp *interp, |
| 56 |
int result) |
| 57 |
{ |
| 58 |
Object *oPtr = data[0]; |
| 59 |
|
| 60 |
if (result != TCL_OK) { |
| 61 |
return result; |
| 62 |
} |
| 63 |
Tcl_SetObjResult(interp, TclOOObjectName(interp, oPtr)); |
| 64 |
return TCL_OK; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* ---------------------------------------------------------------------- |
| 69 |
* |
| 70 |
* TclOO_Class_Constructor -- |
| 71 |
* |
| 72 |
* Implementation for oo::class constructor. |
| 73 |
* |
| 74 |
* ---------------------------------------------------------------------- |
| 75 |
*/ |
| 76 |
|
| 77 |
int |
| 78 |
TclOO_Class_Constructor( |
| 79 |
ClientData clientData, |
| 80 |
Tcl_Interp *interp, |
| 81 |
Tcl_ObjectContext context, |
| 82 |
int objc, |
| 83 |
Tcl_Obj *const *objv) |
| 84 |
{ |
| 85 |
Object *oPtr = (Object *) Tcl_ObjectContextObject(context); |
| 86 |
Tcl_Obj **invoke; |
| 87 |
|
| 88 |
if (objc-1 > Tcl_ObjectContextSkippedArgs(context)) { |
| 89 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 90 |
"?definitionScript?"); |
| 91 |
return TCL_ERROR; |
| 92 |
} else if (objc == Tcl_ObjectContextSkippedArgs(context)) { |
| 93 |
return TCL_OK; |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Delegate to [oo::define] to do the work. |
| 98 |
*/ |
| 99 |
|
| 100 |
invoke = ckalloc(3 * sizeof(Tcl_Obj *)); |
| 101 |
invoke[0] = oPtr->fPtr->defineName; |
| 102 |
invoke[1] = TclOOObjectName(interp, oPtr); |
| 103 |
invoke[2] = objv[objc-1]; |
| 104 |
|
| 105 |
/* |
| 106 |
* Must add references or errors in configuration script will cause |
| 107 |
* trouble. |
| 108 |
*/ |
| 109 |
|
| 110 |
Tcl_IncrRefCount(invoke[0]); |
| 111 |
Tcl_IncrRefCount(invoke[1]); |
| 112 |
Tcl_IncrRefCount(invoke[2]); |
| 113 |
TclNRAddCallback(interp, DecrRefsPostClassConstructor, |
| 114 |
invoke, NULL, NULL, NULL); |
| 115 |
|
| 116 |
/* |
| 117 |
* Tricky point: do not want the extra reported level in the Tcl stack |
| 118 |
* trace, so use TCL_EVAL_NOERR. |
| 119 |
*/ |
| 120 |
|
| 121 |
return TclNREvalObjv(interp, 3, invoke, TCL_EVAL_NOERR, NULL); |
| 122 |
} |
| 123 |
|
| 124 |
static int |
| 125 |
DecrRefsPostClassConstructor( |
| 126 |
ClientData data[], |
| 127 |
Tcl_Interp *interp, |
| 128 |
int result) |
| 129 |
{ |
| 130 |
Tcl_Obj **invoke = data[0]; |
| 131 |
|
| 132 |
TclDecrRefCount(invoke[0]); |
| 133 |
TclDecrRefCount(invoke[1]); |
| 134 |
TclDecrRefCount(invoke[2]); |
| 135 |
ckfree(invoke); |
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* ---------------------------------------------------------------------- |
| 141 |
* |
| 142 |
* TclOO_Class_Create -- |
| 143 |
* |
| 144 |
* Implementation for oo::class->create method. |
| 145 |
* |
| 146 |
* ---------------------------------------------------------------------- |
| 147 |
*/ |
| 148 |
|
| 149 |
int |
| 150 |
TclOO_Class_Create( |
| 151 |
ClientData clientData, /* Ignored. */ |
| 152 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 153 |
* also used for error reporting. */ |
| 154 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 155 |
int objc, /* Number of arguments. */ |
| 156 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 157 |
{ |
| 158 |
Object *oPtr = (Object *) Tcl_ObjectContextObject(context); |
| 159 |
const char *objName; |
| 160 |
int len; |
| 161 |
|
| 162 |
/* |
| 163 |
* Sanity check; should not be possible to invoke this method on a |
| 164 |
* non-class. |
| 165 |
*/ |
| 166 |
|
| 167 |
if (oPtr->classPtr == NULL) { |
| 168 |
Tcl_Obj *cmdnameObj = TclOOObjectName(interp, oPtr); |
| 169 |
|
| 170 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 171 |
"object \"%s\" is not a class", TclGetString(cmdnameObj))); |
| 172 |
Tcl_SetErrorCode(interp, "TCL", "OO", "INSTANTIATE_NONCLASS", (char *)NULL); |
| 173 |
return TCL_ERROR; |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* Check we have the right number of (sensible) arguments. |
| 178 |
*/ |
| 179 |
|
| 180 |
if (objc - Tcl_ObjectContextSkippedArgs(context) < 1) { |
| 181 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 182 |
"objectName ?arg ...?"); |
| 183 |
return TCL_ERROR; |
| 184 |
} |
| 185 |
objName = Tcl_GetStringFromObj( |
| 186 |
objv[Tcl_ObjectContextSkippedArgs(context)], &len); |
| 187 |
if (len == 0) { |
| 188 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 189 |
"object name must not be empty", -1)); |
| 190 |
Tcl_SetErrorCode(interp, "TCL", "OO", "EMPTY_NAME", (char *)NULL); |
| 191 |
return TCL_ERROR; |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* Make the object and return its name. |
| 196 |
*/ |
| 197 |
|
| 198 |
return TclNRNewObjectInstance(interp, (Tcl_Class) oPtr->classPtr, |
| 199 |
objName, NULL, objc, objv, |
| 200 |
Tcl_ObjectContextSkippedArgs(context)+1, |
| 201 |
AddConstructionFinalizer(interp)); |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* ---------------------------------------------------------------------- |
| 206 |
* |
| 207 |
* TclOO_Class_CreateNs -- |
| 208 |
* |
| 209 |
* Implementation for oo::class->createWithNamespace method. |
| 210 |
* |
| 211 |
* ---------------------------------------------------------------------- |
| 212 |
*/ |
| 213 |
|
| 214 |
int |
| 215 |
TclOO_Class_CreateNs( |
| 216 |
ClientData clientData, /* Ignored. */ |
| 217 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 218 |
* also used for error reporting. */ |
| 219 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 220 |
int objc, /* Number of arguments. */ |
| 221 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 222 |
{ |
| 223 |
Object *oPtr = (Object *) Tcl_ObjectContextObject(context); |
| 224 |
const char *objName, *nsName; |
| 225 |
int len; |
| 226 |
|
| 227 |
/* |
| 228 |
* Sanity check; should not be possible to invoke this method on a |
| 229 |
* non-class. |
| 230 |
*/ |
| 231 |
|
| 232 |
if (oPtr->classPtr == NULL) { |
| 233 |
Tcl_Obj *cmdnameObj = TclOOObjectName(interp, oPtr); |
| 234 |
|
| 235 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 236 |
"object \"%s\" is not a class", TclGetString(cmdnameObj))); |
| 237 |
Tcl_SetErrorCode(interp, "TCL", "OO", "INSTANTIATE_NONCLASS", (char *)NULL); |
| 238 |
return TCL_ERROR; |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* Check we have the right number of (sensible) arguments. |
| 243 |
*/ |
| 244 |
|
| 245 |
if (objc - Tcl_ObjectContextSkippedArgs(context) < 2) { |
| 246 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 247 |
"objectName namespaceName ?arg ...?"); |
| 248 |
return TCL_ERROR; |
| 249 |
} |
| 250 |
objName = Tcl_GetStringFromObj( |
| 251 |
objv[Tcl_ObjectContextSkippedArgs(context)], &len); |
| 252 |
if (len == 0) { |
| 253 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 254 |
"object name must not be empty", -1)); |
| 255 |
Tcl_SetErrorCode(interp, "TCL", "OO", "EMPTY_NAME", (char *)NULL); |
| 256 |
return TCL_ERROR; |
| 257 |
} |
| 258 |
nsName = Tcl_GetStringFromObj( |
| 259 |
objv[Tcl_ObjectContextSkippedArgs(context)+1], &len); |
| 260 |
if (len == 0) { |
| 261 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 262 |
"namespace name must not be empty", -1)); |
| 263 |
Tcl_SetErrorCode(interp, "TCL", "OO", "EMPTY_NAME", (char *)NULL); |
| 264 |
return TCL_ERROR; |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* Make the object and return its name. |
| 269 |
*/ |
| 270 |
|
| 271 |
return TclNRNewObjectInstance(interp, (Tcl_Class) oPtr->classPtr, |
| 272 |
objName, nsName, objc, objv, |
| 273 |
Tcl_ObjectContextSkippedArgs(context)+2, |
| 274 |
AddConstructionFinalizer(interp)); |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* ---------------------------------------------------------------------- |
| 279 |
* |
| 280 |
* TclOO_Class_New -- |
| 281 |
* |
| 282 |
* Implementation for oo::class->new method. |
| 283 |
* |
| 284 |
* ---------------------------------------------------------------------- |
| 285 |
*/ |
| 286 |
|
| 287 |
int |
| 288 |
TclOO_Class_New( |
| 289 |
ClientData clientData, /* Ignored. */ |
| 290 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 291 |
* also used for error reporting. */ |
| 292 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 293 |
int objc, /* Number of arguments. */ |
| 294 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 295 |
{ |
| 296 |
Object *oPtr = (Object *) Tcl_ObjectContextObject(context); |
| 297 |
|
| 298 |
/* |
| 299 |
* Sanity check; should not be possible to invoke this method on a |
| 300 |
* non-class. |
| 301 |
*/ |
| 302 |
|
| 303 |
if (oPtr->classPtr == NULL) { |
| 304 |
Tcl_Obj *cmdnameObj = TclOOObjectName(interp, oPtr); |
| 305 |
|
| 306 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 307 |
"object \"%s\" is not a class", TclGetString(cmdnameObj))); |
| 308 |
Tcl_SetErrorCode(interp, "TCL", "OO", "INSTANTIATE_NONCLASS", (char *)NULL); |
| 309 |
return TCL_ERROR; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* Make the object and return its name. |
| 314 |
*/ |
| 315 |
|
| 316 |
return TclNRNewObjectInstance(interp, (Tcl_Class) oPtr->classPtr, |
| 317 |
NULL, NULL, objc, objv, Tcl_ObjectContextSkippedArgs(context), |
| 318 |
AddConstructionFinalizer(interp)); |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
* ---------------------------------------------------------------------- |
| 323 |
* |
| 324 |
* TclOO_Object_Destroy -- |
| 325 |
* |
| 326 |
* Implementation for oo::object->destroy method. |
| 327 |
* |
| 328 |
* ---------------------------------------------------------------------- |
| 329 |
*/ |
| 330 |
|
| 331 |
int |
| 332 |
TclOO_Object_Destroy( |
| 333 |
ClientData clientData, /* Ignored. */ |
| 334 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 335 |
* also used for error reporting. */ |
| 336 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 337 |
int objc, /* Number of arguments. */ |
| 338 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 339 |
{ |
| 340 |
Object *oPtr = (Object *) Tcl_ObjectContextObject(context); |
| 341 |
CallContext *contextPtr; |
| 342 |
|
| 343 |
if (objc != Tcl_ObjectContextSkippedArgs(context)) { |
| 344 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 345 |
NULL); |
| 346 |
return TCL_ERROR; |
| 347 |
} |
| 348 |
if (!(oPtr->flags & DESTRUCTOR_CALLED)) { |
| 349 |
oPtr->flags |= DESTRUCTOR_CALLED; |
| 350 |
contextPtr = TclOOGetCallContext(oPtr, NULL, DESTRUCTOR, NULL); |
| 351 |
if (contextPtr != NULL) { |
| 352 |
contextPtr->callPtr->flags |= DESTRUCTOR; |
| 353 |
contextPtr->skip = 0; |
| 354 |
TclNRAddCallback(interp, AfterNRDestructor, contextPtr, |
| 355 |
NULL, NULL, NULL); |
| 356 |
TclPushTailcallPoint(interp); |
| 357 |
return TclOOInvokeContext(contextPtr, interp, 0, NULL); |
| 358 |
} |
| 359 |
} |
| 360 |
if (oPtr->command) { |
| 361 |
Tcl_DeleteCommandFromToken(interp, oPtr->command); |
| 362 |
} |
| 363 |
return TCL_OK; |
| 364 |
} |
| 365 |
|
| 366 |
static int |
| 367 |
AfterNRDestructor( |
| 368 |
ClientData data[], |
| 369 |
Tcl_Interp *interp, |
| 370 |
int result) |
| 371 |
{ |
| 372 |
CallContext *contextPtr = data[0]; |
| 373 |
|
| 374 |
if (contextPtr->oPtr->command) { |
| 375 |
Tcl_DeleteCommandFromToken(interp, contextPtr->oPtr->command); |
| 376 |
} |
| 377 |
TclOODeleteContext(contextPtr); |
| 378 |
return result; |
| 379 |
} |
| 380 |
|
| 381 |
/* |
| 382 |
* ---------------------------------------------------------------------- |
| 383 |
* |
| 384 |
* TclOO_Object_Eval -- |
| 385 |
* |
| 386 |
* Implementation for oo::object->eval method. |
| 387 |
* |
| 388 |
* ---------------------------------------------------------------------- |
| 389 |
*/ |
| 390 |
|
| 391 |
int |
| 392 |
TclOO_Object_Eval( |
| 393 |
ClientData clientData, /* Ignored. */ |
| 394 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 395 |
* also used for error reporting. */ |
| 396 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 397 |
int objc, /* Number of arguments. */ |
| 398 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 399 |
{ |
| 400 |
CallContext *contextPtr = (CallContext *) context; |
| 401 |
Tcl_Object object = Tcl_ObjectContextObject(context); |
| 402 |
const int skip = Tcl_ObjectContextSkippedArgs(context); |
| 403 |
CallFrame *framePtr, **framePtrPtr = &framePtr; |
| 404 |
Tcl_Obj *scriptPtr; |
| 405 |
CmdFrame *invoker; |
| 406 |
|
| 407 |
if (objc-1 < skip) { |
| 408 |
Tcl_WrongNumArgs(interp, skip, objv, "arg ?arg ...?"); |
| 409 |
return TCL_ERROR; |
| 410 |
} |
| 411 |
|
| 412 |
/* |
| 413 |
* Make the object's namespace the current namespace and evaluate the |
| 414 |
* command(s). |
| 415 |
*/ |
| 416 |
|
| 417 |
(void) TclPushStackFrame(interp, (Tcl_CallFrame **) framePtrPtr, |
| 418 |
Tcl_GetObjectNamespace(object), FRAME_IS_METHOD); |
| 419 |
framePtr->clientData = context; |
| 420 |
framePtr->objc = objc; |
| 421 |
framePtr->objv = objv; /* Reference counts do not need to be |
| 422 |
* incremented here. */ |
| 423 |
|
| 424 |
if (!(contextPtr->callPtr->flags & PUBLIC_METHOD)) { |
| 425 |
object = NULL; /* Now just for error mesage printing. */ |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
* Work out what script we are actually going to evaluate. |
| 430 |
* |
| 431 |
* When there's more than one argument, we concatenate them together with |
| 432 |
* spaces between, then evaluate the result. Tcl_EvalObjEx will delete the |
| 433 |
* object when it decrements its refcount after eval'ing it. |
| 434 |
*/ |
| 435 |
|
| 436 |
if (objc != skip+1) { |
| 437 |
scriptPtr = Tcl_ConcatObj(objc-skip, objv+skip); |
| 438 |
invoker = NULL; |
| 439 |
} else { |
| 440 |
scriptPtr = objv[skip]; |
| 441 |
invoker = ((Interp *) interp)->cmdFramePtr; |
| 442 |
} |
| 443 |
|
| 444 |
/* |
| 445 |
* Evaluate the script now, with FinalizeEval to do the processing after |
| 446 |
* the script completes. |
| 447 |
*/ |
| 448 |
|
| 449 |
TclNRAddCallback(interp, FinalizeEval, object, NULL, NULL, NULL); |
| 450 |
return TclNREvalObjEx(interp, scriptPtr, 0, invoker, skip); |
| 451 |
} |
| 452 |
|
| 453 |
static int |
| 454 |
FinalizeEval( |
| 455 |
ClientData data[], |
| 456 |
Tcl_Interp *interp, |
| 457 |
int result) |
| 458 |
{ |
| 459 |
if (result == TCL_ERROR) { |
| 460 |
Object *oPtr = data[0]; |
| 461 |
const char *namePtr; |
| 462 |
|
| 463 |
if (oPtr) { |
| 464 |
namePtr = TclGetString(TclOOObjectName(interp, oPtr)); |
| 465 |
} else { |
| 466 |
namePtr = "my"; |
| 467 |
} |
| 468 |
|
| 469 |
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( |
| 470 |
"\n (in \"%s eval\" script line %d)", |
| 471 |
namePtr, Tcl_GetErrorLine(interp))); |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* Restore the previous "current" namespace. |
| 476 |
*/ |
| 477 |
|
| 478 |
TclPopStackFrame(interp); |
| 479 |
return result; |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* ---------------------------------------------------------------------- |
| 484 |
* |
| 485 |
* TclOO_Object_Unknown -- |
| 486 |
* |
| 487 |
* Default unknown method handler method (defined in oo::object). This |
| 488 |
* just creates a suitable error message. |
| 489 |
* |
| 490 |
* ---------------------------------------------------------------------- |
| 491 |
*/ |
| 492 |
|
| 493 |
int |
| 494 |
TclOO_Object_Unknown( |
| 495 |
ClientData clientData, /* Ignored. */ |
| 496 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 497 |
* also used for error reporting. */ |
| 498 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 499 |
int objc, /* Number of arguments. */ |
| 500 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 501 |
{ |
| 502 |
CallContext *contextPtr = (CallContext *) context; |
| 503 |
Object *oPtr = contextPtr->oPtr; |
| 504 |
const char **methodNames; |
| 505 |
int numMethodNames, i, skip = Tcl_ObjectContextSkippedArgs(context); |
| 506 |
Tcl_Obj *errorMsg; |
| 507 |
|
| 508 |
/* |
| 509 |
* If no method name, generate an error asking for a method name. (Only by |
| 510 |
* overriding *this* method can an object handle the absence of a method |
| 511 |
* name without an error). |
| 512 |
*/ |
| 513 |
|
| 514 |
if (objc < skip+1) { |
| 515 |
Tcl_WrongNumArgs(interp, skip, objv, "method ?arg ...?"); |
| 516 |
return TCL_ERROR; |
| 517 |
} |
| 518 |
|
| 519 |
/* |
| 520 |
* Get the list of methods that we want to know about. |
| 521 |
*/ |
| 522 |
|
| 523 |
numMethodNames = TclOOGetSortedMethodList(oPtr, |
| 524 |
contextPtr->callPtr->flags & PUBLIC_METHOD, &methodNames); |
| 525 |
|
| 526 |
/* |
| 527 |
* Special message when there are no visible methods at all. |
| 528 |
*/ |
| 529 |
|
| 530 |
if (numMethodNames == 0) { |
| 531 |
Tcl_Obj *tmpBuf = TclOOObjectName(interp, oPtr); |
| 532 |
const char *piece; |
| 533 |
|
| 534 |
if (contextPtr->callPtr->flags & PUBLIC_METHOD) { |
| 535 |
piece = "visible methods"; |
| 536 |
} else { |
| 537 |
piece = "methods"; |
| 538 |
} |
| 539 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 540 |
"object \"%s\" has no %s", TclGetString(tmpBuf), piece)); |
| 541 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", |
| 542 |
TclGetString(objv[skip]), (char *)NULL); |
| 543 |
return TCL_ERROR; |
| 544 |
} |
| 545 |
|
| 546 |
errorMsg = Tcl_ObjPrintf("unknown method \"%s\": must be ", |
| 547 |
TclGetString(objv[skip])); |
| 548 |
for (i=0 ; i<numMethodNames-1 ; i++) { |
| 549 |
if (i) { |
| 550 |
Tcl_AppendToObj(errorMsg, ", ", -1); |
| 551 |
} |
| 552 |
Tcl_AppendToObj(errorMsg, methodNames[i], -1); |
| 553 |
} |
| 554 |
if (i) { |
| 555 |
Tcl_AppendToObj(errorMsg, " or ", -1); |
| 556 |
} |
| 557 |
Tcl_AppendToObj(errorMsg, methodNames[i], -1); |
| 558 |
ckfree(methodNames); |
| 559 |
Tcl_SetObjResult(interp, errorMsg); |
| 560 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", |
| 561 |
TclGetString(objv[skip]), (char *)NULL); |
| 562 |
return TCL_ERROR; |
| 563 |
} |
| 564 |
|
| 565 |
/* |
| 566 |
* ---------------------------------------------------------------------- |
| 567 |
* |
| 568 |
* TclOO_Object_LinkVar -- |
| 569 |
* |
| 570 |
* Implementation of oo::object->variable method. |
| 571 |
* |
| 572 |
* ---------------------------------------------------------------------- |
| 573 |
*/ |
| 574 |
|
| 575 |
int |
| 576 |
TclOO_Object_LinkVar( |
| 577 |
ClientData clientData, /* Ignored. */ |
| 578 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 579 |
* also used for error reporting. */ |
| 580 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 581 |
int objc, /* Number of arguments. */ |
| 582 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 583 |
{ |
| 584 |
Interp *iPtr = (Interp *) interp; |
| 585 |
Tcl_Object object = Tcl_ObjectContextObject(context); |
| 586 |
Namespace *savedNsPtr; |
| 587 |
int i; |
| 588 |
|
| 589 |
if (objc-Tcl_ObjectContextSkippedArgs(context) < 0) { |
| 590 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 591 |
"?varName ...?"); |
| 592 |
return TCL_ERROR; |
| 593 |
} |
| 594 |
|
| 595 |
/* |
| 596 |
* A sanity check. Shouldn't ever happen. (This is all that remains of a |
| 597 |
* more complex check inherited from [global] after we have applied the |
| 598 |
* fix for [Bug 2903811]; note that the fix involved *removing* code.) |
| 599 |
*/ |
| 600 |
|
| 601 |
if (iPtr->varFramePtr == NULL) { |
| 602 |
return TCL_OK; |
| 603 |
} |
| 604 |
|
| 605 |
for (i=Tcl_ObjectContextSkippedArgs(context) ; i<objc ; i++) { |
| 606 |
Var *varPtr, *aryPtr; |
| 607 |
const char *varName = TclGetString(objv[i]); |
| 608 |
|
| 609 |
/* |
| 610 |
* The variable name must not contain a '::' since that's illegal in |
| 611 |
* local names. |
| 612 |
*/ |
| 613 |
|
| 614 |
if (strstr(varName, "::") != NULL) { |
| 615 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 616 |
"variable name \"%s\" illegal: must not contain namespace" |
| 617 |
" separator", varName)); |
| 618 |
Tcl_SetErrorCode(interp, "TCL", "UPVAR", "INVERTED", (char *)NULL); |
| 619 |
return TCL_ERROR; |
| 620 |
} |
| 621 |
|
| 622 |
/* |
| 623 |
* Switch to the object's namespace for the duration of this call. |
| 624 |
* Like this, the variable is looked up in the namespace of the |
| 625 |
* object, and not in the namespace of the caller. Otherwise this |
| 626 |
* would only work if the caller was a method of the object itself, |
| 627 |
* which might not be true if the method was exported. This is a bit |
| 628 |
* of a hack, but the simplest way to do this (pushing a stack frame |
| 629 |
* would be horribly expensive by comparison). |
| 630 |
*/ |
| 631 |
|
| 632 |
savedNsPtr = iPtr->varFramePtr->nsPtr; |
| 633 |
iPtr->varFramePtr->nsPtr = (Namespace *) |
| 634 |
Tcl_GetObjectNamespace(object); |
| 635 |
varPtr = TclObjLookupVar(interp, objv[i], NULL, TCL_NAMESPACE_ONLY, |
| 636 |
"define", 1, 0, &aryPtr); |
| 637 |
iPtr->varFramePtr->nsPtr = savedNsPtr; |
| 638 |
|
| 639 |
if (varPtr == NULL || aryPtr != NULL) { |
| 640 |
/* |
| 641 |
* Variable cannot be an element in an array. If aryPtr is not |
| 642 |
* NULL, it is an element, so throw up an error and return. |
| 643 |
*/ |
| 644 |
|
| 645 |
TclVarErrMsg(interp, varName, NULL, "define", |
| 646 |
"name refers to an element in an array"); |
| 647 |
Tcl_SetErrorCode(interp, "TCL", "UPVAR", "LOCAL_ELEMENT", (char *)NULL); |
| 648 |
return TCL_ERROR; |
| 649 |
} |
| 650 |
|
| 651 |
/* |
| 652 |
* Arrange for the lifetime of the variable to be correctly managed. |
| 653 |
* This is copied out of Tcl_VariableObjCmd... |
| 654 |
*/ |
| 655 |
|
| 656 |
if (!TclIsVarNamespaceVar(varPtr)) { |
| 657 |
TclSetVarNamespaceVar(varPtr); |
| 658 |
} |
| 659 |
|
| 660 |
if (TclPtrMakeUpvar(interp, varPtr, varName, 0, -1) != TCL_OK) { |
| 661 |
return TCL_ERROR; |
| 662 |
} |
| 663 |
} |
| 664 |
return TCL_OK; |
| 665 |
} |
| 666 |
|
| 667 |
/* |
| 668 |
* ---------------------------------------------------------------------- |
| 669 |
* |
| 670 |
* TclOO_Object_VarName -- |
| 671 |
* |
| 672 |
* Implementation of the oo::object->varname method. |
| 673 |
* |
| 674 |
* ---------------------------------------------------------------------- |
| 675 |
*/ |
| 676 |
|
| 677 |
int |
| 678 |
TclOO_Object_VarName( |
| 679 |
ClientData clientData, /* Ignored. */ |
| 680 |
Tcl_Interp *interp, /* Interpreter in which to create the object; |
| 681 |
* also used for error reporting. */ |
| 682 |
Tcl_ObjectContext context, /* The object/call context. */ |
| 683 |
int objc, /* Number of arguments. */ |
| 684 |
Tcl_Obj *const *objv) /* The actual arguments. */ |
| 685 |
{ |
| 686 |
Var *varPtr, *aryVar; |
| 687 |
Tcl_Obj *varNamePtr, *argPtr; |
| 688 |
const char *arg; |
| 689 |
Tcl_Namespace *namespacePtr; |
| 690 |
|
| 691 |
if (Tcl_ObjectContextSkippedArgs(context)+1 != objc) { |
| 692 |
Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, |
| 693 |
"varName"); |
| 694 |
return TCL_ERROR; |
| 695 |
} |
| 696 |
namespacePtr = Tcl_GetObjectNamespace(Tcl_ObjectContextObject(context)); |
| 697 |
argPtr = objv[objc-1]; |
| 698 |
arg = Tcl_GetString(argPtr); |
| 699 |
|
| 700 |
/* |
| 701 |
* Convert the variable name to fully-qualified form if it wasn't already. |
| 702 |
* This has to be done prior to lookup because we can run into problems |
| 703 |
* with resolvers otherwise. [Bug 3603695] |
| 704 |
* |
| 705 |
* We still need to do the lookup; the variable could be linked to another |
| 706 |
* variable and we want the target's name. |
| 707 |
*/ |
| 708 |
|
| 709 |
if (arg[0] == ':' && arg[1] == ':') { |
| 710 |
varNamePtr = argPtr; |
| 711 |
} else { |
| 712 |
varNamePtr = Tcl_NewStringObj(namespacePtr->fullName, -1); |
| 713 |
Tcl_AppendToObj(varNamePtr, "::", 2); |
| 714 |
Tcl_AppendObjToObj(varNamePtr, argPtr); |
| 715 |
} |
| 716 |
Tcl_IncrRefCount(varNamePtr); |
| 717 |
varPtr = TclObjLookupVar(interp, varNamePtr, NULL, |
| 718 |
TCL_NAMESPACE_ONLY|TCL_LEAVE_ERR_MSG, "refer to", 1, 1, &aryVar); |
| 719 |
Tcl_DecrRefCount(varNamePtr); |
| 720 |
if (varPtr == NULL) { |
| 721 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARIABLE", arg, (char *)NULL); |
| 722 |
return TCL_ERROR; |
| 723 |
} |
| 724 |
|
| 725 |
/* |
| 726 |
* The variable reference must not disappear too soon. [Bug 74b6110204] |
| 727 |
*/ |
| 728 |
if (!TclIsVarArrayElement(varPtr)) { |
| 729 |
TclSetVarNamespaceVar(varPtr); |
| 730 |
} |
| 731 |
|
| 732 |
/* |
| 733 |
* Now that we've pinned down what variable we're really talking about |
| 734 |
* (including traversing variable links), convert back to a name. |
| 735 |
*/ |
| 736 |
|
| 737 |
TclNewObj(varNamePtr); |
| 738 |
if (aryVar != NULL) { |
| 739 |
Tcl_HashEntry *hPtr; |
| 740 |
Tcl_HashSearch search; |
| 741 |
|
| 742 |
Tcl_GetVariableFullName(interp, (Tcl_Var) aryVar, varNamePtr); |
| 743 |
|
| 744 |
/* |
| 745 |
* WARNING! This code pokes inside the implementation of hash tables! |
| 746 |
*/ |
| 747 |
|
| 748 |
hPtr = Tcl_FirstHashEntry((Tcl_HashTable *) aryVar->value.tablePtr, |
| 749 |
&search); |
| 750 |
while (hPtr != NULL) { |
| 751 |
if (varPtr == Tcl_GetHashValue(hPtr)) { |
| 752 |
Tcl_AppendPrintfToObj(varNamePtr, "(%s)", |
| 753 |
TclGetString(hPtr->key.objPtr)); |
| 754 |
break; |
| 755 |
} |
| 756 |
hPtr = Tcl_NextHashEntry(&search); |
| 757 |
} |
| 758 |
} else if (!TclIsVarArrayElement(varPtr)) { |
| 759 |
Tcl_GetVariableFullName(interp, (Tcl_Var) varPtr, varNamePtr); |
| 760 |
} else { |
| 761 |
/* |
| 762 |
* Target is an element of an array but we don't know which one. |
| 763 |
* The name in the object's namespace is the best we can do. |
| 764 |
* [Bug 2da1cb0c80] |
| 765 |
*/ |
| 766 |
if (arg[0] == ':' && arg[1] == ':') { |
| 767 |
Tcl_DecrRefCount(varNamePtr); |
| 768 |
varNamePtr = argPtr; |
| 769 |
} else { |
| 770 |
Tcl_AppendPrintfToObj(varNamePtr, "%s::%s", |
| 771 |
namespacePtr->fullName, arg); |
| 772 |
} |
| 773 |
} |
| 774 |
Tcl_SetObjResult(interp, varNamePtr); |
| 775 |
return TCL_OK; |
| 776 |
} |
| 777 |
|
| 778 |
/* |
| 779 |
* ---------------------------------------------------------------------- |
| 780 |
* |
| 781 |
* TclOONextObjCmd, TclOONextToObjCmd -- |
| 782 |
* |
| 783 |
* Implementation of the [next] and [nextto] commands. Note that these |
| 784 |
* commands are only ever to be used inside the body of a procedure-like |
| 785 |
* method. |
| 786 |
* |
| 787 |
* ---------------------------------------------------------------------- |
| 788 |
*/ |
| 789 |
|
| 790 |
int |
| 791 |
TclOONextObjCmd( |
| 792 |
ClientData clientData, |
| 793 |
Tcl_Interp *interp, |
| 794 |
int objc, |
| 795 |
Tcl_Obj *const *objv) |
| 796 |
{ |
| 797 |
Interp *iPtr = (Interp *) interp; |
| 798 |
CallFrame *framePtr = iPtr->varFramePtr; |
| 799 |
Tcl_ObjectContext context; |
| 800 |
|
| 801 |
/* |
| 802 |
* Start with sanity checks on the calling context to make sure that we |
| 803 |
* are invoked from a suitable method context. If so, we can safely |
| 804 |
* retrieve the handle to the object call context. |
| 805 |
*/ |
| 806 |
|
| 807 |
if (framePtr == NULL || !(framePtr->isProcCallFrame & FRAME_IS_METHOD)) { |
| 808 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 809 |
"%s may only be called from inside a method", |
| 810 |
TclGetString(objv[0]))); |
| 811 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); |
| 812 |
return TCL_ERROR; |
| 813 |
} |
| 814 |
context = framePtr->clientData; |
| 815 |
|
| 816 |
/* |
| 817 |
* Invoke the (advanced) method call context in the caller context. Note |
| 818 |
* that this is like [uplevel 1] and not [eval]. |
| 819 |
*/ |
| 820 |
|
| 821 |
TclNRAddCallback(interp, NextRestoreFrame, framePtr, NULL,NULL,NULL); |
| 822 |
iPtr->varFramePtr = framePtr->callerVarPtr; |
| 823 |
return TclNRObjectContextInvokeNext(interp, context, objc, objv, 1); |
| 824 |
} |
| 825 |
|
| 826 |
int |
| 827 |
TclOONextToObjCmd( |
| 828 |
ClientData clientData, |
| 829 |
Tcl_Interp *interp, |
| 830 |
int objc, |
| 831 |
Tcl_Obj *const *objv) |
| 832 |
{ |
| 833 |
Interp *iPtr = (Interp *) interp; |
| 834 |
CallFrame *framePtr = iPtr->varFramePtr; |
| 835 |
Class *classPtr; |
| 836 |
CallContext *contextPtr; |
| 837 |
int i; |
| 838 |
Tcl_Object object; |
| 839 |
const char *methodType; |
| 840 |
|
| 841 |
/* |
| 842 |
* Start with sanity checks on the calling context to make sure that we |
| 843 |
* are invoked from a suitable method context. If so, we can safely |
| 844 |
* retrieve the handle to the object call context. |
| 845 |
*/ |
| 846 |
|
| 847 |
if (framePtr == NULL || !(framePtr->isProcCallFrame & FRAME_IS_METHOD)) { |
| 848 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 849 |
"%s may only be called from inside a method", |
| 850 |
TclGetString(objv[0]))); |
| 851 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); |
| 852 |
return TCL_ERROR; |
| 853 |
} |
| 854 |
contextPtr = framePtr->clientData; |
| 855 |
|
| 856 |
/* |
| 857 |
* Sanity check the arguments; we need the first one to refer to a class. |
| 858 |
*/ |
| 859 |
|
| 860 |
if (objc < 2) { |
| 861 |
Tcl_WrongNumArgs(interp, 1, objv, "class ?arg...?"); |
| 862 |
return TCL_ERROR; |
| 863 |
} |
| 864 |
object = Tcl_GetObjectFromObj(interp, objv[1]); |
| 865 |
if (object == NULL) { |
| 866 |
return TCL_ERROR; |
| 867 |
} |
| 868 |
classPtr = ((Object *)object)->classPtr; |
| 869 |
if (classPtr == NULL) { |
| 870 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 871 |
"\"%s\" is not a class", TclGetString(objv[1]))); |
| 872 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CLASS_REQUIRED", (char *)NULL); |
| 873 |
return TCL_ERROR; |
| 874 |
} |
| 875 |
|
| 876 |
/* |
| 877 |
* Search for an implementation of a method associated with the current |
| 878 |
* call on the call chain past the point where we currently are. Do not |
| 879 |
* allow jumping backwards! |
| 880 |
*/ |
| 881 |
|
| 882 |
for (i=contextPtr->index+1 ; i<contextPtr->callPtr->numChain ; i++) { |
| 883 |
struct MInvoke *miPtr = contextPtr->callPtr->chain + i; |
| 884 |
|
| 885 |
if (!miPtr->isFilter && miPtr->mPtr->declaringClassPtr == classPtr) { |
| 886 |
/* |
| 887 |
* Invoke the (advanced) method call context in the caller |
| 888 |
* context. Note that this is like [uplevel 1] and not [eval]. |
| 889 |
*/ |
| 890 |
|
| 891 |
TclNRAddCallback(interp, NextRestoreFrame, framePtr, |
| 892 |
contextPtr, INT2PTR(contextPtr->index), NULL); |
| 893 |
contextPtr->index = i-1; |
| 894 |
iPtr->varFramePtr = framePtr->callerVarPtr; |
| 895 |
return TclNRObjectContextInvokeNext(interp, |
| 896 |
(Tcl_ObjectContext) contextPtr, objc, objv, 2); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/* |
| 901 |
* Generate an appropriate error message, depending on whether the value |
| 902 |
* is on the chain but unreachable, or not on the chain at all. |
| 903 |
*/ |
| 904 |
|
| 905 |
if (contextPtr->callPtr->flags & CONSTRUCTOR) { |
| 906 |
methodType = "constructor"; |
| 907 |
} else if (contextPtr->callPtr->flags & DESTRUCTOR) { |
| 908 |
methodType = "destructor"; |
| 909 |
} else { |
| 910 |
methodType = "method"; |
| 911 |
} |
| 912 |
|
| 913 |
for (i=contextPtr->index ; i>=0 ; i--) { |
| 914 |
struct MInvoke *miPtr = contextPtr->callPtr->chain + i; |
| 915 |
|
| 916 |
if (!miPtr->isFilter && miPtr->mPtr->declaringClassPtr == classPtr) { |
| 917 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 918 |
"%s implementation by \"%s\" not reachable from here", |
| 919 |
methodType, TclGetString(objv[1]))); |
| 920 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CLASS_NOT_REACHABLE", |
| 921 |
(char *)NULL); |
| 922 |
return TCL_ERROR; |
| 923 |
} |
| 924 |
} |
| 925 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 926 |
"%s has no non-filter implementation by \"%s\"", |
| 927 |
methodType, TclGetString(objv[1]))); |
| 928 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CLASS_NOT_THERE", (char *)NULL); |
| 929 |
return TCL_ERROR; |
| 930 |
} |
| 931 |
|
| 932 |
static int |
| 933 |
NextRestoreFrame( |
| 934 |
ClientData data[], |
| 935 |
Tcl_Interp *interp, |
| 936 |
int result) |
| 937 |
{ |
| 938 |
Interp *iPtr = (Interp *) interp; |
| 939 |
CallContext *contextPtr = data[1]; |
| 940 |
|
| 941 |
iPtr->varFramePtr = data[0]; |
| 942 |
if (contextPtr != NULL) { |
| 943 |
contextPtr->index = PTR2INT(data[2]); |
| 944 |
} |
| 945 |
return result; |
| 946 |
} |
| 947 |
|
| 948 |
/* |
| 949 |
* ---------------------------------------------------------------------- |
| 950 |
* |
| 951 |
* TclOOSelfObjCmd -- |
| 952 |
* |
| 953 |
* Implementation of the [self] command, which provides introspection of |
| 954 |
* the call context. |
| 955 |
* |
| 956 |
* ---------------------------------------------------------------------- |
| 957 |
*/ |
| 958 |
|
| 959 |
int |
| 960 |
TclOOSelfObjCmd( |
| 961 |
ClientData clientData, |
| 962 |
Tcl_Interp *interp, |
| 963 |
int objc, |
| 964 |
Tcl_Obj *const *objv) |
| 965 |
{ |
| 966 |
static const char *const subcmds[] = { |
| 967 |
"call", "caller", "class", "filter", "method", "namespace", "next", |
| 968 |
"object", "target", NULL |
| 969 |
}; |
| 970 |
enum SelfCmds { |
| 971 |
SELF_CALL, SELF_CALLER, SELF_CLASS, SELF_FILTER, SELF_METHOD, SELF_NS, |
| 972 |
SELF_NEXT, SELF_OBJECT, SELF_TARGET |
| 973 |
}; |
| 974 |
Interp *iPtr = (Interp *) interp; |
| 975 |
CallFrame *framePtr = iPtr->varFramePtr; |
| 976 |
CallContext *contextPtr; |
| 977 |
Tcl_Obj *result[3]; |
| 978 |
int index; |
| 979 |
|
| 980 |
#define CurrentlyInvoked(contextPtr) \ |
| 981 |
((contextPtr)->callPtr->chain[(contextPtr)->index]) |
| 982 |
|
| 983 |
/* |
| 984 |
* Start with sanity checks on the calling context and the method context. |
| 985 |
*/ |
| 986 |
|
| 987 |
if (framePtr == NULL || !(framePtr->isProcCallFrame & FRAME_IS_METHOD)) { |
| 988 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 989 |
"%s may only be called from inside a method", |
| 990 |
TclGetString(objv[0]))); |
| 991 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); |
| 992 |
return TCL_ERROR; |
| 993 |
} |
| 994 |
|
| 995 |
contextPtr = framePtr->clientData; |
| 996 |
|
| 997 |
/* |
| 998 |
* Now we do "conventional" argument parsing for a while. Note that no |
| 999 |
* subcommand takes arguments. |
| 1000 |
*/ |
| 1001 |
|
| 1002 |
if (objc > 2) { |
| 1003 |
Tcl_WrongNumArgs(interp, 1, objv, "subcommand"); |
| 1004 |
return TCL_ERROR; |
| 1005 |
} else if (objc == 1) { |
| 1006 |
index = SELF_OBJECT; |
| 1007 |
} else if (Tcl_GetIndexFromObj(interp, objv[1], subcmds, "subcommand", 0, |
| 1008 |
&index) != TCL_OK) { |
| 1009 |
return TCL_ERROR; |
| 1010 |
} |
| 1011 |
|
| 1012 |
switch ((enum SelfCmds) index) { |
| 1013 |
case SELF_OBJECT: |
| 1014 |
Tcl_SetObjResult(interp, TclOOObjectName(interp, contextPtr->oPtr)); |
| 1015 |
return TCL_OK; |
| 1016 |
case SELF_NS: |
| 1017 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1018 |
contextPtr->oPtr->namespacePtr->fullName,-1)); |
| 1019 |
return TCL_OK; |
| 1020 |
case SELF_CLASS: { |
| 1021 |
Class *clsPtr = CurrentlyInvoked(contextPtr).mPtr->declaringClassPtr; |
| 1022 |
|
| 1023 |
if (clsPtr == NULL) { |
| 1024 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1025 |
"method not defined by a class", -1)); |
| 1026 |
Tcl_SetErrorCode(interp, "TCL", "OO", "UNMATCHED_CONTEXT", (char *)NULL); |
| 1027 |
return TCL_ERROR; |
| 1028 |
} |
| 1029 |
|
| 1030 |
Tcl_SetObjResult(interp, TclOOObjectName(interp, clsPtr->thisPtr)); |
| 1031 |
return TCL_OK; |
| 1032 |
} |
| 1033 |
case SELF_METHOD: |
| 1034 |
if (contextPtr->callPtr->flags & CONSTRUCTOR) { |
| 1035 |
Tcl_SetObjResult(interp, contextPtr->oPtr->fPtr->constructorName); |
| 1036 |
} else if (contextPtr->callPtr->flags & DESTRUCTOR) { |
| 1037 |
Tcl_SetObjResult(interp, contextPtr->oPtr->fPtr->destructorName); |
| 1038 |
} else { |
| 1039 |
Tcl_SetObjResult(interp, |
| 1040 |
CurrentlyInvoked(contextPtr).mPtr->namePtr); |
| 1041 |
} |
| 1042 |
return TCL_OK; |
| 1043 |
case SELF_FILTER: |
| 1044 |
if (!CurrentlyInvoked(contextPtr).isFilter) { |
| 1045 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1046 |
"not inside a filtering context", -1)); |
| 1047 |
Tcl_SetErrorCode(interp, "TCL", "OO", "UNMATCHED_CONTEXT", (char *)NULL); |
| 1048 |
return TCL_ERROR; |
| 1049 |
} else { |
| 1050 |
struct MInvoke *miPtr = &CurrentlyInvoked(contextPtr); |
| 1051 |
Object *oPtr; |
| 1052 |
const char *type; |
| 1053 |
|
| 1054 |
if (miPtr->filterDeclarer != NULL) { |
| 1055 |
oPtr = miPtr->filterDeclarer->thisPtr; |
| 1056 |
type = "class"; |
| 1057 |
} else { |
| 1058 |
oPtr = contextPtr->oPtr; |
| 1059 |
type = "object"; |
| 1060 |
} |
| 1061 |
|
| 1062 |
result[0] = TclOOObjectName(interp, oPtr); |
| 1063 |
result[1] = Tcl_NewStringObj(type, -1); |
| 1064 |
result[2] = miPtr->mPtr->namePtr; |
| 1065 |
Tcl_SetObjResult(interp, Tcl_NewListObj(3, result)); |
| 1066 |
return TCL_OK; |
| 1067 |
} |
| 1068 |
case SELF_CALLER: |
| 1069 |
if ((framePtr->callerVarPtr == NULL) || |
| 1070 |
!(framePtr->callerVarPtr->isProcCallFrame & FRAME_IS_METHOD)){ |
| 1071 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1072 |
"caller is not an object", -1)); |
| 1073 |
Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); |
| 1074 |
return TCL_ERROR; |
| 1075 |
} else { |
| 1076 |
CallContext *callerPtr = framePtr->callerVarPtr->clientData; |
| 1077 |
Method *mPtr = callerPtr->callPtr->chain[callerPtr->index].mPtr; |
| 1078 |
Object *declarerPtr; |
| 1079 |
|
| 1080 |
if (mPtr->declaringClassPtr != NULL) { |
| 1081 |
declarerPtr = mPtr->declaringClassPtr->thisPtr; |
| 1082 |
} else if (mPtr->declaringObjectPtr != NULL) { |
| 1083 |
declarerPtr = mPtr->declaringObjectPtr; |
| 1084 |
} else { |
| 1085 |
/* |
| 1086 |
* This should be unreachable code. |
| 1087 |
*/ |
| 1088 |
|
| 1089 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1090 |
"method without declarer!", -1)); |
| 1091 |
return TCL_ERROR; |
| 1092 |
} |
| 1093 |
|
| 1094 |
result[0] = TclOOObjectName(interp, declarerPtr); |
| 1095 |
result[1] = TclOOObjectName(interp, callerPtr->oPtr); |
| 1096 |
if (callerPtr->callPtr->flags & CONSTRUCTOR) { |
| 1097 |
result[2] = declarerPtr->fPtr->constructorName; |
| 1098 |
} else if (callerPtr->callPtr->flags & DESTRUCTOR) { |
| 1099 |
result[2] = declarerPtr->fPtr->destructorName; |
| 1100 |
} else { |
| 1101 |
result[2] = mPtr->namePtr; |
| 1102 |
} |
| 1103 |
Tcl_SetObjResult(interp, Tcl_NewListObj(3, result)); |
| 1104 |
return TCL_OK; |
| 1105 |
} |
| 1106 |
case SELF_NEXT: |
| 1107 |
if (contextPtr->index < contextPtr->callPtr->numChain-1) { |
| 1108 |
Method *mPtr = |
| 1109 |
contextPtr->callPtr->chain[contextPtr->index+1].mPtr; |
| 1110 |
Object *declarerPtr; |
| 1111 |
|
| 1112 |
if (mPtr->declaringClassPtr != NULL) { |
| 1113 |
declarerPtr = mPtr->declaringClassPtr->thisPtr; |
| 1114 |
} else if (mPtr->declaringObjectPtr != NULL) { |
| 1115 |
declarerPtr = mPtr->declaringObjectPtr; |
| 1116 |
} else { |
| 1117 |
/* |
| 1118 |
* This should be unreachable code. |
| 1119 |
*/ |
| 1120 |
|
| 1121 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1122 |
"method without declarer!", -1)); |
| 1123 |
return TCL_ERROR; |
| 1124 |
} |
| 1125 |
|
| 1126 |
result[0] = TclOOObjectName(interp, declarerPtr); |
| 1127 |
if (contextPtr->callPtr->flags & CONSTRUCTOR) { |
| 1128 |
result[1] = declarerPtr->fPtr->constructorName; |
| 1129 |
} else if (contextPtr->callPtr->flags & DESTRUCTOR) { |
| 1130 |
result[1] = declarerPtr->fPtr->destructorName; |
| 1131 |
} else { |
| 1132 |
result[1] = mPtr->namePtr; |
| 1133 |
} |
| 1134 |
Tcl_SetObjResult(interp, Tcl_NewListObj(2, result)); |
| 1135 |
} |
| 1136 |
return TCL_OK; |
| 1137 |
case SELF_TARGET: |
| 1138 |
if (!CurrentlyInvoked(contextPtr).isFilter) { |
| 1139 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1140 |
"not inside a filtering context", -1)); |
| 1141 |
Tcl_SetErrorCode(interp, "TCL", "OO", "UNMATCHED_CONTEXT", (char *)NULL); |
| 1142 |
return TCL_ERROR; |
| 1143 |
} else { |
| 1144 |
Method *mPtr; |
| 1145 |
Object *declarerPtr; |
| 1146 |
int i; |
| 1147 |
|
| 1148 |
for (i=contextPtr->index ; i<contextPtr->callPtr->numChain ; i++){ |
| 1149 |
if (!contextPtr->callPtr->chain[i].isFilter) { |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
if (i == contextPtr->callPtr->numChain) { |
| 1154 |
Tcl_Panic("filtering call chain without terminal non-filter"); |
| 1155 |
} |
| 1156 |
mPtr = contextPtr->callPtr->chain[i].mPtr; |
| 1157 |
if (mPtr->declaringClassPtr != NULL) { |
| 1158 |
declarerPtr = mPtr->declaringClassPtr->thisPtr; |
| 1159 |
} else if (mPtr->declaringObjectPtr != NULL) { |
| 1160 |
declarerPtr = mPtr->declaringObjectPtr; |
| 1161 |
} else { |
| 1162 |
/* |
| 1163 |
* This should be unreachable code. |
| 1164 |
*/ |
| 1165 |
|
| 1166 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1167 |
"method without declarer!", -1)); |
| 1168 |
return TCL_ERROR; |
| 1169 |
} |
| 1170 |
result[0] = TclOOObjectName(interp, declarerPtr); |
| 1171 |
result[1] = mPtr->namePtr; |
| 1172 |
Tcl_SetObjResult(interp, Tcl_NewListObj(2, result)); |
| 1173 |
return TCL_OK; |
| 1174 |
} |
| 1175 |
case SELF_CALL: |
| 1176 |
result[0] = TclOORenderCallChain(interp, contextPtr->callPtr); |
| 1177 |
TclNewIntObj(result[1], contextPtr->index); |
| 1178 |
Tcl_SetObjResult(interp, Tcl_NewListObj(2, result)); |
| 1179 |
return TCL_OK; |
| 1180 |
} |
| 1181 |
return TCL_ERROR; |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* ---------------------------------------------------------------------- |
| 1186 |
* |
| 1187 |
* CopyObjectCmd -- |
| 1188 |
* |
| 1189 |
* Implementation of the [oo::copy] command, which clones an object (but |
| 1190 |
* not its namespace). Note that no constructors are called during this |
| 1191 |
* process. |
| 1192 |
* |
| 1193 |
* ---------------------------------------------------------------------- |
| 1194 |
*/ |
| 1195 |
|
| 1196 |
int |
| 1197 |
TclOOCopyObjectCmd( |
| 1198 |
ClientData clientData, |
| 1199 |
Tcl_Interp *interp, |
| 1200 |
int objc, |
| 1201 |
Tcl_Obj *const *objv) |
| 1202 |
{ |
| 1203 |
Tcl_Object oPtr, o2Ptr; |
| 1204 |
|
| 1205 |
if (objc < 2 || objc > 4) { |
| 1206 |
Tcl_WrongNumArgs(interp, 1, objv, |
| 1207 |
"sourceName ?targetName? ?targetNamespace?"); |
| 1208 |
return TCL_ERROR; |
| 1209 |
} |
| 1210 |
|
| 1211 |
oPtr = Tcl_GetObjectFromObj(interp, objv[1]); |
| 1212 |
if (oPtr == NULL) { |
| 1213 |
return TCL_ERROR; |
| 1214 |
} |
| 1215 |
|
| 1216 |
/* |
| 1217 |
* Create a cloned object of the correct class. Note that constructors are |
| 1218 |
* not called. Also note that we must resolve the object name ourselves |
| 1219 |
* because we do not want to create the object in the current namespace, |
| 1220 |
* but rather in the context of the namespace of the caller of the overall |
| 1221 |
* [oo::define] command. |
| 1222 |
*/ |
| 1223 |
|
| 1224 |
if (objc == 2) { |
| 1225 |
o2Ptr = Tcl_CopyObjectInstance(interp, oPtr, NULL, NULL); |
| 1226 |
} else { |
| 1227 |
const char *name, *namespaceName; |
| 1228 |
|
| 1229 |
name = TclGetString(objv[2]); |
| 1230 |
if (name[0] == '\0') { |
| 1231 |
name = NULL; |
| 1232 |
} |
| 1233 |
|
| 1234 |
/* |
| 1235 |
* Choose a unique namespace name if the user didn't supply one. |
| 1236 |
*/ |
| 1237 |
|
| 1238 |
namespaceName = NULL; |
| 1239 |
if (objc == 4) { |
| 1240 |
namespaceName = TclGetString(objv[3]); |
| 1241 |
|
| 1242 |
if (namespaceName[0] == '\0') { |
| 1243 |
namespaceName = NULL; |
| 1244 |
} else if (Tcl_FindNamespace(interp, namespaceName, NULL, |
| 1245 |
0) != NULL) { |
| 1246 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1247 |
"%s refers to an existing namespace", namespaceName)); |
| 1248 |
return TCL_ERROR; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
o2Ptr = Tcl_CopyObjectInstance(interp, oPtr, name, namespaceName); |
| 1253 |
} |
| 1254 |
|
| 1255 |
if (o2Ptr == NULL) { |
| 1256 |
return TCL_ERROR; |
| 1257 |
} |
| 1258 |
|
| 1259 |
/* |
| 1260 |
* Return the name of the cloned object. |
| 1261 |
*/ |
| 1262 |
|
| 1263 |
Tcl_SetObjResult(interp, TclOOObjectName(interp, (Object *) o2Ptr)); |
| 1264 |
return TCL_OK; |
| 1265 |
} |
| 1266 |
|
| 1267 |
/* |
| 1268 |
* Local Variables: |
| 1269 |
* mode: c |
| 1270 |
* c-basic-offset: 4 |
| 1271 |
* fill-column: 78 |
| 1272 |
* End: |
| 1273 |
*/ |