From 795f5244abc9b9f26ff621fd1997db427289d2ba Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 6 Nov 2015 11:06:58 -0800 Subject: 2377 - stop using operator[] in map I'm still seeing all sorts of failures in turning on layer 11 of edit/, so I'm backing away and nailing down every culprit I run into. First up: stop accidentally inserting empty objects into maps during lookups. Commands run: $ sed -i 's/\(Recipe_ordinal\|Recipe\|Type_ordinal\|Type\|Memory\)\[\([^]]*\)\] = \(.*\);/put(\1, \2, \3);/' 0[1-9]* $ vi 075scenario_console.cc # manually fix up Memory[Memory[CONSOLE]] $ sed -i 's/\(Memory\)\[\([^]]*\)\]/get_or_insert(\1, \2)/' 0[1-9]* $ sed -i 's/\(Recipe_ordinal\|Type_ordinal\)\[\([^]]*\)\]/get(\1, \2)/' 0[1-9]* $ sed -i 's/\(Recipe\|Type\)\[\([^]]*\)\]/get(\1, \2)/' 0[1-9]* Now mu dies pretty quickly because of all the places I try to lookup a missing value. --- 075scenario_console.cc | 52 ++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) (limited to '075scenario_console.cc') diff --git a/075scenario_console.cc b/075scenario_console.cc index 47cb5f29..4ed1d583 100644 --- a/075scenario_console.cc +++ b/075scenario_console.cc @@ -41,7 +41,7 @@ if (s == "console") return true; :(before "End Primitive Recipe Declarations") ASSUME_CONSOLE, :(before "End Primitive Recipe Numbers") -Recipe_ordinal["assume-console"] = ASSUME_CONSOLE; +put(Recipe_ordinal, "assume-console", ASSUME_CONSOLE); :(before "End Primitive Recipe Checks") case ASSUME_CONSOLE: { break; @@ -57,31 +57,31 @@ case ASSUME_CONSOLE: { long long int size = num_events*size_of_event() + /*space for length*/1; ensure_space(size); long long int event_data_address = Current_routine->alloc; - Memory[event_data_address] = num_events; + put(Memory, event_data_address, num_events); ++Current_routine->alloc; for (long long int i = 0; i < SIZE(r.steps); ++i) { const instruction& curr = r.steps.at(i); if (curr.name == "left-click") { - Memory[Current_routine->alloc] = /*tag for 'touch-event' variant of 'event' exclusive-container*/2; - Memory[Current_routine->alloc+1+/*offset of 'type' in 'mouse-event'*/0] = TB_KEY_MOUSE_LEFT; - Memory[Current_routine->alloc+1+/*offset of 'row' in 'mouse-event'*/1] = to_integer(curr.ingredients.at(0).name); - Memory[Current_routine->alloc+1+/*offset of 'column' in 'mouse-event'*/2] = to_integer(curr.ingredients.at(1).name); + put(Memory, Current_routine->alloc, /*tag for 'touch-event' variant of 'event' exclusive-container*/2); + put(Memory, Current_routine->alloc+1+/*offset of 'type' in 'mouse-event'*/0, TB_KEY_MOUSE_LEFT); + put(Memory, Current_routine->alloc+1+/*offset of 'row' in 'mouse-event'*/1, to_integer(curr.ingredients.at(0).name)); + put(Memory, Current_routine->alloc+1+/*offset of 'column' in 'mouse-event'*/2, to_integer(curr.ingredients.at(1).name)); Current_routine->alloc += size_of_event(); } else if (curr.name == "press") { string key = curr.ingredients.at(0).name; if (is_integer(key)) - Memory[Current_routine->alloc+1] = to_integer(key); + put(Memory, Current_routine->alloc+1, to_integer(key)); else if (Key.find(key) != Key.end()) - Memory[Current_routine->alloc+1] = Key[key]; + put(Memory, Current_routine->alloc+1, Key[key]); else raise_error << "assume-console: can't press " << key << '\n' << end(); - if (Memory[Current_routine->alloc+1] < 256) + if (get_or_insert(Memory, Current_routine->alloc+1) < 256) // these keys are in ascii - Memory[Current_routine->alloc] = /*tag for 'text' variant of 'event' exclusive-container*/0; + put(Memory, Current_routine->alloc, /*tag for 'text' variant of 'event' exclusive-container*/0); else { // distinguish from unicode - Memory[Current_routine->alloc] = /*tag for 'keycode' variant of 'event' exclusive-container*/1; + put(Memory, Current_routine->alloc, /*tag for 'keycode' variant of 'event' exclusive-container*/1); } Current_routine->alloc += size_of_event(); } @@ -94,11 +94,11 @@ case ASSUME_CONSOLE: { long long int num_keyboard_events = unicode_length(contents); long long int curr = 0; for (long long int i = 0; i < num_keyboard_events; ++i) { - Memory[Current_routine->alloc] = /*tag for 'text' variant of 'event' exclusive-container*/0; + put(Memory, Current_routine->alloc, /*tag for 'text' variant of 'event' exclusive-container*/0); uint32_t curr_character; assert(curr < SIZE(contents)); tb_utf8_char_to_unicode(&curr_character, &raw_contents[curr]); - Memory[Current_routine->alloc+/*skip exclusive container tag*/1] = curr_character; + put(Memory, Current_routine->alloc+/*skip exclusive container tag*/1, curr_character); curr += tb_utf8_char_length(raw_contents[curr]); Current_routine->alloc += size_of_event(); } @@ -107,9 +107,10 @@ case ASSUME_CONSOLE: { assert(Current_routine->alloc == event_data_address+size); // wrap the array of events in a console object ensure_space(size_of_console()); - Memory[CONSOLE] = Current_routine->alloc; + put(Memory, CONSOLE, Current_routine->alloc); Current_routine->alloc += size_of_console(); - Memory[Memory[CONSOLE]+/*offset of 'data' in container 'events'*/1] = event_data_address; + long long int console_address = get_or_insert(Memory, CONSOLE); + put(Memory, console_address+/*offset of 'data' in container 'events'*/1, event_data_address); break; } @@ -227,7 +228,7 @@ scenario events-in-scenario [ :(before "End Primitive Recipe Declarations") REPLACE_IN_CONSOLE, :(before "End Primitive Recipe Numbers") -Recipe_ordinal["replace-in-console"] = REPLACE_IN_CONSOLE; +put(Recipe_ordinal, "replace-in-console", REPLACE_IN_CONSOLE); :(before "End Primitive Recipe Checks") case REPLACE_IN_CONSOLE: { break; @@ -235,17 +236,18 @@ case REPLACE_IN_CONSOLE: { :(before "End Primitive Recipe Implementations") case REPLACE_IN_CONSOLE: { assert(scalar(ingredients.at(0))); - if (!Memory[CONSOLE]) { + if (!get_or_insert(Memory, CONSOLE)) { raise_error << "console not initialized\n" << end(); break; } - long long int console_data = Memory[Memory[CONSOLE]+1]; - long long int size = Memory[console_data]; // array size + long long int console_address = get_or_insert(Memory, CONSOLE); + long long int console_data = get_or_insert(Memory, console_address+1); + long long int size = get_or_insert(Memory, console_data); // array size for (long long int i = 0, curr = console_data+1; i < size; ++i, curr+=size_of_event()) { - if (Memory[curr] != /*text*/0) continue; - if (Memory[curr+1] != ingredients.at(0).at(0)) continue; + if (get_or_insert(Memory, curr) != /*text*/0) continue; + if (get_or_insert(Memory, curr+1) != ingredients.at(0).at(0)) continue; for (long long int n = 0; n < size_of_event(); ++n) - Memory[curr+n] = ingredients.at(1).at(n); + put(Memory, curr+n, ingredients.at(1).at(n)); } break; } @@ -267,7 +269,7 @@ long long int size_of_event() { // memoize result if already computed static long long int result = 0; if (result) return result; - type_tree* type = new type_tree(Type_ordinal["event"]); + type_tree* type = new type_tree(get(Type_ordinal, "event")); result = size_of(type); delete type; return result; @@ -277,8 +279,8 @@ long long int size_of_console() { // memoize result if already computed static long long int result = 0; if (result) return result; - assert(Type_ordinal["console"]); - type_tree* type = new type_tree(Type_ordinal["console"]); + assert(get(Type_ordinal, "console")); + type_tree* type = new type_tree(get(Type_ordinal, "console")); result = size_of(type); delete type; return result; -- cgit 1.4.1-2-gfad0