Debian Codegraph @

generic/tclOOMethod.c

raw
1 /*
2 * tclOOMethod.c --
3 *
4 * This file contains code to create and manage methods.
5 *
6 * Copyright (c) 2005-2011 Donal K. Fellows
7 *
8 * See the file "license.terms" for information on usage and redistribution of
9 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 #include "tclInt.h"
16 #include "tclOOInt.h"
17 #include "tclCompile.h"
18
19 /*
20 * Structure used to contain all the information needed about a call frame
21 * used in a procedure-like method.
22 */
23
24 typedef struct {
25 CallFrame *framePtr; /* Reference to the call frame itself (it's
26 * actually allocated on the Tcl stack). */
27 ProcErrorProc *errProc; /* The error handler for the body. */
28 Tcl_Obj *nameObj; /* The "name" of the command. Only used for a
29 * few moments, so not reference. */
30 } PMFrameData;
31
32 /*
33 * Structure used to pass information about variable resolution to the
34 * on-the-ground resolvers used when working with resolved compiled variables.
35 */
36
37 typedef struct {
38 Tcl_ResolvedVarInfo info; /* "Type" information so that the compiled
39 * variable can be linked to the namespace
40 * variable at the right time. */
41 Tcl_Obj *variableObj; /* The name of the variable. */
42 Tcl_Var cachedObjectVar; /* TODO: When to flush this cache? Can class
43 * variables be cached? */
44 } OOResVarInfo;
45
46 /*
47 * Function declarations for things defined in this file.
48 */
49
50 static Tcl_Obj ** InitEnsembleRewrite(Tcl_Interp *interp, int objc,
51 Tcl_Obj *const *objv, int toRewrite,
52 int rewriteLength, Tcl_Obj *const *rewriteObjs,
53 int *lengthPtr);
54 static int InvokeProcedureMethod(void *clientData,
55 Tcl_Interp *interp, Tcl_ObjectContext context,
56 int objc, Tcl_Obj *const *objv);
57 static Tcl_NRPostProc FinalizeForwardCall;
58 static Tcl_NRPostProc FinalizePMCall;
59 static int PushMethodCallFrame(Tcl_Interp *interp,
60 CallContext *contextPtr, ProcedureMethod *pmPtr,
61 int objc, Tcl_Obj *const *objv,
62 PMFrameData *fdPtr);
63 static void DeleteProcedureMethodRecord(ProcedureMethod *pmPtr);
64 static void DeleteProcedureMethod(void *clientData);
65 static int CloneProcedureMethod(Tcl_Interp *interp,
66 void *clientData, void **newClientData);
67 static void MethodErrorHandler(Tcl_Interp *interp,
68 Tcl_Obj *procNameObj);
69 static void ConstructorErrorHandler(Tcl_Interp *interp,
70 Tcl_Obj *procNameObj);
71 static void DestructorErrorHandler(Tcl_Interp *interp,
72 Tcl_Obj *procNameObj);
73 static Tcl_Obj * RenderMethodName(void *clientData);
74 static Tcl_Obj * RenderDeclarerName(void *clientData);
75 static int InvokeForwardMethod(void *clientData,
76 Tcl_Interp *interp, Tcl_ObjectContext context,
77 int objc, Tcl_Obj *const *objv);
78 static void DeleteForwardMethod(void *clientData);
79 static int CloneForwardMethod(Tcl_Interp *interp,
80 void *clientData, void **newClientData);
81 static int ProcedureMethodVarResolver(Tcl_Interp *interp,
82 const char *varName, Tcl_Namespace *contextNs,
83 int flags, Tcl_Var *varPtr);
84 static int ProcedureMethodCompiledVarResolver(Tcl_Interp *interp,
85 const char *varName, int length,
86 Tcl_Namespace *contextNs,
87 Tcl_ResolvedVarInfo **rPtrPtr);
88
89 /*
90 * The types of methods defined by the core OO system.
91 */
92
93 static const Tcl_MethodType procMethodType = {
94 TCL_OO_METHOD_VERSION_CURRENT, "method",
95 InvokeProcedureMethod, DeleteProcedureMethod, CloneProcedureMethod
96 };
97 static const Tcl_MethodType fwdMethodType = {
98 TCL_OO_METHOD_VERSION_CURRENT, "forward",
99 InvokeForwardMethod, DeleteForwardMethod, CloneForwardMethod
100 };
101
102 /*
103 * Helper macros (derived from things private to tclVar.c)
104 */
105
106 #define TclVarTable(contextNs) \
107 ((Tcl_HashTable *) (&((Namespace *) (contextNs))->varTable))
108 #define TclVarHashGetValue(hPtr) \
109 ((Tcl_Var) ((char *)hPtr - TclOffset(VarInHash, entry)))
110
111 static inline ProcedureMethod *
112 AllocProcedureMethodRecord(
113 int flags)
114 {
115 ProcedureMethod *pmPtr = (ProcedureMethod *)
116 ckalloc(sizeof(ProcedureMethod));
117 memset(pmPtr, 0, sizeof(ProcedureMethod));
118 pmPtr->version = TCLOO_PROCEDURE_METHOD_VERSION;
119 pmPtr->flags = flags & USE_DECLARER_NS;
120 pmPtr->refCount = 1;
121 pmPtr->cmd.clientData = &pmPtr->efi;
122 return pmPtr;
123 }
124
125 /*
126 * ----------------------------------------------------------------------
127 *
128 * Tcl_NewInstanceMethod --
129 *
130 * Attach a method to an object instance.
131 *
132 * ----------------------------------------------------------------------
133 */
134
135 Tcl_Method
136 Tcl_NewInstanceMethod(
137 Tcl_Interp *interp, /* Unused? */
138 Tcl_Object object, /* The object that has the method attached to
139 * it. */
140 Tcl_Obj *nameObj, /* The name of the method. May be NULL; if so,
141 * up to caller to manage storage (e.g., when
142 * it is a constructor or destructor). */
143 int flags, /* Whether this is a public method. */
144 const Tcl_MethodType *typePtr,
145 /* The type of method this is, which defines
146 * how to invoke, delete and clone the
147 * method. */
148 void *clientData) /* Some data associated with the particular
149 * method to be created. */
150 {
151 Object *oPtr = (Object *) object;
152 Method *mPtr;
153 Tcl_HashEntry *hPtr;
154 int isNew;
155
156 if (nameObj == NULL) {
157 mPtr = (Method *)ckalloc(sizeof(Method));
158 mPtr->namePtr = NULL;
159 mPtr->refCount = 1;
160 goto populate;
161 }
162 if (!oPtr->methodsPtr) {
163 oPtr->methodsPtr = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
164 Tcl_InitObjHashTable(oPtr->methodsPtr);
165 oPtr->flags &= ~USE_CLASS_CACHE;
166 }
167 hPtr = Tcl_CreateHashEntry(oPtr->methodsPtr, (char *) nameObj, &isNew);
168 if (isNew) {
169 mPtr = (Method *)ckalloc(sizeof(Method));
170 mPtr->namePtr = nameObj;
171 mPtr->refCount = 1;
172 Tcl_IncrRefCount(nameObj);
173 Tcl_SetHashValue(hPtr, mPtr);
174 } else {
175 mPtr = (Method *)Tcl_GetHashValue(hPtr);
176 if (mPtr->typePtr != NULL && mPtr->typePtr->deleteProc != NULL) {
177 mPtr->typePtr->deleteProc(mPtr->clientData);
178 }
179 }
180
181 populate:
182 mPtr->typePtr = typePtr;
183 mPtr->clientData = clientData;
184 mPtr->flags = 0;
185 mPtr->declaringObjectPtr = oPtr;
186 mPtr->declaringClassPtr = NULL;
187 if (flags) {
188 mPtr->flags |= flags & (PUBLIC_METHOD | PRIVATE_METHOD);
189 }
190 oPtr->epoch++;
191 return (Tcl_Method) mPtr;
192 }
193
194 /*
195 * ----------------------------------------------------------------------
196 *
197 * Tcl_NewMethod --
198 *
199 * Attach a method to a class.
200 *
201 * ----------------------------------------------------------------------
202 */
203
204 Tcl_Method
205 Tcl_NewMethod(
206 Tcl_Interp *interp, /* The interpreter containing the class. */
207 Tcl_Class cls, /* The class to attach the method to. */
208 Tcl_Obj *nameObj, /* The name of the object. May be NULL (e.g.,
209 * for constructors or destructors); if so, up
210 * to caller to manage storage. */
211 int flags, /* Whether this is a public method. */
212 const Tcl_MethodType *typePtr,
213 /* The type of method this is, which defines
214 * how to invoke, delete and clone the
215 * method. */
216 void *clientData) /* Some data associated with the particular
217 * method to be created. */
218 {
219 Class *clsPtr = (Class *) cls;
220 Method *mPtr;
221 Tcl_HashEntry *hPtr;
222 int isNew;
223
224 if (nameObj == NULL) {
225 mPtr = (Method *)ckalloc(sizeof(Method));
226 mPtr->namePtr = NULL;
227 mPtr->refCount = 1;
228 goto populate;
229 }
230 hPtr = Tcl_CreateHashEntry(&clsPtr->classMethods, (char *)nameObj,&isNew);
231 if (isNew) {
232 mPtr = (Method *)ckalloc(sizeof(Method));
233 mPtr->refCount = 1;
234 mPtr->namePtr = nameObj;
235 Tcl_IncrRefCount(nameObj);
236 Tcl_SetHashValue(hPtr, mPtr);
237 } else {
238 mPtr = (Method *)Tcl_GetHashValue(hPtr);
239 if (mPtr->typePtr != NULL && mPtr->typePtr->deleteProc != NULL) {
240 mPtr->typePtr->deleteProc(mPtr->clientData);
241 }
242 }
243
244 populate:
245 clsPtr->thisPtr->fPtr->epoch++;
246 mPtr->typePtr = typePtr;
247 mPtr->clientData = clientData;
248 mPtr->flags = 0;
249 mPtr->declaringObjectPtr = NULL;
250 mPtr->declaringClassPtr = clsPtr;
251 if (flags) {
252 mPtr->flags |= flags & (PUBLIC_METHOD | PRIVATE_METHOD);
253 }
254
255 return (Tcl_Method) mPtr;
256 }
257
258 /*
259 * ----------------------------------------------------------------------
260 *
261 * TclOODelMethodRef --
262 *
263 * How to delete a method.
264 *
265 * ----------------------------------------------------------------------
266 */
267
268 void
269 TclOODelMethodRef(
270 Method *mPtr)
271 {
272 if ((mPtr != NULL) && (mPtr->refCount-- <= 1)) {
273 if (mPtr->typePtr != NULL && mPtr->typePtr->deleteProc != NULL) {
274 mPtr->typePtr->deleteProc(mPtr->clientData);
275 }
276 if (mPtr->namePtr != NULL) {
277 Tcl_DecrRefCount(mPtr->namePtr);
278 }
279
280 ckfree(mPtr);
281 }
282 }
283
284 /*
285 * ----------------------------------------------------------------------
286 *
287 * TclOONewBasicMethod --
288 *
289 * Helper that makes it cleaner to create very simple methods during
290 * basic system initialization. Not suitable for general use.
291 *
292 * ----------------------------------------------------------------------
293 */
294
295 void
296 TclOONewBasicMethod(
297 Tcl_Interp *interp,
298 Class *clsPtr, /* Class to attach the method to. */
299 const DeclaredClassMethod *dcm)
300 /* Name of the method, whether it is public,
301 * and the function to implement it. */
302 {
303 Tcl_Obj *namePtr = Tcl_NewStringObj(dcm->name, -1);
304
305 Tcl_IncrRefCount(namePtr);
306 Tcl_NewMethod(interp, (Tcl_Class) clsPtr, namePtr,
307 (dcm->isPublic ? PUBLIC_METHOD : 0), &dcm->definition, NULL);
308 Tcl_DecrRefCount(namePtr);
309 }
310
311 /*
312 * ----------------------------------------------------------------------
313 *
314 * TclOONewProcInstanceMethod --
315 *
316 * Create a new procedure-like method for an object.
317 *
318 * ----------------------------------------------------------------------
319 */
320
321 Method *
322 TclOONewProcInstanceMethod(
323 Tcl_Interp *interp, /* The interpreter containing the object. */
324 Object *oPtr, /* The object to modify. */
325 int flags, /* Whether this is a public method. */
326 Tcl_Obj *nameObj, /* The name of the method, which must not be
327 * NULL. */
328 Tcl_Obj *argsObj, /* The formal argument list for the method,
329 * which must not be NULL. */
330 Tcl_Obj *bodyObj, /* The body of the method, which must not be
331 * NULL. */
332 ProcedureMethod **pmPtrPtr) /* Place to write pointer to procedure method
333 * structure to allow for deeper tuning of the
334 * structure's contents. NULL if caller is not
335 * interested. */
336 {
337 int argsLen;
338 ProcedureMethod *pmPtr;
339 Tcl_Method method;
340
341 if (TclListObjLength(interp, argsObj, &argsLen) != TCL_OK) {
342 return NULL;
343 }
344 pmPtr = AllocProcedureMethodRecord(flags);
345 method = TclOOMakeProcInstanceMethod(interp, oPtr, flags, nameObj,
346 argsObj, bodyObj, &procMethodType, pmPtr, &pmPtr->procPtr);
347 if (method == NULL) {
348 ckfree(pmPtr);
349 } else if (pmPtrPtr != NULL) {
350 *pmPtrPtr = pmPtr;
351 }
352 return (Method *) method;
353 }
354
355 /*
356 * ----------------------------------------------------------------------
357 *
358 * TclOONewProcMethod --
359 *
360 * Create a new procedure-like method for a class.
361 *
362 * ----------------------------------------------------------------------
363 */
364
365 Method *
366 TclOONewProcMethod(
367 Tcl_Interp *interp, /* The interpreter containing the class. */
368 Class *clsPtr, /* The class to modify. */
369 int flags, /* Whether this is a public method. */
370 Tcl_Obj *nameObj, /* The name of the method, which may be NULL;
371 * if so, up to caller to manage storage
372 * (e.g., because it is a constructor or
373 * destructor). */
374 Tcl_Obj *argsObj, /* The formal argument list for the method,
375 * which may be NULL; if so, it is equivalent
376 * to an empty list. */
377 Tcl_Obj *bodyObj, /* The body of the method, which must not be
378 * NULL. */
379 ProcedureMethod **pmPtrPtr) /* Place to write pointer to procedure method
380 * structure to allow for deeper tuning of the
381 * structure's contents. NULL if caller is not
382 * interested. */
383 {
384 int argsLen; /* -1 => delete argsObj before exit */
385 ProcedureMethod *pmPtr;
386 const char *procName;
387 Tcl_Method method;
388
389 if (argsObj == NULL) {
390 argsLen = -1;
391 TclNewObj(argsObj);
392 Tcl_IncrRefCount(argsObj);
393 procName = "<destructor>";
394 } else if (TclListObjLength(interp, argsObj, &argsLen) != TCL_OK) {
395 return NULL;
396 } else {
397 procName = (nameObj==NULL ? "<constructor>" : TclGetString(nameObj));
398 }
399
400 pmPtr = AllocProcedureMethodRecord(flags);
401 method = TclOOMakeProcMethod(interp, clsPtr, flags, nameObj, procName,
402 argsObj, bodyObj, &procMethodType, pmPtr, &pmPtr->procPtr);
403
404 if (argsLen == -1) {
405 Tcl_DecrRefCount(argsObj);
406 }
407 if (method == NULL) {
408 ckfree(pmPtr);
409 } else if (pmPtrPtr != NULL) {
410 *pmPtrPtr = pmPtr;
411 }
412
413 return (Method *) method;
414 }
415
416 /*
417 * ----------------------------------------------------------------------
418 *
419 * TclOOMakeProcInstanceMethod --
420 *
421 * The guts of the code to make a procedure-like method for an object.
422 * Split apart so that it is easier for other extensions to reuse (in
423 * particular, it frees them from having to pry so deeply into Tcl's
424 * guts).
425 *
426 * ----------------------------------------------------------------------
427 */
428
429 Tcl_Method
430 TclOOMakeProcInstanceMethod(
431 Tcl_Interp *interp, /* The interpreter containing the object. */
432 Object *oPtr, /* The object to modify. */
433 int flags, /* Whether this is a public method. */
434 Tcl_Obj *nameObj, /* The name of the method, which _must not_ be
435 * NULL. */
436 Tcl_Obj *argsObj, /* The formal argument list for the method,
437 * which _must not_ be NULL. */
438 Tcl_Obj *bodyObj, /* The body of the method, which _must not_ be
439 * NULL. */
440 const Tcl_MethodType *typePtr,
441 /* The type of the method to create. */
442 void *clientData, /* The per-method type-specific data. */
443 Proc **procPtrPtr) /* A pointer to the variable in which to write
444 * the procedure record reference. Presumably
445 * inside the structure indicated by the
446 * pointer in clientData. */
447 {
448 Interp *iPtr = (Interp *) interp;
449 Proc *procPtr;
450
451 if (TclCreateProc(interp, NULL, TclGetString(nameObj), argsObj, bodyObj,
452 procPtrPtr) != TCL_OK) {
453 return NULL;
454 }
455 procPtr = *procPtrPtr;
456 procPtr->cmdPtr = NULL;
457
458 if (iPtr->cmdFramePtr) {
459 CmdFrame context = *iPtr->cmdFramePtr;
460
461 if (context.type == TCL_LOCATION_BC) {
462 /*
463 * Retrieve source information from the bytecode, if possible. If
464 * the information is retrieved successfully, context.type will be
465 * TCL_LOCATION_SOURCE and the reference held by
466 * context.data.eval.path will be counted.
467 */
468
469 TclGetSrcInfoForPc(&context);
470 } else if (context.type == TCL_LOCATION_SOURCE) {
471 /*
472 * The copy into 'context' up above has created another reference
473 * to 'context.data.eval.path'; account for it.
474 */
475
476 Tcl_IncrRefCount(context.data.eval.path);
477 }
478
479 if (context.type == TCL_LOCATION_SOURCE) {
480 /*
481 * We can account for source location within a proc only if the
482 * proc body was not created by substitution.
483 * (FIXME: check that this is sane and correct!)
484 */
485
486 if (context.line
487 && (context.nline >= 4) && (context.line[3] >= 0)) {
488 int isNew;
489 CmdFrame *cfPtr = (CmdFrame *)ckalloc(sizeof(CmdFrame));
490 Tcl_HashEntry *hPtr;
491
492 cfPtr->level = -1;
493 cfPtr->type = context.type;
494 cfPtr->line = (int *)ckalloc(sizeof(int));
495 cfPtr->line[0] = context.line[3];
496 cfPtr->nline = 1;
497 cfPtr->framePtr = NULL;
498 cfPtr->nextPtr = NULL;
499
500 cfPtr->data.eval.path = context.data.eval.path;
501 Tcl_IncrRefCount(cfPtr->data.eval.path);
502
503 cfPtr->cmd = NULL;
504 cfPtr->len = 0;
505
506 hPtr = Tcl_CreateHashEntry(iPtr->linePBodyPtr,
507 (char *) procPtr, &isNew);
508 Tcl_SetHashValue(hPtr, cfPtr);
509 }
510
511 /*
512 * 'context' is going out of scope; account for the reference that
513 * it's holding to the path name.
514 */
515
516 Tcl_DecrRefCount(context.data.eval.path);
517 context.data.eval.path = NULL;
518 }
519 }
520
521 return Tcl_NewInstanceMethod(interp, (Tcl_Object) oPtr, nameObj, flags,
522 typePtr, clientData);
523 }
524
525 /*
526 * ----------------------------------------------------------------------
527 *
528 * TclOOMakeProcMethod --
529 *
530 * The guts of the code to make a procedure-like method for a class.
531 * Split apart so that it is easier for other extensions to reuse (in
532 * particular, it frees them from having to pry so deeply into Tcl's
533 * guts).
534 *
535 * ----------------------------------------------------------------------
536 */
537
538 Tcl_Method
539 TclOOMakeProcMethod(
540 Tcl_Interp *interp, /* The interpreter containing the class. */
541 Class *clsPtr, /* The class to modify. */
542 int flags, /* Whether this is a public method. */
543 Tcl_Obj *nameObj, /* The name of the method, which may be NULL;
544 * if so, up to caller to manage storage
545 * (e.g., because it is a constructor or
546 * destructor). */
547 const char *namePtr, /* The name of the method as a string, which
548 * _must not_ be NULL. */
549 Tcl_Obj *argsObj, /* The formal argument list for the method,
550 * which _must not_ be NULL. */
551 Tcl_Obj *bodyObj, /* The body of the method, which _must not_ be
552 * NULL. */
553 const Tcl_MethodType *typePtr,
554 /* The type of the method to create. */
555 void *clientData, /* The per-method type-specific data. */
556 Proc **procPtrPtr) /* A pointer to the variable in which to write
557 * the procedure record reference. Presumably
558 * inside the structure indicated by the
559 * pointer in clientData. */
560 {
561 Interp *iPtr = (Interp *) interp;
562 Proc *procPtr;
563
564 if (TclCreateProc(interp, NULL, namePtr, argsObj, bodyObj,
565 procPtrPtr) != TCL_OK) {
566 return NULL;
567 }
568 procPtr = *procPtrPtr;
569 procPtr->cmdPtr = NULL;
570
571 if (iPtr->cmdFramePtr) {
572 CmdFrame context = *iPtr->cmdFramePtr;
573
574 if (context.type == TCL_LOCATION_BC) {
575 /*
576 * Retrieve source information from the bytecode, if possible. If
577 * the information is retrieved successfully, context.type will be
578 * TCL_LOCATION_SOURCE and the reference held by
579 * context.data.eval.path will be counted.
580 */
581
582 TclGetSrcInfoForPc(&context);
583 } else if (context.type == TCL_LOCATION_SOURCE) {
584 /*
585 * The copy into 'context' up above has created another reference
586 * to 'context.data.eval.path'; account for it.
587 */
588
589 Tcl_IncrRefCount(context.data.eval.path);
590 }
591
592 if (context.type == TCL_LOCATION_SOURCE) {
593 /*
594 * We can account for source location within a proc only if the
595 * proc body was not created by substitution.
596 * (FIXME: check that this is sane and correct!)
597 */
598
599 if (context.line
600 && (context.nline >= 4) && (context.line[3] >= 0)) {
601 int isNew;
602 CmdFrame *cfPtr = (CmdFrame *)ckalloc(sizeof(CmdFrame));
603 Tcl_HashEntry *hPtr;
604
605 cfPtr->level = -1;
606 cfPtr->type = context.type;
607 cfPtr->line = (int *)ckalloc(sizeof(int));
608 cfPtr->line[0] = context.line[3];
609 cfPtr->nline = 1;
610 cfPtr->framePtr = NULL;
611 cfPtr->nextPtr = NULL;
612
613 cfPtr->data.eval.path = context.data.eval.path;
614 Tcl_IncrRefCount(cfPtr->data.eval.path);
615
616 cfPtr->cmd = NULL;
617 cfPtr->len = 0;
618
619 hPtr = Tcl_CreateHashEntry(iPtr->linePBodyPtr,
620 (char *) procPtr, &isNew);
621 Tcl_SetHashValue(hPtr, cfPtr);
622 }
623
624 /*
625 * 'context' is going out of scope; account for the reference that
626 * it's holding to the path name.
627 */
628
629 Tcl_DecrRefCount(context.data.eval.path);
630 context.data.eval.path = NULL;
631 }
632 }
633
634 return Tcl_NewMethod(interp, (Tcl_Class) clsPtr, nameObj, flags, typePtr,
635 clientData);
636 }
637
638 /*
639 * ----------------------------------------------------------------------
640 *
641 * InvokeProcedureMethod, PushMethodCallFrame --
642 *
643 * How to invoke a procedure-like method.
644 *
645 * ----------------------------------------------------------------------
646 */
647
648 static int
649 InvokeProcedureMethod(
650 void *clientData, /* Pointer to some per-method context. */
651 Tcl_Interp *interp,
652 Tcl_ObjectContext context, /* The method calling context. */
653 int objc, /* Number of arguments. */
654 Tcl_Obj *const *objv) /* Arguments as actually seen. */
655 {
656 ProcedureMethod *pmPtr = (ProcedureMethod *)clientData;
657 int result;
658 PMFrameData *fdPtr; /* Important data that has to have a lifetime
659 * matched by this function (or rather, by the
660 * call frame's lifetime). */
661
662 /*
663 * If the object namespace (or interpreter) were deleted, we just skip to
664 * the next thing in the chain.
665 */
666
667 if (TclOOObjectDestroyed(((CallContext *)context)->oPtr) ||
668 Tcl_InterpDeleted(interp)
669 ) {
670 return TclNRObjectContextInvokeNext(interp, context, objc, objv,
671 Tcl_ObjectContextSkippedArgs(context));
672 }
673
674 /*
675 * Finishes filling out the extra frame info so that [info frame] works if
676 * that is not already set up.
677 */
678
679 if (pmPtr->efi.length == 0) {
680 Tcl_Method method = Tcl_ObjectContextMethod(context);
681
682 pmPtr->efi.length = 2;
683 pmPtr->efi.fields[0].name = "method";
684 pmPtr->efi.fields[0].proc = RenderMethodName;
685 pmPtr->efi.fields[0].clientData = pmPtr;
686 pmPtr->callSiteFlags = ((CallContext *)
687 context)->callPtr->flags & (CONSTRUCTOR | DESTRUCTOR);
688 pmPtr->interp = interp;
689 pmPtr->method = method;
690 if (pmPtr->gfivProc != NULL) {
691 pmPtr->efi.fields[1].name = "";
692 pmPtr->efi.fields[1].proc = pmPtr->gfivProc;
693 pmPtr->efi.fields[1].clientData = pmPtr;
694 } else {
695 if (Tcl_MethodDeclarerObject(method) != NULL) {
696 pmPtr->efi.fields[1].name = "object";
697 } else {
698 pmPtr->efi.fields[1].name = "class";
699 }
700 pmPtr->efi.fields[1].proc = RenderDeclarerName;
701 pmPtr->efi.fields[1].clientData = pmPtr;
702 }
703 }
704
705 /*
706 * Allocate the special frame data.
707 */
708
709 fdPtr = (PMFrameData *)TclStackAlloc(interp, sizeof(PMFrameData));
710
711 /*
712 * Create a call frame for this method.
713 */
714
715 result = PushMethodCallFrame(interp, (CallContext *) context, pmPtr,
716 objc, objv, fdPtr);
717 if (result != TCL_OK) {
718 TclStackFree(interp, fdPtr);
719 return result;
720 }
721 pmPtr->refCount++;
722
723 /*
724 * Give the pre-call callback a chance to do some setup and, possibly,
725 * veto the call.
726 */
727
728 if (pmPtr->preCallProc != NULL) {
729 int isFinished;
730
731 result = pmPtr->preCallProc(pmPtr->clientData, interp, context,
732 (Tcl_CallFrame *) fdPtr->framePtr, &isFinished);
733 if (isFinished || result != TCL_OK) {
734 Tcl_PopCallFrame(interp);
735 TclStackFree(interp, fdPtr->framePtr);
736 if (pmPtr->refCount-- <= 1) {
737 DeleteProcedureMethodRecord(pmPtr);
738 }
739 TclStackFree(interp, fdPtr);
740 return result;
741 }
742 }
743
744 /*
745 * Now invoke the body of the method.
746 */
747
748 TclNRAddCallback(interp, FinalizePMCall, pmPtr, context, fdPtr, NULL);
749 return TclNRInterpProcCore(interp, fdPtr->nameObj,
750 Tcl_ObjectContextSkippedArgs(context), fdPtr->errProc);
751 }
752
753 static int
754 FinalizePMCall(
755 void *data[],
756 Tcl_Interp *interp,
757 int result)
758 {
759 ProcedureMethod *pmPtr = (ProcedureMethod *)data[0];
760 Tcl_ObjectContext context = (Tcl_ObjectContext)data[1];
761 PMFrameData *fdPtr = (PMFrameData *)data[2];
762
763 /*
764 * Give the post-call callback a chance to do some cleanup. Note that at
765 * this point the call frame itself is invalid; it's already been popped.
766 */
767
768 if (pmPtr->postCallProc) {
769 result = pmPtr->postCallProc(pmPtr->clientData, interp, context,
770 Tcl_GetObjectNamespace(Tcl_ObjectContextObject(context)),
771 result);
772 }
773
774 /*
775 * Scrap the special frame data now that we're done with it. Note that we
776 * are inlining DeleteProcedureMethod() here; this location is highly
777 * sensitive when it comes to performance!
778 */
779
780 if (pmPtr->refCount-- <= 1) {
781 DeleteProcedureMethodRecord(pmPtr);
782 }
783 TclStackFree(interp, fdPtr);
784 return result;
785 }
786
787 static int
788 PushMethodCallFrame(
789 Tcl_Interp *interp, /* Current interpreter. */
790 CallContext *contextPtr, /* Current method call context. */
791 ProcedureMethod *pmPtr, /* Information about this procedure-like
792 * method. */
793 int objc, /* Number of arguments. */
794 Tcl_Obj *const *objv, /* Array of arguments. */
795 PMFrameData *fdPtr) /* Place to store information about the call
796 * frame. */
797 {
798 Namespace *nsPtr = (Namespace *) contextPtr->oPtr->namespacePtr;
799 int result;
800 CallFrame **framePtrPtr = &fdPtr->framePtr;
801
802 /*
803 * Compute basic information on the basis of the type of method it is.
804 */
805
806 if (contextPtr->callPtr->flags & CONSTRUCTOR) {
807 fdPtr->nameObj = contextPtr->oPtr->fPtr->constructorName;
808 fdPtr->errProc = ConstructorErrorHandler;
809 } else if (contextPtr->callPtr->flags & DESTRUCTOR) {
810 fdPtr->nameObj = contextPtr->oPtr->fPtr->destructorName;
811 fdPtr->errProc = DestructorErrorHandler;
812 } else {
813 fdPtr->nameObj = Tcl_MethodName(
814 Tcl_ObjectContextMethod((Tcl_ObjectContext) contextPtr));
815 fdPtr->errProc = MethodErrorHandler;
816 }
817 if (pmPtr->errProc != NULL) {
818 fdPtr->errProc = pmPtr->errProc;
819 }
820
821 /*
822 * Magic to enable things like [incr Tcl], which wants methods to run in
823 * their class's namespace.
824 */
825
826 if (pmPtr->flags & USE_DECLARER_NS) {
827 Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr;
828
829 if (mPtr->declaringClassPtr != NULL) {
830 nsPtr = (Namespace *)
831 mPtr->declaringClassPtr->thisPtr->namespacePtr;
832 } else {
833 nsPtr = (Namespace *) mPtr->declaringObjectPtr->namespacePtr;
834 }
835 }
836
837 /*
838 * Compile the body.
839 *
840 * [Bug 2037727] Always call TclProcCompileProc so that we check not only
841 * that we have bytecode, but also that it remains valid. Note that we set
842 * the namespace of the code here directly; this is a hack, but the
843 * alternative is *so* slow...
844 */
845
846 pmPtr->procPtr->cmdPtr = &pmPtr->cmd;
847 if (pmPtr->procPtr->bodyPtr->typePtr == &tclByteCodeType) {
848 ByteCode *codePtr = (ByteCode *)
849 pmPtr->procPtr->bodyPtr->internalRep.twoPtrValue.ptr1;
850
851 codePtr->nsPtr = nsPtr;
852 }
853 result = TclProcCompileProc(interp, pmPtr->procPtr,
854 pmPtr->procPtr->bodyPtr, nsPtr, "body of method",
855 TclGetString(fdPtr->nameObj));
856 if (result != TCL_OK) {
857 return result;
858 }
859
860 /*
861 * Make the stack frame and fill it out with information about this call.
862 * This operation doesn't ever actually fail.
863 */
864
865 (void) TclPushStackFrame(interp, (Tcl_CallFrame **) framePtrPtr,
866 (Tcl_Namespace *) nsPtr, FRAME_IS_PROC|FRAME_IS_METHOD);
867
868 fdPtr->framePtr->clientData = contextPtr;
869 fdPtr->framePtr->objc = objc;
870 fdPtr->framePtr->objv = objv;
871 fdPtr->framePtr->procPtr = pmPtr->procPtr;
872
873 return TCL_OK;
874 }
875
876 /*
877 * ----------------------------------------------------------------------
878 *
879 * TclOOSetupVariableResolver, etc. --
880 *
881 * Variable resolution engine used to connect declared variables to local
882 * variables used in methods. The compiled variable resolver is more
883 * important, but both are needed as it is possible to have a variable
884 * that is only referred to in ways that aren't compilable and we can't
885 * force LVT presence. [TIP #320]
886 *
887 * ----------------------------------------------------------------------
888 */
889
890 void
891 TclOOSetupVariableResolver(
892 Tcl_Namespace *nsPtr)
893 {
894 Tcl_ResolverInfo info;
895
896 Tcl_GetNamespaceResolvers(nsPtr, &info);
897 if (info.compiledVarResProc == NULL) {
898 Tcl_SetNamespaceResolvers(nsPtr, NULL, ProcedureMethodVarResolver,
899 ProcedureMethodCompiledVarResolver);
900 }
901 }
902
903 static int
904 ProcedureMethodVarResolver(
905 Tcl_Interp *interp,
906 const char *varName,
907 Tcl_Namespace *contextNs,
908 int flags,
909 Tcl_Var *varPtr)
910 {
911 int result;
912 Tcl_ResolvedVarInfo *rPtr = NULL;
913
914 result = ProcedureMethodCompiledVarResolver(interp, varName,
915 strlen(varName), contextNs, &rPtr);
916
917 if (result != TCL_OK) {
918 return result;
919 }
920
921 *varPtr = rPtr->fetchProc(interp, rPtr);
922
923 /*
924 * Must not retain reference to resolved information. [Bug 3105999]
925 */
926
927 rPtr->deleteProc(rPtr);
928 return (*varPtr ? TCL_OK : TCL_CONTINUE);
929 }
930
931 static Tcl_Var
932 ProcedureMethodCompiledVarConnect(
933 Tcl_Interp *interp,
934 Tcl_ResolvedVarInfo *rPtr)
935 {
936 OOResVarInfo *infoPtr = (OOResVarInfo *) rPtr;
937 Interp *iPtr = (Interp *) interp;
938 CallFrame *framePtr = iPtr->varFramePtr;
939 CallContext *contextPtr;
940 Tcl_Obj *variableObj;
941 Tcl_HashEntry *hPtr;
942 int i, isNew, cacheIt, varLen, len;
943 const char *match, *varName;
944
945 /*
946 * Check that the variable is being requested in a context that is also a
947 * method call; if not (i.e. we're evaluating in the object's namespace or
948 * in a procedure of that namespace) then we do nothing.
949 */
950
951 if (framePtr == NULL || !(framePtr->isProcCallFrame & FRAME_IS_METHOD)) {
952 return NULL;
953 }
954 contextPtr = (CallContext *)framePtr->clientData;
955
956 /*
957 * If we've done the work before (in a comparable context) then reuse that
958 * rather than performing resolution ourselves.
959 */
960
961 if (infoPtr->cachedObjectVar) {
962 return infoPtr->cachedObjectVar;
963 }
964
965 /*
966 * Check if the variable is one we want to resolve at all (i.e. whether it
967 * is in the list provided by the user). If not, we mustn't do anything
968 * either.
969 */
970
971 varName = TclGetStringFromObj(infoPtr->variableObj, &varLen);
972 if (contextPtr->callPtr->chain[contextPtr->index]
973 .mPtr->declaringClassPtr != NULL) {
974 FOREACH(variableObj, contextPtr->callPtr->chain[contextPtr->index]
975 .mPtr->declaringClassPtr->variables) {
976 match = TclGetStringFromObj(variableObj, &len);
977 if ((len == varLen) && !memcmp(match, varName, len)) {
978 cacheIt = 0;
979 goto gotMatch;
980 }
981 }
982 } else {
983 FOREACH(variableObj, contextPtr->oPtr->variables) {
984 match = TclGetStringFromObj(variableObj, &len);
985 if ((len == varLen) && !memcmp(match, varName, len)) {
986 cacheIt = 1;
987 goto gotMatch;
988 }
989 }
990 }
991 return NULL;
992
993 /*
994 * It is a variable we want to resolve, so resolve it.
995 */
996
997 gotMatch:
998 hPtr = Tcl_CreateHashEntry(TclVarTable(contextPtr->oPtr->namespacePtr),
999 (char *) variableObj, &isNew);
1000 if (isNew) {
1001 TclSetVarNamespaceVar((Var *) TclVarHashGetValue(hPtr));
1002 }
1003 if (cacheIt) {
1004 infoPtr->cachedObjectVar = TclVarHashGetValue(hPtr);
1005
1006 /*
1007 * We must keep a reference to the variable so everything will
1008 * continue to work correctly even if it is unset; being unset does
1009 * not end the life of the variable at this level. [Bug 3185009]
1010 */
1011
1012 VarHashRefCount(infoPtr->cachedObjectVar)++;
1013 }
1014 return TclVarHashGetValue(hPtr);
1015 }
1016
1017 static void
1018 ProcedureMethodCompiledVarDelete(
1019 Tcl_ResolvedVarInfo *rPtr)
1020 {
1021 OOResVarInfo *infoPtr = (OOResVarInfo *) rPtr;
1022
1023 /*
1024 * Release the reference to the variable if we were holding it.
1025 */
1026
1027 if (infoPtr->cachedObjectVar) {
1028 VarHashRefCount(infoPtr->cachedObjectVar)--;
1029 TclCleanupVar((Var *) infoPtr->cachedObjectVar, NULL);
1030 }
1031 Tcl_DecrRefCount(infoPtr->variableObj);
1032 ckfree(infoPtr);
1033 }
1034
1035 static int
1036 ProcedureMethodCompiledVarResolver(
1037 Tcl_Interp *interp,
1038 const char *varName,
1039 int length,
1040 Tcl_Namespace *contextNs,
1041 Tcl_ResolvedVarInfo **rPtrPtr)
1042 {
1043 OOResVarInfo *infoPtr;
1044 Tcl_Obj *variableObj = Tcl_NewStringObj(varName, length);
1045
1046 /*
1047 * Do not create resolvers for cases that contain namespace separators or
1048 * which look like array accesses. Both will lead us astray.
1049 */
1050
1051 if (strstr(Tcl_GetString(variableObj), "::") != NULL ||
1052 Tcl_StringMatch(Tcl_GetString(variableObj), "*(*)")) {
1053 Tcl_DecrRefCount(variableObj);
1054 return TCL_CONTINUE;
1055 }
1056
1057 infoPtr = (OOResVarInfo *)ckalloc(sizeof(OOResVarInfo));
1058 infoPtr->info.fetchProc = ProcedureMethodCompiledVarConnect;
1059 infoPtr->info.deleteProc = ProcedureMethodCompiledVarDelete;
1060 infoPtr->cachedObjectVar = NULL;
1061 infoPtr->variableObj = variableObj;
1062 Tcl_IncrRefCount(variableObj);
1063 *rPtrPtr = &infoPtr->info;
1064 return TCL_OK;
1065 }
1066
1067 /*
1068 * ----------------------------------------------------------------------
1069 *
1070 * RenderMethodName --
1071 *
1072 * Returns the name of the declared method. Used for producing information
1073 * for [info frame].
1074 *
1075 * ----------------------------------------------------------------------
1076 */
1077
1078 static Tcl_Obj *
1079 RenderMethodName(
1080 void *clientData)
1081 {
1082 ProcedureMethod *pmPtr = (ProcedureMethod *) clientData;
1083
1084 if (pmPtr->callSiteFlags & CONSTRUCTOR) {
1085 return TclOOGetFoundation(pmPtr->interp)->constructorName;
1086 } else if (pmPtr->callSiteFlags & DESTRUCTOR) {
1087 return TclOOGetFoundation(pmPtr->interp)->destructorName;
1088 } else {
1089 return Tcl_MethodName(pmPtr->method);
1090 }
1091 }
1092
1093 /*
1094 * ----------------------------------------------------------------------
1095 *
1096 * RenderDeclarerName --
1097 *
1098 * Returns the name of the entity (object or class) which declared a
1099 * method. Used for producing information for [info frame] in such a way
1100 * that the expensive part of this (generating the object or class name
1101 * itself) isn't done until it is needed.
1102 *
1103 * ----------------------------------------------------------------------
1104 */
1105
1106 static Tcl_Obj *
1107 RenderDeclarerName(
1108 void *clientData)
1109 {
1110 ProcedureMethod *pmPtr = (ProcedureMethod *) clientData;
1111 Tcl_Object object = Tcl_MethodDeclarerObject(pmPtr->method);
1112
1113 if (object == NULL) {
1114 object = Tcl_GetClassAsObject(Tcl_MethodDeclarerClass(pmPtr->method));
1115 }
1116 return TclOOObjectName(pmPtr->interp, (Object *) object);
1117 }
1118
1119 /*
1120 * ----------------------------------------------------------------------
1121 *
1122 * MethodErrorHandler, ConstructorErrorHandler, DestructorErrorHandler --
1123 *
1124 * How to fill in the stack trace correctly upon error in various forms
1125 * of procedure-like methods. LIMIT is how long the inserted strings in
1126 * the error traces should get before being converted to have ellipses,
1127 * and ELLIPSIFY is a macro to do the conversion (with the help of a
1128 * %.*s%s format field). Note that ELLIPSIFY is only safe for use in
1129 * suitable formatting contexts.
1130 *
1131 * ----------------------------------------------------------------------
1132 */
1133
1134 #define LIMIT 60
1135 #define ELLIPSIFY(str,len) \
1136 ((len) > LIMIT ? LIMIT : (len)), (str), ((len) > LIMIT ? "..." : "")
1137
1138 static void
1139 CommonMethErrorHandler(
1140 Tcl_Interp *interp,
1141 const char *special)
1142 {
1143 int objectNameLen;
1144 CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData;
1145 Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr;
1146 const char *objectName, *kindName = "instance";
1147 Object *declarerPtr = NULL;
1148
1149 if (mPtr->declaringObjectPtr != NULL) {
1150 declarerPtr = mPtr->declaringObjectPtr;
1151 kindName = "object";
1152 } else if (mPtr->declaringClassPtr != NULL) {
1153 declarerPtr = mPtr->declaringClassPtr->thisPtr;
1154 kindName = "class";
1155 }
1156
1157 if (declarerPtr) {
1158 objectName = Tcl_GetStringFromObj(TclOOObjectName(interp, declarerPtr),
1159 &objectNameLen);
1160 } else {
1161 objectName = "unknown or deleted";
1162 objectNameLen = 18;
1163 }
1164 if (!special) {
1165 int nameLen;
1166 const char *methodName = Tcl_GetStringFromObj(mPtr->namePtr, &nameLen);
1167 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
1168 "\n (%s \"%.*s%s\" method \"%.*s%s\" line %d)",
1169 kindName, ELLIPSIFY(objectName, objectNameLen),
1170 ELLIPSIFY(methodName, nameLen), Tcl_GetErrorLine(interp)));
1171 } else {
1172 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
1173 "\n (%s \"%.*s%s\" %s line %d)", kindName,
1174 ELLIPSIFY(objectName, objectNameLen), special, Tcl_GetErrorLine(interp)));
1175 }
1176 }
1177
1178 static void
1179 MethodErrorHandler(
1180 Tcl_Interp *interp,
1181 Tcl_Obj *methodNameObj)
1182 {
1183 (void)methodNameObj;/* We pull the method name out of context instead of from argument */
1184 CommonMethErrorHandler(interp, NULL);
1185 }
1186
1187 static void
1188 ConstructorErrorHandler(
1189 Tcl_Interp *interp,
1190 Tcl_Obj *methodNameObj)
1191 {
1192 (void)methodNameObj;/* Ignore. We know it is the constructor. */
1193 CommonMethErrorHandler(interp, "constructor");
1194 }
1195
1196 static void
1197 DestructorErrorHandler(
1198 Tcl_Interp *interp,
1199 Tcl_Obj *methodNameObj)
1200 {
1201 (void)methodNameObj; /* Ignore. We know it is the destructor. */
1202 CommonMethErrorHandler(interp, "destructor");
1203 }
1204
1205 /*
1206 * ----------------------------------------------------------------------
1207 *
1208 * DeleteProcedureMethod, CloneProcedureMethod --
1209 *
1210 * How to delete and clone procedure-like methods.
1211 *
1212 * ----------------------------------------------------------------------
1213 */
1214
1215 static void
1216 DeleteProcedureMethodRecord(
1217 ProcedureMethod *pmPtr)
1218 {
1219 TclProcDeleteProc(pmPtr->procPtr);
1220 if (pmPtr->deleteClientdataProc) {
1221 pmPtr->deleteClientdataProc(pmPtr->clientData);
1222 }
1223 ckfree(pmPtr);
1224 }
1225
1226 static void
1227 DeleteProcedureMethod(
1228 void *clientData)
1229 {
1230 ProcedureMethod *pmPtr = (ProcedureMethod *)clientData;
1231
1232 if (pmPtr->refCount-- <= 1) {
1233 DeleteProcedureMethodRecord(pmPtr);
1234 }
1235 }
1236
1237 static int
1238 CloneProcedureMethod(
1239 Tcl_Interp *interp,
1240 void *clientData,
1241 void **newClientData)
1242 {
1243 ProcedureMethod *pmPtr = (ProcedureMethod *)clientData;
1244 ProcedureMethod *pm2Ptr;
1245 Tcl_Obj *bodyObj, *argsObj;
1246 CompiledLocal *localPtr;
1247
1248 /*
1249 * Copy the argument list.
1250 */
1251
1252 TclNewObj(argsObj);
1253 for (localPtr=pmPtr->procPtr->firstLocalPtr; localPtr!=NULL;
1254 localPtr=localPtr->nextPtr) {
1255 if (TclIsVarArgument(localPtr)) {
1256 Tcl_Obj *argObj;
1257
1258 TclNewObj(argObj);
1259 Tcl_ListObjAppendElement(NULL, argObj,
1260 Tcl_NewStringObj(localPtr->name, -1));
1261 if (localPtr->defValuePtr != NULL) {
1262 Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr);
1263 }
1264 Tcl_ListObjAppendElement(NULL, argsObj, argObj);
1265 }
1266 }
1267
1268 /*
1269 * Must strip the internal representation in order to ensure that any
1270 * bound references to instance variables are removed. [Bug 3609693]
1271 */
1272
1273 bodyObj = Tcl_DuplicateObj(pmPtr->procPtr->bodyPtr);
1274 Tcl_GetString(bodyObj);
1275 TclFreeIntRep(bodyObj);
1276
1277 /*
1278 * Create the actual copy of the method record, manufacturing a new proc
1279 * record.
1280 */
1281
1282 pm2Ptr = (ProcedureMethod *)ckalloc(sizeof(ProcedureMethod));
1283 memcpy(pm2Ptr, pmPtr, sizeof(ProcedureMethod));
1284 pm2Ptr->refCount = 1;
1285 pm2Ptr->cmd.clientData = &pm2Ptr->efi;
1286 pm2Ptr->efi.length = 0; /* Trigger a reinit of this. */
1287 Tcl_IncrRefCount(argsObj);
1288 Tcl_IncrRefCount(bodyObj);
1289 if (TclCreateProc(interp, NULL, "", argsObj, bodyObj,
1290 &pm2Ptr->procPtr) != TCL_OK) {
1291 Tcl_DecrRefCount(argsObj);
1292 Tcl_DecrRefCount(bodyObj);
1293 ckfree(pm2Ptr);
1294 return TCL_ERROR;
1295 }
1296 Tcl_DecrRefCount(argsObj);
1297 Tcl_DecrRefCount(bodyObj);
1298
1299 if (pmPtr->cloneClientdataProc) {
1300 pm2Ptr->clientData = pmPtr->cloneClientdataProc(pmPtr->clientData);
1301 }
1302 *newClientData = pm2Ptr;
1303 return TCL_OK;
1304 }
1305
1306 /*
1307 * ----------------------------------------------------------------------
1308 *
1309 * TclOONewForwardInstanceMethod --
1310 *
1311 * Create a forwarded method for an object.
1312 *
1313 * ----------------------------------------------------------------------
1314 */
1315
1316 Method *
1317 TclOONewForwardInstanceMethod(
1318 Tcl_Interp *interp, /* Interpreter for error reporting. */
1319 Object *oPtr, /* The object to attach the method to. */
1320 int flags, /* Whether the method is public or not. */
1321 Tcl_Obj *nameObj, /* The name of the method. */
1322 Tcl_Obj *prefixObj) /* List of arguments that form the command
1323 * prefix to forward to. */
1324 {
1325 int prefixLen;
1326 ForwardMethod *fmPtr;
1327
1328 if (TclListObjLength(interp, prefixObj, &prefixLen) != TCL_OK) {
1329 return NULL;
1330 }
1331 if (prefixLen < 1) {
1332 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1333 "method forward prefix must be non-empty", -1));
1334 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_FORWARD", (char *)NULL);
1335 return NULL;
1336 }
1337
1338 fmPtr = (ForwardMethod *)ckalloc(sizeof(ForwardMethod));
1339 fmPtr->prefixObj = prefixObj;
1340 Tcl_IncrRefCount(prefixObj);
1341 return (Method *) Tcl_NewInstanceMethod(interp, (Tcl_Object) oPtr,
1342 nameObj, flags, &fwdMethodType, fmPtr);
1343 }
1344
1345 /*
1346 * ----------------------------------------------------------------------
1347 *
1348 * TclOONewForwardMethod --
1349 *
1350 * Create a new forwarded method for a class.
1351 *
1352 * ----------------------------------------------------------------------
1353 */
1354
1355 Method *
1356 TclOONewForwardMethod(
1357 Tcl_Interp *interp, /* Interpreter for error reporting. */
1358 Class *clsPtr, /* The class to attach the method to. */
1359 int flags, /* Whether the method is public or not. */
1360 Tcl_Obj *nameObj, /* The name of the method. */
1361 Tcl_Obj *prefixObj) /* List of arguments that form the command
1362 * prefix to forward to. */
1363 {
1364 int prefixLen;
1365 ForwardMethod *fmPtr;
1366
1367 if (TclListObjLength(interp, prefixObj, &prefixLen) != TCL_OK) {
1368 return NULL;
1369 }
1370 if (prefixLen < 1) {
1371 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1372 "method forward prefix must be non-empty", -1));
1373 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_FORWARD", (char *)NULL);
1374 return NULL;
1375 }
1376
1377 fmPtr = (ForwardMethod *)ckalloc(sizeof(ForwardMethod));
1378 fmPtr->prefixObj = prefixObj;
1379 Tcl_IncrRefCount(prefixObj);
1380 return (Method *) Tcl_NewMethod(interp, (Tcl_Class) clsPtr, nameObj,
1381 flags, &fwdMethodType, fmPtr);
1382 }
1383
1384 /*
1385 * ----------------------------------------------------------------------
1386 *
1387 * InvokeForwardMethod --
1388 *
1389 * How to invoke a forwarded method. Works by doing some ensemble-like
1390 * command rearranging and then invokes some other Tcl command.
1391 *
1392 * ----------------------------------------------------------------------
1393 */
1394
1395 static int
1396 InvokeForwardMethod(
1397 void *clientData, /* Pointer to some per-method context. */
1398 Tcl_Interp *interp,
1399 Tcl_ObjectContext context, /* The method calling context. */
1400 int objc, /* Number of arguments. */
1401 Tcl_Obj *const *objv) /* Arguments as actually seen. */
1402 {
1403 CallContext *contextPtr = (CallContext *) context;
1404 ForwardMethod *fmPtr = (ForwardMethod *)clientData;
1405 Tcl_Obj **argObjs, **prefixObjs;
1406 int numPrefixes, len, skip = contextPtr->skip;
1407
1408 /*
1409 * Build the real list of arguments to use. Note that we know that the
1410 * prefixObj field of the ForwardMethod structure holds a reference to a
1411 * non-empty list, so there's a whole class of failures ("not a list") we
1412 * can ignore here.
1413 */
1414
1415 TclListObjGetElements(NULL, fmPtr->prefixObj, &numPrefixes, &prefixObjs);
1416 argObjs = InitEnsembleRewrite(interp, objc, objv, skip,
1417 numPrefixes, prefixObjs, &len);
1418 Tcl_NRAddCallback(interp, FinalizeForwardCall, argObjs, NULL, NULL, NULL);
1419 /*
1420 * NOTE: The combination of direct set of iPtr->lookupNsPtr and the use
1421 * of the TCL_EVAL_NOERR flag results in an evaluation configuration
1422 * very much like TCL_EVAL_INVOKE.
1423 */
1424 ((Interp *)interp)->lookupNsPtr
1425 = (Namespace *) contextPtr->oPtr->namespacePtr;
1426 return TclNREvalObjv(interp, len, argObjs, TCL_EVAL_NOERR, NULL);
1427 }
1428
1429 static int
1430 FinalizeForwardCall(
1431 void *data[],
1432 Tcl_Interp *interp,
1433 int result)
1434 {
1435 Tcl_Obj **argObjs = (Tcl_Obj **)data[0];
1436
1437 TclStackFree(interp, argObjs);
1438 return result;
1439 }
1440
1441 /*
1442 * ----------------------------------------------------------------------
1443 *
1444 * DeleteForwardMethod, CloneForwardMethod --
1445 *
1446 * How to delete and clone forwarded methods.
1447 *
1448 * ----------------------------------------------------------------------
1449 */
1450
1451 static void
1452 DeleteForwardMethod(
1453 void *clientData)
1454 {
1455 ForwardMethod *fmPtr = (ForwardMethod *)clientData;
1456
1457 Tcl_DecrRefCount(fmPtr->prefixObj);
1458 ckfree(fmPtr);
1459 }
1460
1461 static int
1462 CloneForwardMethod(
1463 Tcl_Interp *interp,
1464 void *clientData,
1465 void **newClientData)
1466 {
1467 ForwardMethod *fmPtr = (ForwardMethod *)clientData;
1468 ForwardMethod *fm2Ptr = (ForwardMethod *)ckalloc(sizeof(ForwardMethod));
1469
1470 fm2Ptr->prefixObj = fmPtr->prefixObj;
1471 Tcl_IncrRefCount(fm2Ptr->prefixObj);
1472 *newClientData = fm2Ptr;
1473 return TCL_OK;
1474 }
1475
1476 /*
1477 * ----------------------------------------------------------------------
1478 *
1479 * TclOOGetProcFromMethod, TclOOGetFwdFromMethod --
1480 *
1481 * Utility functions used for procedure-like and forwarding method
1482 * introspection.
1483 *
1484 * ----------------------------------------------------------------------
1485 */
1486
1487 Proc *
1488 TclOOGetProcFromMethod(
1489 Method *mPtr)
1490 {
1491 if (mPtr->typePtr == &procMethodType) {
1492 ProcedureMethod *pmPtr = (ProcedureMethod *)mPtr->clientData;
1493
1494 return pmPtr->procPtr;
1495 }
1496 return NULL;
1497 }
1498
1499 Tcl_Obj *
1500 TclOOGetMethodBody(
1501 Method *mPtr)
1502 {
1503 if (mPtr->typePtr == &procMethodType) {
1504 ProcedureMethod *pmPtr = (ProcedureMethod *)mPtr->clientData;
1505
1506 if (pmPtr->procPtr->bodyPtr->bytes == NULL) {
1507 (void) Tcl_GetString(pmPtr->procPtr->bodyPtr);
1508 }
1509 return pmPtr->procPtr->bodyPtr;
1510 }
1511 return NULL;
1512 }
1513
1514 Tcl_Obj *
1515 TclOOGetFwdFromMethod(
1516 Method *mPtr)
1517 {
1518 if (mPtr->typePtr == &fwdMethodType) {
1519 ForwardMethod *fwPtr = (ForwardMethod *)mPtr->clientData;
1520
1521 return fwPtr->prefixObj;
1522 }
1523 return NULL;
1524 }
1525
1526 /*
1527 * ----------------------------------------------------------------------
1528 *
1529 * InitEnsembleRewrite --
1530 *
1531 * Utility function that wraps up a lot of the complexity involved in
1532 * doing ensemble-like command forwarding. Here is a picture of memory
1533 * management plan:
1534 *
1535 * <-----------------objc---------------------->
1536 * objv: |=============|===============================|
1537 * <-toRewrite-> |
1538 * \
1539 * <-rewriteLength-> \
1540 * rewriteObjs: |=================| \
1541 * | |
1542 * V V
1543 * argObjs: |=================|===============================|
1544 * <------------------*lengthPtr------------------->
1545 *
1546 * ----------------------------------------------------------------------
1547 */
1548
1549 static Tcl_Obj **
1550 InitEnsembleRewrite(
1551 Tcl_Interp *interp, /* Place to log the rewrite info. */
1552 int objc, /* Number of real arguments. */
1553 Tcl_Obj *const *objv, /* The real arguments. */
1554 int toRewrite, /* Number of real arguments to replace. */
1555 int rewriteLength, /* Number of arguments to insert instead. */
1556 Tcl_Obj *const *rewriteObjs,/* Arguments to insert instead. */
1557 int *lengthPtr) /* Where to write the resulting length of the
1558 * array of rewritten arguments. */
1559 {
1560 unsigned len = rewriteLength + objc - toRewrite;
1561 Tcl_Obj **argObjs = (Tcl_Obj **)TclStackAlloc(interp, sizeof(Tcl_Obj *) * len);
1562
1563 memcpy(argObjs, rewriteObjs, rewriteLength * sizeof(Tcl_Obj *));
1564 memcpy(argObjs + rewriteLength, objv + toRewrite,
1565 sizeof(Tcl_Obj *) * (objc - toRewrite));
1566
1567 /*
1568 * Now plumb this into the core ensemble rewrite logging system so that
1569 * Tcl_WrongNumArgs() can rewrite its result appropriately. The rules for
1570 * how to store the rewrite rules get complex solely because of the case
1571 * where an ensemble rewrites itself out of the picture; when that
1572 * happens, the quality of the error message rewrite falls drastically
1573 * (and unavoidably).
1574 */
1575
1576 if (TclInitRewriteEnsemble(interp, toRewrite, rewriteLength, objv)) {
1577 TclNRAddCallback(interp, TclClearRootEnsemble, NULL, NULL, NULL, NULL);
1578 }
1579 *lengthPtr = len;
1580 return argObjs;
1581 }
1582
1583 /*
1584 * ----------------------------------------------------------------------
1585 *
1586 * assorted trivial 'getter' functions
1587 *
1588 * ----------------------------------------------------------------------
1589 */
1590
1591 Tcl_Object
1592 Tcl_MethodDeclarerObject(
1593 Tcl_Method method)
1594 {
1595 return (Tcl_Object) ((Method *) method)->declaringObjectPtr;
1596 }
1597
1598 Tcl_Class
1599 Tcl_MethodDeclarerClass(
1600 Tcl_Method method)
1601 {
1602 return (Tcl_Class) ((Method *) method)->declaringClassPtr;
1603 }
1604
1605 Tcl_Obj *
1606 Tcl_MethodName(
1607 Tcl_Method method)
1608 {
1609 return ((Method *) method)->namePtr;
1610 }
1611
1612 int
1613 Tcl_MethodIsType(
1614 Tcl_Method method,
1615 const Tcl_MethodType *typePtr,
1616 void **clientDataPtr)
1617 {
1618 Method *mPtr = (Method *) method;
1619
1620 if (mPtr->typePtr == typePtr) {
1621 if (clientDataPtr != NULL) {
1622 *clientDataPtr = mPtr->clientData;
1623 }
1624 return 1;
1625 }
1626 return 0;
1627 }
1628
1629 int
1630 Tcl_MethodIsPublic(
1631 Tcl_Method method)
1632 {
1633 return (((Method *)method)->flags & PUBLIC_METHOD) ? 1 : 0;
1634 }
1635
1636 /*
1637 * Extended method construction for itcl-ng.
1638 */
1639
1640 Tcl_Method
1641 TclOONewProcInstanceMethodEx(
1642 Tcl_Interp *interp, /* The interpreter containing the object. */
1643 Tcl_Object oPtr, /* The object to modify. */
1644 TclOO_PreCallProc *preCallPtr,
1645 TclOO_PostCallProc *postCallPtr,
1646 ProcErrorProc *errProc,
1647 void *clientData,
1648 Tcl_Obj *nameObj, /* The name of the method, which must not be
1649 * NULL. */
1650 Tcl_Obj *argsObj, /* The formal argument list for the method,
1651 * which must not be NULL. */
1652 Tcl_Obj *bodyObj, /* The body of the method, which must not be
1653 * NULL. */
1654 int flags, /* Whether this is a public method. */
1655 void **internalTokenPtr) /* If non-NULL, points to a variable that gets
1656 * the reference to the ProcedureMethod
1657 * structure. */
1658 {
1659 ProcedureMethod *pmPtr;
1660 Tcl_Method method = (Tcl_Method) TclOONewProcInstanceMethod(interp,
1661 (Object *) oPtr, flags, nameObj, argsObj, bodyObj, &pmPtr);
1662
1663 if (method == NULL) {
1664 return NULL;
1665 }
1666 pmPtr->flags = flags & USE_DECLARER_NS;
1667 pmPtr->preCallProc = preCallPtr;
1668 pmPtr->postCallProc = postCallPtr;
1669 pmPtr->errProc = errProc;
1670 pmPtr->clientData = clientData;
1671 if (internalTokenPtr != NULL) {
1672 *internalTokenPtr = pmPtr;
1673 }
1674 return method;
1675 }
1676
1677 Tcl_Method
1678 TclOONewProcMethodEx(
1679 Tcl_Interp *interp, /* The interpreter containing the class. */
1680 Tcl_Class clsPtr, /* The class to modify. */
1681 TclOO_PreCallProc *preCallPtr,
1682 TclOO_PostCallProc *postCallPtr,
1683 ProcErrorProc *errProc,
1684 void *clientData,
1685 Tcl_Obj *nameObj, /* The name of the method, which may be NULL;
1686 * if so, up to caller to manage storage
1687 * (e.g., because it is a constructor or
1688 * destructor). */
1689 Tcl_Obj *argsObj, /* The formal argument list for the method,
1690 * which may be NULL; if so, it is equivalent
1691 * to an empty list. */
1692 Tcl_Obj *bodyObj, /* The body of the method, which must not be
1693 * NULL. */
1694 int flags, /* Whether this is a public method. */
1695 void **internalTokenPtr) /* If non-NULL, points to a variable that gets
1696 * the reference to the ProcedureMethod
1697 * structure. */
1698 {
1699 ProcedureMethod *pmPtr;
1700 Tcl_Method method = (Tcl_Method) TclOONewProcMethod(interp,
1701 (Class *) clsPtr, flags, nameObj, argsObj, bodyObj, &pmPtr);
1702
1703 if (method == NULL) {
1704 return NULL;
1705 }
1706 pmPtr->flags = flags & USE_DECLARER_NS;
1707 pmPtr->preCallProc = preCallPtr;
1708 pmPtr->postCallProc = postCallPtr;
1709 pmPtr->errProc = errProc;
1710 pmPtr->clientData = clientData;
1711 if (internalTokenPtr != NULL) {
1712 *internalTokenPtr = pmPtr;
1713 }
1714 return method;
1715 }
1716
1717 /*
1718 * Local Variables:
1719 * mode: c
1720 * c-basic-offset: 4
1721 * fill-column: 78
1722 * End:
1723 */