<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 101run_sandboxed.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.traceContains { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Helper for various programming environments: run arbitrary mu code and</span>
<span class="Comment">//: return some result in string form.</span>
<span class="Delimiter">:(scenario run_interactive_code)</span>
def main [
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [<span class="Constant">1</span>:num/<span class="Special">raw <- </span>copy <span class="Constant">34</span>]
run-sandboxed <span class="Constant">2</span>:text
<span class="Constant">3</span>:num/<span class="Special">raw <- </span>copy <span class="Constant">1</span>:num/<span class="Special">raw</span>
]
<span class="traceContains">+mem: storing 34 in location 3</span>
<span class="Delimiter">:(scenario run_interactive_empty)</span>
def main [
<span class="Constant">1</span>:text<span class="Special"> <- </span>copy <span class="Constant">0</span>/unsafe
<span class="Constant">2</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
]
<span class="Comment"># result is null</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="Comment">//: As the name suggests, 'run-sandboxed' will prevent certain operations that</span>
<span class="Comment">//: regular Mu code can perform.</span>
<span class="Delimiter">:(before "End Globals")</span>
<span class="Normal">bool</span> Sandbox_mode = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Comment">//: for starters, users can't override 'main' when the environment is running</span>
<span class="Delimiter">:(before "End Load Recipe Name")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Sandbox_mode && result<span class="Delimiter">.</span>name == <span class="Constant">"main"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
slurp_balanced_bracket<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Identifier">return</span> -<span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: run code in 'interactive mode', i.e. with errors off and return:</span>
<span class="Comment">//: stringified output in case we want to print it to screen</span>
<span class="Comment">//: any errors encountered</span>
<span class="Comment">//: simulated screen any prints went to</span>
<span class="Comment">//: any 'app' layer traces generated</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
RUN_SANDBOXED<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"run-sandboxed"</span><span class="Delimiter">,</span> RUN_SANDBOXED<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> RUN_SANDBOXED: <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'run-sandboxed' requires exactly one ingredient, but got '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_text<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"first ingredient of 'run-sandboxed' should be a string, but got '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> RUN_SANDBOXED: <span class="Delimiter">{</span>
<span class="Normal">bool</span> new_code_pushed_to_stack = run_interactive<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!new_code_pushed_to_stack<span class="Delimiter">)</span> <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">5</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_error_contents<span class="Delimiter">());</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">3</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_app_contents<span class="Delimiter">());</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">4</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span> <span class="Comment">// completed</span>
run_code_end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Comment">// done with this instruction</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// not done with caller; don't increment current_step_index()</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: To show results in the sandbox Mu uses a hack: it saves the products</span>
<span class="Comment">//: returned by each instruction while Track_most_recent_products is true, and</span>
<span class="Comment">//: keeps the most recent such result around so that it can be returned as the</span>
<span class="Comment">//: result of a sandbox.</span>
<span class="Delimiter">:(before "End Globals")</span>
<span class="Normal">bool</span> Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
string Most_recent_products<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
Most_recent_products = <span class="Constant">""</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Globals")</span>
trace_stream* Save_trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
string Save_trace_file<span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Comment">// reads a string, tries to call it as code (treating it as a test), saving</span>
<span class="Comment">// all errors.</span>
<span class="Comment">// returns true if successfully called (no errors found during load and transform)</span>
<span class="Normal">bool</span> run_interactive<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"interactive"</span><span class="Delimiter">)</span> && get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"interactive"</span><span class="Delimiter">)</span> != <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// try to sandbox the run as best you can</span>
<span class="Comment">// todo: test this</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!Current_scenario<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">1</span><span class="Delimiter">;</span> i < Reserved_for_tests<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span>
Memory<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
string command = trim<span class="Delimiter">(</span>strip_comments<span class="Delimiter">(</span>read_mu_text<span class="Delimiter">(</span>address<span class="Delimiter">)));</span>
Name[get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"interactive"</span><span class="Delimiter">)</span>]<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
run_code_begin<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">should_stash_snapshots</span><span class="Comment">*/</span><span class="Constant">true</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>command<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Comment">// don't kill the current routine on parse errors</span>
routine* save_current_routine = Current_routine<span class="Delimiter">;</span>
Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span class="Comment">// call run(string) but without the scheduling</span>
load<span class="Delimiter">(</span>string<span class="Delimiter">(</span><span class="Constant">"recipe! interactive [</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">)</span> +
<span class="Constant">"new-default-space</span><span class="cSpecial">\n</span><span class="Constant">"</span> + <span class="Comment">// disable automatic abandon so tests can see changes</span>
<span class="Constant">"screen:&:screen <- next-ingredient</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"$start-tracking-products</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
command + <span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"$stop-tracking-products</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"return screen</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"]</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">);</span>
transform_all<span class="Delimiter">();</span>
Current_routine = save_current_routine<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>trace_count<span class="Delimiter">(</span><span class="Constant">"error"</span><span class="Delimiter">)</span> > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Comment">// now call 'sandbox' which will run 'interactive' in a separate routine,</span>
<span class="Comment">// and wait for it</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Save_trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
++Save_trace_stream<span class="Delimiter">-></span>callstack_depth<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"trace"</span><span class="Delimiter">)</span> << <span class="Constant">"run-sandboxed: incrementing callstack depth to "</span> << Save_trace_stream<span class="Delimiter">-></span>callstack_depth << end<span class="Delimiter">();</span>
assert<span class="Delimiter">(</span>Save_trace_stream<span class="Delimiter">-></span>callstack_depth < <span class="Constant">9000</span><span class="Delimiter">);</span> <span class="Comment">// 9998-101 plus cushion</span>
<span class="Delimiter">}</span>
Current_routine<span class="Delimiter">-></span>calls<span class="Delimiter">.</span>push_front<span class="Delimiter">(</span>call<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"sandbox"</span><span class="Delimiter">)));</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Carefully update all state to exactly how it was -- including snapshots.</span>
<span class="Delimiter">:(before "End Globals")</span>
map<string<span class="Delimiter">,</span> recipe_ordinal> Recipe_ordinal_snapshot_stash<span class="Delimiter">;</span>
map<recipe_ordinal<span class="Delimiter">,</span> recipe> Recipe_snapshot_stash<span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> type_ordinal> Type_ordinal_snapshot_stash<span class="Delimiter">;</span>
map<type_ordinal<span class="Delimiter">,</span> type_info> Type_snapshot_stash<span class="Delimiter">;</span>
map<recipe_ordinal<span class="Delimiter">,</span> map<string<span class="Delimiter">,</span> <span class="Normal">int</span>> > Name_snapshot_stash<span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> vector<recipe_ordinal> > Recipe_variants_snapshot_stash<span class="Delimiter">;</span>
map<string<span class="Delimiter">,</span> type_tree*> Type_abbreviations_snapshot_stash<span class="Delimiter">;</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> run_code_begin<span class="Delimiter">(</span><span class="Normal">bool</span> should_stash_snapshots<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// stuff to undo later, in run_code_end()</span>
Hide_errors = <span class="Constant">true</span><span class="Delimiter">;</span>
Disable_redefine_checks = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>should_stash_snapshots<span class="Delimiter">)</span>
stash_snapshots<span class="Delimiter">();</span>
Save_trace_stream = Trace_stream<span class="Delimiter">;</span>
Trace_stream = <span class="Normal">new</span> trace_stream<span class="Delimiter">;</span>
Trace_stream<span class="Delimiter">-></span>collect_depth = App_depth<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> run_code_end<span class="Delimiter">()</span> <span class="Delimiter">{</span>
Hide_errors = <span class="Constant">false</span><span class="Delimiter">;</span>
Disable_redefine_checks = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="CommentedCode">//? ofstream fout("sandbox.log");</span>
<span class="CommentedCode">//? fout << Trace_stream->readable_contents("");</span>
<span class="CommentedCode">//? fout.close();</span>
<span class="Normal">delete</span> Trace_stream<span class="Delimiter">;</span>
Trace_stream = Save_trace_stream<span class="Delimiter">;</span>
Save_trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
Save_trace_file<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Recipe<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"interactive"</span><span class="Delimiter">));</span> <span class="Comment">// keep past sandboxes from inserting errors</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!Recipe_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
unstash_snapshots<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Comment">// keep sync'd with save_snapshots and restore_snapshots</span>
<span class="Normal">void</span> stash_snapshots<span class="Delimiter">()</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>Recipe_ordinal_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Recipe_ordinal_snapshot_stash = Recipe_ordinal_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Recipe_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Recipe_snapshot_stash = Recipe_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Type_ordinal_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Type_ordinal_snapshot_stash = Type_ordinal_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Type_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Type_snapshot_stash = Type_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Name_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Name_snapshot_stash = Name_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Recipe_variants_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Recipe_variants_snapshot_stash = Recipe_variants_snapshot<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Type_abbreviations_snapshot_stash<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
Type_abbreviations_snapshot_stash = Type_abbreviations_snapshot<span class="Delimiter">;</span>
save_snapshots<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> unstash_snapshots<span class="Delimiter">()</span> <span class="Delimiter">{</span>
restore_snapshots<span class="Delimiter">();</span>
Recipe_ordinal_snapshot = Recipe_ordinal_snapshot_stash<span class="Delimiter">;</span> Recipe_ordinal_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Recipe_snapshot = Recipe_snapshot_stash<span class="Delimiter">;</span> Recipe_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Type_ordinal_snapshot = Type_ordinal_snapshot_stash<span class="Delimiter">;</span> Type_ordinal_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Type_snapshot = Type_snapshot_stash<span class="Delimiter">;</span> Type_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Name_snapshot = Name_snapshot_stash<span class="Delimiter">;</span> Name_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Recipe_variants_snapshot = Recipe_variants_snapshot_stash<span class="Delimiter">;</span> Recipe_variants_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
Type_abbreviations_snapshot = Type_abbreviations_snapshot_stash<span class="Delimiter">;</span> Type_abbreviations_snapshot_stash<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Load Recipes")</span>
load<span class="Delimiter">(</span>string<span class="Delimiter">(</span>
<span class="Constant">"recipe interactive [</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">)</span> + <span class="Comment">// just a dummy version to initialize the Recipe_ordinal and so on</span>
<span class="Constant">"]</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"recipe sandbox [</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"local-scope</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"screen:&:screen <- new-fake-screen 30, 5</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"routine-id:num <- start-running interactive, screen</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"limit-time routine-id, 100000/instructions</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"wait-for-routine routine-id</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"instructions-run:num <- number-of-instructions routine-id</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"stash instructions-run [instructions run]</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"sandbox-state:num <- routine-state routine-id</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"completed?:bool <- equal sandbox-state, 1/completed</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"output:text <- $most-recent-products</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"errors:text <- save-errors</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"stashes:text <- save-app-trace</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"$cleanup-run-sandboxed</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"return output, errors, screen, stashes, completed?</span><span class="cSpecial">\n</span><span class="Constant">"</span> +
<span class="Constant">"]</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">);</span>
<span class="Comment">//: adjust errors in the sandbox</span>
<span class="Delimiter">:(after "string maybe(string s)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">"interactive"</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span>
<span class="Delimiter">:(scenario run_interactive_comments)</span>
def main [
<span class="Constant">1</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [<span class="Comment"># ab</span>
add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>]
<span class="Constant">2</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
<span class="Constant">3</span>:@:<span class="Normal">char</span><span class="Special"> <- </span>copy *<span class="Constant">2</span>:text
]
<span class="traceContains">+mem: storing 52 in location 4</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
_START_TRACKING_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"$start-tracking-products"</span><span class="Delimiter">,</span> _START_TRACKING_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> _START_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> _START_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
Track_most_recent_products = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
_STOP_TRACKING_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"$stop-tracking-products"</span><span class="Delimiter">,</span> _STOP_TRACKING_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> _STOP_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> _STOP_TRACKING_PRODUCTS: <span class="Delimiter">{</span>
Track_most_recent_products = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
_MOST_RECENT_PRODUCTS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"$most-recent-products"</span><span class="Delimiter">,</span> _MOST_RECENT_PRODUCTS<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> _MOST_RECENT_PRODUCTS: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> _MOST_RECENT_PRODUCTS: <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>new_mu_text<span class="Delimiter">(</span>Most_recent_products<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
SAVE_ERRORS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"save-errors"</span><span class="Delimiter">,</span> SAVE_ERRORS<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> SAVE_ERRORS: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> SAVE_ERRORS: <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_error_contents<span class="Delimiter">());</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
SAVE_APP_TRACE<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"save-app-trace"</span><span class="Delimiter">,</span> SAVE_APP_TRACE<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> SAVE_APP_TRACE: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> SAVE_APP_TRACE: <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_app_contents<span class="Delimiter">());</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
_CLEANUP_RUN_SANDBOXED<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"$cleanup-run-sandboxed"</span><span class="Delimiter">,</span> _CLEANUP_RUN_SANDBOXED<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> _CLEANUP_RUN_SANDBOXED: <span class="Delimiter">{</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> _CLEANUP_RUN_SANDBOXED: <span class="Delimiter">{</span>
run_code_end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario "run_interactive_converts_result_to_text")</span>
def main [
<span class="Comment"># try to interactively add 2 and 2</span>
<span class="Constant">1</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>]
<span class="Constant">2</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
<span class="Constant">10</span>:@:<span class="Normal">char</span><span class="Special"> <- </span>copy <span class="Constant">2</span>:text/lookup
]
<span class="Comment"># first letter in the output should be '4' in unicode</span>
<span class="traceContains">+mem: storing 52 in location 11</span>
<span class="Delimiter">:(scenario "run_interactive_returns_text")</span>
def main [
<span class="Comment"># try to interactively add 2 and 2</span>
<span class="Constant">1</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [
<span class="Normal">x</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [a]
<span class="Normal">y</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [b]
<span class="Normal">z</span>:text<span class="Special"> <- </span>append x:text<span class="Delimiter">,</span> y:text
]
<span class="Constant">2</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
<span class="Constant">10</span>:@:<span class="Normal">char</span><span class="Special"> <- </span>copy <span class="Constant">2</span>:text/lookup
]
<span class="Comment"># output contains "ab"</span>
<span class="traceContains">+mem: storing 97 in location 11</span>
<span class="traceContains">+mem: storing 98 in location 12</span>
<span class="Delimiter">:(scenario "run_interactive_returns_errors")</span>
def main [
<span class="Comment"># run a command that generates an error</span>
<span class="Constant">1</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [x:num<span class="Special"> <- </span>copy <span class="Constant">34</span>
get x:num<span class="Delimiter">,</span> foo:offset]
<span class="Constant">2</span>:text<span class="Delimiter">,</span> <span class="Constant">3</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
<span class="Constant">10</span>:@:<span class="Normal">char</span><span class="Special"> <- </span>copy <span class="Constant">3</span>:text/lookup
]
<span class="Comment"># error should be "unknown element foo in container number"</span>
<span class="traceContains">+mem: storing 117 in location 11</span>
<span class="traceContains">+mem: storing 110 in location 12</span>
<span class="traceContains">+mem: storing 107 in location 13</span>
<span class="traceContains">+mem: storing 110 in location 14</span>
<span class="Comment"># ...</span>
<span class="Delimiter">:(scenario run_interactive_with_comment)</span>
def main [
<span class="Comment"># 2 instructions, with a comment after the first</span>
<span class="Constant">1</span>:&:@:num<span class="Special"> <- </span><span class="Normal">new</span> [a:num<span class="Special"> <- </span>copy <span class="Constant">0</span> <span class="Comment"># abc</span>
<span class="Normal">b</span>:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="Constant">2</span>:text<span class="Delimiter">,</span> <span class="Constant">3</span>:text<span class="Special"> <- </span>run-sandboxed <span class="Constant">1</span>:text
]
<span class="Comment"># no errors</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Delimiter">:(before "End Running One Instruction")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Track_most_recent_products<span class="Delimiter">)</span> <span class="Delimiter">{</span>
track_most_recent_products<span class="Delimiter">(</span>current_instruction<span class="Delimiter">(),</span> products<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> track_most_recent_products<span class="Delimiter">(</span><span class="Normal">const</span> instruction& instruction<span class="Delimiter">,</span> <span class="Normal">const</span> vector<vector<<span class="Normal">double</span>> >& products<span class="Delimiter">)</span> <span class="Delimiter">{</span>
ostringstream out<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// A sandbox can print a string result, but only if it is actually saved</span>
<span class="Comment">// to a variable in the sandbox, because otherwise the results are</span>
<span class="Comment">// reclaimed before the sandbox sees them. So you get these interactions</span>
<span class="Comment">// in the sandbox:</span>
<span class="Comment">//</span>
<span class="Comment">// new [abc]</span>
<span class="Comment">// => <address></span>
<span class="Comment">//</span>
<span class="Comment">// x:text <- new [abc]</span>
<span class="Comment">// => abc</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>i < SIZE<span class="Delimiter">(</span>instruction<span class="Delimiter">.</span>products<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_text<span class="Delimiter">(</span>instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// error handled elsewhere</span>
out << read_mu_text<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span> j < SIZE<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span> ++j<span class="Delimiter">)</span>
out << no_scientific<span class="Delimiter">(</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span>j<span class="Delimiter">))</span> << <span class="Constant">' '</span><span class="Delimiter">;</span>
out << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
Most_recent_products = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
string strip_comments<span class="Delimiter">(</span>string in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
ostringstream result<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>in<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> != <span class="Constant">'#'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
result << in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>i+<span class="Constant">1</span> < SIZE<span class="Delimiter">(</span>in<span class="Delimiter">)</span> && in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i+<span class="Constant">1</span><span class="Delimiter">)</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span>
++i<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> stringified_value_of_location<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// convert to string</span>
ostringstream out<span class="Delimiter">;</span>
out << no_scientific<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">));</span>
<span class="Identifier">return</span> new_mu_text<span class="Delimiter">(</span>out<span class="Delimiter">.</span>str<span class="Delimiter">());</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> trace_error_contents<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
ostringstream out<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>vector<trace_line>::iterator p = Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-></span>label != <span class="Constant">"error"</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
out << p<span class="Delimiter">-></span>contents<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>*--p<span class="Delimiter">-></span>contents<span class="Delimiter">.</span>end<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
string result = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
truncate<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">return</span> new_mu_text<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">int</span> trace_app_contents<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
ostringstream out<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>vector<trace_line>::iterator p = Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>p<span class="Delimiter">-></span>depth != App_depth<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
out << p<span class="Delimiter">-></span>contents<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>*--p<span class="Delimiter">-></span>contents<span class="Delimiter">.</span>end<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> out << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
string result = out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
truncate<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">return</span> new_mu_text<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> truncate<span class="Delimiter">(</span>string& x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>x<span class="Delimiter">)</span> > <span class="Constant">1024</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
x<span class="Delimiter">.</span>erase<span class="Delimiter">(</span><span class="Constant">1024</span><span class="Delimiter">);</span>
*x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
*++x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="Constant">'.'</span><span class="Delimiter">;</span>
*++++x<span class="Delimiter">.</span>rbegin<span class="Delimiter">()</span> = <span class="Constant">'.'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: simpler version of run-sandboxed: doesn't do any running, just loads</span>
<span class="Comment">//: recipes and reports errors.</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
RELOAD<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"reload"</span><span class="Delimiter">,</span> RELOAD<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> RELOAD: <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'reload' requires exactly one ingredient, but got '"</span> << inst<span class="Delimiter">.</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_text<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"first ingredient of 'reload' should be a string, but got '"</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> RELOAD: <span class="Delimiter">{</span>
restore_non_recipe_snapshots<span class="Delimiter">();</span>
string code = read_mu_text<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
run_code_begin<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">should_stash_snapshots</span><span class="Comment">*/</span><span class="Constant">false</span><span class="Delimiter">);</span>
routine* save_current_routine = Current_routine<span class="Delimiter">;</span>
Current_routine = <span class="Constant">NULL</span><span class="Delimiter">;</span>
Sandbox_mode = <span class="Constant">true</span><span class="Delimiter">;</span>
vector<recipe_ordinal> recipes_reloaded = load<span class="Delimiter">(</span>code<span class="Delimiter">);</span>
transform_all<span class="Delimiter">();</span>
Trace_stream<span class="Delimiter">-></span>newline<span class="Delimiter">();</span> <span class="Comment">// flush trace</span>
Sandbox_mode = <span class="Constant">false</span><span class="Delimiter">;</span>
Current_routine = save_current_routine<span class="Delimiter">;</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>trace_error_contents<span class="Delimiter">());</span>
run_code_end<span class="Delimiter">();</span> <span class="Comment">// wait until we're done with the trace contents</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario reload_continues_past_error)</span>
def main [
local-scope
<span class="Normal">x</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [recipe foo [
get <span class="Constant">1234</span>:num<span class="Delimiter">,</span> foo:offset
]]
reload x
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="Delimiter">:(scenario reload_can_repeatedly_load_container_definitions)</span>
<span class="Comment"># define a container and try to create it (merge requires knowing container size)</span>
def main [
local-scope
<span class="Normal">x</span>:text<span class="Special"> <- </span><span class="Normal">new</span> [
container foo [
<span class="Normal">x</span>:num
<span class="Normal">y</span>:num
]
recipe bar [
local-scope
<span class="Normal">x</span>:foo<span class="Special"> <- </span>merge <span class="Constant">34</span><span class="Delimiter">,</span> <span class="Constant">35</span>
]
]
<span class="Comment"># save warning addresses in locations of type 'number' to avoid spurious changes to them due to 'abandon'</span>
<span class="Constant">1</span>:num/<span class="Special">raw <- </span>reload x
<span class="Constant">2</span>:num/<span class="Special">raw <- </span>reload x
]
<span class="Comment"># no errors on either load</span>
<span class="traceContains">+mem: storing 0 in location 1</span>
<span class="traceContains">+mem: storing 0 in location 2</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->