Scratchpad

If you are new to Scratchpad, and want full access as a Scratchpad editor, create an account!
If you already have an account, log in and have fun!!

READ MORE

Scratchpad
Advertisement

Smalltalk by example: Creating an executable image


Introduction

The idea of having smalltalk run self-contained images and building your program from within the running image. Although higly flexible and probably the way of the future, it doesn't seem to interact with the other programs at the moment.

This example shows you how to dump a running image in GNU Smalltalk, so you can run the image like a program. Code

This is an example of the 'echo' program. It will print the given arguments to the stdout. Please note that this is a stripped down version of the real GNU echo and doesn't support the same arguments. This code was written by Paolo Bonzini in reply to a question on the GNU Smalltalk help mailinglist.


Object subclass: #EchoMain
      instanceVariableNames: ''
      classVariableNames: ''
      poolDictionaries: ''
      category: 'Language-Implementation'!

!EchoMain class methodsFor: 'foo'!

update: aspect
   aspect == #returnFromSnapshot ifTrue: [
       Smalltalk arguments = #('--repl') ifFalse: [
           self main: Smalltalk arguments.
           ObjectMemory quit ] ]!

main: argv
    "I love Java names!"
   argv
       do: [ :a| Transcript nextPutAll: a]
       separatedBy: [ Transcript nextPutAll: ' '].
   Transcript nl!
!!

ObjectMemory addDependent: EchoMain.
ObjectMemory snapshot: 'echo.im'!

First a subclass called EchoMain is created with a main function accepting the program arguments. The code then puts all arguments on the Transcript (stdout by default), ending it with a newline. Finally the script will register the EchoMain class to the ObjectMemory and then dump a snapshot of the image in the file echo.im. Creating the image and running it

The script will dump itself, so just run gst echo.st and it will create an echo.im file. Then use chmod a+x echo.im to make the image executable. This will then allow you to do ./echo.im hello world and the main function of the EchoMain class will put 'hello' and 'world' on the Transcript.


Back to GST by example

Advertisement