1 //: Helper for various programming environments: run arbitrary Mu code and
2 //: return some result in text form.
4 void test_run_interactive_code() {
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"
15 "mem: storing 34 in location 20\n"
19 void test_run_interactive_empty() {
22 " 10:text <- copy null\n"
23 " 20:text <- run-sandboxed 10:text\n"
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
);
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")
51 :(before
"End Primitive Recipe Numbers")
52 put(Recipe_ordinal
, "run-sandboxed", RUN_SANDBOXED
);
53 :(before
"End Primitive Recipe Checks")
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();
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();
65 :(before
"End Primitive Recipe Implementations")
67 bool new_code_pushed_to_stack
= run_interactive(ingredients
.at(0).at(/*skip alloc id*/1));
68 if (!new_code_pushed_to_stack
) {
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
80 break; // done with this instruction
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
;
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;
106 // reads a string, tries to call it as code (treating it as a test), saving
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
114 if (!Current_scenario
) {
115 for (int i
= 1; i
< Reserved_for_tests
; ++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") +
129 "screen:&:screen <- next-ingredient\n" +
130 "$start-tracking-products\n" +
132 "$stop-tracking-products\n" +
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,
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")));
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
;
164 void run_code_begin(bool should_stash_snapshots
) {
165 // stuff to undo later, in run_code_end()
167 Disable_redefine_checks
= true;
168 Run_profiler_stash
= Run_profiler
;
169 Run_profiler
= false;
170 if (should_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() {
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("");
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())
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
;
221 void unstash_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")
236 "recipe interactive [\n") + // just a dummy version to initialize the Recipe_ordinal and so on
238 "recipe sandbox [\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" +
260 //: adjust errors in the sandbox
261 :(before
"End maybe(recipe_name) Special-cases")
262 if (recipe_name
== "interactive") return "";
265 void test_run_interactive_comments() {
268 " 1:text <- new [# ab\n"
270 " 2:text <- run-sandboxed 1:text\n"
271 " 3:@:char <- copy *2:text\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
: {
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
);
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
: {
302 :(before
"End Primitive Recipe Implementations")
303 case _STOP_TRACKING_PRODUCTS
: {
304 Track_most_recent_products
= false;
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
: {
316 :(before
"End Primitive Recipe Implementations")
317 case _MOST_RECENT_PRODUCTS
: {
319 products
.at(0).push_back(/*alloc id*/0);
320 products
.at(0).push_back(new_mu_text(Most_recent_products
));
324 :(before
"End Primitive Recipe Declarations")
326 :(before
"End Primitive Recipe Numbers")
327 put(Recipe_ordinal
, "save-errors", SAVE_ERRORS
);
328 :(before
"End Primitive Recipe Checks")
332 :(before
"End Primitive Recipe Implementations")
335 products
.at(0).push_back(/*alloc id*/0);
336 products
.at(0).push_back(trace_error_contents());
340 :(before
"End Primitive Recipe Declarations")
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
: {
348 :(before
"End Primitive Recipe Implementations")
349 case SAVE_APP_TRACE
: {
351 products
.at(0).push_back(/*alloc id*/0);
352 products
.at(0).push_back(trace_app_contents());
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
: {
364 :(before
"End Primitive Recipe Implementations")
365 case _CLEANUP_RUN_SANDBOXED
: {
371 void test_run_interactive_converts_result_to_text() {
372 // try to interactively add 2 and 2
375 " 10:text <- new [add 2, 2]\n"
376 " 20:text <- run-sandboxed 10:text\n"
377 " 30:@:char <- copy *20:text\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() {
389 " 10:text <- new [foo]\n"
390 " 20:text <- run-sandboxed 10:text\n"
391 " 30:@:char <- copy *20:text\n"
394 " 40:num <- copy 1234\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() {
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"
417 " 40:num <- copy 1234\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() {
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"
440 " 40:num <- copy 1234\n"
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
458 " x:text <- new [a]\n"
459 " y:text <- new [b]\n"
460 " z:text <- append x:text, y:text\n"
462 " 10:text <- run-sandboxed 1:text\n"
463 //? " $print 10:text 10/newline\n"
464 " 20:@:char <- copy *10:text\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() {
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"
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"
494 void test_run_interactive_with_comment() {
497 // 2 instructions, with a comment after the first
498 " 10:text <- new [a:num <- copy 0 # abc\n"
501 " 20:text, 30:text <- run-sandboxed 10:text\n"
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';
524 string
track_most_recent_products(const instruction
& instruction
, const vector
<vector
<double> >& products
) {
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
535 // x:text <- new [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';
544 for (int j
= 0; j
< SIZE(products
.at(i
)); ++j
)
545 out
<< no_scientific(products
.at(i
).at(j
)) << ' ';
552 string
strip_comments(string in
) {
553 ostringstream result
;
554 for (int i
= 0; i
< SIZE(in
); ++i
) {
555 if (in
.at(i
) != '#') {
559 while (i
+1 < SIZE(in
) && in
.at(i
+1) != '\n')
566 int stringified_value_of_location(int address
) {
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;
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;
579 if (*--p
->contents
.end() != '\n') out
<< '\n';
581 string result
= out
.str();
583 if (result
.empty()) return 0;
584 return new_mu_text(result
);
587 int trace_app_contents() {
588 if (!Trace_stream
) return 0;
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;
593 if (*--p
->contents
.end() != '\n') out
<< '\n';
595 string result
= out
.str();
596 if (result
.empty()) return 0;
598 return new_mu_text(result
);
601 void truncate(string
& x
) {
602 if (SIZE(x
) > 1024) {
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")
615 :(before
"End Primitive Recipe Numbers")
616 put(Recipe_ordinal
, "reload", RELOAD
);
617 :(before
"End Primitive Recipe Checks")
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();
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();
629 :(before
"End Primitive Recipe Implementations")
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
;
637 vector
<recipe_ordinal
> recipes_reloaded
= load(code
);
639 Trace_stream
->newline(); // flush trace
640 Sandbox_mode
= false;
641 Current_routine
= save_current_routine
;
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
650 void test_reload_loads_function_definitions() {
654 " x:text <- new [recipe foo [\n"
655 " 1:num/raw <- copy 34\n"
658 " run-sandboxed [foo]\n"
659 " 2:num/raw <- copy 1:num/raw\n"
662 CHECK_TRACE_CONTENTS(
663 "mem: storing 34 in location 2\n"
667 void test_reload_continues_past_error() {
671 " x:text <- new [recipe foo [\n"
672 " get 1234:num, foo:offset\n"
675 " 1:num/raw <- copy 34\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)
695 " x:foo <- merge 34, 35\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"
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"