fix other mandelbrot variants
[mu.git] / archive / 1.vm / 101run_sandboxed.cc
blobe464bbe3921330b03dc6eaf7fde044de54bef31c
1 //: Helper for various programming environments: run arbitrary Mu code and
2 //: return some result in text form.
4 void test_run_interactive_code() {
5 run(
6 "def main [\n"
7 " 1:num <- copy 0\n" // reserve space for the sandbox
8 " 10:text <- new [1:num/raw <- copy 34]\n"
9 //? " $print 10:num [|] 11:num [: ] 1000:num [|] *10:text [ (] 10:text [)] 10/newline\n"
10 " run-sandboxed 10:text\n"
11 " 20:num <- copy 1:num\n"
12 "]\n"
14 CHECK_TRACE_CONTENTS(
15 "mem: storing 34 in location 20\n"
19 void test_run_interactive_empty() {
20 run(
21 "def main [\n"
22 " 10:text <- copy null\n"
23 " 20:text <- run-sandboxed 10:text\n"
24 "]\n"
26 // result is null
27 CHECK_TRACE_CONTENTS(
28 "mem: storing 0 in location 20\n"
29 "mem: storing 0 in location 21\n"
33 //: As the name suggests, 'run-sandboxed' will prevent certain operations that
34 //: regular Mu code can perform.
35 :(before "End Globals")
36 bool Sandbox_mode = false;
37 //: for starters, users can't override 'main' when the environment is running
38 :(before "End Load Recipe Name")
39 if (Sandbox_mode && result.name == "main") {
40 slurp_balanced_bracket(in);
41 return -1;
44 //: run code in 'interactive mode', i.e. with errors off and return:
45 //: stringified output in case we want to print it to screen
46 //: any errors encountered
47 //: simulated screen any prints went to
48 //: any 'app' layer traces generated
49 :(before "End Primitive Recipe Declarations")
50 RUN_SANDBOXED,
51 :(before "End Primitive Recipe Numbers")
52 put(Recipe_ordinal, "run-sandboxed", RUN_SANDBOXED);
53 :(before "End Primitive Recipe Checks")
54 case RUN_SANDBOXED: {
55 if (SIZE(inst.ingredients) != 1) {
56 raise << maybe(get(Recipe, r).name) << "'run-sandboxed' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
57 break;
59 if (!is_mu_text(inst.ingredients.at(0))) {
60 raise << maybe(get(Recipe, r).name) << "first ingredient of 'run-sandboxed' should be a string, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
61 break;
63 break;
65 :(before "End Primitive Recipe Implementations")
66 case RUN_SANDBOXED: {
67 bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(/*skip alloc id*/1));
68 if (!new_code_pushed_to_stack) {
69 products.resize(5);
70 products.at(0).push_back(/*alloc id*/0);
71 products.at(0).push_back(0);
72 products.at(1).push_back(/*alloc id*/0);
73 products.at(1).push_back(trace_error_contents());
74 products.at(2).push_back(/*alloc id*/0);
75 products.at(2).push_back(0);
76 products.at(3).push_back(/*alloc id*/0);
77 products.at(3).push_back(trace_app_contents());
78 products.at(4).push_back(1); // completed
79 run_code_end();
80 break; // done with this instruction
82 else {
83 continue; // not done with caller; don't increment current_step_index()
87 //: To show results in the sandbox Mu uses a hack: it saves the products
88 //: returned by each instruction while Track_most_recent_products is true, and
89 //: keeps the most recent such result around so that it can be returned as the
90 //: result of a sandbox.
92 :(before "End Globals")
93 bool Track_most_recent_products = false;
94 int Call_depth_to_track_most_recent_products_at = 0;
95 string Most_recent_products;
96 :(before "End Reset")
97 Track_most_recent_products = false;
98 Call_depth_to_track_most_recent_products_at = 0;
99 Most_recent_products = "";
101 :(before "End Globals")
102 trace_stream* Save_trace_stream = NULL;
103 string Save_trace_file;
104 int Save_callstack_depth = 0;
105 :(code)
106 // reads a string, tries to call it as code (treating it as a test), saving
107 // all errors.
108 // returns true if successfully called (no errors found during load and transform)
109 bool run_interactive(int address) {
110 //? cerr << "run_interactive: " << address << '\n';
111 assert(contains_key(Recipe_ordinal, "interactive") && get(Recipe_ordinal, "interactive") != 0);
112 // try to sandbox the run as best you can
113 // todo: test this
114 if (!Current_scenario) {
115 for (int i = 1; i < Reserved_for_tests; ++i)
116 Memory.erase(i);
118 string command = trim(strip_comments(read_mu_text(address)));
119 //? cerr << "command: " << command << '\n';
120 Name[get(Recipe_ordinal, "interactive")].clear();
121 run_code_begin(/*should_stash_snapshots*/true);
122 if (command.empty()) return false;
123 // don't kill the current routine on parse errors
124 routine* save_current_routine = Current_routine;
125 Current_routine = NULL;
126 // call run(string) but without the scheduling
127 load(string("recipe! interactive [\n") +
128 "local-scope\n" +
129 "screen:&:screen <- next-ingredient\n" +
130 "$start-tracking-products\n" +
131 command + "\n" +
132 "$stop-tracking-products\n" +
133 "return screen\n" +
134 "]\n");
135 transform_all();
136 Current_routine = save_current_routine;
137 if (trace_count("error") > 0) return false;
138 // now call 'sandbox' which will run 'interactive' in a separate routine,
139 // and wait for it
140 if (Save_trace_stream) {
141 ++Save_callstack_depth;
142 trace(Save_callstack_depth+1, "trace") << "run-sandboxed: incrementing callstack depth to " << Save_callstack_depth << end();
143 assert(Save_callstack_depth < Max_depth);
145 Current_routine->calls.push_front(call(get(Recipe_ordinal, "sandbox")));
146 return true;
149 //: Carefully update all state to exactly how it was -- including snapshots.
151 :(before "End Globals")
152 bool Run_profiler_stash = false;
153 map<string, recipe_ordinal> Recipe_ordinal_snapshot_stash;
154 map<recipe_ordinal, recipe> Recipe_snapshot_stash;
155 map<string, type_ordinal> Type_ordinal_snapshot_stash;
156 map<type_ordinal, type_info> Type_snapshot_stash;
157 map<recipe_ordinal, map<string, int> > Name_snapshot_stash;
158 map<string, vector<recipe_ordinal> > Recipe_variants_snapshot_stash;
159 map<string, type_tree*> Type_abbreviations_snapshot_stash;
160 vector<scenario> Scenarios_snapshot_stash;
161 set<string> Scenario_names_snapshot_stash;
163 :(code)
164 void run_code_begin(bool should_stash_snapshots) {
165 // stuff to undo later, in run_code_end()
166 Hide_errors = true;
167 Disable_redefine_checks = true;
168 Run_profiler_stash = Run_profiler;
169 Run_profiler = false;
170 if (should_stash_snapshots)
171 stash_snapshots();
172 Save_trace_stream = Trace_stream;
173 Save_callstack_depth = Callstack_depth;
174 Callstack_depth = Initial_callstack_depth;
175 Trace_stream = new trace_stream;
176 if (Save_trace_stream)
177 Trace_stream->collect_depth = Save_trace_stream->collect_depth;
180 void run_code_end() {
181 Hide_errors = false;
182 Disable_redefine_checks = false;
183 Run_profiler = Run_profiler_stash;
184 Run_profiler_stash = false;
185 //? ofstream fout("sandbox.log");
186 //? fout << Trace_stream->readable_contents("");
187 //? fout.close();
188 delete Trace_stream;
189 Trace_stream = Save_trace_stream;
190 Callstack_depth = Save_callstack_depth;
191 Save_trace_stream = NULL;
192 Save_trace_file.clear();
193 Save_callstack_depth = 0;
194 Recipe.erase(get(Recipe_ordinal, "interactive")); // keep past sandboxes from inserting errors
195 if (!Recipe_snapshot_stash.empty())
196 unstash_snapshots();
199 // keep sync'd with save_snapshots and restore_snapshots
200 void stash_snapshots() {
201 assert(Recipe_ordinal_snapshot_stash.empty());
202 Recipe_ordinal_snapshot_stash = Recipe_ordinal_snapshot;
203 assert(Recipe_snapshot_stash.empty());
204 Recipe_snapshot_stash = Recipe_snapshot;
205 assert(Type_ordinal_snapshot_stash.empty());
206 Type_ordinal_snapshot_stash = Type_ordinal_snapshot;
207 assert(Type_snapshot_stash.empty());
208 Type_snapshot_stash = Type_snapshot;
209 assert(Name_snapshot_stash.empty());
210 Name_snapshot_stash = Name_snapshot;
211 assert(Recipe_variants_snapshot_stash.empty());
212 Recipe_variants_snapshot_stash = Recipe_variants_snapshot;
213 assert(Type_abbreviations_snapshot_stash.empty());
214 Type_abbreviations_snapshot_stash = Type_abbreviations_snapshot;
215 assert(Scenarios_snapshot_stash.empty());
216 Scenarios_snapshot_stash = Scenarios_snapshot;
217 assert(Scenario_names_snapshot_stash.empty());
218 Scenario_names_snapshot_stash = Scenario_names_snapshot;
219 save_snapshots();
221 void unstash_snapshots() {
222 restore_snapshots();
223 Recipe_ordinal_snapshot = Recipe_ordinal_snapshot_stash; Recipe_ordinal_snapshot_stash.clear();
224 Recipe_snapshot = Recipe_snapshot_stash; Recipe_snapshot_stash.clear();
225 Type_ordinal_snapshot = Type_ordinal_snapshot_stash; Type_ordinal_snapshot_stash.clear();
226 Type_snapshot = Type_snapshot_stash; Type_snapshot_stash.clear();
227 Name_snapshot = Name_snapshot_stash; Name_snapshot_stash.clear();
228 Recipe_variants_snapshot = Recipe_variants_snapshot_stash; Recipe_variants_snapshot_stash.clear();
229 Type_abbreviations_snapshot = Type_abbreviations_snapshot_stash; Type_abbreviations_snapshot_stash.clear();
230 Scenarios_snapshot = Scenarios_snapshot_stash; Scenarios_snapshot_stash.clear();
231 Scenario_names_snapshot = Scenario_names_snapshot_stash; Scenario_names_snapshot_stash.clear();
234 :(before "End Mu Prelude")
235 load(string(
236 "recipe interactive [\n") + // just a dummy version to initialize the Recipe_ordinal and so on
237 "]\n" +
238 "recipe sandbox [\n" +
239 "local-scope\n" +
240 //? "$print [aaa] 10/newline\n" +
241 "screen:&:screen <- new-fake-screen 30, 5\n" +
242 "routine-id:num <- start-running interactive, screen\n" +
243 "limit-time routine-id, 100000/instructions\n" +
244 "wait-for-routine routine-id\n" +
245 //? "$print [bbb] 10/newline\n" +
246 "instructions-run:num <- number-of-instructions routine-id\n" +
247 "stash instructions-run [instructions run]\n" +
248 "sandbox-state:num <- routine-state routine-id\n" +
249 "completed?:bool <- equal sandbox-state, 1/completed\n" +
250 //? "$print [completed: ] completed? 10/newline\n" +
251 "output:text <- $most-recent-products\n" +
252 //? "$print [zzz] 10/newline\n" +
253 //? "$print output\n" +
254 "errors:text <- save-errors\n" +
255 "stashes:text <- save-app-trace\n" +
256 "$cleanup-run-sandboxed\n" +
257 "return output, errors, screen, stashes, completed?\n" +
258 "]\n");
260 //: adjust errors in the sandbox
261 :(before "End maybe(recipe_name) Special-cases")
262 if (recipe_name == "interactive") return "";
264 :(code)
265 void test_run_interactive_comments() {
266 run(
267 "def main [\n"
268 " 1:text <- new [# ab\n"
269 "add 2, 2]\n"
270 " 2:text <- run-sandboxed 1:text\n"
271 " 3:@:char <- copy *2:text\n"
272 "]\n"
274 CHECK_TRACE_CONTENTS(
275 "mem: storing 52 in location 4\n"
279 :(before "End Primitive Recipe Declarations")
280 _START_TRACKING_PRODUCTS,
281 :(before "End Primitive Recipe Numbers")
282 put(Recipe_ordinal, "$start-tracking-products", _START_TRACKING_PRODUCTS);
283 :(before "End Primitive Recipe Checks")
284 case _START_TRACKING_PRODUCTS: {
285 break;
287 :(before "End Primitive Recipe Implementations")
288 case _START_TRACKING_PRODUCTS: {
289 Track_most_recent_products = true;
290 Call_depth_to_track_most_recent_products_at = SIZE(Current_routine->calls);
291 break;
294 :(before "End Primitive Recipe Declarations")
295 _STOP_TRACKING_PRODUCTS,
296 :(before "End Primitive Recipe Numbers")
297 put(Recipe_ordinal, "$stop-tracking-products", _STOP_TRACKING_PRODUCTS);
298 :(before "End Primitive Recipe Checks")
299 case _STOP_TRACKING_PRODUCTS: {
300 break;
302 :(before "End Primitive Recipe Implementations")
303 case _STOP_TRACKING_PRODUCTS: {
304 Track_most_recent_products = false;
305 break;
308 :(before "End Primitive Recipe Declarations")
309 _MOST_RECENT_PRODUCTS,
310 :(before "End Primitive Recipe Numbers")
311 put(Recipe_ordinal, "$most-recent-products", _MOST_RECENT_PRODUCTS);
312 :(before "End Primitive Recipe Checks")
313 case _MOST_RECENT_PRODUCTS: {
314 break;
316 :(before "End Primitive Recipe Implementations")
317 case _MOST_RECENT_PRODUCTS: {
318 products.resize(1);
319 products.at(0).push_back(/*alloc id*/0);
320 products.at(0).push_back(new_mu_text(Most_recent_products));
321 break;
324 :(before "End Primitive Recipe Declarations")
325 SAVE_ERRORS,
326 :(before "End Primitive Recipe Numbers")
327 put(Recipe_ordinal, "save-errors", SAVE_ERRORS);
328 :(before "End Primitive Recipe Checks")
329 case SAVE_ERRORS: {
330 break;
332 :(before "End Primitive Recipe Implementations")
333 case SAVE_ERRORS: {
334 products.resize(1);
335 products.at(0).push_back(/*alloc id*/0);
336 products.at(0).push_back(trace_error_contents());
337 break;
340 :(before "End Primitive Recipe Declarations")
341 SAVE_APP_TRACE,
342 :(before "End Primitive Recipe Numbers")
343 put(Recipe_ordinal, "save-app-trace", SAVE_APP_TRACE);
344 :(before "End Primitive Recipe Checks")
345 case SAVE_APP_TRACE: {
346 break;
348 :(before "End Primitive Recipe Implementations")
349 case SAVE_APP_TRACE: {
350 products.resize(1);
351 products.at(0).push_back(/*alloc id*/0);
352 products.at(0).push_back(trace_app_contents());
353 break;
356 :(before "End Primitive Recipe Declarations")
357 _CLEANUP_RUN_SANDBOXED,
358 :(before "End Primitive Recipe Numbers")
359 put(Recipe_ordinal, "$cleanup-run-sandboxed", _CLEANUP_RUN_SANDBOXED);
360 :(before "End Primitive Recipe Checks")
361 case _CLEANUP_RUN_SANDBOXED: {
362 break;
364 :(before "End Primitive Recipe Implementations")
365 case _CLEANUP_RUN_SANDBOXED: {
366 run_code_end();
367 break;
370 :(code)
371 void test_run_interactive_converts_result_to_text() {
372 // try to interactively add 2 and 2
373 run(
374 "def main [\n"
375 " 10:text <- new [add 2, 2]\n"
376 " 20:text <- run-sandboxed 10:text\n"
377 " 30:@:char <- copy *20:text\n"
378 "]\n"
380 // first letter in the output should be '4' in utf-8
381 CHECK_TRACE_CONTENTS(
382 "mem: storing 52 in location 31\n"
386 void test_run_interactive_ignores_products_in_nested_functions() {
387 run(
388 "def main [\n"
389 " 10:text <- new [foo]\n"
390 " 20:text <- run-sandboxed 10:text\n"
391 " 30:@:char <- copy *20:text\n"
392 "]\n"
393 "def foo [\n"
394 " 40:num <- copy 1234\n"
395 " {\n"
396 " break\n"
397 " reply 5678\n"
398 " }\n"
399 "]\n"
401 // no product should have been tracked
402 CHECK_TRACE_CONTENTS(
403 "mem: storing 0 in location 30\n"
407 void test_run_interactive_ignores_products_in_previous_instructions() {
408 run(
409 "def main [\n"
410 " 10:text <- new [\n"
411 " add 1, 1\n" // generates a product
412 " foo]\n" // no products
413 " 20:text <- run-sandboxed 10:text\n"
414 " 30:@:char <- copy *20:text\n"
415 "]\n"
416 "def foo [\n"
417 " 40:num <- copy 1234\n"
418 " {\n"
419 " break\n"
420 " reply 5678\n"
421 " }\n"
422 "]\n"
424 // no product should have been tracked
425 CHECK_TRACE_CONTENTS(
426 "mem: storing 0 in location 30\n"
430 void test_run_interactive_remembers_products_before_final_label() {
431 run(
432 "def main [\n"
433 " 10:text <- new [\n"
434 " add 1, 1\n" // generates a product
435 " +foo]\n" // no products
436 " 20:text <- run-sandboxed 10:text\n"
437 " 30:@:char <- copy *20:text\n"
438 "]\n"
439 "def foo [\n"
440 " 40:num <- copy 1234\n"
441 " {\n"
442 " break\n"
443 " reply 5678\n"
444 " }\n"
445 "]\n"
447 // product tracked
448 CHECK_TRACE_CONTENTS(
449 "mem: storing 50 in location 31\n"
453 void test_run_interactive_returns_text() {
454 // try to interactively add 2 and 2
455 run(
456 "def main [\n"
457 " 1:text <- new [\n"
458 " x:text <- new [a]\n"
459 " y:text <- new [b]\n"
460 " z:text <- append x:text, y:text\n"
461 " ]\n"
462 " 10:text <- run-sandboxed 1:text\n"
463 //? " $print 10:text 10/newline\n"
464 " 20:@:char <- copy *10:text\n"
465 "]\n"
467 // output contains "ab"
468 CHECK_TRACE_CONTENTS(
469 "mem: storing 97 in location 21\n"
470 "mem: storing 98 in location 22\n"
474 void test_run_interactive_returns_errors() {
475 run(
476 "def main [\n"
477 // run a command that generates an error
478 " 10:text <- new [x:num <- copy 34\n"
479 "get x:num, foo:offset]\n"
480 " 20:text, 30:text <- run-sandboxed 10:text\n"
481 " 40:@:char <- copy *30:text\n"
482 "]\n"
484 // error should be "unknown element foo in container number"
485 CHECK_TRACE_CONTENTS(
486 "mem: storing 117 in location 41\n"
487 "mem: storing 110 in location 42\n"
488 "mem: storing 107 in location 43\n"
489 "mem: storing 110 in location 44\n"
490 // ...
494 void test_run_interactive_with_comment() {
495 run(
496 "def main [\n"
497 // 2 instructions, with a comment after the first
498 " 10:text <- new [a:num <- copy 0 # abc\n"
499 "b:num <- copy 0\n"
500 "]\n"
501 " 20:text, 30:text <- run-sandboxed 10:text\n"
502 "]\n"
504 // no errors
505 // skip alloc id
506 CHECK_TRACE_CONTENTS(
507 "mem: storing 0 in location 30\n"
508 "mem: storing 0 in location 31\n"
512 :(after "Running One Instruction")
513 if (Track_most_recent_products && SIZE(Current_routine->calls) == Call_depth_to_track_most_recent_products_at
514 && !current_instruction().is_label
515 && current_instruction().name != "$stop-tracking-products") {
516 Most_recent_products = "";
518 :(before "End Running One Instruction")
519 if (Track_most_recent_products && SIZE(Current_routine->calls) == Call_depth_to_track_most_recent_products_at) {
520 Most_recent_products = track_most_recent_products(current_instruction(), products);
521 //? cerr << "most recent products: " << Most_recent_products << '\n';
523 :(code)
524 string track_most_recent_products(const instruction& instruction, const vector<vector<double> >& products) {
525 ostringstream out;
526 for (int i = 0; i < SIZE(products); ++i) {
527 // A sandbox can print a string result, but only if it is actually saved
528 // to a variable in the sandbox, because otherwise the results are
529 // reclaimed before the sandbox sees them. So you get these interactions
530 // in the sandbox:
532 // new [abc]
533 // => <address>
535 // x:text <- new [abc]
536 // => abc
537 if (i < SIZE(instruction.products)) {
538 if (is_mu_text(instruction.products.at(i))) {
539 if (SIZE(products.at(i)) != 2) continue; // weak silent check for address
540 out << read_mu_text(products.at(i).at(/*skip alloc id*/1)) << '\n';
541 continue;
544 for (int j = 0; j < SIZE(products.at(i)); ++j)
545 out << no_scientific(products.at(i).at(j)) << ' ';
546 out << '\n';
548 return out.str();
551 :(code)
552 string strip_comments(string in) {
553 ostringstream result;
554 for (int i = 0; i < SIZE(in); ++i) {
555 if (in.at(i) != '#') {
556 result << in.at(i);
558 else {
559 while (i+1 < SIZE(in) && in.at(i+1) != '\n')
560 ++i;
563 return result.str();
566 int stringified_value_of_location(int address) {
567 // convert to string
568 ostringstream out;
569 out << no_scientific(get_or_insert(Memory, address));
570 return new_mu_text(out.str());
573 int trace_error_contents() {
574 if (!Trace_stream) return 0;
575 ostringstream out;
576 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
577 if (p->label != "error") continue;
578 out << p->contents;
579 if (*--p->contents.end() != '\n') out << '\n';
581 string result = out.str();
582 truncate(result);
583 if (result.empty()) return 0;
584 return new_mu_text(result);
587 int trace_app_contents() {
588 if (!Trace_stream) return 0;
589 ostringstream out;
590 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
591 if (p->depth != App_depth) continue;
592 out << p->contents;
593 if (*--p->contents.end() != '\n') out << '\n';
595 string result = out.str();
596 if (result.empty()) return 0;
597 truncate(result);
598 return new_mu_text(result);
601 void truncate(string& x) {
602 if (SIZE(x) > 1024) {
603 x.erase(1024);
604 *x.rbegin() = '\n';
605 *++x.rbegin() = '.';
606 *++++x.rbegin() = '.';
610 //: simpler version of run-sandboxed: doesn't do any running, just loads
611 //: recipes and reports errors.
613 :(before "End Primitive Recipe Declarations")
614 RELOAD,
615 :(before "End Primitive Recipe Numbers")
616 put(Recipe_ordinal, "reload", RELOAD);
617 :(before "End Primitive Recipe Checks")
618 case RELOAD: {
619 if (SIZE(inst.ingredients) != 1) {
620 raise << maybe(get(Recipe, r).name) << "'reload' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
621 break;
623 if (!is_mu_text(inst.ingredients.at(0))) {
624 raise << maybe(get(Recipe, r).name) << "first ingredient of 'reload' should be a string, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
625 break;
627 break;
629 :(before "End Primitive Recipe Implementations")
630 case RELOAD: {
631 restore_non_recipe_snapshots();
632 string code = read_mu_text(ingredients.at(0).at(/*skip alloc id*/1));
633 run_code_begin(/*should_stash_snapshots*/false);
634 routine* save_current_routine = Current_routine;
635 Current_routine = NULL;
636 Sandbox_mode = true;
637 vector<recipe_ordinal> recipes_reloaded = load(code);
638 transform_all();
639 Trace_stream->newline(); // flush trace
640 Sandbox_mode = false;
641 Current_routine = save_current_routine;
642 products.resize(1);
643 products.at(0).push_back(/*alloc id*/0);
644 products.at(0).push_back(trace_error_contents());
645 run_code_end(); // wait until we're done with the trace contents
646 break;
649 :(code)
650 void test_reload_loads_function_definitions() {
651 run(
652 "def main [\n"
653 " local-scope\n"
654 " x:text <- new [recipe foo [\n"
655 " 1:num/raw <- copy 34\n"
656 " ]]\n"
657 " reload x\n"
658 " run-sandboxed [foo]\n"
659 " 2:num/raw <- copy 1:num/raw\n"
660 "]\n"
662 CHECK_TRACE_CONTENTS(
663 "mem: storing 34 in location 2\n"
667 void test_reload_continues_past_error() {
668 run(
669 "def main [\n"
670 " local-scope\n"
671 " x:text <- new [recipe foo [\n"
672 " get 1234:num, foo:offset\n"
673 " ]]\n"
674 " reload x\n"
675 " 1:num/raw <- copy 34\n"
676 "]\n"
678 CHECK_TRACE_CONTENTS(
679 "mem: storing 34 in location 1\n"
683 void test_reload_can_repeatedly_load_container_definitions() {
684 // define a container and try to create it (merge requires knowing container size)
685 run(
686 "def main [\n"
687 " local-scope\n"
688 " x:text <- new [\n"
689 " container foo [\n"
690 " x:num\n"
691 " y:num\n"
692 " ]\n"
693 " recipe bar [\n"
694 " local-scope\n"
695 " x:foo <- merge 34, 35\n"
696 " ]\n"
697 " ]\n"
698 // save warning addresses in locations of type 'number' to avoid
699 // spurious changes to them due to 'abandon'
700 " 10:text/raw <- reload x\n"
701 " 20:text/raw <- reload x\n"
702 "]\n"
704 // no errors on either load
705 CHECK_TRACE_CONTENTS(
706 "mem: storing 0 in location 10\n"
707 "mem: storing 0 in location 11\n"
708 "mem: storing 0 in location 20\n"
709 "mem: storing 0 in location 21\n"