<br><br><div class="gmail_quote">On Fri, Apr 1, 2011 at 5:51 AM, Javier Pimás <span dir="ltr">&lt;<a href="mailto:elpochodelagente@gmail.com">elpochodelagente@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
 <br>Hi, this method of Interpreter is used for debugging, and should print the name of the class passed in console:<div><br></div><div><div>printNameOfClass: classOop count: cnt</div><div><span style="white-space:pre-wrap">        </span>&quot;Details: The count argument is used to avoid a possible infinite recursion if classOop is a corrupted object.&quot;</div>


<div><br></div><div><span style="white-space:pre-wrap">        </span>cnt &lt;= 0 ifTrue: [ ^ self print: &#39;bad class&#39; ].</div><div><span style="white-space:pre-wrap">        </span>(self sizeBitsOf: classOop) = (7 * self bytesPerWord)<span style="white-space:pre-wrap">        </span>&quot;(Metaclass instSize+1 * 4)&quot;</div>


<div><span style="white-space:pre-wrap">                </span>ifTrue: [self printNameOfClass: (self fetchPointer: 5 &quot;thisClass&quot; ofObject: classOop) </div><div><span style="white-space:pre-wrap">                                        </span>count: cnt - 1.</div>


<div><span style="white-space:pre-wrap">                                </span>self print: &#39; class&#39;]</div><div><span style="white-space:pre-wrap">        </span>ifFalse: [self printStringOf: (self fetchPointer: 6 &quot;name&quot; ofObject: classOop)]</div>


<div><br></div><div>it&#39;s logic is this: if the classOop is of a normal Class instance, it should use the instance var number six,<br> </div></div></blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div>which corresponds to the class&#39; name. If not, then the classOop represents a Metaclass instance, and then it will fetch inst var number 5, which is the corresponding class, print its name, and then print &#39;class&#39; to let you distinguish it that it is a metaclass.</div>
</div></blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div>

<div><br></div><div>The problem of the method is that it determines if it&#39;s a class instance or a metaclass instance by looking at the size of the oop, which seems to have changed, at least in pharo (Metaclass instSize+1 * 4 = 36 = 9*bytesPerWord). </div>
</div></blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div>

<div><br></div><div>The method could be fixed by changing 7 to 9 then. But it will break if Metaclass changes again. </div></div></blockquote><div><br>yes<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div>So I have this idea: make it check if the classOop is a metaclass instance. Metaclass class is not a special object, but can be obtained by accessing any special one, like with </div>

<div><br></div><div>Integer class class</div><div><br></div><div>or directly with</div><div><br></div><div>nil class class class (!!)</div><div><br></div><div>then the proposed code is:</div><div><br></div><div><span style="white-space:pre-wrap">printNameOfClass: classOop count: cnt
        &quot;Details: The count argument is used to avoid a possible infinite recursion if classOop is a corrupted object.&quot;
        | metaclassClass |
        cnt &lt;= 0 ifTrue: [ ^ self print: &#39;bad class&#39; ].
        metaclassClass := self fetchClassOf: (self fetchClassOf: (self fetchPointer: ClassInteger ofObject: specialObjectsOop)).
        metaclassClass = (self fetchClassOf: classOop) &quot;Is it a metaclass?&quot;
                ifTrue: [self printNameOfClass: (self fetchPointer: 5 &quot;thisClass&quot; ofObject: classOop) 
                                        count: cnt - 1.
                                self print: &#39; class&#39;]
        ifFalse: [self printStringOf: (self fetchPointer: 6 &quot;name&quot; ofObject: classOop)]</span></div><div><br></div></div></blockquote><div><br><br>Looking at Cog, I think Eliot has already solved this problem:<br><br>
printNameOfClass: classOop count: cnt<br>    &quot;Details: The count argument is used to avoid a possible infinite recursion if classOop is a corrupted object.&quot;<br>    &lt;inline: false&gt;<br>    (classOop = 0 or: [cnt &lt;= 0]) ifTrue: [^self print: &#39;bad class&#39;].<br>
    ((objectMemory sizeBitsOf: classOop) = <span style="background-color: rgb(255, 255, 51);">metaclassSizeBytes</span><br>      and: [metaclassSizeBytes &gt; (<span style="background-color: rgb(255, 255, 51);">thisClassIndex</span> * BytesPerWord)])    &quot;(Metaclass instSize * 4)&quot;<br>
        ifTrue: [self printNameOfClass: (objectMemory fetchPointer: <span style="background-color: rgb(255, 255, 51);">thisClassIndex</span> ofObject: classOop) count: cnt - 1.<br>                self print: &#39; class&#39;]<br>
        ifFalse: [self printStringOf: (objectMemory fetchPointer: <span style="background-color: rgb(255, 255, 51);">classNameIndex</span> ofObject: classOop)]<br><br><br>And metaclassSizeBytes is set in a &quot;similar&quot; way you proposed:<br>
<br>StackInterpreter &gt;&gt; initializeExtraClassInstVarIndices<br>    &quot;Initialize metaclassSizeBytes and thisClassIndex which are used in debug printing, and<br>     classNameIndex which is used not only for debug printing but for is:KindOf: &amp; is:MemberOf:<br>
     via classNameOf:is: (evil but a reality we have to accept).&quot;<br>    | classArrayObj classArrayClass |<br><span style="background-color: rgb(255, 255, 51);">    classNameIndex := 6. &quot;default&quot;</span><br style="background-color: rgb(255, 255, 51);">
<span style="background-color: rgb(255, 255, 51);">    thisClassIndex := 5. &quot;default&quot;</span><br>    classArrayObj := objectMemory splObj: ClassArray.<br>    classArrayClass := objectMemory fetchClassOfNonInt: classArrayObj.<br>
 <span style="background-color: rgb(255, 255, 51);">   metaclassSizeBytes := objectMemory sizeBitsOf: classArrayClass.    &quot;determine actual (Metaclass instSize * 4)&quot;</span><br> ......<br><br><br>So...it is correct to assume that this is solved in Cog ?<br>
<br>Thanks<br><br>Mariano<br><br><br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div><br></div><div>I&#39;d also like to have this included too:</div>
<div><br></div><div><div>printClassOf: oop</div>

<div><span style="white-space:pre-wrap">        </span>&quot;Print the class of the oop in console&quot;</div><div><span style="white-space:pre-wrap">        </span>self printNameOfClass: (self fetchClassOf: oop) count: 5. </div>

</div><div><br></div><div>which is simple but pretty much useful.</div><div><br></div><div>What do you think?</div><div><br></div><div>Regards,</div><div>           Javier.</div><br>-- <br>Javier Pimás<br>Ciudad de Buenos Aires<br>



</div>
<br></blockquote></div><br><br clear="all"><br>-- <br>Mariano<br><a href="http://marianopeck.wordpress.com" target="_blank">http://marianopeck.wordpress.com</a><br><br>