| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | // These operations allocate before publishing their result or mutating an |
| 7 | // existing object. A denied allocation follows the ordinary typed-error path, |
| 8 | // restoring the active stack and managed resource scopes. |
| 9 | struct BoundedDocument { |
| 10 | mut: |
| 11 | document reader.Document |
| 12 | error ?IError |
| 13 | allocation ?arena.AllocationFailure |
| 14 | } |
| 15 | |
| 16 | fn read_bounded_source(vm &VM, input SourceInput) !reader.Document { |
| 17 | mut out := BoundedDocument{} |
| 18 | read_unpublished_source(vm, input, mut out) |
| 19 | if kind := out.allocation { return bounded_allocation_request(kind) } |
| 20 | if err := out.error { return err } |
| 21 | return out.document |
| 22 | } |
| 23 | |
| 24 | fn read_unpublished_source(vm &VM, input SourceInput, mut out BoundedDocument) { |
| 25 | defer { |
| 26 | if message := recover() { |
| 27 | out.allocation = arena.allocation_failure() or { panic(message) } |
| 28 | } |
| 29 | } |
| 30 | out.document = input.read(vm) or { |
| 31 | out.error = err |
| 32 | return |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn bounded_allocation_request(kind arena.AllocationFailure) IError { |
| 37 | if kind == .chunk_limit { return &vm_chunk_failure } |
| 38 | return &vm_backing_failure |
| 39 | } |
| 40 | |
| 41 | fn bounded_argument_list(items []Value) !Value { |
| 42 | if items.len == 0 { return nil_value() } |
| 43 | if items.len > max_int / int(sizeof(Pair)) { return &vm_backing_failure } |
| 44 | mut result := BoundedResult{} |
| 45 | build_unpublished_argument_list(items, mut result) |
| 46 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 47 | return result.value |
| 48 | } |
| 49 | |
| 50 | // Reserve the whole spine, as for bounded make-list. Individual cons calls |
| 51 | // could consume the remaining budget before denying a later pair. The stack |
| 52 | // view is borrowed only here; each pair retains its argument value directly. |
| 53 | // These cells need no array ownership header or per-element initialization. |
| 54 | fn build_unpublished_argument_list(items []Value, mut result BoundedResult) { |
| 55 | defer { |
| 56 | if message := recover() { |
| 57 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 58 | } |
| 59 | } |
| 60 | pairs := unsafe { &Pair(arena.current_allocator().calloc(items.len * int(sizeof(Pair)))) } |
| 61 | mut tail := nil_value() |
| 62 | for i := items.len - 1; i >= 0; i-- { |
| 63 | unsafe { |
| 64 | mut cell := pairs + i |
| 65 | cell.car = items[i] |
| 66 | cell.cdr = tail |
| 67 | tail = payload_value(.pair, cell) |
| 68 | } |
| 69 | } |
| 70 | result.value = tail |
| 71 | } |
| 72 | |
| 73 | fn bounded_reserve_buffer(mut machine Machine, buffer MachineBuffer, count int) ! { |
| 74 | mut result := BoundedResult{} |
| 75 | reserve_unpublished_buffer(mut machine, buffer, count, mut result) |
| 76 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 77 | } |
| 78 | |
| 79 | fn bounded_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value) !Value { |
| 80 | mut result := BoundedResult{} |
| 81 | prepare_unpublished_goops_next(vm, scope, function, next, keywords, mut result) |
| 82 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 83 | return result.value |
| 84 | } |
| 85 | |
| 86 | fn prepare_unpublished_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value, mut result BoundedResult) { |
| 87 | defer { |
| 88 | if message := recover() { |
| 89 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 90 | } |
| 91 | } |
| 92 | result.value = new_goops_next(vm, scope, function, next, keywords) |
| 93 | } |
| 94 | |
| 95 | fn reserve_unpublished_buffer(mut machine Machine, buffer MachineBuffer, count int, mut result BoundedResult) { |
| 96 | defer { |
| 97 | if message := recover() { |
| 98 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 99 | } |
| 100 | } |
| 101 | match buffer { |
| 102 | .stack { machine.stack.ensure_cap(count) } |
| 103 | .frames { machine.frames.ensure_cap(count) } |
| 104 | .bridge_sites { machine.bridge_sites.ensure_cap(count) } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | struct BoundedActivation { |
| 109 | mut: |
| 110 | scope &Environment = unsafe { nil } |
| 111 | exhausted ?arena.AllocationFailure |
| 112 | } |
| 113 | |
| 114 | fn bounded_activation(mut machine Machine, function &Function, parent &Environment, tail bool) !&Environment { |
| 115 | mut result := BoundedActivation{} |
| 116 | prepare_unpublished_activation(mut machine, function, parent, tail, mut result) |
| 117 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 118 | return result.scope |
| 119 | } |
| 120 | |
| 121 | // Recycled unescaped environments and spare-list bookkeeping are private to |
| 122 | // this machine. A refused allocation abandons it before the callee can run. |
| 123 | // Captured environments remain protected by the existing escaped/parent rules. |
| 124 | fn prepare_unpublished_activation(mut machine Machine, function &Function, parent &Environment, tail bool, mut result BoundedActivation) { |
| 125 | defer { |
| 126 | if message := recover() { |
| 127 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 128 | } |
| 129 | } |
| 130 | result.scope = machine.activation_for(function, parent, tail) |
| 131 | } |
| 132 | |
| 133 | struct BoundedCompilation { |
| 134 | mut: |
| 135 | function &Function = unsafe { nil } |
| 136 | error ?IError |
| 137 | exhausted ?arena.AllocationFailure |
| 138 | } |
| 139 | |
| 140 | fn bounded_build_program(vm &VM, doc reader.Document, context u64, space &Namespace, path string, discard bool) !&Function { |
| 141 | mut result := BoundedCompilation{} |
| 142 | build_unpublished_program(vm, doc, context, space, path, discard, mut result) |
| 143 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 144 | return result.function |
| 145 | } |
| 146 | |
| 147 | fn build_unpublished_program(vm &VM, doc reader.Document, context u64, space &Namespace, path string, discard bool, mut result BoundedCompilation) { |
| 148 | defer { |
| 149 | if message := recover() { |
| 150 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 151 | } |
| 152 | } |
| 153 | result.function = source_program(vm.new_code(doc, context, space, path), doc.forms, discard) |
| 154 | } |
| 155 | |
| 156 | fn bounded_compile_form(mut vm VM, mut machine Machine, source reader.NodeId) !&Function { |
| 157 | mut result := BoundedCompilation{} |
| 158 | compile_unpublished_form(mut vm, mut machine, source, mut result) |
| 159 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 160 | if err := result.error { return err } |
| 161 | return result.function |
| 162 | } |
| 163 | |
| 164 | fn bounded_compile_value(mut vm VM, expression Value, environment Value, path string) !&Function { |
| 165 | mut result := BoundedCompilation{} |
| 166 | compile_unpublished_value(mut vm, expression, environment, path, mut result) |
| 167 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 168 | if err := result.error { return err } |
| 169 | return result.function |
| 170 | } |
| 171 | |
| 172 | // Datum conversion writes only this new Code and its generated syntax. Neither |
| 173 | // the input graph nor its environment changes before the source driver returns. |
| 174 | fn compile_unpublished_value(mut vm VM, expression Value, environment Value, path string, mut result BoundedCompilation) { |
| 175 | defer { |
| 176 | if message := recover() { |
| 177 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 178 | } |
| 179 | } |
| 180 | result.function = vm.compile_value_inner(expression, environment, path) or { |
| 181 | result.error = err |
| 182 | return |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Compiler scratch unwinds before this boundary. Shared literal entries contain |
| 187 | // complete values, and declaration locations change only when definitions run. |
| 188 | // A nested native callback still latches interruption in its own VM entry. |
| 189 | fn compile_unpublished_form(mut vm VM, mut machine Machine, source reader.NodeId, mut result BoundedCompilation) { |
| 190 | defer { |
| 191 | if message := recover() { |
| 192 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 193 | } |
| 194 | } |
| 195 | result.function = machine.compile_source_form_inner(mut vm, source) or { |
| 196 | result.error = err |
| 197 | return |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fn bounded_enter_program(mut vm VM, mut machine Machine) !ActiveEntry { |
| 202 | mut entry := ActiveEntry{} |
| 203 | mut result := BoundedResult{} |
| 204 | prepare_bounded_program(mut vm, mut machine, mut entry, mut result) |
| 205 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 206 | return entry |
| 207 | } |
| 208 | |
| 209 | // Build the activation before publishing its machine in the active registry. |
| 210 | // A denied entry leaves any enclosing evaluation and its parameters in place. |
| 211 | fn prepare_bounded_program(mut vm VM, mut machine Machine, mut entry ActiveEntry, mut result BoundedResult) { |
| 212 | defer { |
| 213 | if message := recover() { |
| 214 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 215 | } |
| 216 | } |
| 217 | machine.env = machine.activation_for(machine.function, unsafe { nil }, false) |
| 218 | entry = vm.enter_context(&machine) |
| 219 | } |
| 220 | |
| 221 | fn bounded_enter_call(mut vm VM, mut m Machine, procedure Value, args []Value) !ActiveEntry { |
| 222 | if args.len >= max_int / int(sizeof(Value)) { return failure('too many procedure arguments') } |
| 223 | mut entry := ActiveEntry{} |
| 224 | mut result := BoundedResult{} |
| 225 | prepare_bounded_call(mut vm, mut m, procedure, args, mut entry, mut result) |
| 226 | if kind := result.exhausted { return bounded_allocation_request(kind) } |
| 227 | return entry |
| 228 | } |
| 229 | |
| 230 | // Prepare the argument stack before publishing the new active machine. Nested |
| 231 | // hosts can catch a denied entry and continue executing in the enclosing VM. |
| 232 | fn prepare_bounded_call(mut vm VM, mut m Machine, procedure Value, args []Value, mut entry ActiveEntry, mut result BoundedResult) { |
| 233 | defer { |
| 234 | if message := recover() { |
| 235 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 236 | } |
| 237 | } |
| 238 | if args.len > 1 && m.stack.cap < args.len + 1 { |
| 239 | // Keep the old warm buffer until the replacement allocation succeeds. |
| 240 | mut grown := []Value{cap: args.len + 1} |
| 241 | unsafe { m.stack.free() } |
| 242 | m.stack = unsafe { grown } |
| 243 | } |
| 244 | entry = vm.enter_host_call(mut m, procedure, args) |
| 245 | } |
| 246 | |
| 247 | fn bounded_primitive(p Primitive, args []Value, name string) !Value { |
| 248 | // These builtins operate on their arguments. Mutators validate before |
| 249 | // storing and build replacement storage before publishing it. |
| 250 | mut out := BoundedResult{} |
| 251 | build_unpublished_value(p, args, name, mut out) |
| 252 | if kind := out.exhausted { return bounded_allocation_request(kind) } |
| 253 | if err := out.error { return err } |
| 254 | return out.value |
| 255 | } |
| 256 | |
| 257 | fn build_unpublished_value(p Primitive, args []Value, name string, mut out BoundedResult) { |
| 258 | defer { |
| 259 | if message := recover() { |
| 260 | out.exhausted = arena.allocation_failure() or { panic(message) } |
| 261 | } |
| 262 | } |
| 263 | if args.len == 2 && int(p) <= int(Primitive.greater_equal) |
| 264 | && args[0].tag == .integer && args[1].tag == .integer { |
| 265 | out.value = binary_integer(p, args[0], args[1]) or { |
| 266 | out.error = err |
| 267 | return |
| 268 | } |
| 269 | return |
| 270 | } |
| 271 | if p == .make_list { |
| 272 | out.value = bounded_make_list(args, name) or { |
| 273 | out.error = err |
| 274 | return |
| 275 | } |
| 276 | return |
| 277 | } |
| 278 | out.value = primitive_call(p, args, name) or { |
| 279 | out.error = err |
| 280 | return |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Reserve the complete spine before linking any pairs. A large denied |
| 285 | // make-list then leaves room in the VM for its next evaluation. |
| 286 | fn bounded_make_list(args []Value, name string) !Value { |
| 287 | check_arity(name, args.len, 1, 2)! |
| 288 | count := allocation_count(args[0], int(sizeof(Pair)))! |
| 289 | if count == 0 { return nil_value() } |
| 290 | fill := if args.len == 2 { args[1] } else { unspecified() } |
| 291 | mut pairs := []Pair{len: count} |
| 292 | mut tail := nil_value() |
| 293 | for i := count - 1; i >= 0; i-- { |
| 294 | unsafe { |
| 295 | mut cell := &pairs[i] |
| 296 | cell.car = fill |
| 297 | cell.cdr = tail |
| 298 | tail = payload_value(.pair, cell) |
| 299 | } |
| 300 | } |
| 301 | return tail |
| 302 | } |
| 303 | |
| 304 | struct BoundedBindingWrite { |
| 305 | mut: |
| 306 | syntax PreparedSyntaxLocation |
| 307 | exhausted ?arena.AllocationFailure |
| 308 | } |
| 309 | |
| 310 | // V3's map insertion publishes the dense key and its sparse metadata only |
| 311 | // after allocations succeed. Stage one global value and undo its append if |
| 312 | // key cloning or map growth fails. Existing indices are stable across growth. |
| 313 | fn bounded_publish_binding(mut vm VM, space &Namespace, name string, value Value) !int { |
| 314 | before := vm.globals.len |
| 315 | mut result := BoundedBindingWrite{} |
| 316 | slot := store_bounded_binding(mut vm, space, name, value, before, mut result) |
| 317 | if kind := result.exhausted { |
| 318 | if vm.globals.len > before { |
| 319 | container_store_validated(mut vm.globals, before, Value{}) |
| 320 | unsafe { vm.globals.len = before } |
| 321 | } |
| 322 | if result.syntax.created { |
| 323 | // This was the last insertion in the location table. Removing its |
| 324 | // dense tail neither allocates a tombstone nor moves another key. |
| 325 | if isnil(space) { |
| 326 | mut owner := vm.owner |
| 327 | owner.syntax_locations.delete(name) |
| 328 | } else { |
| 329 | mut target := unsafe { &Namespace(space) } |
| 330 | target.syntax_locations.delete(name) |
| 331 | } |
| 332 | } |
| 333 | return bounded_allocation_request(kind) |
| 334 | } |
| 335 | if !isnil(result.syntax.location) { vm.commit_syntax_location(space, result.syntax, .global, slot) } |
| 336 | return slot |
| 337 | } |
| 338 | |
| 339 | fn store_bounded_binding(mut vm VM, space &Namespace, name string, value Value, slot int, mut out BoundedBindingWrite) int { |
| 340 | defer { |
| 341 | if message := recover() { |
| 342 | out.exhausted = arena.allocation_failure() or { panic(message) } |
| 343 | } |
| 344 | } |
| 345 | if vm.owner.syntax_tracking { out.syntax = vm.prepare_syntax_location(space, name, name) } |
| 346 | mut bindings := if isnil(space) { vm.bindings } else { unsafe { &space.bindings } } |
| 347 | if index := (*bindings)[name] { |
| 348 | vm.replace_global(index, value) |
| 349 | return index |
| 350 | } |
| 351 | vm.globals << value |
| 352 | (*bindings)[name] = slot |
| 353 | vm.binding_epoch++ |
| 354 | return slot |
| 355 | } |
| 356 | |
| 357 | fn bounded_binding_name(name string, options reader.ReadOptions) !string { |
| 358 | mut result := BoundedResult{} |
| 359 | key := read_unpublished_binding_name(name, options, mut result) |
| 360 | if reason := result.exhausted { return bounded_allocation_request(reason) } |
| 361 | if err := result.error { return err } |
| 362 | return key |
| 363 | } |
| 364 | |
| 365 | fn read_unpublished_binding_name(name string, options reader.ReadOptions, mut result BoundedResult) string { |
| 366 | defer { |
| 367 | if message := recover() { |
| 368 | result.exhausted = arena.allocation_failure() or { panic(message) } |
| 369 | } |
| 370 | } |
| 371 | return binding_name(name, options) or { |
| 372 | result.error = err |
| 373 | return '' |
| 374 | } |
| 375 | } |
| 376 | |