<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: self</title>
 <link href="http://bluishcoder.co.nz/tag/self/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Exporting Self objects to source files using the Transporter</title>
   <link href="http://bluishcoder.co.nz/2015/11/19/exporting-self-objects-to-source-files-via-transporter.html"/>
   <updated>2015-11-19T15:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2015/11/19/exporting-self-objects-to-source-files-via-transporter</id>
   <content type="html">&lt;p&gt;Following on from my previous video on &lt;a href=&quot;http://bluishcoder.co.nz/2015/11/18/demo-of-programming-in-self.html&quot;&gt;using the Self GUI&lt;/a&gt; to create objects, I&#39;ve done another that demonstrates how to export the objects to Self source files where they can be managed with a version control system and shared with other developers.&lt;/p&gt;

&lt;p&gt;&lt;video width=&quot;640&quot; controls=&quot;controls&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_transporter.mp4&#39; type=&#39;video/mp4&#39; /&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_transporter.webm&#39; type=&#39;video/webm&#39; /&gt;
&lt;/video&gt;&lt;/p&gt;

&lt;p&gt;The video is available as &lt;a href=&quot;http://bluishcoder.co.nz/self/self_transporter.webm&quot;&gt;self_transporter.webm&lt;/a&gt;, &lt;a href=&quot;http://bluishcoder.co.nz/self/self_transporter.mp4&quot;&gt;self_transporter.mp4&lt;/a&gt; or on &lt;a href=&quot;https://www.youtube.com/watch?v=JgCr0BOz8p0&quot;&gt;YouTube&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More information on using the transporter tool can be obtained from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://handbook.selflanguage.org/4.5/howtoprg.html#the-transporter&quot;&gt;Self Handbook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/self/transporter.pdf&quot;&gt;Annotating objects for transporting to other worlds (PDF)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://groups.yahoo.com/neo/groups/self-interest/conversations/messages/3187&quot;&gt;Transporter trees&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Demo of programming in Self</title>
   <link href="http://bluishcoder.co.nz/2015/11/18/demo-of-programming-in-self.html"/>
   <updated>2015-11-18T15:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2015/11/18/demo-of-programming-in-self</id>
   <content type="html">&lt;p&gt;The &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self programming language&lt;/a&gt; has a GUI environment which can be a bit intimidating to people new to the system. The &lt;a href=&quot;http://handbook.selflanguage.org/4.5/howtoprg.html&quot;&gt;Self Handbook&lt;/a&gt; covers it but it&#39;s difficult to convey usage with text and static images.&lt;/p&gt;

&lt;p&gt;I&#39;ve created a video that demonstrates how to develop in Self using the GUI to create a &#39;bank account&#39; object and manipulate it. The idea of this was based on a discussion on the Self issue tracker about &lt;a href=&quot;https://github.com/russellallen/self/issues/53&quot;&gt;tutorials for Self&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;video width=&quot;640&quot; controls=&quot;controls&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_bankaccount.webm&#39; type=&#39;video/webm&#39; /&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_bankaccount.mp4&#39; type=&#39;video/mp4&#39; /&gt;
&lt;/video&gt;&lt;/p&gt;

&lt;p&gt;The video is available as &lt;a href=&quot;http://bluishcoder.co.nz/self/self_bankaccount.webm&quot;&gt;self_bankaccount.webm&lt;/a&gt;, &lt;a href=&quot;http://bluishcoder.co.nz/self/self_bankaccount.mp4&quot;&gt;self_bankaccount.mp4&lt;/a&gt; or on &lt;a href=&quot;https://www.youtube.com/watch?v=5Jhi5yN9S1o&quot;&gt;YouTube&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Improving Linux Font Support in Self</title>
   <link href="http://bluishcoder.co.nz/2015/01/08/improving-linux-font-support-in-self.html"/>
   <updated>2015-01-08T19:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2015/01/08/improving-linux-font-support-in-self</id>
   <content type="html">&lt;p&gt;In the Linux version of the &lt;a href=&quot;http://www.selflanguage.org/&quot;&gt;Self programming language implementation&lt;/a&gt; the fonts used are standard X11 fonts. On modern Linux systems these don&#39;t look great and a common question asked in the mailing list is how to improve it. Fonts on the Mac OS X build of Self use a different system and they look much better. It would be good to convert the Linux version to use &lt;a href=&quot;http://www.freetype.org/&quot;&gt;freetype&lt;/a&gt; to gain more control over fonts.&lt;/p&gt;

&lt;p&gt;I worked on adding Freetype support a couple of years ago and &lt;a href=&quot;https://groups.yahoo.com/neo/groups/self-interest/conversations/messages/2927&quot;&gt;wrote about it on the mailing list&lt;/a&gt;. I haven&#39;t done much on it since then but the code is in my github repository under the &lt;a href=&quot;https://github.com/doublec/self/tree/linux_fonts&quot;&gt;linux_fonts&lt;/a&gt; branch. That work adds Self primitives to access Freetype but does not integrate it into the Self font system. I hope to be able to continue this work sometime but I&#39;m unlikely to get to it in the near future. This post is to point to the code, show how to use the primitives, in case someone else would like to take it forward as a project.&lt;/p&gt;

&lt;p&gt;To try the code out you&#39;ll need to build Self in the usual manner but using that branch. Once the desktop is launched I test it by creating an object with the following slots:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(| parent* = traits oddball.
   window &amp;lt;- desktop w anyWindowCanvas.
   draw &amp;lt;-- nil.
   font &amp;lt;- nil.
   xftcolor &amp;lt;- nil.
   xrcolor &amp;lt;- nil.
 |)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With an outliner for this object I create an evaulator for it and, one at a time, evaluate the following code snippets:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;draw: window display xftDrawCreate: window platformWindow
  Visual: window display screen defaultVisualOfScreen
  Colormap: window display screen defaultColormapOfScreen

xftcolor: xlib xftColor new.
xrcolor: xlib xRenderColor new.
xrcolor alpha: 16rffff

window display xftColorAllocValue: window display screen defaultVisualOfScreen
  Colormap: window display screen defaultColormapOfScreen
  RenderColor: xrcolor
  XftColor: xftcolor

font: window display xftFontOpenNameOnScreen: window display screen number
 Name: &#39;verdana-18&#39;

draw xftDrawString8: xftcolor
  Font: font X: 100 Y: 100
  String: &#39;Hello World!&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The results in the text &#39;Hello World!&#39; appearing at position 100@100 on the desktop in the Verdana font. The video below demonstrates this to show how it&#39;s done in the Self user interface:&lt;/p&gt;

&lt;p&gt;&lt;video controls=&quot;controls&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_font.webm&#39; type=&#39;video/webm&#39; /&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_font.mp4&#39; type=&#39;video/mp4&#39; /&gt;
  The video is available as &lt;a href=&quot;http://bluishcoder.co.nz/self/self_font.webm&quot;&gt;self_font.webm&lt;/a&gt; or &lt;a href=&quot;http://bluishcoder.co.nz/self/self_font.mp4&quot;&gt;self_font.mp4&lt;/a&gt;.
&lt;/video&gt;&lt;/p&gt;

&lt;p&gt;This is a common workflow I use to prototype things in Self. I create an empty object and populate it with slots to hold data. With an evaluator created for that object these slots are accessible without needing to have an object to call them on. This is known as &#39;implicit self calls&#39;. The message for the slot implicitly is sent to the current object. I create and remove slots as needed. I can use the Self outliner to drill down on the slots to look at and manipulate those objects specifically if needed.&lt;/p&gt;

&lt;p&gt;Hopefully this Freetype support can be used as a base for better looking fonts on Linux. If you are keen to take it further, or have ideas on how to integrate it, I can be contacted using the details at the bottom of my blog, or you can raise it in the &lt;a href=&quot;https://groups.yahoo.com/neo/groups/self-interest&quot;&gt;Self mailing list&lt;/a&gt; or on github.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Self Benchmarking</title>
   <link href="http://bluishcoder.co.nz/2014/12/22/self-benchmarking.html"/>
   <updated>2014-12-22T23:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2014/12/22/self-benchmarking</id>
   <content type="html">&lt;p&gt;I was asked on twitter about the current speed of the &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self implementation&lt;/a&gt;. The request was for a method send benchmark so I wrote a simple one and compared against &lt;a href=&quot;http://pharo.org/&quot;&gt;Pharo&lt;/a&gt;, a Smalltalk implementation.&lt;/p&gt;

&lt;p&gt;The implementation and technology behind Self is quite old in comparison to modern compiler implementations but at the time it was state of the art. I hoped it would hold up reasonably well. The test I wrote in Self was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(|
  doSomething = ( ^self ).
  test = ( |n &amp;lt;- 0|
           [ n &amp;lt; 100000000 ] whileTrue: [ doSomething. n: n + 1 ]
         )
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Running this in the Self shell shows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 1&quot; _AddSlots: ...code snippet from above...
shell
&quot;Self 2&quot; [ test ] time.
2587
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;2.5 seconds seems a bit slow to me but I tested in Pharo to confirm and to see how it compares. The Pharo code looks almost exactly like the Self code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;doSomething = ^self
test = |count|
       count := 0.
       [ count &amp;lt; 100000000 ] whileTrue: [
         count := count + 1.
         doSomething.
       ].

[ MyObject new test ] timeToRun
  =&amp;gt; 0:00:00:00.239
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&#39;s 239ms vs 2,587ms, a factor of over 10x. Further investigation revealed that calling &#39;time&#39; in Self seems to cause the code to run slower. If I call the &#39;test&#39; method first, and then call &#39;time&#39; then it&#39;s much faster:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 2&quot; [ test ] time.
2587
&quot;Self 3&quot; [ test ] time.
2579
&quot;Self 4&quot; test.
nil
&quot;Self 5&quot; [ test ] time.
650
&quot;Self 6&quot; [ test ] time.
628
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At 650ms it is about 2.7x slower than Pharo, an improvement over 10x. More investigation is needed to see if there is room for other improvements.&lt;/p&gt;

&lt;p&gt;The Self implementation has some primitives that can be changed to show debugging information from the JIT. All primitives can be listed with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;primitives primitiveList do: [ | :e | e printLine ].
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looking through this shows some interesting ones prefixed with &lt;code&gt;_Print&lt;/code&gt; that can be set to output debug data. One is &lt;code&gt;_PrintCompiledCode&lt;/code&gt;. Setting this to true allows viewing the generated assembler code on the Self console.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 16&quot; _PrintCompiledCode: true.
false
&quot;Self 17&quot; 40 + 2.
...
  // loadOop2
movl $0xa0 (40), #-16(%ebp)
  // loadOop2
movl $0x8 (2), #-20(%ebp)
  // loadArg
movl #-20(%ebp), %ebx
movl %ebx, #4(%esp)
  // selfCall
movl #-16(%ebp), %ebx
movl %ebx, (%esp)
nop
nop
nop
call 0x8186597 &amp;lt;SendMessage_stub&amp;gt; (bp)
  // begin SendDesc
jmp L7f
  .data 3
jmp L9f
  .data 0
  .data 0
  .data 0x4578341 (&#39;+&#39;)
  .data 4
L7: 
L8: 
  // end SendDesc
movl %eax, #-16(%ebp)
  // epilogue
movl #-16(%ebp), %eax
  // restore_frame_and_return
leave
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Others, like &lt;code&gt;_PrintInlining&lt;/code&gt; display debug information related to inlining code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 18&quot; _PrintInlining: true
fales
&quot;Self 19&quot; test.
*inlining size, cost 0/size 0 (0x8b7e864)
*PIC-type-predicting - (1 maps)
*type-casing -
*inlining - (smallInt.self:153), cost 1/size 0 (0x8b7ee38)*
 *inlining asSmallInteger (number.self:108), cost 1/size 0 (0x8b7fa94)*
  *inlining raiseError, cost 0/size 0 (0x8b80530)*
  *inlining asSmallIntegerIfFail: (smallInt.self:302), cost 0/size 0 (0x8b808fc)*
 *inlining TSubCC:
 *cannot inline value:With:, cost = 10 (rejected)
 *marking value:With: send ReceiverStatic
 *sending value:With:
*sending -
*inlining size:, cost 0/size 0 (0x8b8434c)
*inlining rep, cost 0/size 0 (0x8b846a8)
*PIC-type-predicting removeFirstLink (1 maps)
*type-casing removeFirstLink
*inlining removeFirstLink (list.self:300), cost 2/size 0 (0x8b84b48)*
 *inlining next, cost 0/size 0 (0x8b85628)
 *PIC-type-predicting remove (1 maps)
 *type-casing remove
 *cannot inline remove, cost = 9 (rejected)
 *sending remove
*sending removeFirstLink
*PIC-type-predicting value (1 maps)
*type-casing value
*inlining value, cost 0/size 0 (0x8b86570)*
*sending value
*inlining asSmallInteger (number.self:108), cost 1/size 0 (0x8b7e5b0)*
 *inlining raiseError, cost 0/size 0 (0x8b7f074)*
 *inlining asSmallIntegerIfFail: (smallInt.self:302), cost 0/size 0 (0x8b7f440)*
*inlining TSubCC:
*cannot inline value:With:, cost = 10 (rejected)
*marking value:With: send ReceiverStatic
*sending value:With:
nil
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more involved benchmarks there is some code shipped with the Self source. It can be loaded with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 28&quot; bootstrap read: &#39;allTests&#39; From: &#39;tests&#39;.
reading ./tests/allTests.self...
reading ./tests/tests.self...
reading ./tests/programmingTests.self...
reading ./tests/debugTests.self...
reading ./tests/lowLevelTests.self...
reading ./tests/numberTests.self...
reading ./tests/deltablue.self...
reading ./tests/sicTests.self...
reading ./tests/branchTests.self...
reading ./tests/nicTests.self...
reading ./tests/testSuite.self...
reading ./tests/languageTests.self...
reading ./tests/cons.self...
reading ./tests/benchmarks.self...
reading ./tests/richards.self...
reading ./tests/parser.self...
reading ./tests/parseNodes.self...
modules allTests
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are methods on the &lt;code&gt;bootstrap&lt;/code&gt; object for running the tests and printing results. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 32&quot; benchmarks measurePerformance

                 compile    mean       C    mean/C       %
recur:                 5       0
sumTo:                 2       7
sumFromTo:             2       7
fastSumTo:             2       6
nestedLoop:            2      10
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is also &lt;code&gt;measurePerformance2&lt;/code&gt; and &lt;code&gt;measurePerformance3&lt;/code&gt; methods. The code comments for the &lt;code&gt;measure2&lt;/code&gt; and &lt;code&gt;measure3&lt;/code&gt; methods explain the differences.&lt;/p&gt;

&lt;p&gt;Self 2 was well known for generating very fast code that compared favourably with C. This implementation of this was described in &lt;a href=&quot;https://www.cs.ucsb.edu/~urs/oocsb/self/papers/craig-thesis.html&quot;&gt;Craig Chamber&#39;s thesis&lt;/a&gt;. Compilation was slow however so in Self 3 and 4 two new compilers were created. These were &#39;nic&#39; and &#39;sic&#39;. I believe this is covered in &lt;a href=&quot;http://www.cs.ucsb.edu/~urs/oocsb/self/papers/urs-thesis.html&quot;&gt;Urs Holzle&#39;s thesis&lt;/a&gt; The &#39;nic&#39; compiler is the &#39;Non Inlining Compiler&#39; and is simpler to implement. It&#39;s the compiler you write to get Self bootstrapped and running on new platforms fairly quickly. There is no inlining and no type feedback so performance is slower as shown by the benchmarking when changing the compiler used, as described below. The &#39;sic&#39;, or &#39;Single Inlining Compiler&#39;, generates better code through more optimisations. While neither is as fast as the Self 2 compiler it is faster to compile code and makes for a better interactive system. You can read more about this in the &lt;a href=&quot;http://merlintec.com/swiki/Self/2.html&quot;&gt;Merlintec Self FAQ&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is a &lt;code&gt;defaultCompiler&lt;/code&gt; slot in the &lt;code&gt;benchmark&lt;/code&gt; object that can be set to &lt;code&gt;nic&lt;/code&gt; or &lt;code&gt;sic&lt;/code&gt; to compare the different JIT compilers that Self implements. Comparing the &#39;nic&#39; compiler vs the &#39;sic&#39; compiler shows a speedup of about 6x in the &#39;richards&#39; benchmark when using &#39;sic&#39;.&lt;/p&gt;

&lt;p&gt;There&#39;s probably a fair bit of low hanging fruit to improve run times. I don&#39;t think the x86 backend has had as much work on it as the Sparc or PPC backends. The downside is much of the compiler code is written in C++ so for people interested in &#39;Self the language&#39; it&#39;s not as fun to hack on. &lt;a href=&quot;http://kleinvm.sourceforge.net/&quot;&gt;Klein&lt;/a&gt; was an attempt to write a Self VM in Self and includes a compiler and assembler which might make a more interesting project for those that want to use Self itself to implement compiler code.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Changing Attributes in Self objects</title>
   <link href="http://bluishcoder.co.nz/2014/12/17/changing-attributes-in-self-objects.html"/>
   <updated>2014-12-17T17:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2014/12/17/changing-attributes-in-self-objects</id>
   <content type="html">&lt;p&gt;A question came up in the Self irc channel today about how to change the &#39;comment&#39; string for an object. Comments are stored as attributes on an object. These are accessed using &lt;a href=&quot;http://dl.acm.org/citation.cfm?id=1029004&quot;&gt;mirrors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A mirror for an object can be obtained using the &lt;code&gt;asMirror&lt;/code&gt; method. The trait &lt;code&gt;traits mirrors abstractMirror&lt;/code&gt; provides a number of methods that can be used to change the object via the mirror. This involves everything that is normally done through the programming environment. It includes adding, removing and changing slots, categories, attributes amongst other things. By drilling down in this trait you can see a &lt;code&gt;programming&lt;/code&gt; category, which expands to include a &lt;code&gt;changing annotation&lt;/code&gt; category and that holds a &lt;code&gt;comments&lt;/code&gt; category. Within that you can find the methods for setting comments.&lt;/p&gt;

&lt;p&gt;The video below shows the steps to find this from within the programming environment and demonstrates changing the comment on the initial &lt;code&gt;shell&lt;/code&gt; object on the desktop. The code to change that comment is just:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;self asMirror comment: &#39;hello world&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;video controls=&quot;controls&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_comment.webm&#39; type=&#39;video/webm&#39; /&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/self_comment.mp4&#39; type=&#39;video/mp4&#39; /&gt;
  The video is available as &lt;a href=&quot;http://bluishcoder.co.nz/self/self_comment.webm&quot;&gt;self_comment.webm&lt;/a&gt; or &lt;a href=&quot;http://bluishcoder.co.nz/self/self_comment.mp4&quot;&gt;self_comment.mp4&lt;/a&gt;.
&lt;/video&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Multiple Users in a Self World</title>
   <link href="http://bluishcoder.co.nz/2013/09/16/multiple-users-in-a-self-world.html"/>
   <updated>2013-09-16T23:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2013/09/16/multiple-users-in-a-self-world</id>
   <content type="html">&lt;p&gt;One of the things I like about the &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self programming language&lt;/a&gt; is it has lots of interesting old projects written in Self included in the source distribution. These include a &lt;a href=&quot;http://opendylan.org/&quot;&gt;Dylan&lt;/a&gt; interpreter, &lt;a href=&quot;http://www.cs.washington.edu/research/projects/cecil/&quot;&gt;Cecil&lt;/a&gt; interpreter, web browser, and partial Java implementation amongst other things.&lt;/p&gt;

&lt;p&gt;Once of the features Self has is the ability to allow remote users to connect to a Self instance running the Morphic user interface. This is usually done using X11&#39;s facility to share remote windows. But included in the Self source is a client written in Java and a server written in Self that allows remote users to connect using a Java Applet in a web browser.&lt;/p&gt;

&lt;p&gt;The code for this has bitrot slightly but I&#39;ve updated it just enough to work and demo. My fixes have been merged into the &lt;a href=&quot;https://github.com/russellallen/self/&quot;&gt;official Self repository&lt;/a&gt; on github.&lt;/p&gt;

&lt;h2&gt;Server side&lt;/h2&gt;

&lt;p&gt;With these changes and a build of Self you should start with a fresh image containing the Morphic code (UI2). From the self console run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd objects
$ ../Self 
&quot;Self 1&quot; &#39;applications/javaServer/loadJavaServer.self&#39; _RunScript
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will load the server code. It&#39;s best to do this in a fresh instance as it modifies some UI objects that result in Self having to rebuild a number of dependencies for the objects in the system. This can make an existing system sluggish for a bit.&lt;/p&gt;

&lt;p&gt;Once loaded, start the desktop:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 2&quot; desktop open
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From a shell in the desktop, create a copy of the &lt;code&gt;javaDaemon&lt;/code&gt; object using &#39;Get it&#39; and put it somewhere:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;javaDaemon copy
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The outliner for this allows changing the &lt;code&gt;port&lt;/code&gt; slot if needed, which defaults to &lt;code&gt;1420&lt;/code&gt;. Middle click on the outliner&#39;s title and choose &#39;Show Morph&#39;. This creates a default tan square that can be placed anywhere. The world that this morph lives in is the world that remote users will connect to.&lt;/p&gt;

&lt;p&gt;From the outliner run the &lt;code&gt;start&lt;/code&gt; method. There will be a prompt warning you that this is a security issue as you&#39;re allowing remote access to your machine. Answering &lt;code&gt;Yes&lt;/code&gt; will run the server.&lt;/p&gt;

&lt;h2&gt;Client side&lt;/h2&gt;

&lt;p&gt;The client code is written in Java. The source is in &lt;code&gt;objects/applications/javaClient&lt;/code&gt;. The shell script &lt;code&gt;BuildJavaClient&lt;/code&gt; will build the Java code. The &lt;code&gt;Test.html&lt;/code&gt; page in that directory should be served from a web server and accessed by a web browser. Note that due to Java security settings this must be served from an actual web server. I used a local &lt;a href=&quot;http://acme.com/software/thttpd/&quot;&gt;thttpd&lt;/a&gt; instance. There may also need to be some changing of Java Applet settings in the Java control panel to allow the applet to make socket connections.&lt;/p&gt;

&lt;p&gt;Once &#39;Test.html&#39; is loaded you can enter a username, keep the password empty, and connect. You should now see the Self desktop.&lt;/p&gt;

&lt;h1&gt;Usage&lt;/h1&gt;

&lt;p&gt;Unfortunately when the client connects sometimes the Self desktop stops and it won&#39;t respond. To restart it enter in the Self console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;desktop go
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When this is done you&#39;ll see the remote users &#39;hand&#39; (Self term for mouse pointer) in the Self desktop as an arrow with their name. A border will be in the world with the color of the user showing the extent of the desktop that the user can see in their remote session.&lt;/p&gt;

&lt;p&gt;Dragging items around in both sessions is reflected in the world. Other users can connect too. The Java client uses &#39;Shift+Left Click&#39; as the right mouse button and &#39;Control+Left Click&#39; as the middle mouse button.&lt;/p&gt;

&lt;h2&gt;Demo&lt;/h2&gt;

&lt;p&gt;I did a short screencast of using the remote sharing to show how it works. If your browser supports HTML video it should show below.&lt;/p&gt;

&lt;p&gt;&lt;video controls=&quot;controls&quot; width=&quot;624&quot; height=&quot;480&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/kansas/kansas.ogv&#39; type=&#39;video/ogg&#39;/&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/self/kansas/kansas2.mp4&#39; type=&#39;video/mp4&#39;/&gt;
  If your browser doesn&#39;t support HTML video, you can view directly
  &lt;a href=&quot;http://bluishcoder.co.nz/self/kansas/kansas.ogv&quot;&gt;kansas.ogv&lt;/a&gt; or
  &lt;a href=&quot;http://bluishcoder.co.nz/self/kansas/kansas2.mp4&quot;&gt;kansas2.mp4&lt;/a&gt;.
&lt;/video&gt;&lt;/p&gt;

&lt;h2&gt;Future&lt;/h2&gt;

&lt;p&gt;I think a dedicated protocol like this is probably better than using remote X11. Self currently is quite tied to X but if it could be moved off it it would allow easier porting to Windows, Android and other non-X platforms. Perhaps using something like &lt;a href=&quot;http://www.libsdl.org/&quot;&gt;SDL2&lt;/a&gt; to get cross platform and hardware acceleration.&lt;/p&gt;

&lt;p&gt;More information about the protocol and implementation of the Java client is available in &lt;a href=&quot;https://github.com/russellallen/self/blob/master/objects/applications/javaClient/JavaClientServerOverview.txt&quot;&gt;JavaClientServerOverview.txt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An interesting project would to be port the &lt;code&gt;javaClient&lt;/code&gt; code to JavaScript using an HTML canvas or WebGL to display the remote work. The server could be updated to communicate via HTTP, WebSockets, or even WebRTC&#39;s peer to peer facilities.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Running the Self Programming Language on Android</title>
   <link href="http://bluishcoder.co.nz/2013/09/11/running-self-programming-language-on-android.html"/>
   <updated>2013-09-11T23:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2013/09/11/running-self-programming-language-on-android</id>
   <content type="html">&lt;p&gt;I&#39;ve &lt;a href=&quot;http://bluishcoder.co.nz/tags/self/&quot;&gt;written about&lt;/a&gt; the &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self&lt;/a&gt;
programming language before and still occasionally fire up the GUI
environment to explore prototype object development. The Self implementation is JIT based and doesn&#39;t have an interpreter. I&#39;ve wanted to have the Self environment running on a mobile device but most tablets and phones are ARM based and Self doesn&#39;t have an ARM backend.&lt;/p&gt;

&lt;p&gt;Intel based mobile devices are starting to appear however so I decided to try and get Self running on one. The GUI backend for Self on Linux uses X11 and it&#39;s a bit too large a project to do an Android GUI backend so I just tried getting the console REPL of Self working.&lt;/p&gt;

&lt;h2&gt;Building on Linux&lt;/h2&gt;

&lt;p&gt;My first step was to revive my port of the Self build system that uses &lt;a href=&quot;http://gittup.org/tup/&quot;&gt;Tup&lt;/a&gt; instead of the original custom shell scripts. By doing this I&#39;d gain a better understanding of how Self is built and then move to using Android tools. My rebased version of Self that uses &lt;code&gt;tup&lt;/code&gt; to build is in the &lt;code&gt;tup&lt;/code&gt; branch of &lt;a href=&quot;https://github.com/doublec/self/tree/tup&quot;&gt;my Self github fork&lt;/a&gt;. With &lt;code&gt;tup&lt;/code&gt; installed this can be built with (probably works on Linux only, sorry):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/doublec/self
$ cd self
$ git checkout -b tup origin/tup
$ tup init
$ tup upd
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates a &lt;code&gt;Self&lt;/code&gt; executable in the root of the Self checkout. A bootstrapped snapshot can be created with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd objects
$ ../Self 
Self Virtual Machine Version 4.1.13
Copyright 1989-2003: The Self Group (type _Credits for credits)

for I386:  LogVMMessages = true
for I386:  PrintScriptName  = true
for I386:  Inline = true
for I386:  SICDeferUncommonBranches = false (not implemented)
for I386:  SICReplaceOnStack = false (not implemented)
for I386:  SaveOutgoingArgumentsOfPatchedFrames = true
VM# &#39;worldBuilder.self&#39; _RunScript
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;&#39;worldBuilder.self&#39; _RunScript&lt;/code&gt; command loads a script that loads the code for the standard Self runtime. It prompts if you want &lt;code&gt;UI2&lt;/code&gt;, which you do if you want the GUI, and &lt;code&gt;UI1&lt;/code&gt;, which you don&#39;t unless you want to explore the old gui.&lt;/p&gt;

&lt;p&gt;Once loaded you can run some basic commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 0&quot; &#39;hello world&#39; print
hello world&#39;hello world&#39;
&quot;Self 1&quot; 100 factorial.
a bigInt(9332621544394415...00000000000000000000)
&quot;Self 2&quot; [ 1000 factorial ] time.
1552
&quot;Self 3&quot; desktop open.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The last comand, &lt;code&gt;desktop open&lt;/code&gt;, runs the Self GUI. You can see a screencast of me using the GUI in my &lt;a href=&quot;http://bluishcoder.co.nz/2009/07/27/displaying-images-with-self.html&quot;&gt;displaying images with Self post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Android Standalone Toolchain&lt;/h2&gt;

&lt;p&gt;To build Self for Android we need to generate a &lt;a href=&quot;http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html&quot;&gt;standalone toolchain from the Android NDK&lt;/a&gt;. To generate an x86 toolchain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd $NDK_HOME
$ ./build/tools/make-standalone-toolchain.sh  \
     --arch=x86 \
     --install-dir=/somewhere/stk
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This generates the toolchain and copies it to the &lt;code&gt;/somewhere/stk&lt;/code&gt; directory. That should be changed to where you want it installed and it needs to be added to the path:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export PATH=/somewhere/stk/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Android ncurses&lt;/h2&gt;

&lt;p&gt;Self uses &lt;code&gt;termlib&lt;/code&gt; from the &lt;a href=&quot;http://ftp.gnu.org/pub/gnu/ncurses/&quot;&gt;ncurses&lt;/a&gt; library. This can be built from source and installed using the standalone toolchain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
$ tar xvf ncurses-5.9.tar.gz
$ cd ../ncurses-5.9
$ ./configure --host=i686-android-linux \
              --prefix=/somewhere/stk \
              --with-termlib
$ make
$ make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;prefix&lt;/code&gt; in the configure line above will install it in the location of the standalone toolchain so it can be found easily when building Self.&lt;/p&gt;

&lt;h2&gt;Android Self&lt;/h2&gt;

&lt;p&gt;I&#39;ve generated a &lt;a href=&quot;http://bluishcoder.co.nz/self/android.patch&quot;&gt;patch file&lt;/a&gt; that allows building Self in Android. It&#39;s pretty rough but gets the console REPL working and can then be fixed up later. To apply and build:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/doublec/self
$ cd self
$ git checkout -b tup origin/tup
$ curl http://bluishcoder.co.nz/self/android.patch | patch -p1
$ tup init
$ tup upd
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The resulting executable should be pushed to a directory on the Android device. Pushing the &lt;code&gt;objects&lt;/code&gt; directory will provide the files to allow bootstrapping the Self runtime system:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ adb push Self /data/local/tmp
$ adb push objects /data/local/tmp
$ adb shell
device$ cd /data/local/tmp
device$ chmod 0755 Self
device$ ./Self
VM# &#39;worldBuilder.self&#39; _RunScript
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also run &lt;code&gt;Self&lt;/code&gt; from within a &lt;a href=&quot;https://f-droid.org/repository/browse/?fdid=jackpal.androidterm&quot;&gt;terminal emulator&lt;/a&gt;. If you do this I recommend installing the &lt;a href=&quot;https://f-droid.org/repository/browse/?fdid=org.pocketworkstation.pckeyboard&quot;&gt;Hacker&#39;s Keyboard&lt;/a&gt; as well.&lt;/p&gt;

&lt;p&gt;Running the benchmark suite on my laptop gives about 2 seconds elapsed time. On the Asus Fonepad it&#39;s 10 seconds, so approximately 5x slower. The benchmark can be run with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;Self 0&quot; [ benchmarks run ] time
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Binaries&lt;/h2&gt;

&lt;p&gt;A precompiled binary is available at &lt;a href=&quot;http://bluishcoder.co.nz/self/i686-android-linux-Self.bz2&quot;&gt;i686-android-linux-Self.bz2&lt;/a&gt;. Uncompress it before copying it to the device. You&#39;ll need the &lt;code&gt;objects&lt;/code&gt; directory from the git source to boostrap the runtime environment:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ wget http://bluishcoder.co.nz/self/i686-android-linux-Self.bz2
$ bunzip2 i686-android-linux-Self.bz2
$ mv i686-android-linux-Self.bz2 Self
$ adb push Self /data/local/tmp
$ git clone https://github.com/doublec/self
$ cd self
$ git checkout -b tup origin/tup
$ adb push objects /data/local/tmp
...as previously to run...
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;This provides basic console Self usage. It requires knowing how to program in Self without using the GUI, which is not the most common way of writing Self programs. An interesting next exercise would be trying to get a GUI working.&lt;/p&gt;

&lt;p&gt;The images below show Self running on two x86 Android devices. On the left is a screenshot of running on a Samsung Galaxy Tab 3 10.1 and on the right running on an Asus Fonepad with bluetooth keyboard. Click on the images for the larger version.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://bluishcoder.co.nz/images/android_self.png&quot;&gt;&lt;img src=&quot;http://bluishcoder.co.nz/images/android_self_small.png&quot; /&gt;&lt;/a&gt;
  &lt;a href=&quot;http://bluishcoder.co.nz/images/android_self2.jpg&quot;&gt;&lt;img src=&quot;http://bluishcoder.co.nz/images/android_self2_small.jpg&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately this build doesn&#39;t work on ARM as mentioned previously. I suspect an ARM backend would be a significant amount of work.&lt;/p&gt;

&lt;p&gt;For more reading on Self:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.selflanguage.org/4.5/&quot;&gt;Self Handbook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://selflanguage.org/documentation/published/index.html&quot;&gt;Self Papers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/07/16/prototype-based-programming-languages.html&quot;&gt;Prototype Based Programming Languages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://crpit.com/abstracts/CRPITV13Noble.html&quot;&gt;Attack of the clones&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://groups.yahoo.com/group/self-interest/&quot;&gt;self-interest mailing list&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Self IRC channel is #self-lang on Freenode&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Displaying images with Self</title>
   <link href="http://bluishcoder.co.nz/2009/07/27/displaying-images-with-self.html"/>
   <updated>2009-07-27T23:32:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2009/07/27/displaying-images-with-self</id>
   <content type="html">&lt;p&gt;The &lt;a href=&quot;http://www.selflanguage.org&quot;&gt;Self&lt;/a&gt; source code has some example programs, one of which is a web browser. This was developed quite a long time ago and is unable to nicely display most pages on the modern web but it does a reasonable job of basic pages. On the Linux port there were some problems with displaying images however. I tracked this down to two issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It only natively decoded image formats that aren&#39;t really used on the web (Sun Raster format for example). The GIF and JPG handling code spawned external programs, which I didn&#39;t have on my Linux install, to convert to a format it could handle before displaying.&lt;/li&gt;
&lt;li&gt;There was a bug in the color management code on X11 for displays greater than 8 bit.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The first was a quick fix by changing the programs that it used to do the conversion to one readily available on Linux. I changed it to use the &lt;a href=&quot;http://www.imagemagick.org/&quot;&gt;Image Magick&lt;/a&gt; &#39;&lt;a href=&quot;http://www.imagemagick.org/script/convert.php&quot;&gt;convert&lt;/a&gt;&#39; program. This enabled displaying code in an imageMorph by setting the image for the morph:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;setImage: ui2Image copyFromGIFFile: &#39;test.gif&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I tracked the second issue down to code that was sending a message to an object that didn&#39;t understand that message (Static typing fans can say &#39;I told you so&#39; here). Self has a &#39;paintManager&#39; which handles color mapping for displays that can&#39;t display a large number of colors. On 8 bit displays this is a &#39;paintManager&#39;. On displays with more colors it&#39;s a &#39;unmappedPaintManager&#39;. The latter doesn&#39;t have some of the slots of the former and there was code trying to do this. I changed this to check if the paintManager instance can do color mapping before sending the messages.&lt;/p&gt;

&lt;p&gt;With that change the web browser could display images. An example page that works with the browser (one of the few...), and code to display it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&#39;http://www.bluishcoder.co.nz/self/imagetest.html&#39; asURL getPageForUser
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The patches with these fixes are available from the &lt;a href=&quot;http://github.com/doublec/self/tree/webbrowser&quot;&gt;webbrowser branch&lt;/a&gt; of my fork of the Self git repostory on github. The fixes were only a few lines of code.&lt;/p&gt;

&lt;p&gt;I did a &lt;a href=&quot;http://bluishcoder.co.nz/self/selfimage.ogg&quot;&gt;short screencast&lt;/a&gt; showing loading image morph&#39;s and displaying the web page in the browser. And before you say, &quot;wow, that GUI looks so 90&#39;s&quot;, don&#39;t forget - it was.&lt;/p&gt;

&lt;p&gt;&lt;video src=&#39;http://bluishcoder.co.nz/self/selfimage.ogg&#39; controls=&#39;controls&#39;&gt;&lt;a href=&quot;http://bluishcoder.co.nz/self/selfimage.ogg&quot;&gt;Screencast&lt;/a&gt;&lt;/video&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Building and using Self on Linux</title>
   <link href="http://bluishcoder.co.nz/2009/07/27/building-and-using-self-on-linux.html"/>
   <updated>2009-07-27T00:02:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2009/07/27/building-and-using-self-on-linux</id>
   <content type="html">&lt;p&gt;The original implementation of the &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self programming language&lt;/a&gt; is easy to build from source. Russell Allen maintains a &lt;a href=&quot;http://github.com/russellallen/self/tree/master&quot;&gt;git repository on github&lt;/a&gt; containing the source.&lt;/p&gt;

&lt;p&gt;I use &lt;a href=&quot;http://www.archlinux.org/&quot;&gt;Arch Linux&lt;/a&gt; with &#39;flex&#39; and &#39;tcsh&#39; packages installed for various parts of the build process. To build a Self binary:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/russellallen/self.git
$ cd self/release
 $ sh buildLinuxVM
$ cd ..
$ ./Self
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The build takes about 30 minutes on my laptop. There will be many compilation warnings but these can be safely ignored. The result of the build is a &#39;Self&#39; executable in the root directory of the cloned source. The executable can be run but most of the Self library is not loaded. You need to create an image to run or use an existing one. About the only thing you can do with the Self VM without an image is use primitives.&lt;/p&gt;

&lt;p&gt;The &#39;objects&#39; directory contains the source for the core Self library, the User Inteface, as well as other applications and interesting stuff. These can be used to build an image to run Self. The file &#39;all2.self&#39; can be used to create an image including the graphical user interface. &#39;small.self&#39; can be used to build an image without the user interface. The primitive &#39;_RunScript&#39; is used to load these files. Once loaded you can save a &#39;snapshot&#39; (this is the term Self uses for images) which can be resumed in later sessions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd objects
$ ../Self
Self Virtual Machine Version 4.1.13, Sun 26 Jul 09 16:59:28 Linux
Copyright 1989-2003: The Self Group (type _Credits for credits)

for I386:  LogVMMessages = true
for I386:  PrintScriptName  = true
for I386:  Inline = true
for I386:  SICDeferUncommonBranches = false (not implemented)
for I386:  SICReplaceOnStack = false (not implemented)
for I386:  SaveOutgoingArgumentsOfPatchedFrames = true
VM # &#39;all2.self&#39; _RunScript
reading all2.self...
reading ./core/init.self...
reading ./core/allCore.self...
reading ./core/systemStructure.self...
reading ./core/defaultBehavior.self...
...
reading ./ui2/outliner/profileSliceGrpMod.self...
reading ./ui2/outliner/profileSelfSlotMdl.self...
reading ./ui2/outliner/powerOperations.self...
verifying newgen: eden from to oldgen: old0 old1 z p r S v O m N M i  done
Starting: Collecting Garbage...
Finished: Collecting Garbage
Starting: Refilling module cache...
Finished: Refilling module cache
&quot;Self 0&quot; saveAs: &#39;ui.snap&#39;
Starting: Writing snapshot to ui.snap...
Finished: Writing snapshot to ui.snap
shell
&quot;Self 1&quot; quit
Save to ui.snap before quitting? 
  y =&amp;gt; save, then quit
  n =&amp;gt; quit without saving
  RET =&amp;gt; cancel
Response: n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the call to &#39;saveAs:&#39; to save the image snapshot. &#39;quit&#39; is used to exit the running Self session. This image can now be resumed using the &#39;-s&#39; argument to &#39;Self&#39; whenever you want to run Self:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd objects
$ ../Self -s ui.snap
for I386:  LogVMMessages = true
for I386:  PrintScriptName  = true
for I386:  Inline = true
for I386:  SICDeferUncommonBranches = false (not implemented)
for I386:  SICReplaceOnStack = false (not implemented)
for I386:  SaveOutgoingArgumentsOfPatchedFrames = true

Welcome to the Self system!  (Version 4.4)


Copyright 1992-2009 AUTHORS, Sun Microsystems, Inc. and Stanford University.
See the LICENSE file for license information.

Type _Credits for full credits.

VM version: 4.1.13

&quot;Self 1&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At this point you can now use any Self expressions. All code is run in the contents of an object called &#39;shell&#39;. You can start the graphical user interface by passing the &#39;open&#39; message to the &#39;desktop&#39; object:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;desktop open
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The desktop will appear with a &#39;trash can&#39; object and an outliner for the &#39;shell&#39; object. Left clicking on the middle button labelled &#39;E&#39; in the group of three buttons on the right of the shell will open an expression evaluator allowing you to enter Self expressions. Middle click on objects to get an object specific menu. On the desktop this provides a menu item to quit when you want to leave the session.&lt;/p&gt;

&lt;p&gt;There are some interesting applications in the &#39;objects&#39; directory. For example, there is a Smalltalk implementation written in Self. This can be loaded at the shell with the command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bootstrap read: &#39;smalltalk&#39; From: &#39;applications/smalltalk&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When it completes loading you can find a &#39;smalltalk&#39; item on the desktop&#39;s right click menu. Choosing that provides a menu to load a standard Smalltalk browser, workspace and inspector, allowing you to write Smalltalk code. There&#39;s a document that &lt;a href=&quot;http://selflanguage.org/_static/manuals/smalltalk.ps.gz&quot;&gt;describes the Smalltalk emulator&lt;/a&gt; for more information about it. There are other interesting applications in the objects directory including a web browser, Java emulator, a C preprocessor, &lt;a href=&quot;http://www.cs.washington.edu/research/projects/cecil/&quot;&gt;Cecil&lt;/a&gt; implementation and a parser/lexer generator. Most of this code is old and in various states of usability but it provides some interesting examples of Self code to look at and play with.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://selflanguage.org/_static/tutorial/Tutorial/index.html&quot;&gt;Self tutorial&lt;/a&gt; provides a good introduction to using the Self user interface.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Prototype Based Programming Languages</title>
   <link href="http://bluishcoder.co.nz/2009/07/16/prototype-based-programming-languages.html"/>
   <updated>2009-07-16T13:11:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2009/07/16/prototype-based-programming-languages</id>
   <content type="html">&lt;p&gt;I&#39;ve been reading up on &lt;a href=&quot;http://en.wikipedia.org/wiki/Prototype-based_programming&quot;&gt;protoype based&lt;/a&gt; programming languages recently. Mainly using the &lt;a href=&quot;http://iolanguage.com/&quot;&gt;Io Programming Language&lt;/a&gt; and &lt;a href=&quot;http://selflanguage.org/&quot;&gt;Self&lt;/a&gt; but also looking at &lt;a href=&quot;http://www.ccs.neu.edu/home/ivan/moo/lm_toc.html&quot;&gt;LambdaMOO&lt;/a&gt; and similar languages. A good overview of using the prototype based approach to building programs is &lt;a href=&quot;http://research.sun.com/self/papers/organizing-programs.html&quot;&gt;Organizing Programs without Classes&lt;/a&gt;. This post is based on examples from that paper and from &lt;a href=&quot;http://crpit.com/abstracts/CRPITV13Noble.html&quot;&gt;Attack of the Clones&lt;/a&gt; which covers design patterns using Self.&lt;/p&gt;

&lt;h2&gt;Self&lt;/h2&gt;

&lt;p&gt;In the Self programming language objects are created using a literal object syntax. This syntax defines code and slots within the &lt;code&gt;(|&lt;/code&gt; and &lt;code&gt;|)&lt;/code&gt; delimiters. An example object looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;point = (|
  parent* = traits point.
  x.
  y.
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates an object with 3 slots. The slots are &#39;parent&#39;, &#39;x&#39; and &#39;y&#39;. The &#39;parent&#39; slot is assigned an inital value of &#39;traits point&#39; which is the traits object for points (more on what this is later). The &#39;*&#39; that is suffixed to the &#39;parent&#39; slot means it is a prototype slot and used in the prototype slot lookup chain.&lt;/p&gt;

&lt;p&gt;This means when a message is sent to the &#39;point&#39; object the lookup starts with the &#39;point&#39; object. If a slot with the messages name is not found in that object then each prototype slot (those suffixed with &#39;*&#39;) are searched looking for a slot with that name.&lt;/p&gt;

&lt;p&gt;So in the &#39;point&#39; case, looking for &#39;x&#39; will find it immediately in the point object. Looking for &#39;print&#39; will not so it will look for it in the &#39;traits point&#39; object. If it&#39;s not there it will look in that objects prototype slots and so on until it is found or the search is exhausted.&lt;/p&gt;

&lt;p&gt;The idiom in Self (and other prototype based languages) is to create global objects like these and use &#39;clone&#39; to create copies of it. So creating two different points would look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;a = point clone.
b = point clone.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that &#39;point&#39; has no method slots. Only the &#39;x&#39; and &#39;y&#39; which contain data. The methods are defined in the &#39;traits point&#39; object. The definition of that could look something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;traits point = (|
  parent* = traits clonable.
  print = x println y println.
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This provides a method to print the &#39;x&#39; and &#39;y&#39; values of the object and another parent that provides other basic functionality like the ability to clone. The &#39;traits point&#39; object doesn&#39;t define any data slots. It defines only methods. However it uses &#39;x&#39; and &#39;y&#39; messages that aren&#39;t defined. It expects to be used in a prototype slot of another object that defines the &#39;x&#39; and &#39;y&#39; slots (like our &#39;point&#39; example earlier.&lt;/p&gt;

&lt;p&gt;Separating the code out into data objects and trait objects allows the trait object to be reused in other objects. For example, an object that computes the &#39;x&#39; and &#39;y&#39; values rather than storing them can re-use the traits object:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;computed_point = (|
  parent* = traits point.
  x = ..compute x..
  y = ..compute y..
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This &#39;computed_point&#39; can be used anywhere a point is expected. The traits object sends the &#39;x&#39; and &#39;y&#39; messages when it needs their value and it doesn&#39;t matter if there stored as data as in the &#39;point&#39; object, or as methods that calculate the value as in the &#39;computed_point&#39; object. Each &#39;point&#39; and &#39;computed_point&#39; object shares a single trait object instance. This avoids the need to have multiple copies of the methods in each point object instance.&lt;/p&gt;

&lt;p&gt;The prototype slots of an object effectively form an inheritance relationship. The way to subclass objects in Self is to create a new object and set a prototype slot to an instance of the parent object. Usually it&#39;s the trait objects that map the subclass relationship since it is those objects that contain the object behaviour (ie. the methods and no data). An example follows in Self of how a &#39;polygon&#39; and &#39;filled_polygon&#39; object can be modelled (this is from the &#39;Organizing Programs without Classes&#39; paper):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;polygon_traits = (|
  draw = ...draw using &#39;vertices&#39; slot to get points...
|)

polygon = (|
  parent* = polygon_traits.
  vertices.
|)

filled_polygon_traits = (|
  parent* = polygon_traits.
  draw = ...draw using &#39;vertices&#39; and &#39;fillPattern&#39;...
|)

filled_polygon = (|
  parent* = filled_polygon_traits.
  vertices.
  fillPattern;
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Cloning the &#39;polygon&#39; object and sending the &#39;draw&#39; message will draw the polygon using the list of points held in the &#39;vertices&#39; slot. Cloning the &#39;filled_polygon&#39; object and sending the &#39;draw&#39; message will draw using the specialized &#39;draw&#39; method that also uses the &#39;fillPattern&#39; slot to fill the polygon when drawing. This can re-use the &#39;draw&#39; method in the &#39;polygon_traits&#39; object if needed.&lt;/p&gt;

&lt;p&gt;The new &#39;filled_polygon&#39; object did require defining a new &#39;vertices&#39; slot. Self allows multiple prototype slots, each of which is involved in the lookup for slot names. We can share the &#39;vertices&#39; from the &#39;polygon&#39; object by making that an additional prototype slot in &#39;filled_polygon&#39;. This is often termed a &#39;data parent&#39;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;filled_polygon = (|
  parent* = filled_polygon_traits.
  dataParent* = polygon clone.
  fillPattern;
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the &#39;dataParent&#39; slot is a prototype slot (suffixed with a &#39;*&#39;). This means it participates in the slot lookup process. The data parent approach has an advantage over the previous example in that if we change the representation of the &#39;polygon&#39; object then all new &#39;filled_polygon&#39; instances will get this new representation. We don&#39;t need to edit the &#39;filled_polygon&#39; definition for the modified or additional slots.&lt;/p&gt;

&lt;p&gt;In the &#39;filled_polygon&#39; example we re-used the &#39;vertices&#39; slot from &#39;polygon&#39;. We can also define subclasses that implement &#39;vertices&#39; differently than &#39;polygon&#39;. For example, a rectangle that stores the four corners and computes the vertices from this. Due to the seperation of state and behaviour this can be modelled easily:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rectangle_traits = (|
  parent* = polygon_traits.
  draw = ...draw rectangle using left, right, top, bottom...
  vertices = ...compute vertices list using left, right, etc...
|)

rectangle = (|
  parent* = rectangle_traits.
  left.
  right.
  top.
  bottom.
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Inheritance in Self can be dynamic if the prototype slots are made assignable. This means, at runtime, we can change the value of the slot used during message lookup, resulting in different behaviour. This can be used for objects that can be in different states.&lt;/p&gt;

&lt;p&gt;An example is a &#39;file&#39; object. It can be opened or closed. Some methods in &#39;file&#39; can only be used when the file is open, and some only when it is closed. This could be managed by conditional checks in each method. Or the parent of the object could be changed to a different traits object depending on the state - this avoids the need for each method to check if the file is in the open or closed state:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;open_file_traits = (|
  read = ...
  close = setParent: close_file_traits.
|)

closed_file_traits = (|
  open = setParent: open_file_traits.
|)

file = (|
  parent* = closed_file_traits.
|)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Methods like &#39;open&#39; are only available on closed files. &#39;read&#39; can only be called on opened files. This is basically the &lt;a href=&quot;http://en.wikipedia.org/wiki/Strategy_pattern&quot;&gt;Strategy pattern&lt;/a&gt; made easy using Self&#39;s dynamic inheritance.&lt;/p&gt;

&lt;h2&gt;Io&lt;/h2&gt;

&lt;p&gt;Whereas Self defines the prototype lookup chain to be that of the prototype slots in an object, Io instead has a slot called &#39;protos&#39; which is a list of all objects in the prototype chain. Instead of creating slots with a name suffixed with &#39;*&#39; you append to the existing &#39;protos&#39; list.&lt;/p&gt;

&lt;p&gt;The &#39;protos&#39; list is initially populated when you clone an object with the object that you cloned. This is unlike Self where copying an object does a shallow copy of all the slots of that object. In Io you get a &#39;differential inheritance&#39; model where your newly created object has no slots, just a &#39;protos&#39; field that contains the original object that was cloned. The Self &#39;point&#39; example I used earlier looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Point := Object clone do(
  x := 0
  y := 0
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Calling &#39;clone&#39; on this new &#39;Point&#39; object results in a new object that does not contain it&#39;s own &#39;x&#39; and &#39;y&#39; values. Instead its &#39;protos&#39; field points to the &#39;Point&#39; object which contains the values. When you set the &#39;x&#39; value on the clone it will then create its own &#39;x&#39; slot rather than changing the prototypes. In this way clones of big objects where relatively few slots are changed will save some memory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;a := Point clone
b := Point clone
a x = 5
a x println
 =&amp;gt; 5
b x println
 =&amp;gt; 0
a protos first == Point
 =&amp;gt; true
b protos first == Point
 =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This does have an interesting side effect in that if you clone a clone then you can end up with a longish prototype chain for the method lookup:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;a := Point clone
b := a clone
c := b clone
c protos first == b
 =&amp;gt; true
c protos first protos first == a
 =&amp;gt; true
c protos first protos first protos first == Point
 =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Inheritance is handled in much the same manner as Self but you need to manipulate the &#39;protos&#39; slot instead of having multiple prototype slots. The filled polygon example looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;PolygonTraits = Object clone do(
  draw := method(vertices foreach(v, v println))
)

Polygon := PolygonTraits clone do(
  vertices := list(1,2,3,4)
)

FilledPolygonTraits := PolygonTraits clone do(
  draw := method(resend; fillPattern println)
)

FilledPolygon := FilledPolygonTraits clone do(
  appendProto(Polygon clone)
  fillPattern := &quot;solid&quot;
)

Polygon clone draw
  =&amp;gt; 1
     2
     3
FilledPolygon clone draw
  =&amp;gt; 1
     2
     3
     &quot;solid&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&#39;appendProto&#39; appends an object to the prototype chain which is initially &#39;FilledPolygonTraits&#39; in this examples as that was the initial object we cloned. The dynamic inheritance example can also be done in Io:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;OpenFileTraits := Object clone do(
  read := method(n, &quot;Reading #{n} bytes&quot; interpolate println)
  close := method(
    self removeProto(OpenFileTraits)
    self appendProto(ClosedFileTraits)
  )
)

ClosedFileTraits := Object clone do(
  open := method(
    self removeProto(ClosedFileTraits)
    self appendProto(OpenFileTraits)
  )
)

File := Object clone do(
  init := method(self appendProto(ClosedFileTraits))
)

f := File clone
f read(255)
  =&amp;gt; Exception: File does not respond to &#39;read&#39;
f open
f read(255)
  =&amp;gt; reading 255 bytes
f open
  =&amp;gt; Exception: File does not respond to &#39;open&#39;
f close
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&#39;s a bit more work than in Self to manually manage the prototype chain but does work.&lt;/p&gt;

&lt;h2&gt;JavaScript&lt;/h2&gt;

&lt;p&gt;JavaScript is also a prototype based programming language. Unlike Self or Io it only allows one object to be used as the prototype in any given object. This is stored in a hidden &#39;&lt;strong&gt;proto&lt;/strong&gt;&#39; member and cannot be updated once set on construction (some implementations allow changing it however). Objects are created by using the &#39;new&#39; keyword on a constructor function that initializes the object. For now I&#39;ll leave it as an exercise for the reader to implement the examples above in JavaScript. I&#39;d be interested in the approaches people take.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
