<br><br><div class="gmail_quote">On Wed, Jul 8, 2009 at 4:53 AM, David Goehrig <span dir="ltr">&lt;<a href="mailto:dave@nexttolast.com">dave@nexttolast.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br><br><div class="gmail_quote">On Wed, Jul 8, 2009 at 2:04 AM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="gmail_quote"><div><div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div>

<div><br></div></div></div></div></blockquote></div></div><div>You could do something analogous with messages and methods an d have the Vm associate the receiver with the activation in... a message send :)</div>
<div>If you had a dictionary that cached translations form short forms of selectors, e.g. myJSFunc:with: -&gt; myJSFunc:with:with: then your code might look something like</div><div><div><br></div><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse"><div>


doesNotUnderstand: aMessage</div><div><span style="white-space:pre">        </span>&quot;Generic catch all that performs all selectors&quot;</div></span><div><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse;white-space:pre">        </span>(self can: aMessage selector) ifTrue:</div>


</div><div><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse;white-space:pre">                <span style="border-collapse:separate;font-family:arial;font-size:small;white-space:normal">[actualSelector := self s: aMessage selector.</span></span></div>


<div><span style="font-family:arial, sans-serif;border-collapse:collapse;white-space:pre"></span></div><div><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse;white-space:pre">                 ^<span style="border-collapse:separate;font-family:arial;font-size:small;white-space:normal">self perform: actualSelector</span></span></div>


<div><span style="font-family:arial, sans-serif;border-collapse:collapse;white-space:pre"></span></div><div><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse;white-space:pre">                        <span style="border-collapse:separate;font-size:small;white-space:normal">withArguments: (aMessage arguments, (Array new: actualSelector numArgs - aMessage selector numArgs))]</span></span></div>


<div><span style="font-family:arial, sans-serif;border-collapse:collapse;white-space:pre"></span></div><div><br></div><div>Would this be better? </div></div></blockquote></div><br></div>Well it it had a prayer of working :)  Here&#39;s a little backstory.  The way this object works is it inherits from IdentityDictionary has two accessor methods:<div>

<br></div><div>has: aProperty of: aValue</div><div>        &quot; This corresponds to object.property = value and object[&#39;property&#39;] = value&quot;</div><div>        self at: aProperty put: aValue.</div><div>        ^ self.</div>

<div><br></div><div>and</div><div><br></div><div>s: aProperty</div><div>       &quot; This corresponds to object.property and object[&#39;property&#39;]&quot;</div><div>       ^ self at: aProperty ifAbsent: [^ nil ].<br clear="all">

<br></div><div>For a very limited sets of methods such as these, the ones that can not be overridden by end user code, these fields are accessed by doing a message</div><div>send to the object, and the dNU routine determines which route to return based on the method can:</div>

<div><br></div><div>can: aSymbol</div><div>     &quot; This corresponds to the javascript:  Object.prototype.can = function(k) { return this.hasOwnProperty(k) &amp;&amp; typeof(this[k]) == &#39;function&#39; }&quot;</div>

<div>     ^ ( self s: aSymbol ) isBlock.</div><div><br></div><div>What happens in most of the code is objects generate block closures which they then set as the values of other object&#39;s properties. The code ends up looking like this toy example:</div>

<div>----</div><div><div>(An object named: #Casper) has: #greets: of: [ :x | Transcript show: &#39;Hi &#39;, x name , &#39;! My name is &#39;, self name; cr].</div><div>(An object named: #George) from: Casper.</div><div>
<br>
</div><div>Casper greets: George.</div><div>George greets: Casper.</div><div><br></div><div>Casper has: #greets: of: [ :x | Transcript show: &#39;Goodbye &#39;, x name; cr]</div></div><div><br></div><div>Casper greets: George.</div>

<div>George greets: Casper.</div><div>----</div><div><br></div><div>And the Transcript then reads:</div><div>----</div><div><div>Hi George! My name is Casper</div><div>Hi Casper! My name is George</div><div>Goodbye George</div>

<div>Hi Casper! My name is George</div></div><div>----</div><div><br></div><div>The name and named: methods actually set key value and find key by value in the Smalltalk dictionary, so they&#39;re globals as in editing Javascript&#39;s window object.</div>

<div><br></div><div>But as you see, I&#39;m passing a block closure to one object, and copying it to another object, and both objects are referencing the same block closure!  I&#39;m overwriting the</div><div>original block closure&#39;s outerContext&#39;s receiver (which in this example is originally nil!), with whatever object is referencing it.  This is similar to what happens with Javascript </div>

<div>method calls, where the this variable is set by hacking the scope object at call time.  But this lets these objects respond to any message that may come their way, and usually</div><div>do the right thing.  My original dNU used the valueWithEnoughArugments: method, because it was the only way I could find to neatly get all the block closures to work in both</div>

<div>the case of over and underflow, as their equivalent functions worked in the JS version.</div><div><br></div><div>If I compile an actual method send, and dNU is not called, I then need to always compile a method every time a property is set, even if it is never executed. </div>

<div><br></div><div>In this application, objects inherit based on a shallow copy of object properties.  So you may inherit some methods using from: from one object, a bunch of others from another,</div><div>and then override some of each of those in a third.  (This scenario actually the norm not the exception btw!)  99% of the time, I want whatever the values the block closure </div>

<div>derived from its original defining context, with the sole exception of the outerContext&#39;s receiver, which is almost always wrong for my purposes.    </div><div><br></div><div>I&#39;m mostly wondering if there&#39;s a cleaner, more optimizeable way of messing around with the internals of either the resulting MethodContext or your closure objects :)</div>
</blockquote><div><br></div><div>Well the obvious way to clean up those closures is to pass the self reference as an argument, so</div><div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>
(An object named: #Casper) has: #greets: of: [ :theSelf :x | Transcript show: &#39;Hi &#39;, x name , &#39;! My name is &#39;, theSelf name; cr].</div><div><br></div><div>and</div><div><br></div><div><div>doesNotUnderstand: aMessage</div>
<div><span style="white-space: pre; ">        </span>&quot;Generic catch all that performs all selectors&quot;</div><div><span style="white-space: pre; ">        </span>(self can: aMessage selector) ifTrue:</div><div><span style="white-space: pre; ">                [<span class="Apple-style-span" style="white-space: normal; ">^( self s: aMessage selector ) valueWithEffectiveReceiver: self andEnoughArguments: aMessage arguments ].</span></span></div>
<div><span style="white-space: pre; ">        </span>^ self s: aMessage selector.</div><div><br></div><div>and for a little bit of extra efficiency I would combine can: and s:, e.g.</div><div><br></div><div><div>doesNotUnderstand: aMessage</div>
<div><span style="white-space: pre; ">        </span>&quot;Generic catch all that performs all selectors&quot;</div><div><span style="white-space: pre; ">        ^</span>(self s: aMessage selector)</div><div><span class="Apple-style-span" style="white-space: pre; ">                <span class="Apple-style-span" style="white-space: normal; ">ifNotNil: <span class="Apple-style-span" style="white-space: pre; ">[:block| <span class="Apple-style-span" style="white-space: normal; ">block valueWithEffectiveReceiver: self andEnoughArguments: aMessage arguments]</span></span></span></span></div>
<div><span style="white-space: pre; ">                ifNil: [</span>self s: aMessage selector]</div><div><br></div><div><br></div><div>As regards more generic property handling I don&#39;t know of a better way than the doesNotUnderstand: handler for handling properties held in per-instance dictionaries if the constraint is that you definitely don&#39;t want to compile accessors for the property and you are using the vanilla VM.  You would indeed need to modify the VM to provide for a cheap property accessor, for example, being able to add something as the value in a method dictionary that said &quot;invoke this selector with the actual selector as an additional argument&quot;, so in the method dictionary you would have (avoiding support for varargs)</div>
<div><br></div><div>    myProperty -&gt; { #getProperty: } asPropertyAccessorThingy</div><div>    myProperty: -&gt; { #setProperty:to: } asPropertyAccessorThingy</div><div>    myMoreGenericFunction:with: -&gt; { #evaluateGenericProperty:with:with: } asPropertyAccessorThingy</div>
<div><br></div><div>etc and</div><div><br></div><div>MyPrototypeObject getProperty: propertyName</div><div>         ^properties at: propertyName ifAbsent: [parent getProperty: propertyName]</div><div><br></div><div>etc</div>
<div><br></div><div><br></div><div>But methods are cheap and so I don&#39;t see the benefit of avoiding methods in favour of blocks.  When one uses mehtods all sorts of optimizations are enabled, such as efficiently mapping high-dynamic-frequency properties to slots a la V8.  Vassili Bykov, Claus Gittinger and I did a prototype JavaScript implementation in Smalltalk in the summer of 2006 which did this and it was fast.  Vassili came up with the great idea of generating bytecode for slot accessors directly and minting a set of 256 accessors that traversed the parent chain in a loop.  Effectively each accessor method looked like</div>
<div><br></div><div>accessSlot0</div><div>    | object |</div><div>    object := self.</div><div>    [object slot0 ifNotNil: [:value| ^value].</div><div>     (object := object parentSlot) ~~ nil] whileTrue.</div><div>    ^#undefined</div>
<div><br></div><div>The closure bytecode pushTemp:inVectorAt: can be used to fetch an arbitrary slot of an object held in a temporary and so is used to implement object slotN and object parentSlot.</div><div><br></div><div>
Both Claus Gittinger and Dan Ingalls have had the idea of creating an object that is an instance of itself.  One way, without VM mods (and ignoring issues with compact classes), is to do something like</div><div><br></div>
<div>prototype: aPrototype numNewSlots: numNewSlots</div><div>    | format object |</div><div>    object := (Array new: aPrototype numberOfSlots + numNewSlots + 3).</div><div>    object</div><div>         at: 1 &quot;superclass&quot; put: aPrototype;</div>
<div>         at: 2 &quot;methods&quot;    put: MethodDictionary new;</div><div>         at: 3 &quot;format&quot;       put: (self formatOf: aPrototype format withAdditionalSlots: numNewSlots).</div><div>   object changeClassTo: object.</div>
<div>   ^object</div><div><br></div><div>If one flattens MethodDictionary into an array of pairs, which is good for space and even faster for lookup for most dictionaries, because they&#39;re small, and linear scan is fast, then nil looks like an empty method dictionary.  Then a primitive implementation is no more than a single allocation that fills in the prototype and format slots and sets the class.</div>
<div><br></div><div>Combine one with the other and you have the potential for a lean and mean prototype-based system using the existing VM technology, which would mean an efficient JavaScript implementation within Smalltalk.</div>
<div> </div><div>so much to do and so little time ;)</div><div><br></div><div>best</div><div>Eliot</div><div><br></div></div></div></span></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div class="h5"><div>Dave</div><div><br></div><div><br></div><div>-- <br>-=-=-=-=-=-=-=-=-=-=- <a href="http://blog.dloh.org/" target="_blank">http://blog.dloh.org/</a></div></div></div></blockquote></div><br>