===== How Overlays Work ===== ==== Method Source Overlays ==== There are four kinds of overlays: This article deals with method source overlays (MSO) only. Method source overlays allow the JAS user to store Smalltalk code (methods) in scenario data as overlays to existing or new methods. When the simulation starts to run, JAS reads the overlay data, compiles it, and then replaces the old method code with the overlaid code. Overlays are possible because Smalltalk supports [[http://en.wikipedia.org/wiki/Just-in-time_compilation| just-in-time-compilation]] (JIT). JIT was invented by Smalltalk in the early 1980s, and was extended by the languages Self and Java. ==== A Trivial Example ==== The class Circle implements a method called area, which allows Circle objects to answer their area when they are sent the area message. The base code for method Circle>>area looks like this: area ^Float pi * radius * radius But let us suppose that you, for whatever reason, want your circles to have a slightly larger area. You could create a MSO for method Circle>>area that looks like this: area ^(Float pi * radius * radius) + 1 Now all your circles are 1 unit bigger! A triumph of modeling! ==== A Detailed Technical Explanation ==== As mentioned above, MSOs are only possible because Smalltalk supports [[http://en.wikipedia.org/wiki/Just-in-time_compilation| just-in-time-compilation]] (JIT). Non-jitted languages, such as C++, cannot support MSOs. Here is an explanation. We will use the infamous Hello, World program as an example. In C, it is: main() { print_Hello_World(); } print_Hello_World() { printf(“Hello, World\n”); } This C source code is compiled by a C compiler, which checks for syntactical correctness, and then a linker program is run to 'link in' the necessary operating system routines which finally produces a standalone executable program called hello.exe. This executable contains an entry (start) point called main, and a table of the functions in the program and where they are. Hello.exe has just the one entry for print_Hello_World in its function table. If we wanted to change the implementation of print_Hello_World to also print "Hi, Mom", we would have to edit the source code print_Hello_World() { printf(“Hello, World\n”); printf(“Hi, Mom\n”); } and then recompile, relink, and recreate the executable hello.exe. Because hello.exe is staticly linked, there is no way to change what the function print_Hello_World does at runtime. Let's explore a similar example in Smalltalk. Because we are conscientious, we will include in our program the ability to read overlays when the program starts: main self read_Overlays. self print_Hello_World. print_Hello_World Transcript show: “Hello, World";cr.