Debian Codegraph @

generic/tclOODefineCmds.c

raw
1 /*
2 * tclOODefineCmds.c --
3 *
4 * This file contains the implementation of the ::oo::define command,
5 * part of the object-system core (NB: not Tcl_Obj, but ::oo).
6 *
7 * Copyright (c) 2006-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 /*
20 * The maximum length of fully-qualified object name to use in an errorinfo
21 * message. Longer than this will be curtailed.
22 */
23
24 #define OBJNAME_LENGTH_IN_ERRORINFO_LIMIT 30
25
26 /*
27 * Some things that make it easier to declare a slot.
28 */
29
30 struct DeclaredSlot {
31 const char *name;
32 const Tcl_MethodType getterType;
33 const Tcl_MethodType setterType;
34 };
35
36 #define SLOT(name,getter,setter) \
37 {"::oo::" name, \
38 {TCL_OO_METHOD_VERSION_CURRENT, "core method: " name " Getter", \
39 getter, NULL, NULL}, \
40 {TCL_OO_METHOD_VERSION_CURRENT, "core method: " name " Setter", \
41 setter, NULL, NULL}}
42
43 /*
44 * A [string match] pattern used to determine if a method should be exported.
45 */
46
47 #define PUBLIC_PATTERN "[a-z]*"
48
49 /*
50 * Forward declarations.
51 */
52
53 static inline void BumpGlobalEpoch(Tcl_Interp *interp, Class *classPtr);
54 static Tcl_Command FindCommand(Tcl_Interp *interp, Tcl_Obj *stringObj,
55 Tcl_Namespace *const namespacePtr);
56 static inline void GenerateErrorInfo(Tcl_Interp *interp, Object *oPtr,
57 Tcl_Obj *savedNameObj, const char *typeOfSubject);
58 static inline int MagicDefinitionInvoke(Tcl_Interp *interp,
59 Tcl_Namespace *nsPtr, int cmdIndex,
60 int objc, Tcl_Obj *const *objv);
61 static inline Class * GetClassInOuterContext(Tcl_Interp *interp,
62 Tcl_Obj *className, const char *errMsg);
63 static inline int InitDefineContext(Tcl_Interp *interp,
64 Tcl_Namespace *namespacePtr, Object *oPtr,
65 int objc, Tcl_Obj *const objv[]);
66 static inline void RecomputeClassCacheFlag(Object *oPtr);
67 static int RenameDeleteMethod(Tcl_Interp *interp, Object *oPtr,
68 int useClass, Tcl_Obj *const fromPtr,
69 Tcl_Obj *const toPtr);
70 static int ClassFilterGet(ClientData clientData,
71 Tcl_Interp *interp, Tcl_ObjectContext context,
72 int objc, Tcl_Obj *const *objv);
73 static int ClassFilterSet(ClientData clientData,
74 Tcl_Interp *interp, Tcl_ObjectContext context,
75 int objc, Tcl_Obj *const *objv);
76 static int ClassMixinGet(ClientData clientData,
77 Tcl_Interp *interp, Tcl_ObjectContext context,
78 int objc, Tcl_Obj *const *objv);
79 static int ClassMixinSet(ClientData clientData,
80 Tcl_Interp *interp, Tcl_ObjectContext context,
81 int objc, Tcl_Obj *const *objv);
82 static int ClassSuperGet(ClientData clientData,
83 Tcl_Interp *interp, Tcl_ObjectContext context,
84 int objc, Tcl_Obj *const *objv);
85 static int ClassSuperSet(ClientData clientData,
86 Tcl_Interp *interp, Tcl_ObjectContext context,
87 int objc, Tcl_Obj *const *objv);
88 static int ClassVarsGet(ClientData clientData,
89 Tcl_Interp *interp, Tcl_ObjectContext context,
90 int objc, Tcl_Obj *const *objv);
91 static int ClassVarsSet(ClientData clientData,
92 Tcl_Interp *interp, Tcl_ObjectContext context,
93 int objc, Tcl_Obj *const *objv);
94 static int ObjFilterGet(ClientData clientData,
95 Tcl_Interp *interp, Tcl_ObjectContext context,
96 int objc, Tcl_Obj *const *objv);
97 static int ObjFilterSet(ClientData clientData,
98 Tcl_Interp *interp, Tcl_ObjectContext context,
99 int objc, Tcl_Obj *const *objv);
100 static int ObjMixinGet(ClientData clientData,
101 Tcl_Interp *interp, Tcl_ObjectContext context,
102 int objc, Tcl_Obj *const *objv);
103 static int ObjMixinSet(ClientData clientData,
104 Tcl_Interp *interp, Tcl_ObjectContext context,
105 int objc, Tcl_Obj *const *objv);
106 static int ObjVarsGet(ClientData clientData,
107 Tcl_Interp *interp, Tcl_ObjectContext context,
108 int objc, Tcl_Obj *const *objv);
109 static int ObjVarsSet(ClientData clientData,
110 Tcl_Interp *interp, Tcl_ObjectContext context,
111 int objc, Tcl_Obj *const *objv);
112
113 /*
114 * Now define the slots used in declarations.
115 */
116
117 static const struct DeclaredSlot slots[] = {
118 SLOT("define::filter", ClassFilterGet, ClassFilterSet),
119 SLOT("define::mixin", ClassMixinGet, ClassMixinSet),
120 SLOT("define::superclass", ClassSuperGet, ClassSuperSet),
121 SLOT("define::variable", ClassVarsGet, ClassVarsSet),
122 SLOT("objdefine::filter", ObjFilterGet, ObjFilterSet),
123 SLOT("objdefine::mixin", ObjMixinGet, ObjMixinSet),
124 SLOT("objdefine::variable", ObjVarsGet, ObjVarsSet),
125 {NULL, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}
126 };
127
128 /*
129 * ----------------------------------------------------------------------
130 *
131 * BumpGlobalEpoch --
132 * Utility that ensures that call chains that are invalid will get thrown
133 * away at an appropriate time. Note that exactly which epoch gets
134 * advanced will depend on exactly what the class is tangled up in; in
135 * the worst case, the simplest option is to advance the global epoch,
136 * causing *everything* to be thrown away on next usage.
137 *
138 * ----------------------------------------------------------------------
139 */
140
141 static inline void
142 BumpGlobalEpoch(
143 Tcl_Interp *interp,
144 Class *classPtr)
145 {
146 if (classPtr != NULL
147 && classPtr->subclasses.num == 0
148 && classPtr->instances.num == 0
149 && classPtr->mixinSubs.num == 0) {
150 /*
151 * If a class has no subclasses or instances, and is not mixed into
152 * anything, a change to its structure does not require us to
153 * invalidate any call chains. Note that we still bump our object's
154 * epoch if it has any mixins; the relation between a class and its
155 * representative object is special. But it won't hurt.
156 */
157
158 if (classPtr->thisPtr->mixins.num > 0) {
159 classPtr->thisPtr->epoch++;
160 }
161 return;
162 }
163
164 /*
165 * Either there's no class (?!) or we're reconfiguring something that is
166 * in use. Force regeneration of call chains.
167 */
168
169 TclOOGetFoundation(interp)->epoch++;
170 }
171
172 /*
173 * ----------------------------------------------------------------------
174 *
175 * RecomputeClassCacheFlag --
176 * Determine whether the object is prototypical of its class, and hence
177 * able to use the class's method chain cache.
178 *
179 * ----------------------------------------------------------------------
180 */
181
182 static inline void
183 RecomputeClassCacheFlag(
184 Object *oPtr)
185 {
186 if ((oPtr->methodsPtr == NULL || oPtr->methodsPtr->numEntries == 0)
187 && (oPtr->mixins.num == 0) && (oPtr->filters.num == 0)) {
188 oPtr->flags |= USE_CLASS_CACHE;
189 } else {
190 oPtr->flags &= ~USE_CLASS_CACHE;
191 }
192 }
193
194 /*
195 * ----------------------------------------------------------------------
196 *
197 * TclOOObjectSetFilters --
198 * Install a list of filter method names into an object.
199 *
200 * ----------------------------------------------------------------------
201 */
202
203 void
204 TclOOObjectSetFilters(
205 Object *oPtr,
206 int numFilters,
207 Tcl_Obj *const *filters)
208 {
209 int i;
210
211 if (oPtr->filters.num) {
212 Tcl_Obj *filterObj;
213
214 FOREACH(filterObj, oPtr->filters) {
215 Tcl_DecrRefCount(filterObj);
216 }
217 }
218
219 if (numFilters == 0) {
220 /*
221 * No list of filters was supplied, so we're deleting filters.
222 */
223
224 ckfree(oPtr->filters.list);
225 oPtr->filters.list = NULL;
226 oPtr->filters.num = 0;
227 RecomputeClassCacheFlag(oPtr);
228 } else {
229 /*
230 * We've got a list of filters, so we're creating filters.
231 */
232
233 Tcl_Obj **filtersList;
234 int size = sizeof(Tcl_Obj *) * numFilters; /* should be size_t */
235
236 if (oPtr->filters.num == 0) {
237 filtersList = ckalloc(size);
238 } else {
239 filtersList = ckrealloc(oPtr->filters.list, size);
240 }
241 for (i = 0 ; i < numFilters ; i++) {
242 filtersList[i] = filters[i];
243 Tcl_IncrRefCount(filters[i]);
244 }
245 oPtr->filters.list = filtersList;
246 oPtr->filters.num = numFilters;
247 oPtr->flags &= ~USE_CLASS_CACHE;
248 }
249 oPtr->epoch++; /* Only this object can be affected. */
250 }
251
252 /*
253 * ----------------------------------------------------------------------
254 *
255 * TclOOClassSetFilters --
256 * Install a list of filter method names into a class.
257 *
258 * ----------------------------------------------------------------------
259 */
260
261 void
262 TclOOClassSetFilters(
263 Tcl_Interp *interp,
264 Class *classPtr,
265 int numFilters,
266 Tcl_Obj *const *filters)
267 {
268 int i;
269
270 if (classPtr->filters.num) {
271 Tcl_Obj *filterObj;
272
273 FOREACH(filterObj, classPtr->filters) {
274 Tcl_DecrRefCount(filterObj);
275 }
276 }
277
278 if (numFilters == 0) {
279 /*
280 * No list of filters was supplied, so we're deleting filters.
281 */
282
283 ckfree(classPtr->filters.list);
284 classPtr->filters.list = NULL;
285 classPtr->filters.num = 0;
286 } else {
287 /*
288 * We've got a list of filters, so we're creating filters.
289 */
290
291 Tcl_Obj **filtersList;
292 int size = sizeof(Tcl_Obj *) * numFilters; /* should be size_t */
293
294 if (classPtr->filters.num == 0) {
295 filtersList = ckalloc(size);
296 } else {
297 filtersList = ckrealloc(classPtr->filters.list, size);
298 }
299 for (i = 0 ; i < numFilters ; i++) {
300 filtersList[i] = filters[i];
301 Tcl_IncrRefCount(filters[i]);
302 }
303 classPtr->filters.list = filtersList;
304 classPtr->filters.num = numFilters;
305 }
306
307 /*
308 * There may be many objects affected, so bump the global epoch.
309 */
310
311 BumpGlobalEpoch(interp, classPtr);
312 }
313
314 /*
315 * ----------------------------------------------------------------------
316 *
317 * TclOOObjectSetMixins --
318 * Install a list of mixin classes into an object.
319 *
320 * ----------------------------------------------------------------------
321 */
322
323 void
324 TclOOObjectSetMixins(
325 Object *oPtr,
326 int numMixins,
327 Class *const *mixins)
328 {
329 Class *mixinPtr;
330 int i;
331
332 if (numMixins == 0) {
333 if (oPtr->mixins.num != 0) {
334 FOREACH(mixinPtr, oPtr->mixins) {
335 TclOORemoveFromInstances(oPtr, mixinPtr);
336 TclOODecrRefCount(mixinPtr->thisPtr);
337 }
338 ckfree(oPtr->mixins.list);
339 oPtr->mixins.num = 0;
340 }
341 RecomputeClassCacheFlag(oPtr);
342 } else {
343 if (oPtr->mixins.num != 0) {
344 FOREACH(mixinPtr, oPtr->mixins) {
345 if (mixinPtr && mixinPtr != oPtr->selfCls) {
346 TclOORemoveFromInstances(oPtr, mixinPtr);
347 }
348 TclOODecrRefCount(mixinPtr->thisPtr);
349 }
350 oPtr->mixins.list = ckrealloc(oPtr->mixins.list,
351 sizeof(Class *) * numMixins);
352 } else {
353 oPtr->mixins.list = ckalloc(sizeof(Class *) * numMixins);
354 oPtr->flags &= ~USE_CLASS_CACHE;
355 }
356 oPtr->mixins.num = numMixins;
357 memcpy(oPtr->mixins.list, mixins, sizeof(Class *) * numMixins);
358 FOREACH(mixinPtr, oPtr->mixins) {
359 if (mixinPtr != oPtr->selfCls) {
360 TclOOAddToInstances(oPtr, mixinPtr);
361
362 /*
363 * For the new copy created by memcpy().
364 */
365
366 AddRef(mixinPtr->thisPtr);
367 }
368 }
369 }
370 oPtr->epoch++;
371 }
372
373 /*
374 * ----------------------------------------------------------------------
375 *
376 * TclOOClassSetMixins --
377 * Install a list of mixin classes into a class.
378 *
379 * ----------------------------------------------------------------------
380 */
381
382 void
383 TclOOClassSetMixins(
384 Tcl_Interp *interp,
385 Class *classPtr,
386 int numMixins,
387 Class *const *mixins)
388 {
389 Class *mixinPtr;
390 int i;
391
392 if (numMixins == 0) {
393 if (classPtr->mixins.num != 0) {
394 FOREACH(mixinPtr, classPtr->mixins) {
395 TclOORemoveFromMixinSubs(classPtr, mixinPtr);
396 TclOODecrRefCount(mixinPtr->thisPtr);
397 }
398 ckfree(classPtr->mixins.list);
399 classPtr->mixins.num = 0;
400 }
401 } else {
402 if (classPtr->mixins.num != 0) {
403 FOREACH(mixinPtr, classPtr->mixins) {
404 TclOORemoveFromMixinSubs(classPtr, mixinPtr);
405 TclOODecrRefCount(mixinPtr->thisPtr);
406 }
407 classPtr->mixins.list = ckrealloc(classPtr->mixins.list,
408 sizeof(Class *) * numMixins);
409 } else {
410 classPtr->mixins.list = ckalloc(sizeof(Class *) * numMixins);
411 }
412 classPtr->mixins.num = numMixins;
413 memcpy(classPtr->mixins.list, mixins, sizeof(Class *) * numMixins);
414 FOREACH(mixinPtr, classPtr->mixins) {
415 TclOOAddToMixinSubs(classPtr, mixinPtr);
416
417 /*
418 * For the new copy created by memcpy.
419 */
420
421 AddRef(mixinPtr->thisPtr);
422 }
423 }
424 BumpGlobalEpoch(interp, classPtr);
425 }
426
427 /*
428 * ----------------------------------------------------------------------
429 *
430 * RenameDeleteMethod --
431 * Core of the code to rename and delete methods.
432 *
433 * ----------------------------------------------------------------------
434 */
435
436 static int
437 RenameDeleteMethod(
438 Tcl_Interp *interp,
439 Object *oPtr,
440 int useClass,
441 Tcl_Obj *const fromPtr,
442 Tcl_Obj *const toPtr)
443 {
444 Tcl_HashEntry *hPtr, *newHPtr = NULL;
445 Method *mPtr;
446 int isNew;
447
448 if (!useClass) {
449 if (!oPtr->methodsPtr) {
450 noSuchMethod:
451 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
452 "method %s does not exist", TclGetString(fromPtr)));
453 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD",
454 TclGetString(fromPtr), (char *)NULL);
455 return TCL_ERROR;
456 }
457 hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, (char *) fromPtr);
458 if (hPtr == NULL) {
459 goto noSuchMethod;
460 }
461 if (toPtr) {
462 newHPtr = Tcl_CreateHashEntry(oPtr->methodsPtr, (char *) toPtr,
463 &isNew);
464 if (hPtr == newHPtr) {
465 renameToSelf:
466 Tcl_SetObjResult(interp, Tcl_NewStringObj(
467 "cannot rename method to itself", -1));
468 Tcl_SetErrorCode(interp, "TCL", "OO", "RENAME_TO_SELF", (char *)NULL);
469 return TCL_ERROR;
470 } else if (!isNew) {
471 renameToExisting:
472 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
473 "method called %s already exists",
474 TclGetString(toPtr)));
475 Tcl_SetErrorCode(interp, "TCL", "OO", "RENAME_OVER", (char *)NULL);
476 return TCL_ERROR;
477 }
478 }
479 } else {
480 hPtr = Tcl_FindHashEntry(&oPtr->classPtr->classMethods,
481 (char *) fromPtr);
482 if (hPtr == NULL) {
483 goto noSuchMethod;
484 }
485 if (toPtr) {
486 newHPtr = Tcl_CreateHashEntry(&oPtr->classPtr->classMethods,
487 (char *) toPtr, &isNew);
488 if (hPtr == newHPtr) {
489 goto renameToSelf;
490 } else if (!isNew) {
491 goto renameToExisting;
492 }
493 }
494 }
495
496 /*
497 * Complete the splicing by changing the method's name.
498 */
499
500 mPtr = Tcl_GetHashValue(hPtr);
501 if (toPtr) {
502 Tcl_IncrRefCount(toPtr);
503 Tcl_DecrRefCount(mPtr->namePtr);
504 mPtr->namePtr = toPtr;
505 Tcl_SetHashValue(newHPtr, mPtr);
506 } else {
507 if (!useClass) {
508 RecomputeClassCacheFlag(oPtr);
509 }
510 TclOODelMethodRef(mPtr);
511 }
512 Tcl_DeleteHashEntry(hPtr);
513 return TCL_OK;
514 }
515
516 /*
517 * ----------------------------------------------------------------------
518 *
519 * TclOOUnknownDefinition --
520 * Handles what happens when an unknown command is encountered during the
521 * processing of a definition script. Works by finding a command in the
522 * operating definition namespace that the requested command is a unique
523 * prefix of.
524 *
525 * ----------------------------------------------------------------------
526 */
527
528 int
529 TclOOUnknownDefinition(
530 ClientData clientData,
531 Tcl_Interp *interp,
532 int objc,
533 Tcl_Obj *const *objv)
534 {
535 Namespace *nsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp);
536 Tcl_HashSearch search;
537 Tcl_HashEntry *hPtr;
538 int soughtLen;
539 const char *soughtStr, *matchedStr = NULL;
540
541 if (objc < 2) {
542 Tcl_SetObjResult(interp, Tcl_NewStringObj(
543 "bad call of unknown handler", -1));
544 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_UNKNOWN", (char *)NULL);
545 return TCL_ERROR;
546 }
547 if (TclOOGetDefineCmdContext(interp) == NULL) {
548 return TCL_ERROR;
549 }
550
551 soughtStr = Tcl_GetStringFromObj(objv[1], &soughtLen);
552 if (soughtLen == 0) {
553 goto noMatch;
554 }
555 hPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search);
556 while (hPtr != NULL) {
557 const char *nameStr = Tcl_GetHashKey(&nsPtr->cmdTable, hPtr);
558
559 if (strncmp(soughtStr, nameStr, soughtLen) == 0) {
560 if (matchedStr != NULL) {
561 goto noMatch;
562 }
563 matchedStr = nameStr;
564 }
565 hPtr = Tcl_NextHashEntry(&search);
566 }
567
568 if (matchedStr != NULL) {
569 /*
570 * Got one match, and only one match!
571 */
572
573 Tcl_Obj **newObjv =
574 TclStackAlloc(interp, sizeof(Tcl_Obj*) * (objc - 1));
575 int result;
576
577 newObjv[0] = Tcl_NewStringObj(matchedStr, -1);
578 Tcl_IncrRefCount(newObjv[0]);
579 if (objc > 2) {
580 memcpy(newObjv + 1, objv + 2, sizeof(Tcl_Obj *) * (objc - 2));
581 }
582 result = Tcl_EvalObjv(interp, objc - 1, newObjv, 0);
583 Tcl_DecrRefCount(newObjv[0]);
584 TclStackFree(interp, newObjv);
585 return result;
586 }
587
588 noMatch:
589 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
590 "invalid command name \"%s\"", soughtStr));
591 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "COMMAND", soughtStr, (char *)NULL);
592 return TCL_ERROR;
593 }
594
595 /*
596 * ----------------------------------------------------------------------
597 *
598 * FindCommand --
599 * Specialized version of Tcl_FindCommand that handles command prefixes
600 * and disallows namespace magic.
601 *
602 * ----------------------------------------------------------------------
603 */
604
605 static Tcl_Command
606 FindCommand(
607 Tcl_Interp *interp,
608 Tcl_Obj *stringObj,
609 Tcl_Namespace *const namespacePtr)
610 {
611 int length;
612 const char *nameStr, *string = Tcl_GetStringFromObj(stringObj, &length);
613 Namespace *const nsPtr = (Namespace *) namespacePtr;
614 FOREACH_HASH_DECLS;
615 Tcl_Command cmd, cmd2;
616
617 /*
618 * If someone is playing games, we stop playing right now.
619 */
620
621 if (string[0] == '\0' || strstr(string, "::") != NULL) {
622 return NULL;
623 }
624
625 /*
626 * Do the exact lookup first.
627 */
628
629 cmd = Tcl_FindCommand(interp, string, namespacePtr, TCL_NAMESPACE_ONLY);
630 if (cmd != NULL) {
631 return cmd;
632 }
633
634 /*
635 * Bother, need to perform an approximate match. Iterate across the hash
636 * table of commands in the namespace.
637 */
638
639 FOREACH_HASH(nameStr, cmd2, &nsPtr->cmdTable) {
640 if (strncmp(string, nameStr, length) == 0) {
641 if (cmd != NULL) {
642 return NULL;
643 }
644 cmd = cmd2;
645 }
646 }
647
648 /*
649 * Either we found one thing or we found nothing. Either way, return it.
650 */
651
652 return cmd;
653 }
654
655 /*
656 * ----------------------------------------------------------------------
657 *
658 * InitDefineContext --
659 * Does the magic incantations necessary to push the special stack frame
660 * used when processing object definitions. It is up to the caller to
661 * dispose of the frame (with TclPopStackFrame) when finished.
662 *
663 * ----------------------------------------------------------------------
664 */
665
666 static inline int
667 InitDefineContext(
668 Tcl_Interp *interp,
669 Tcl_Namespace *namespacePtr,
670 Object *oPtr,
671 int objc,
672 Tcl_Obj *const objv[])
673 {
674 CallFrame *framePtr, **framePtrPtr = &framePtr;
675
676 if (namespacePtr == NULL) {
677 Tcl_SetObjResult(interp, Tcl_NewStringObj(
678 "cannot process definitions; support namespace deleted",
679 -1));
680 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
681 return TCL_ERROR;
682 }
683
684 /*
685 * framePtrPtr is needed to satisfy GCC 3.3's strict aliasing rules.
686 */
687
688 (void) TclPushStackFrame(interp, (Tcl_CallFrame **) framePtrPtr,
689 namespacePtr, FRAME_IS_OO_DEFINE);
690 framePtr->clientData = oPtr;
691 framePtr->objc = objc;
692 framePtr->objv = objv; /* Reference counts do not need to be
693 * incremented here. */
694 return TCL_OK;
695 }
696
697 /*
698 * ----------------------------------------------------------------------
699 *
700 * TclOOGetDefineCmdContext --
701 * Extracts the magic token from the current stack frame, or returns NULL
702 * (and leaves an error message) otherwise.
703 *
704 * ----------------------------------------------------------------------
705 */
706
707 Tcl_Object
708 TclOOGetDefineCmdContext(
709 Tcl_Interp *interp)
710 {
711 Interp *iPtr = (Interp *) interp;
712 Tcl_Object object;
713
714 if ((iPtr->varFramePtr == NULL)
715 || (iPtr->varFramePtr->isProcCallFrame != FRAME_IS_OO_DEFINE)) {
716 Tcl_SetObjResult(interp, Tcl_NewStringObj(
717 "this command may only be called from within the context of"
718 " an ::oo::define or ::oo::objdefine command", -1));
719 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
720 return NULL;
721 }
722 object = iPtr->varFramePtr->clientData;
723 if (Tcl_ObjectDeleted(object)) {
724 Tcl_SetObjResult(interp, Tcl_NewStringObj(
725 "this command cannot be called when the object has been"
726 " deleted", -1));
727 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
728 return NULL;
729 }
730 return object;
731 }
732
733 /*
734 * ----------------------------------------------------------------------
735 *
736 * GetClassInOuterContext --
737 * Wrapper round Tcl_GetObjectFromObj to perform the lookup in the
738 * context that called oo::define (or equivalent). Note that this may
739 * have to go up multiple levels to get the level that we started doing
740 * definitions at.
741 *
742 * ----------------------------------------------------------------------
743 */
744
745 static inline Class *
746 GetClassInOuterContext(
747 Tcl_Interp *interp,
748 Tcl_Obj *className,
749 const char *errMsg)
750 {
751 Interp *iPtr = (Interp *) interp;
752 Object *oPtr;
753 CallFrame *savedFramePtr = iPtr->varFramePtr;
754
755 while (iPtr->varFramePtr->isProcCallFrame == FRAME_IS_OO_DEFINE) {
756 if (iPtr->varFramePtr->callerVarPtr == NULL) {
757 Tcl_Panic("getting outer context when already in global context");
758 }
759 iPtr->varFramePtr = iPtr->varFramePtr->callerVarPtr;
760 }
761 oPtr = (Object *) Tcl_GetObjectFromObj(interp, className);
762 iPtr->varFramePtr = savedFramePtr;
763 if (oPtr == NULL) {
764 return NULL;
765 }
766 if (oPtr->classPtr == NULL) {
767 Tcl_SetObjResult(interp, Tcl_NewStringObj(errMsg, -1));
768 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
769 TclGetString(className), (char *)NULL);
770 return NULL;
771 }
772 return oPtr->classPtr;
773 }
774
775 /*
776 * ----------------------------------------------------------------------
777 *
778 * GenerateErrorInfo --
779 * Factored out code to generate part of the error trace messages.
780 *
781 * ----------------------------------------------------------------------
782 */
783
784 static inline void
785 GenerateErrorInfo(
786 Tcl_Interp *interp, /* Where to store the error info trace. */
787 Object *oPtr, /* What object (or class) was being configured
788 * when the error occurred? */
789 Tcl_Obj *savedNameObj, /* Name of object saved from before script was
790 * evaluated, which is needed if the object
791 * goes away part way through execution. OTOH,
792 * if the object isn't deleted then its
793 * current name (post-execution) has to be
794 * used. This matters, because the object
795 * could have been renamed... */
796 const char *typeOfSubject) /* Part of the message, saying whether it was
797 * an object, class or class-as-object that
798 * was being configured. */
799 {
800 int length;
801 Tcl_Obj *realNameObj = Tcl_ObjectDeleted((Tcl_Object) oPtr)
802 ? savedNameObj : TclOOObjectName(interp, oPtr);
803 const char *objName = Tcl_GetStringFromObj(realNameObj, &length);
804 int limit = OBJNAME_LENGTH_IN_ERRORINFO_LIMIT;
805 int overflow = (length > limit);
806
807 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
808 "\n (in definition script for %s \"%.*s%s\" line %d)",
809 typeOfSubject, (overflow ? limit : length), objName,
810 (overflow ? "..." : ""), Tcl_GetErrorLine(interp)));
811 }
812
813 /*
814 * ----------------------------------------------------------------------
815 *
816 * MagicDefinitionInvoke --
817 * Part of the implementation of the "oo::define" and "oo::objdefine"
818 * commands that is used to implement the more-than-one-argument case,
819 * applying ensemble-like tricks with dispatch so that error messages are
820 * clearer. Doesn't handle the management of the stack frame.
821 *
822 * ----------------------------------------------------------------------
823 */
824
825 static inline int
826 MagicDefinitionInvoke(
827 Tcl_Interp *interp,
828 Tcl_Namespace *nsPtr,
829 int cmdIndex,
830 int objc,
831 Tcl_Obj *const *objv)
832 {
833 Tcl_Obj *objPtr, *obj2Ptr, **objs;
834 Tcl_Command cmd;
835 int isRoot, dummy, result, offset = cmdIndex + 1;
836
837 /*
838 * More than one argument: fire them through the ensemble processing
839 * engine so that everything appears to be good and proper in error
840 * messages. Note that we cannot just concatenate and send through
841 * Tcl_EvalObjEx, as that doesn't do ensemble processing, and we cannot go
842 * through Tcl_EvalObjv without the extra work to pre-find the command, as
843 * that finds command names in the wrong namespace at the moment. Ugly!
844 */
845
846 isRoot = TclInitRewriteEnsemble(interp, offset, 1, objv);
847
848 /*
849 * Build the list of arguments using a Tcl_Obj as a workspace. See
850 * comments above for why these contortions are necessary.
851 */
852
853 TclNewObj(objPtr);
854 TclNewObj(obj2Ptr);
855 cmd = FindCommand(interp, objv[cmdIndex], nsPtr);
856 if (cmd == NULL) {
857 /*
858 * Punt this case!
859 */
860
861 Tcl_AppendObjToObj(obj2Ptr, objv[cmdIndex]);
862 } else {
863 Tcl_GetCommandFullName(interp, cmd, obj2Ptr);
864 }
865 Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr);
866 /* TODO: overflow? */
867 Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc - offset, objv + offset);
868 TclListObjGetElements(NULL, objPtr, &dummy, &objs);
869
870 result = Tcl_EvalObjv(interp, objc - cmdIndex, objs, TCL_EVAL_INVOKE);
871 if (isRoot) {
872 TclResetRewriteEnsemble(interp, 1);
873 }
874 Tcl_DecrRefCount(objPtr);
875
876 return result;
877 }
878
879 /*
880 * ----------------------------------------------------------------------
881 *
882 * TclOODefineObjCmd --
883 * Implementation of the "oo::define" command. Works by effectively doing
884 * the same as 'namespace eval', but with extra magic applied so that the
885 * object to be modified is known to the commands in the target
886 * namespace. Also does ensemble-like tricks with dispatch so that error
887 * messages are clearer.
888 *
889 * ----------------------------------------------------------------------
890 */
891
892 int
893 TclOODefineObjCmd(
894 ClientData clientData,
895 Tcl_Interp *interp,
896 int objc,
897 Tcl_Obj *const *objv)
898 {
899 Foundation *fPtr = TclOOGetFoundation(interp);
900 Object *oPtr;
901 int result;
902
903 if (objc < 3) {
904 Tcl_WrongNumArgs(interp, 1, objv, "className arg ?arg ...?");
905 return TCL_ERROR;
906 }
907
908 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[1]);
909 if (oPtr == NULL) {
910 return TCL_ERROR;
911 }
912 if (oPtr->classPtr == NULL) {
913 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
914 "%s does not refer to a class",TclGetString(objv[1])));
915 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
916 TclGetString(objv[1]), (char *)NULL);
917 return TCL_ERROR;
918 }
919
920 /*
921 * Make the oo::define namespace the current namespace and evaluate the
922 * command(s).
923 */
924
925 if (InitDefineContext(interp, fPtr->defineNs, oPtr, objc,objv) != TCL_OK){
926 return TCL_ERROR;
927 }
928
929 AddRef(oPtr);
930 if (objc == 3) {
931 Tcl_Obj *objNameObj = TclOOObjectName(interp, oPtr);
932
933 Tcl_IncrRefCount(objNameObj);
934 result = TclEvalObjEx(interp, objv[2], 0,
935 ((Interp *)interp)->cmdFramePtr, 2);
936 if (result == TCL_ERROR) {
937 GenerateErrorInfo(interp, oPtr, objNameObj, "class");
938 }
939 TclDecrRefCount(objNameObj);
940 } else {
941 result = MagicDefinitionInvoke(interp, fPtr->defineNs, 2, objc, objv);
942 }
943 TclOODecrRefCount(oPtr);
944
945 /*
946 * Restore the previous "current" namespace.
947 */
948
949 TclPopStackFrame(interp);
950 return result;
951 }
952
953 /*
954 * ----------------------------------------------------------------------
955 *
956 * TclOOObjDefObjCmd --
957 * Implementation of the "oo::objdefine" command. Works by effectively
958 * doing the same as 'namespace eval', but with extra magic applied so
959 * that the object to be modified is known to the commands in the target
960 * namespace. Also does ensemble-like tricks with dispatch so that error
961 * messages are clearer.
962 *
963 * ----------------------------------------------------------------------
964 */
965
966 int
967 TclOOObjDefObjCmd(
968 ClientData clientData,
969 Tcl_Interp *interp,
970 int objc,
971 Tcl_Obj *const *objv)
972 {
973 Foundation *fPtr = TclOOGetFoundation(interp);
974 Object *oPtr;
975 int result;
976
977 if (objc < 3) {
978 Tcl_WrongNumArgs(interp, 1, objv, "objectName arg ?arg ...?");
979 return TCL_ERROR;
980 }
981
982 oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[1]);
983 if (oPtr == NULL) {
984 return TCL_ERROR;
985 }
986
987 /*
988 * Make the oo::objdefine namespace the current namespace and evaluate the
989 * command(s).
990 */
991
992 if (InitDefineContext(interp, fPtr->objdefNs, oPtr, objc,objv) != TCL_OK){
993 return TCL_ERROR;
994 }
995
996 AddRef(oPtr);
997 if (objc == 3) {
998 Tcl_Obj *objNameObj = TclOOObjectName(interp, oPtr);
999
1000 Tcl_IncrRefCount(objNameObj);
1001 result = TclEvalObjEx(interp, objv[2], 0,
1002 ((Interp *)interp)->cmdFramePtr, 2);
1003 if (result == TCL_ERROR) {
1004 GenerateErrorInfo(interp, oPtr, objNameObj, "object");
1005 }
1006 TclDecrRefCount(objNameObj);
1007 } else {
1008 result = MagicDefinitionInvoke(interp, fPtr->objdefNs, 2, objc, objv);
1009 }
1010 TclOODecrRefCount(oPtr);
1011
1012 /*
1013 * Restore the previous "current" namespace.
1014 */
1015
1016 TclPopStackFrame(interp);
1017 return result;
1018 }
1019
1020 /*
1021 * ----------------------------------------------------------------------
1022 *
1023 * TclOODefineSelfObjCmd --
1024 * Implementation of the "self" subcommand of the "oo::define" command.
1025 * Works by effectively doing the same as 'namespace eval', but with
1026 * extra magic applied so that the object to be modified is known to the
1027 * commands in the target namespace. Also does ensemble-like tricks with
1028 * dispatch so that error messages are clearer.
1029 *
1030 * ----------------------------------------------------------------------
1031 */
1032
1033 int
1034 TclOODefineSelfObjCmd(
1035 ClientData clientData,
1036 Tcl_Interp *interp,
1037 int objc,
1038 Tcl_Obj *const *objv)
1039 {
1040 Foundation *fPtr = TclOOGetFoundation(interp);
1041 Object *oPtr;
1042 int result;
1043
1044 if (objc < 2) {
1045 Tcl_WrongNumArgs(interp, 1, objv, "arg ?arg ...?");
1046 return TCL_ERROR;
1047 }
1048
1049 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1050 if (oPtr == NULL) {
1051 return TCL_ERROR;
1052 }
1053
1054 /*
1055 * Make the oo::objdefine namespace the current namespace and evaluate the
1056 * command(s).
1057 */
1058
1059 if (InitDefineContext(interp, fPtr->objdefNs, oPtr, objc,objv) != TCL_OK){
1060 return TCL_ERROR;
1061 }
1062
1063 AddRef(oPtr);
1064 if (objc == 2) {
1065 Tcl_Obj *objNameObj = TclOOObjectName(interp, oPtr);
1066
1067 Tcl_IncrRefCount(objNameObj);
1068 result = TclEvalObjEx(interp, objv[1], 0,
1069 ((Interp *)interp)->cmdFramePtr, 2);
1070 if (result == TCL_ERROR) {
1071 GenerateErrorInfo(interp, oPtr, objNameObj, "class object");
1072 }
1073 TclDecrRefCount(objNameObj);
1074 } else {
1075 result = MagicDefinitionInvoke(interp, fPtr->objdefNs, 1, objc, objv);
1076 }
1077 TclOODecrRefCount(oPtr);
1078
1079 /*
1080 * Restore the previous "current" namespace.
1081 */
1082
1083 TclPopStackFrame(interp);
1084 return result;
1085 }
1086
1087 /*
1088 * ----------------------------------------------------------------------
1089 *
1090 * TclOODefineClassObjCmd --
1091 * Implementation of the "class" subcommand of the "oo::objdefine"
1092 * command.
1093 *
1094 * ----------------------------------------------------------------------
1095 */
1096
1097 int
1098 TclOODefineClassObjCmd(
1099 ClientData clientData,
1100 Tcl_Interp *interp,
1101 int objc,
1102 Tcl_Obj *const *objv)
1103 {
1104 Object *oPtr;
1105 Class *clsPtr;
1106 Foundation *fPtr = TclOOGetFoundation(interp);
1107 int wasClass, willBeClass;
1108
1109 /*
1110 * Parse the context to get the object to operate on.
1111 */
1112
1113 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1114 if (oPtr == NULL) {
1115 return TCL_ERROR;
1116 }
1117 if (oPtr->flags & ROOT_OBJECT) {
1118 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1119 "may not modify the class of the root object class", -1));
1120 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1121 return TCL_ERROR;
1122 }
1123 if (oPtr->flags & ROOT_CLASS) {
1124 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1125 "may not modify the class of the class of classes", -1));
1126 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1127 return TCL_ERROR;
1128 }
1129
1130 /*
1131 * Parse the argument to get the class to set the object's class to.
1132 */
1133
1134 if (objc != 2) {
1135 Tcl_WrongNumArgs(interp, 1, objv, "className");
1136 return TCL_ERROR;
1137 }
1138 clsPtr = GetClassInOuterContext(interp, objv[1],
1139 "the class of an object must be a class");
1140 if (clsPtr == NULL) {
1141 return TCL_ERROR;
1142 }
1143 if (oPtr == clsPtr->thisPtr) {
1144 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1145 "may not change classes into an instance of themselves", -1));
1146 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1147 return TCL_ERROR;
1148 }
1149
1150 /*
1151 * Set the object's class.
1152 */
1153
1154 wasClass = (oPtr->classPtr != NULL);
1155 willBeClass = (TclOOIsReachable(fPtr->classCls, clsPtr));
1156
1157 if (oPtr->selfCls != clsPtr) {
1158 TclOORemoveFromInstances(oPtr, oPtr->selfCls);
1159 TclOODecrRefCount(oPtr->selfCls->thisPtr);
1160 oPtr->selfCls = clsPtr;
1161 AddRef(oPtr->selfCls->thisPtr);
1162 TclOOAddToInstances(oPtr, oPtr->selfCls);
1163
1164 /*
1165 * Create or delete the class guts if necessary.
1166 */
1167
1168 if (wasClass && !willBeClass) {
1169 /*
1170 * This is the most global of all epochs. Bump it! No cache can be
1171 * trusted!
1172 */
1173
1174 TclOORemoveFromMixins(oPtr->classPtr, oPtr);
1175 oPtr->fPtr->epoch++;
1176 oPtr->flags |= DONT_DELETE;
1177 TclOODeleteDescendants(interp, oPtr);
1178 oPtr->flags &= ~DONT_DELETE;
1179 TclOOReleaseClassContents(interp, oPtr);
1180 ckfree(oPtr->classPtr);
1181 oPtr->classPtr = NULL;
1182 } else if (!wasClass && willBeClass) {
1183 TclOOAllocClass(interp, oPtr);
1184 }
1185
1186 if (oPtr->classPtr != NULL) {
1187 BumpGlobalEpoch(interp, oPtr->classPtr);
1188 } else {
1189 oPtr->epoch++;
1190 }
1191 }
1192 return TCL_OK;
1193 }
1194
1195 /*
1196 * ----------------------------------------------------------------------
1197 *
1198 * TclOODefineConstructorObjCmd --
1199 * Implementation of the "constructor" subcommand of the "oo::define"
1200 * command.
1201 *
1202 * ----------------------------------------------------------------------
1203 */
1204
1205 int
1206 TclOODefineConstructorObjCmd(
1207 ClientData clientData,
1208 Tcl_Interp *interp,
1209 int objc,
1210 Tcl_Obj *const *objv)
1211 {
1212 Object *oPtr;
1213 Class *clsPtr;
1214 Tcl_Method method;
1215 int bodyLength;
1216
1217 if (objc != 3) {
1218 Tcl_WrongNumArgs(interp, 1, objv, "arguments body");
1219 return TCL_ERROR;
1220 }
1221
1222 /*
1223 * Extract and validate the context, which is the class that we wish to
1224 * modify.
1225 */
1226
1227 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1228 if (oPtr == NULL) {
1229 return TCL_ERROR;
1230 }
1231 clsPtr = oPtr->classPtr;
1232
1233 Tcl_GetStringFromObj(objv[2], &bodyLength);
1234 if (bodyLength > 0) {
1235 /*
1236 * Create the method structure.
1237 */
1238
1239 method = (Tcl_Method) TclOONewProcMethod(interp, clsPtr,
1240 PUBLIC_METHOD, NULL, objv[1], objv[2], NULL);
1241 if (method == NULL) {
1242 return TCL_ERROR;
1243 }
1244 } else {
1245 /*
1246 * Delete the constructor method record and set the field in the
1247 * class record to NULL.
1248 */
1249
1250 method = NULL;
1251 }
1252
1253 /*
1254 * Place the method structure in the class record. Note that we might not
1255 * immediately delete the constructor as this might be being done during
1256 * execution of the constructor itself.
1257 */
1258
1259 Tcl_ClassSetConstructor(interp, (Tcl_Class) clsPtr, method);
1260 return TCL_OK;
1261 }
1262
1263 /*
1264 * ----------------------------------------------------------------------
1265 *
1266 * TclOODefineDeleteMethodObjCmd --
1267 * Implementation of the "deletemethod" subcommand of the "oo::define"
1268 * and "oo::objdefine" commands.
1269 *
1270 * ----------------------------------------------------------------------
1271 */
1272
1273 int
1274 TclOODefineDeleteMethodObjCmd(
1275 ClientData clientData,
1276 Tcl_Interp *interp,
1277 int objc,
1278 Tcl_Obj *const *objv)
1279 {
1280 int isInstanceDeleteMethod = (clientData != NULL);
1281 Object *oPtr;
1282 int i;
1283
1284 if (objc < 2) {
1285 Tcl_WrongNumArgs(interp, 1, objv, "name ?name ...?");
1286 return TCL_ERROR;
1287 }
1288
1289 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1290 if (oPtr == NULL) {
1291 return TCL_ERROR;
1292 }
1293 if (!isInstanceDeleteMethod && !oPtr->classPtr) {
1294 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1295 "attempt to misuse API", -1));
1296 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1297 return TCL_ERROR;
1298 }
1299
1300 for (i = 1; i < objc; i++) {
1301 /*
1302 * Delete the method structure from the appropriate hash table.
1303 */
1304
1305 if (RenameDeleteMethod(interp, oPtr, !isInstanceDeleteMethod,
1306 objv[i], NULL) != TCL_OK) {
1307 return TCL_ERROR;
1308 }
1309 }
1310
1311 if (isInstanceDeleteMethod) {
1312 oPtr->epoch++;
1313 } else {
1314 BumpGlobalEpoch(interp, oPtr->classPtr);
1315 }
1316 return TCL_OK;
1317 }
1318
1319 /*
1320 * ----------------------------------------------------------------------
1321 *
1322 * TclOODefineDestructorObjCmd --
1323 * Implementation of the "destructor" subcommand of the "oo::define"
1324 * command.
1325 *
1326 * ----------------------------------------------------------------------
1327 */
1328
1329 int
1330 TclOODefineDestructorObjCmd(
1331 ClientData clientData,
1332 Tcl_Interp *interp,
1333 int objc,
1334 Tcl_Obj *const *objv)
1335 {
1336 Object *oPtr;
1337 Class *clsPtr;
1338 Tcl_Method method;
1339 int bodyLength;
1340
1341 if (objc != 2) {
1342 Tcl_WrongNumArgs(interp, 1, objv, "body");
1343 return TCL_ERROR;
1344 }
1345
1346 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1347 if (oPtr == NULL) {
1348 return TCL_ERROR;
1349 }
1350 clsPtr = oPtr->classPtr;
1351
1352 Tcl_GetStringFromObj(objv[1], &bodyLength);
1353 if (bodyLength > 0) {
1354 /*
1355 * Create the method structure.
1356 */
1357
1358 method = (Tcl_Method) TclOONewProcMethod(interp, clsPtr,
1359 PUBLIC_METHOD, NULL, NULL, objv[1], NULL);
1360 if (method == NULL) {
1361 return TCL_ERROR;
1362 }
1363 } else {
1364 /*
1365 * Delete the destructor method record and set the field in the class
1366 * record to NULL.
1367 */
1368
1369 method = NULL;
1370 }
1371
1372 /*
1373 * Place the method structure in the class record. Note that we might not
1374 * immediately delete the destructor as this might be being done during
1375 * execution of the destructor itself. Also note that setting a
1376 * destructor during a destructor is fairly dumb anyway.
1377 */
1378
1379 Tcl_ClassSetDestructor(interp, (Tcl_Class) clsPtr, method);
1380 return TCL_OK;
1381 }
1382
1383 /*
1384 * ----------------------------------------------------------------------
1385 *
1386 * TclOODefineExportObjCmd --
1387 * Implementation of the "export" subcommand of the "oo::define" and
1388 * "oo::objdefine" commands.
1389 *
1390 * ----------------------------------------------------------------------
1391 */
1392
1393 int
1394 TclOODefineExportObjCmd(
1395 ClientData clientData,
1396 Tcl_Interp *interp,
1397 int objc,
1398 Tcl_Obj *const *objv)
1399 {
1400 int isInstanceExport = (clientData != NULL);
1401 Object *oPtr;
1402 Method *mPtr;
1403 Tcl_HashEntry *hPtr;
1404 Class *clsPtr;
1405 int i, isNew, changed = 0;
1406
1407 if (objc < 2) {
1408 Tcl_WrongNumArgs(interp, 1, objv, "name ?name ...?");
1409 return TCL_ERROR;
1410 }
1411
1412 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1413 if (oPtr == NULL) {
1414 return TCL_ERROR;
1415 }
1416 clsPtr = oPtr->classPtr;
1417 if (!isInstanceExport && !clsPtr) {
1418 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1419 "attempt to misuse API", -1));
1420 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1421 return TCL_ERROR;
1422 }
1423
1424 for (i = 1; i < objc; i++) {
1425 /*
1426 * Exporting is done by adding the PUBLIC_METHOD flag to the method
1427 * record. If there is no such method in this object or class (i.e.
1428 * the method comes from something inherited from or that we're an
1429 * instance of) then we put in a blank record with that flag; such
1430 * records are skipped over by the call chain engine *except* for
1431 * their flags member.
1432 */
1433
1434 if (isInstanceExport) {
1435 if (!oPtr->methodsPtr) {
1436 oPtr->methodsPtr = ckalloc(sizeof(Tcl_HashTable));
1437 Tcl_InitObjHashTable(oPtr->methodsPtr);
1438 oPtr->flags &= ~USE_CLASS_CACHE;
1439 }
1440 hPtr = Tcl_CreateHashEntry(oPtr->methodsPtr, (char *) objv[i],
1441 &isNew);
1442 } else {
1443 hPtr = Tcl_CreateHashEntry(&clsPtr->classMethods, (char*) objv[i],
1444 &isNew);
1445 }
1446
1447 if (isNew) {
1448 mPtr = ckalloc(sizeof(Method));
1449 memset(mPtr, 0, sizeof(Method));
1450 mPtr->refCount = 1;
1451 mPtr->namePtr = objv[i];
1452 Tcl_IncrRefCount(objv[i]);
1453 Tcl_SetHashValue(hPtr, mPtr);
1454 } else {
1455 mPtr = Tcl_GetHashValue(hPtr);
1456 }
1457 if (isNew || !(mPtr->flags & PUBLIC_METHOD)) {
1458 mPtr->flags |= PUBLIC_METHOD;
1459 changed = 1;
1460 }
1461 }
1462
1463 /*
1464 * Bump the right epoch if we actually changed anything.
1465 */
1466
1467 if (changed) {
1468 if (isInstanceExport) {
1469 oPtr->epoch++;
1470 } else {
1471 BumpGlobalEpoch(interp, clsPtr);
1472 }
1473 }
1474 return TCL_OK;
1475 }
1476
1477 /*
1478 * ----------------------------------------------------------------------
1479 *
1480 * TclOODefineForwardObjCmd --
1481 * Implementation of the "forward" subcommand of the "oo::define" and
1482 * "oo::objdefine" commands.
1483 *
1484 * ----------------------------------------------------------------------
1485 */
1486
1487 int
1488 TclOODefineForwardObjCmd(
1489 ClientData clientData,
1490 Tcl_Interp *interp,
1491 int objc,
1492 Tcl_Obj *const *objv)
1493 {
1494 int isInstanceForward = (clientData != NULL);
1495 Object *oPtr;
1496 Method *mPtr;
1497 int isPublic;
1498 Tcl_Obj *prefixObj;
1499
1500 if (objc < 3) {
1501 Tcl_WrongNumArgs(interp, 1, objv, "name cmdName ?arg ...?");
1502 return TCL_ERROR;
1503 }
1504
1505 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1506 if (oPtr == NULL) {
1507 return TCL_ERROR;
1508 }
1509 if (!isInstanceForward && !oPtr->classPtr) {
1510 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1511 "attempt to misuse API", -1));
1512 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1513 return TCL_ERROR;
1514 }
1515 isPublic = Tcl_StringMatch(TclGetString(objv[1]), PUBLIC_PATTERN)
1516 ? PUBLIC_METHOD : 0;
1517
1518 /*
1519 * Create the method structure.
1520 */
1521
1522 prefixObj = Tcl_NewListObj(objc - 2, objv + 2);
1523 if (isInstanceForward) {
1524 mPtr = TclOONewForwardInstanceMethod(interp, oPtr, isPublic, objv[1],
1525 prefixObj);
1526 } else {
1527 mPtr = TclOONewForwardMethod(interp, oPtr->classPtr, isPublic,
1528 objv[1], prefixObj);
1529 }
1530 if (mPtr == NULL) {
1531 Tcl_DecrRefCount(prefixObj);
1532 return TCL_ERROR;
1533 }
1534 return TCL_OK;
1535 }
1536
1537 /*
1538 * ----------------------------------------------------------------------
1539 *
1540 * TclOODefineMethodObjCmd --
1541 * Implementation of the "method" subcommand of the "oo::define" and
1542 * "oo::objdefine" commands.
1543 *
1544 * ----------------------------------------------------------------------
1545 */
1546
1547 int
1548 TclOODefineMethodObjCmd(
1549 ClientData clientData,
1550 Tcl_Interp *interp,
1551 int objc,
1552 Tcl_Obj *const *objv)
1553 {
1554 int isInstanceMethod = (clientData != NULL);
1555 Object *oPtr;
1556 int isPublic;
1557
1558 if (objc != 4) {
1559 Tcl_WrongNumArgs(interp, 1, objv, "name args body");
1560 return TCL_ERROR;
1561 }
1562
1563 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1564 if (oPtr == NULL) {
1565 return TCL_ERROR;
1566 }
1567 if (!isInstanceMethod && !oPtr->classPtr) {
1568 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1569 "attempt to misuse API", -1));
1570 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1571 return TCL_ERROR;
1572 }
1573 isPublic = Tcl_StringMatch(TclGetString(objv[1]), PUBLIC_PATTERN)
1574 ? PUBLIC_METHOD : 0;
1575
1576 /*
1577 * Create the method by using the right back-end API.
1578 */
1579
1580 if (isInstanceMethod) {
1581 if (TclOONewProcInstanceMethod(interp, oPtr, isPublic, objv[1],
1582 objv[2], objv[3], NULL) == NULL) {
1583 return TCL_ERROR;
1584 }
1585 } else {
1586 if (TclOONewProcMethod(interp, oPtr->classPtr, isPublic, objv[1],
1587 objv[2], objv[3], NULL) == NULL) {
1588 return TCL_ERROR;
1589 }
1590 }
1591 return TCL_OK;
1592 }
1593
1594 /*
1595 * ----------------------------------------------------------------------
1596 *
1597 * TclOODefineRenameMethodObjCmd --
1598 * Implementation of the "renamemethod" subcommand of the "oo::define"
1599 * and "oo::objdefine" commands.
1600 *
1601 * ----------------------------------------------------------------------
1602 */
1603
1604 int
1605 TclOODefineRenameMethodObjCmd(
1606 ClientData clientData,
1607 Tcl_Interp *interp,
1608 int objc,
1609 Tcl_Obj *const *objv)
1610 {
1611 int isInstanceRenameMethod = (clientData != NULL);
1612 Object *oPtr;
1613
1614 if (objc != 3) {
1615 Tcl_WrongNumArgs(interp, 1, objv, "oldName newName");
1616 return TCL_ERROR;
1617 }
1618
1619 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1620 if (oPtr == NULL) {
1621 return TCL_ERROR;
1622 }
1623 if (!isInstanceRenameMethod && !oPtr->classPtr) {
1624 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1625 "attempt to misuse API", -1));
1626 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1627 return TCL_ERROR;
1628 }
1629
1630 /*
1631 * Delete the method entry from the appropriate hash table, and transfer
1632 * the thing it points to to its new entry. To do this, we first need to
1633 * get the entries from the appropriate hash tables (this can generate a
1634 * range of errors...)
1635 */
1636
1637 if (RenameDeleteMethod(interp, oPtr, !isInstanceRenameMethod,
1638 objv[1], objv[2]) != TCL_OK) {
1639 return TCL_ERROR;
1640 }
1641
1642 if (isInstanceRenameMethod) {
1643 oPtr->epoch++;
1644 } else {
1645 BumpGlobalEpoch(interp, oPtr->classPtr);
1646 }
1647 return TCL_OK;
1648 }
1649
1650 /*
1651 * ----------------------------------------------------------------------
1652 *
1653 * TclOODefineUnexportObjCmd --
1654 * Implementation of the "unexport" subcommand of the "oo::define" and
1655 * "oo::objdefine" commands.
1656 *
1657 * ----------------------------------------------------------------------
1658 */
1659
1660 int
1661 TclOODefineUnexportObjCmd(
1662 ClientData clientData,
1663 Tcl_Interp *interp,
1664 int objc,
1665 Tcl_Obj *const *objv)
1666 {
1667 int isInstanceUnexport = (clientData != NULL);
1668 Object *oPtr;
1669 Method *mPtr;
1670 Tcl_HashEntry *hPtr;
1671 Class *clsPtr;
1672 int i, isNew, changed = 0;
1673
1674 if (objc < 2) {
1675 Tcl_WrongNumArgs(interp, 1, objv, "name ?name ...?");
1676 return TCL_ERROR;
1677 }
1678
1679 oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1680 if (oPtr == NULL) {
1681 return TCL_ERROR;
1682 }
1683 clsPtr = oPtr->classPtr;
1684 if (!isInstanceUnexport && !clsPtr) {
1685 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1686 "attempt to misuse API", -1));
1687 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1688 return TCL_ERROR;
1689 }
1690
1691 for (i = 1; i < objc; i++) {
1692 /*
1693 * Unexporting is done by removing the PUBLIC_METHOD flag from the
1694 * method record. If there is no such method in this object or class
1695 * (i.e. the method comes from something inherited from or that we're
1696 * an instance of) then we put in a blank record without that flag;
1697 * such records are skipped over by the call chain engine *except* for
1698 * their flags member.
1699 */
1700
1701 if (isInstanceUnexport) {
1702 if (!oPtr->methodsPtr) {
1703 oPtr->methodsPtr = ckalloc(sizeof(Tcl_HashTable));
1704 Tcl_InitObjHashTable(oPtr->methodsPtr);
1705 oPtr->flags &= ~USE_CLASS_CACHE;
1706 }
1707 hPtr = Tcl_CreateHashEntry(oPtr->methodsPtr, (char *) objv[i],
1708 &isNew);
1709 } else {
1710 hPtr = Tcl_CreateHashEntry(&clsPtr->classMethods, (char*) objv[i],
1711 &isNew);
1712 }
1713
1714 if (isNew) {
1715 mPtr = ckalloc(sizeof(Method));
1716 memset(mPtr, 0, sizeof(Method));
1717 mPtr->refCount = 1;
1718 mPtr->namePtr = objv[i];
1719 Tcl_IncrRefCount(objv[i]);
1720 Tcl_SetHashValue(hPtr, mPtr);
1721 } else {
1722 mPtr = Tcl_GetHashValue(hPtr);
1723 }
1724 if (isNew || mPtr->flags & PUBLIC_METHOD) {
1725 mPtr->flags &= ~PUBLIC_METHOD;
1726 changed = 1;
1727 }
1728 }
1729
1730 /*
1731 * Bump the right epoch if we actually changed anything.
1732 */
1733
1734 if (changed) {
1735 if (isInstanceUnexport) {
1736 oPtr->epoch++;
1737 } else {
1738 BumpGlobalEpoch(interp, clsPtr);
1739 }
1740 }
1741 return TCL_OK;
1742 }
1743
1744 /*
1745 * ----------------------------------------------------------------------
1746 *
1747 * Tcl_ClassSetConstructor, Tcl_ClassSetDestructor --
1748 * How to install a constructor or destructor into a class; API to call
1749 * from C.
1750 *
1751 * ----------------------------------------------------------------------
1752 */
1753
1754 void
1755 Tcl_ClassSetConstructor(
1756 Tcl_Interp *interp,
1757 Tcl_Class clazz,
1758 Tcl_Method method)
1759 {
1760 Class *clsPtr = (Class *) clazz;
1761
1762 if (method != (Tcl_Method) clsPtr->constructorPtr) {
1763 TclOODelMethodRef(clsPtr->constructorPtr);
1764 clsPtr->constructorPtr = (Method *) method;
1765
1766 /*
1767 * Remember to invalidate the cached constructor chain for this class.
1768 * [Bug 2531577]
1769 */
1770
1771 if (clsPtr->constructorChainPtr) {
1772 TclOODeleteChain(clsPtr->constructorChainPtr);
1773 clsPtr->constructorChainPtr = NULL;
1774 }
1775 BumpGlobalEpoch(interp, clsPtr);
1776 }
1777 }
1778
1779 void
1780 Tcl_ClassSetDestructor(
1781 Tcl_Interp *interp,
1782 Tcl_Class clazz,
1783 Tcl_Method method)
1784 {
1785 Class *clsPtr = (Class *) clazz;
1786
1787 if (method != (Tcl_Method) clsPtr->destructorPtr) {
1788 TclOODelMethodRef(clsPtr->destructorPtr);
1789 clsPtr->destructorPtr = (Method *) method;
1790 if (clsPtr->destructorChainPtr) {
1791 TclOODeleteChain(clsPtr->destructorChainPtr);
1792 clsPtr->destructorChainPtr = NULL;
1793 }
1794 BumpGlobalEpoch(interp, clsPtr);
1795 }
1796 }
1797
1798 /*
1799 * ----------------------------------------------------------------------
1800 *
1801 * TclOODefineSlots --
1802 * Create the "::oo::Slot" class and its standard instances. Class
1803 * definition is empty at the stage (added by scripting).
1804 *
1805 * ----------------------------------------------------------------------
1806 */
1807
1808 int
1809 TclOODefineSlots(
1810 Foundation *fPtr)
1811 {
1812 const struct DeclaredSlot *slotInfoPtr;
1813 Tcl_Obj *getName = Tcl_NewStringObj("Get", -1);
1814 Tcl_Obj *setName = Tcl_NewStringObj("Set", -1);
1815 Class *slotCls;
1816
1817 slotCls = ((Object *) Tcl_NewObjectInstance(fPtr->interp, (Tcl_Class)
1818 fPtr->classCls, "::oo::Slot", NULL, -1, NULL, 0))->classPtr;
1819 if (slotCls == NULL) {
1820 return TCL_ERROR;
1821 }
1822 Tcl_IncrRefCount(getName);
1823 Tcl_IncrRefCount(setName);
1824 for (slotInfoPtr = slots ; slotInfoPtr->name ; slotInfoPtr++) {
1825 Tcl_Object slotObject = Tcl_NewObjectInstance(fPtr->interp,
1826 (Tcl_Class) slotCls, slotInfoPtr->name, NULL, -1, NULL, 0);
1827
1828 if (slotObject == NULL) {
1829 continue;
1830 }
1831 Tcl_NewInstanceMethod(fPtr->interp, slotObject, getName, 0,
1832 &slotInfoPtr->getterType, NULL);
1833 Tcl_NewInstanceMethod(fPtr->interp, slotObject, setName, 0,
1834 &slotInfoPtr->setterType, NULL);
1835 }
1836 Tcl_DecrRefCount(getName);
1837 Tcl_DecrRefCount(setName);
1838 return TCL_OK;
1839 }
1840
1841 /*
1842 * ----------------------------------------------------------------------
1843 *
1844 * ClassFilterGet, ClassFilterSet --
1845 * Implementation of the "filter" slot accessors of the "oo::define"
1846 * command.
1847 *
1848 * ----------------------------------------------------------------------
1849 */
1850
1851 static int
1852 ClassFilterGet(
1853 ClientData clientData,
1854 Tcl_Interp *interp,
1855 Tcl_ObjectContext context,
1856 int objc,
1857 Tcl_Obj *const *objv)
1858 {
1859 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1860 Tcl_Obj *resultObj, *filterObj;
1861 int i;
1862
1863 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
1864 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
1865 NULL);
1866 return TCL_ERROR;
1867 }
1868 if (oPtr == NULL) {
1869 return TCL_ERROR;
1870 } else if (!oPtr->classPtr) {
1871 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1872 "attempt to misuse API", -1));
1873 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1874 return TCL_ERROR;
1875 }
1876
1877 TclNewObj(resultObj);
1878 FOREACH(filterObj, oPtr->classPtr->filters) {
1879 Tcl_ListObjAppendElement(NULL, resultObj, filterObj);
1880 }
1881 Tcl_SetObjResult(interp, resultObj);
1882 return TCL_OK;
1883 }
1884
1885 static int
1886 ClassFilterSet(
1887 ClientData clientData,
1888 Tcl_Interp *interp,
1889 Tcl_ObjectContext context,
1890 int objc,
1891 Tcl_Obj *const *objv)
1892 {
1893 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1894 int filterc;
1895 Tcl_Obj **filterv;
1896
1897 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
1898 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
1899 "filterList");
1900 return TCL_ERROR;
1901 }
1902 objv += Tcl_ObjectContextSkippedArgs(context);
1903
1904 if (oPtr == NULL) {
1905 return TCL_ERROR;
1906 } else if (!oPtr->classPtr) {
1907 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1908 "attempt to misuse API", -1));
1909 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1910 return TCL_ERROR;
1911 } else if (TclListObjGetElements(interp, objv[0], &filterc,
1912 &filterv) != TCL_OK) {
1913 return TCL_ERROR;
1914 }
1915
1916 TclOOClassSetFilters(interp, oPtr->classPtr, filterc, filterv);
1917 return TCL_OK;
1918 }
1919
1920 /*
1921 * ----------------------------------------------------------------------
1922 *
1923 * ClassMixinGet, ClassMixinSet --
1924 * Implementation of the "mixin" slot accessors of the "oo::define"
1925 * command.
1926 *
1927 * ----------------------------------------------------------------------
1928 */
1929
1930 static int
1931 ClassMixinGet(
1932 ClientData clientData,
1933 Tcl_Interp *interp,
1934 Tcl_ObjectContext context,
1935 int objc,
1936 Tcl_Obj *const *objv)
1937 {
1938 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1939 Tcl_Obj *resultObj;
1940 Class *mixinPtr;
1941 int i;
1942
1943 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
1944 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
1945 NULL);
1946 return TCL_ERROR;
1947 }
1948 if (oPtr == NULL) {
1949 return TCL_ERROR;
1950 } else if (!oPtr->classPtr) {
1951 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1952 "attempt to misuse API", -1));
1953 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1954 return TCL_ERROR;
1955 }
1956
1957 TclNewObj(resultObj);
1958 FOREACH(mixinPtr, oPtr->classPtr->mixins) {
1959 Tcl_ListObjAppendElement(NULL, resultObj,
1960 TclOOObjectName(interp, mixinPtr->thisPtr));
1961 }
1962 Tcl_SetObjResult(interp, resultObj);
1963 return TCL_OK;
1964
1965 }
1966
1967 static int
1968 ClassMixinSet(
1969 ClientData clientData,
1970 Tcl_Interp *interp,
1971 Tcl_ObjectContext context,
1972 int objc,
1973 Tcl_Obj *const *objv)
1974 {
1975 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
1976 int mixinc, i;
1977 Tcl_Obj **mixinv;
1978 Class **mixins;
1979
1980 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
1981 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
1982 "mixinList");
1983 return TCL_ERROR;
1984 }
1985 objv += Tcl_ObjectContextSkippedArgs(context);
1986
1987 if (oPtr == NULL) {
1988 return TCL_ERROR;
1989 } else if (!oPtr->classPtr) {
1990 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1991 "attempt to misuse API", -1));
1992 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
1993 return TCL_ERROR;
1994 } else if (TclListObjGetElements(interp, objv[0], &mixinc,
1995 &mixinv) != TCL_OK) {
1996 return TCL_ERROR;
1997 }
1998
1999 mixins = TclStackAlloc(interp, sizeof(Class *) * mixinc);
2000
2001 for (i = 0; i < mixinc; i++) {
2002 mixins[i] = GetClassInOuterContext(interp, mixinv[i],
2003 "may only mix in classes");
2004 if (mixins[i] == NULL) {
2005 i--;
2006 goto freeAndError;
2007 }
2008 if (TclOOIsReachable(oPtr->classPtr, mixins[i])) {
2009 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2010 "may not mix a class into itself", -1));
2011 Tcl_SetErrorCode(interp, "TCL", "OO", "SELF_MIXIN", (char *)NULL);
2012 goto freeAndError;
2013 }
2014 }
2015
2016 TclOOClassSetMixins(interp, oPtr->classPtr, mixinc, mixins);
2017 TclStackFree(interp, mixins);
2018 return TCL_OK;
2019
2020 freeAndError:
2021 TclStackFree(interp, mixins);
2022 return TCL_ERROR;
2023 }
2024
2025 /*
2026 * ----------------------------------------------------------------------
2027 *
2028 * ClassSuperGet, ClassSuperSet --
2029 * Implementation of the "superclass" slot accessors of the "oo::define"
2030 * command.
2031 *
2032 * ----------------------------------------------------------------------
2033 */
2034
2035 static int
2036 ClassSuperGet(
2037 ClientData clientData,
2038 Tcl_Interp *interp,
2039 Tcl_ObjectContext context,
2040 int objc,
2041 Tcl_Obj *const *objv)
2042 {
2043 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2044 Tcl_Obj *resultObj;
2045 Class *superPtr;
2046 int i;
2047
2048 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
2049 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2050 NULL);
2051 return TCL_ERROR;
2052 }
2053 if (oPtr == NULL) {
2054 return TCL_ERROR;
2055 } else if (!oPtr->classPtr) {
2056 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2057 "attempt to misuse API", -1));
2058 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
2059 return TCL_ERROR;
2060 }
2061
2062 TclNewObj(resultObj);
2063 FOREACH(superPtr, oPtr->classPtr->superclasses) {
2064 Tcl_ListObjAppendElement(NULL, resultObj,
2065 TclOOObjectName(interp, superPtr->thisPtr));
2066 }
2067 Tcl_SetObjResult(interp, resultObj);
2068 return TCL_OK;
2069 }
2070
2071 static int
2072 ClassSuperSet(
2073 ClientData clientData,
2074 Tcl_Interp *interp,
2075 Tcl_ObjectContext context,
2076 int objc,
2077 Tcl_Obj *const *objv)
2078 {
2079 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2080 int superc, i, j;
2081 Tcl_Obj **superv;
2082 Class **superclasses, *superPtr;
2083
2084 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
2085 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2086 "superclassList");
2087 return TCL_ERROR;
2088 }
2089 objv += Tcl_ObjectContextSkippedArgs(context);
2090
2091 if (oPtr == NULL) {
2092 return TCL_ERROR;
2093 } else if (!oPtr->classPtr) {
2094 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2095 "attempt to misuse API", -1));
2096 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
2097 return TCL_ERROR;
2098 } else if (oPtr == oPtr->fPtr->objectCls->thisPtr) {
2099 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2100 "may not modify the superclass of the root object", -1));
2101 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
2102 return TCL_ERROR;
2103 } else if (TclListObjGetElements(interp, objv[0], &superc,
2104 &superv) != TCL_OK) {
2105 return TCL_ERROR;
2106 }
2107
2108 /*
2109 * Allocate some working space.
2110 */
2111
2112 superclasses = (Class **) ckalloc(sizeof(Class *) * superc);
2113
2114 /*
2115 * Parse the arguments to get the class to use as superclasses.
2116 *
2117 * Note that zero classes is special, as it is equivalent to just the
2118 * class of objects. [Bug 9d61624b3d]
2119 */
2120
2121 if (superc == 0) {
2122 superclasses = ckrealloc(superclasses, sizeof(Class *));
2123 if (TclOOIsReachable(oPtr->fPtr->classCls, oPtr->classPtr)) {
2124 superclasses[0] = oPtr->fPtr->classCls;
2125 } else {
2126 superclasses[0] = oPtr->fPtr->objectCls;
2127 }
2128 superc = 1;
2129 AddRef(superclasses[0]->thisPtr);
2130 } else {
2131 for (i = 0; i < superc; i++) {
2132 superclasses[i] = GetClassInOuterContext(interp, superv[i],
2133 "only a class can be a superclass");
2134 if (superclasses[i] == NULL) {
2135 goto failedAfterAlloc;
2136 }
2137 for (j = 0; j < i; j++) {
2138 if (superclasses[j] == superclasses[i]) {
2139 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2140 "class should only be a direct superclass once",
2141 -1));
2142 Tcl_SetErrorCode(interp, "TCL", "OO", "REPETITIOUS", (char *)NULL);
2143 goto failedAfterAlloc;
2144 }
2145 }
2146 if (TclOOIsReachable(oPtr->classPtr, superclasses[i])) {
2147 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2148 "attempt to form circular dependency graph", -1));
2149 Tcl_SetErrorCode(interp, "TCL", "OO", "CIRCULARITY", (char *)NULL);
2150 failedAfterAlloc:
2151 for (; i-- > 0 ;) {
2152 TclOODecrRefCount(superclasses[i]->thisPtr);
2153 }
2154 ckfree(superclasses);
2155 return TCL_ERROR;
2156 }
2157
2158 /*
2159 * Corresponding TclOODecrRefCount() is near the end of this
2160 * function.
2161 */
2162
2163 AddRef(superclasses[i]->thisPtr);
2164 }
2165 }
2166
2167 /*
2168 * Install the list of superclasses into the class. Note that this also
2169 * involves splicing the class out of the superclasses' subclass list that
2170 * it used to be a member of and splicing it into the new superclasses'
2171 * subclass list.
2172 */
2173
2174 if (oPtr->classPtr->superclasses.num != 0) {
2175 FOREACH(superPtr, oPtr->classPtr->superclasses) {
2176 TclOORemoveFromSubclasses(oPtr->classPtr, superPtr);
2177 TclOODecrRefCount(superPtr->thisPtr);
2178 }
2179 ckfree((char *) oPtr->classPtr->superclasses.list);
2180 }
2181 oPtr->classPtr->superclasses.list = superclasses;
2182 oPtr->classPtr->superclasses.num = superc;
2183 FOREACH(superPtr, oPtr->classPtr->superclasses) {
2184 TclOOAddToSubclasses(oPtr->classPtr, superPtr);
2185 }
2186 BumpGlobalEpoch(interp, oPtr->classPtr);
2187
2188 return TCL_OK;
2189 }
2190
2191 /*
2192 * ----------------------------------------------------------------------
2193 *
2194 * ClassVarsGet, ClassVarsSet --
2195 * Implementation of the "variable" slot accessors of the "oo::define"
2196 * command.
2197 *
2198 * ----------------------------------------------------------------------
2199 */
2200
2201 static int
2202 ClassVarsGet(
2203 ClientData clientData,
2204 Tcl_Interp *interp,
2205 Tcl_ObjectContext context,
2206 int objc,
2207 Tcl_Obj *const *objv)
2208 {
2209 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2210 Tcl_Obj *resultObj, *variableObj;
2211 int i;
2212
2213 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
2214 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2215 NULL);
2216 return TCL_ERROR;
2217 }
2218 if (oPtr == NULL) {
2219 return TCL_ERROR;
2220 } else if (!oPtr->classPtr) {
2221 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2222 "attempt to misuse API", -1));
2223 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
2224 return TCL_ERROR;
2225 }
2226
2227 TclNewObj(resultObj);
2228 FOREACH(variableObj, oPtr->classPtr->variables) {
2229 Tcl_ListObjAppendElement(NULL, resultObj, variableObj);
2230 }
2231 Tcl_SetObjResult(interp, resultObj);
2232 return TCL_OK;
2233 }
2234
2235 static int
2236 ClassVarsSet(
2237 ClientData clientData,
2238 Tcl_Interp *interp,
2239 Tcl_ObjectContext context,
2240 int objc,
2241 Tcl_Obj *const *objv)
2242 {
2243 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2244 int varc;
2245 Tcl_Obj **varv, *variableObj;
2246 int i;
2247
2248 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
2249 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2250 "filterList");
2251 return TCL_ERROR;
2252 }
2253 objv += Tcl_ObjectContextSkippedArgs(context);
2254
2255 if (oPtr == NULL) {
2256 return TCL_ERROR;
2257 } else if (!oPtr->classPtr) {
2258 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2259 "attempt to misuse API", -1));
2260 Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL);
2261 return TCL_ERROR;
2262 } else if (TclListObjGetElements(interp, objv[0], &varc,
2263 &varv) != TCL_OK) {
2264 return TCL_ERROR;
2265 }
2266
2267 for (i = 0; i < varc; i++) {
2268 const char *varName = Tcl_GetString(varv[i]);
2269
2270 if (strstr(varName, "::") != NULL) {
2271 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2272 "invalid declared variable name \"%s\": must not %s",
2273 varName, "contain namespace separators"));
2274 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_DECLVAR", (char *)NULL);
2275 return TCL_ERROR;
2276 }
2277 if (Tcl_StringMatch(varName, "*(*)")) {
2278 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2279 "invalid declared variable name \"%s\": must not %s",
2280 varName, "refer to an array element"));
2281 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_DECLVAR", (char *)NULL);
2282 return TCL_ERROR;
2283 }
2284 }
2285
2286 for (i = 0; i < varc; i++) {
2287 Tcl_IncrRefCount(varv[i]);
2288 }
2289 FOREACH(variableObj, oPtr->classPtr->variables) {
2290 Tcl_DecrRefCount(variableObj);
2291 }
2292 if (i != varc) {
2293 if (varc == 0) {
2294 ckfree((char *) oPtr->classPtr->variables.list);
2295 } else if (i) {
2296 oPtr->classPtr->variables.list = (Tcl_Obj **)
2297 ckrealloc((char *) oPtr->classPtr->variables.list,
2298 sizeof(Tcl_Obj *) * varc);
2299 } else {
2300 oPtr->classPtr->variables.list = (Tcl_Obj **)
2301 ckalloc(sizeof(Tcl_Obj *) * varc);
2302 }
2303 }
2304
2305 oPtr->classPtr->variables.num = 0;
2306 if (varc > 0) {
2307 int created, n;
2308 Tcl_HashTable uniqueTable;
2309
2310 Tcl_InitObjHashTable(&uniqueTable);
2311 for (i = n = 0; i < varc; i++) {
2312 Tcl_CreateHashEntry(&uniqueTable, varv[i], &created);
2313 if (created) {
2314 oPtr->classPtr->variables.list[n++] = varv[i];
2315 } else {
2316 Tcl_DecrRefCount(varv[i]);
2317 }
2318 }
2319 oPtr->classPtr->variables.num = n;
2320
2321 /*
2322 * Shouldn't be necessary, but maintain num/list invariant.
2323 */
2324
2325 oPtr->classPtr->variables.list = (Tcl_Obj **)
2326 ckrealloc((char *) oPtr->classPtr->variables.list,
2327 sizeof(Tcl_Obj *) * n);
2328 Tcl_DeleteHashTable(&uniqueTable);
2329 }
2330 return TCL_OK;
2331 }
2332
2333 /*
2334 * ----------------------------------------------------------------------
2335 *
2336 * ObjectFilterGet, ObjectFilterSet --
2337 * Implementation of the "filter" slot accessors of the "oo::objdefine"
2338 * command.
2339 *
2340 * ----------------------------------------------------------------------
2341 */
2342
2343 static int
2344 ObjFilterGet(
2345 ClientData clientData,
2346 Tcl_Interp *interp,
2347 Tcl_ObjectContext context,
2348 int objc,
2349 Tcl_Obj *const *objv)
2350 {
2351 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2352 Tcl_Obj *resultObj, *filterObj;
2353 int i;
2354
2355 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
2356 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2357 NULL);
2358 return TCL_ERROR;
2359 } else if (oPtr == NULL) {
2360 return TCL_ERROR;
2361 }
2362
2363 TclNewObj(resultObj);
2364 FOREACH(filterObj, oPtr->filters) {
2365 Tcl_ListObjAppendElement(NULL, resultObj, filterObj);
2366 }
2367 Tcl_SetObjResult(interp, resultObj);
2368 return TCL_OK;
2369 }
2370
2371 static int
2372 ObjFilterSet(
2373 ClientData clientData,
2374 Tcl_Interp *interp,
2375 Tcl_ObjectContext context,
2376 int objc,
2377 Tcl_Obj *const *objv)
2378 {
2379 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2380 int filterc;
2381 Tcl_Obj **filterv;
2382
2383 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
2384 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2385 "filterList");
2386 return TCL_ERROR;
2387 } else if (oPtr == NULL) {
2388 return TCL_ERROR;
2389 }
2390 objv += Tcl_ObjectContextSkippedArgs(context);
2391 if (TclListObjGetElements(interp, objv[0], &filterc,
2392 &filterv) != TCL_OK) {
2393 return TCL_ERROR;
2394 }
2395
2396 TclOOObjectSetFilters(oPtr, filterc, filterv);
2397 return TCL_OK;
2398 }
2399
2400 /*
2401 * ----------------------------------------------------------------------
2402 *
2403 * ObjectMixinGet, ObjectMixinSet --
2404 * Implementation of the "mixin" slot accessors of the "oo::objdefine"
2405 * command.
2406 *
2407 * ----------------------------------------------------------------------
2408 */
2409
2410 static int
2411 ObjMixinGet(
2412 ClientData clientData,
2413 Tcl_Interp *interp,
2414 Tcl_ObjectContext context,
2415 int objc,
2416 Tcl_Obj *const *objv)
2417 {
2418 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2419 Tcl_Obj *resultObj;
2420 Class *mixinPtr;
2421 int i;
2422
2423 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
2424 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2425 NULL);
2426 return TCL_ERROR;
2427 } else if (oPtr == NULL) {
2428 return TCL_ERROR;
2429 }
2430
2431 TclNewObj(resultObj);
2432 FOREACH(mixinPtr, oPtr->mixins) {
2433 if (mixinPtr) {
2434 Tcl_ListObjAppendElement(NULL, resultObj,
2435 TclOOObjectName(interp, mixinPtr->thisPtr));
2436 }
2437 }
2438 Tcl_SetObjResult(interp, resultObj);
2439 return TCL_OK;
2440 }
2441
2442 static int
2443 ObjMixinSet(
2444 ClientData clientData,
2445 Tcl_Interp *interp,
2446 Tcl_ObjectContext context,
2447 int objc,
2448 Tcl_Obj *const *objv)
2449 {
2450 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2451 int mixinc;
2452 Tcl_Obj **mixinv;
2453 Class **mixins;
2454 int i;
2455
2456 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
2457 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2458 "mixinList");
2459 return TCL_ERROR;
2460 } else if (oPtr == NULL) {
2461 return TCL_ERROR;
2462 }
2463 objv += Tcl_ObjectContextSkippedArgs(context);
2464 if (TclListObjGetElements(interp, objv[0], &mixinc,
2465 &mixinv) != TCL_OK) {
2466 return TCL_ERROR;
2467 }
2468
2469 mixins = TclStackAlloc(interp, sizeof(Class *) * mixinc);
2470
2471 for (i = 0; i < mixinc; i++) {
2472 mixins[i] = GetClassInOuterContext(interp, mixinv[i],
2473 "may only mix in classes");
2474 if (mixins[i] == NULL) {
2475 TclStackFree(interp, mixins);
2476 return TCL_ERROR;
2477 }
2478 }
2479
2480 TclOOObjectSetMixins(oPtr, mixinc, mixins);
2481 TclStackFree(interp, mixins);
2482 return TCL_OK;
2483 }
2484
2485 /*
2486 * ----------------------------------------------------------------------
2487 *
2488 * ObjectVarsGet, ObjectVarsSet --
2489 * Implementation of the "variable" slot accessors of the "oo::objdefine"
2490 * command.
2491 *
2492 * ----------------------------------------------------------------------
2493 */
2494
2495 static int
2496 ObjVarsGet(
2497 ClientData clientData,
2498 Tcl_Interp *interp,
2499 Tcl_ObjectContext context,
2500 int objc,
2501 Tcl_Obj *const *objv)
2502 {
2503 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2504 Tcl_Obj *resultObj, *variableObj;
2505 int i;
2506
2507 if (Tcl_ObjectContextSkippedArgs(context) != objc) {
2508 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2509 NULL);
2510 return TCL_ERROR;
2511 } else if (oPtr == NULL) {
2512 return TCL_ERROR;
2513 }
2514
2515 TclNewObj(resultObj);
2516 FOREACH(variableObj, oPtr->variables) {
2517 Tcl_ListObjAppendElement(NULL, resultObj, variableObj);
2518 }
2519 Tcl_SetObjResult(interp, resultObj);
2520 return TCL_OK;
2521 }
2522
2523 static int
2524 ObjVarsSet(
2525 ClientData clientData,
2526 Tcl_Interp *interp,
2527 Tcl_ObjectContext context,
2528 int objc,
2529 Tcl_Obj *const *objv)
2530 {
2531 Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp);
2532 int varc, i;
2533 Tcl_Obj **varv, *variableObj;
2534
2535 if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) {
2536 Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv,
2537 "variableList");
2538 return TCL_ERROR;
2539 } else if (oPtr == NULL) {
2540 return TCL_ERROR;
2541 }
2542 objv += Tcl_ObjectContextSkippedArgs(context);
2543 if (TclListObjGetElements(interp, objv[0], &varc,
2544 &varv) != TCL_OK) {
2545 return TCL_ERROR;
2546 }
2547
2548 for (i = 0; i < varc; i++) {
2549 const char *varName = Tcl_GetString(varv[i]);
2550
2551 if (strstr(varName, "::") != NULL) {
2552 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2553 "invalid declared variable name \"%s\": must not %s",
2554 varName, "contain namespace separators"));
2555 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_DECLVAR", (char *)NULL);
2556 return TCL_ERROR;
2557 }
2558 if (Tcl_StringMatch(varName, "*(*)")) {
2559 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2560 "invalid declared variable name \"%s\": must not %s",
2561 varName, "refer to an array element"));
2562 Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_DECLVAR", (char *)NULL);
2563 return TCL_ERROR;
2564 }
2565 }
2566 for (i = 0; i < varc; i++) {
2567 Tcl_IncrRefCount(varv[i]);
2568 }
2569
2570 FOREACH(variableObj, oPtr->variables) {
2571 Tcl_DecrRefCount(variableObj);
2572 }
2573 if (i != varc) {
2574 if (varc == 0) {
2575 ckfree((char *) oPtr->variables.list);
2576 } else if (i) {
2577 oPtr->variables.list = (Tcl_Obj **)
2578 ckrealloc((char *) oPtr->variables.list,
2579 sizeof(Tcl_Obj *) * varc);
2580 } else {
2581 oPtr->variables.list = (Tcl_Obj **)
2582 ckalloc(sizeof(Tcl_Obj *) * varc);
2583 }
2584 }
2585 oPtr->variables.num = 0;
2586 if (varc > 0) {
2587 int created, n;
2588 Tcl_HashTable uniqueTable;
2589
2590 Tcl_InitObjHashTable(&uniqueTable);
2591 for (i = n = 0; i < varc; i++) {
2592 Tcl_CreateHashEntry(&uniqueTable, varv[i], &created);
2593 if (created) {
2594 oPtr->variables.list[n++] = varv[i];
2595 } else {
2596 Tcl_DecrRefCount(varv[i]);
2597 }
2598 }
2599 oPtr->variables.num = n;
2600
2601 /*
2602 * Shouldn't be necessary, but maintain num/list invariant.
2603 */
2604
2605 oPtr->variables.list = (Tcl_Obj **)
2606 ckrealloc((char *) oPtr->variables.list,
2607 sizeof(Tcl_Obj *) * n);
2608 Tcl_DeleteHashTable(&uniqueTable);
2609 }
2610 return TCL_OK;
2611 }
2612
2613 /*
2614 * Local Variables:
2615 * mode: c
2616 * c-basic-offset: 4
2617 * fill-column: 78
2618 * End:
2619 */