| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | enum CompilerScratchAttempt { |
| 7 | quasiquote_constructor |
| 8 | quasiquote_jobs |
| 9 | quasiquote_marks |
| 10 | datum_constructor |
| 11 | datum_jobs |
| 12 | template_analysis |
| 13 | template_indices |
| 14 | template_emission |
| 15 | value_reference |
| 16 | value_symbol |
| 17 | value_conversion |
| 18 | capture_spelling |
| 19 | input_origin_keep |
| 20 | hydration_jobs |
| 21 | marked_lookup |
| 22 | } |
| 23 | |
| 24 | struct CompilerScratchFixture { |
| 25 | storage arena.Arena |
| 26 | wrap &SyntaxWrap |
| 27 | mut: |
| 28 | quasiquote QQGraphScratch |
| 29 | datum DatumGraphJobs |
| 30 | render SyntaxCaseRenderState |
| 31 | source SyntaxValueSource |
| 32 | analysis []i64 |
| 33 | origins SyntaxInputOrigins |
| 34 | hydrator SyntaxHydrator |
| 35 | context &SyntaxLookupContext |
| 36 | } |
| 37 | |
| 38 | fn attempt_compiler_scratch(which CompilerScratchAttempt, mut fixture CompilerScratchFixture, mut result BoundedResult) { |
| 39 | defer { |
| 40 | if message := recover() { |
| 41 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 42 | } |
| 43 | } |
| 44 | match which { |
| 45 | .quasiquote_constructor { |
| 46 | mut scratch := new_qq_graph_scratch() |
| 47 | scratch.release() |
| 48 | } |
| 49 | .quasiquote_jobs { fixture.quasiquote.add(reader.NodeId(0), 0, false) } |
| 50 | .quasiquote_marks { fixture.quasiquote.mark(reader.NodeId(0), 0, false) } |
| 51 | .datum_constructor { |
| 52 | mut jobs := new_datum_graph_jobs() |
| 53 | jobs.release() |
| 54 | } |
| 55 | .datum_jobs { fixture.datum.add(reader.NodeId(0), true) } |
| 56 | .template_analysis { template_analysis_push(mut fixture.analysis, fixture.storage, 0) } |
| 57 | .template_indices { fixture.render.push(0) } |
| 58 | .template_emission { fixture.render.acquire_emission(unsafe { nil }) } |
| 59 | .value_reference { fixture.source.reference(integer(42)) or { panic(err) } } |
| 60 | .value_symbol { fixture.source.symbol('answer') or { panic(err) } } |
| 61 | .value_conversion { map_syntax_value_graph(integer(42), false, unsafe { nil }, unsafe { nil }, unsafe { nil }) or { panic(err) } } |
| 62 | .capture_spelling { syntax_capture_spelling(fixture.wrap, fixture.storage, true) } |
| 63 | .input_origin_keep { fixture.origins.keep(0, reader.NodeId(0)) } |
| 64 | .hydration_jobs { fixture.hydrator.push(SyntaxHydrationJob{reader.NodeId(0), reader.NodeId(0)}) } |
| 65 | .marked_lookup { fixture.context.binding_with_marks('answer', fixture.wrap) } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn check_compiler_scratch_failure(which CompilerScratchAttempt) { |
| 70 | limits := if which in [.quasiquote_constructor, .datum_constructor, .value_conversion, .marked_lookup] { |
| 71 | arena.ChunkLimits{ max_live_bytes: 0 } |
| 72 | } else { |
| 73 | arena.ChunkLimits{} |
| 74 | } |
| 75 | mut owner := arena.new(limits: limits) |
| 76 | defer { owner.free() } |
| 77 | mut storage := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) |
| 78 | defer { storage.free() } |
| 79 | mut fixture := CompilerScratchFixture{ |
| 80 | storage: storage |
| 81 | wrap: &SyntaxWrap{ domain: unsafe { nil }, mark: 1 } |
| 82 | quasiquote: QQGraphScratch{ storage: storage } |
| 83 | datum: DatumGraphJobs{ storage: storage } |
| 84 | render: SyntaxCaseRenderState{ storage: storage } |
| 85 | source: SyntaxValueSource{ root: nil_value(), storage: storage } |
| 86 | origins: SyntaxInputOrigins{ storage: storage } |
| 87 | hydrator: SyntaxHydrator{ |
| 88 | source: unsafe { nil } |
| 89 | input: unsafe { nil } |
| 90 | origins: unsafe { nil } |
| 91 | storage: storage |
| 92 | marks: SyntaxExpansion{ domain: unsafe { nil } } |
| 93 | } |
| 94 | context: &SyntaxLookupContext{} |
| 95 | } |
| 96 | mut result := BoundedResult{} |
| 97 | owner.push() |
| 98 | attempt_compiler_scratch(which, mut fixture, mut result) |
| 99 | restored := owner.is_current() |
| 100 | // Clean up the old implementation's selected scratch arena before reporting |
| 101 | // a failed assertion. Caller-owned storage still has its ordinary defer. |
| 102 | if !restored { |
| 103 | borrowed := storage.is_current() |
| 104 | child := v_arena_current() |
| 105 | unsafe { |
| 106 | v_arena_pop(child) |
| 107 | if !borrowed { v_arena_free(child) } |
| 108 | } |
| 109 | } |
| 110 | owner.pop() |
| 111 | assert restored, '${which}' |
| 112 | assert (result.exhausted or { panic('expected compiler scratch denial') }) == .chunk_limit |
| 113 | } |
| 114 | |
| 115 | fn test_denied_quasiquote_scratch_construction_restores_allocator() { |
| 116 | check_compiler_scratch_failure(.quasiquote_constructor) |
| 117 | } |
| 118 | |
| 119 | fn test_denied_quasiquote_worklist_growth_restores_allocator() { |
| 120 | check_compiler_scratch_failure(.quasiquote_jobs) |
| 121 | } |
| 122 | |
| 123 | fn test_denied_quasiquote_mark_insertion_restores_allocator() { |
| 124 | check_compiler_scratch_failure(.quasiquote_marks) |
| 125 | } |
| 126 | |
| 127 | fn test_denied_datum_worklist_construction_restores_allocator() { |
| 128 | check_compiler_scratch_failure(.datum_constructor) |
| 129 | } |
| 130 | |
| 131 | fn test_denied_datum_worklist_growth_restores_allocator() { |
| 132 | check_compiler_scratch_failure(.datum_jobs) |
| 133 | } |
| 134 | |
| 135 | fn test_denied_template_analysis_growth_restores_allocator() { |
| 136 | check_compiler_scratch_failure(.template_analysis) |
| 137 | } |
| 138 | |
| 139 | fn test_denied_template_render_index_growth_restores_allocator() { |
| 140 | check_compiler_scratch_failure(.template_indices) |
| 141 | } |
| 142 | |
| 143 | fn test_denied_template_emission_creation_restores_allocator() { |
| 144 | check_compiler_scratch_failure(.template_emission) |
| 145 | } |
| 146 | |
| 147 | fn test_denied_syntax_value_reference_growth_restores_allocator() { |
| 148 | check_compiler_scratch_failure(.value_reference) |
| 149 | } |
| 150 | |
| 151 | fn test_denied_syntax_symbol_memo_insertion_restores_allocator() { |
| 152 | check_compiler_scratch_failure(.value_symbol) |
| 153 | } |
| 154 | |
| 155 | fn test_denied_syntax_value_facade_construction_restores_allocator() { |
| 156 | check_compiler_scratch_failure(.value_conversion) |
| 157 | } |
| 158 | |
| 159 | fn test_denied_syntax_mark_spelling_restores_allocator() { |
| 160 | check_compiler_scratch_failure(.capture_spelling) |
| 161 | } |
| 162 | |
| 163 | fn test_denied_procedural_input_origin_insertion_restores_allocator() { |
| 164 | check_compiler_scratch_failure(.input_origin_keep) |
| 165 | } |
| 166 | |
| 167 | fn test_denied_procedural_hydration_worklist_growth_restores_allocator() { |
| 168 | check_compiler_scratch_failure(.hydration_jobs) |
| 169 | } |
| 170 | |
| 171 | fn test_denied_marked_identifier_lookup_restores_allocator() { |
| 172 | check_compiler_scratch_failure(.marked_lookup) |
| 173 | } |
| 174 | |