<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>MLtonWorld</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</div>
</div>
<div id="header">
<h1>MLtonWorld</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">signature MLTON_WORLD =
   sig
      datatype status = Clone | Original

      val load: string -&gt; 'a
      val save: string -&gt; status
      val saveThread: string * Thread.Runnable.t -&gt; unit
   end</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><code>datatype status</code></p>
<div class="paragraph">
<p>specifies whether a world is original or restarted (a clone).</p>
</div>
</li>
<li>
<p><code>load f</code></p>
<div class="paragraph">
<p>loads the saved computation from file <code>f</code>.</p>
</div>
</li>
<li>
<p><code>save f</code></p>
<div class="paragraph">
<p>saves the entire state of the computation to the file <code>f</code>.  The
computation can then be restarted at a later time using <code>World.load</code>
or the <code>load-world</code> <a href="RunTimeOptions">runtime option</a>.  The call to
<code>save</code> in the original computation returns <code>Original</code> and the call in
the restarted world returns <code>Clone</code>.</p>
</div>
</li>
<li>
<p><code>saveThread (f, rt)</code></p>
<div class="paragraph">
<p>saves the entire state of the computation to the file <code>f</code> that will
resume with thread <code>rt</code> upon restart.</p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_notes">Notes</h2>
<div class="sectionbody">
<div id="ASLR" class="paragraph">
<p>Executables that save and load worlds are incompatible with
<a href="http://en.wikipedia.org/wiki/Address_space_layout_randomization">address space layout randomization (ASLR)</a>
of the executable (though, not of shared libraries).  The state of a
computation includes addresses into the code and data segments of the
executable (e.g., static runtime-system data, return addresses); such
addresses are invalid when interpreted by the executable loaded at a
different base address.</p>
</div>
<div class="paragraph">
<p>Executables that save and load worlds should be compiled with an
option to suppress the generation of position-independent executables.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="RunningOnDarwin">Darwin 11 (Mac OS X Lion) and higher</a> : <code>-link-opt -fno-PIE</code></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Suppose that <code>save-world.sml</code> contains the following.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in MLtonWorld.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/save-world/save-world.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then, if we compile <code>save-world.sml</code> and run it, the <code>Original</code>
branch will execute, and a file named <code>world</code> will be created.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlton save-world.sml
% ./save-world
I am the original</pre>
</div>
</div>
<div class="paragraph">
<p>We can then load <code>world</code> using the <code>load-world</code>
<a href="RunTimeOptions">run time option</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% ./save-world @MLton load-world world --
I am the clone</pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>