GST Script

212,895pages on
this wiki
Add New Page
Add New Page Discuss this page0

Smalltalk by example: Creating an executable image


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!"
       do: [ :a| Transcript nextPutAll: a]
       separatedBy: [ Transcript nextPutAll: ' '].
   Transcript nl!

ObjectMemory addDependent: EchoMain.
ObjectMemory snapshot: ''!

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 Creating the image and running it

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

Back to GST by example

Also on Fandom

Random wikia