module scheme import arena import reader // These operations allocate before publishing their result or mutating an // existing object. A denied allocation follows the ordinary typed-error path, // restoring the active stack and managed resource scopes. struct BoundedDocument { mut: document reader.Document error ?IError allocation ?arena.AllocationFailure } fn read_bounded_source(vm &VM, input SourceInput) !reader.Document { mut out := BoundedDocument{} read_unpublished_source(vm, input, mut out) if kind := out.allocation { return bounded_allocation_request(kind) } if err := out.error { return err } return out.document } fn read_unpublished_source(vm &VM, input SourceInput, mut out BoundedDocument) { defer { if message := recover() { out.allocation = arena.allocation_failure() or { panic(message) } } } out.document = input.read(vm) or { out.error = err return } } fn bounded_allocation_request(kind arena.AllocationFailure) IError { if kind == .chunk_limit { return &vm_chunk_failure } return &vm_backing_failure } fn bounded_argument_list(items []Value) !Value { if items.len == 0 { return nil_value() } if items.len > max_int / int(sizeof(Pair)) { return &vm_backing_failure } mut result := BoundedResult{} build_unpublished_argument_list(items, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } return result.value } // Reserve the whole spine, as for bounded make-list. Individual cons calls // could consume the remaining budget before denying a later pair. The stack // view is borrowed only here; each pair retains its argument value directly. // These cells need no array ownership header or per-element initialization. fn build_unpublished_argument_list(items []Value, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } pairs := unsafe { &Pair(arena.current_allocator().calloc(items.len * int(sizeof(Pair)))) } mut tail := nil_value() for i := items.len - 1; i >= 0; i-- { unsafe { mut cell := pairs + i cell.car = items[i] cell.cdr = tail tail = payload_value(.pair, cell) } } result.value = tail } fn bounded_reserve_buffer(mut machine Machine, buffer MachineBuffer, count int) ! { mut result := BoundedResult{} reserve_unpublished_buffer(mut machine, buffer, count, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } } fn bounded_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value) !Value { mut result := BoundedResult{} prepare_unpublished_goops_next(vm, scope, function, next, keywords, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } return result.value } fn prepare_unpublished_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } result.value = new_goops_next(vm, scope, function, next, keywords) } fn reserve_unpublished_buffer(mut machine Machine, buffer MachineBuffer, count int, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } match buffer { .stack { machine.stack.ensure_cap(count) } .frames { machine.frames.ensure_cap(count) } .bridge_sites { machine.bridge_sites.ensure_cap(count) } } } struct BoundedActivation { mut: scope &Environment = unsafe { nil } exhausted ?arena.AllocationFailure } fn bounded_activation(mut machine Machine, function &Function, parent &Environment, tail bool) !&Environment { mut result := BoundedActivation{} prepare_unpublished_activation(mut machine, function, parent, tail, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } return result.scope } // Recycled unescaped environments and spare-list bookkeeping are private to // this machine. A refused allocation abandons it before the callee can run. // Captured environments remain protected by the existing escaped/parent rules. fn prepare_unpublished_activation(mut machine Machine, function &Function, parent &Environment, tail bool, mut result BoundedActivation) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } result.scope = machine.activation_for(function, parent, tail) } struct BoundedCompilation { mut: function &Function = unsafe { nil } error ?IError exhausted ?arena.AllocationFailure } fn bounded_build_program(vm &VM, doc reader.Document, context u64, space &Namespace, path string, discard bool) !&Function { mut result := BoundedCompilation{} build_unpublished_program(vm, doc, context, space, path, discard, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } return result.function } fn build_unpublished_program(vm &VM, doc reader.Document, context u64, space &Namespace, path string, discard bool, mut result BoundedCompilation) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } result.function = source_program(vm.new_code(doc, context, space, path), doc.forms, discard) } fn bounded_compile_form(mut vm VM, mut machine Machine, source reader.NodeId) !&Function { mut result := BoundedCompilation{} compile_unpublished_form(mut vm, mut machine, source, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } if err := result.error { return err } return result.function } fn bounded_compile_value(mut vm VM, expression Value, environment Value, path string) !&Function { mut result := BoundedCompilation{} compile_unpublished_value(mut vm, expression, environment, path, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } if err := result.error { return err } return result.function } // Datum conversion writes only this new Code and its generated syntax. Neither // the input graph nor its environment changes before the source driver returns. fn compile_unpublished_value(mut vm VM, expression Value, environment Value, path string, mut result BoundedCompilation) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } result.function = vm.compile_value_inner(expression, environment, path) or { result.error = err return } } // Compiler scratch unwinds before this boundary. Shared literal entries contain // complete values, and declaration locations change only when definitions run. // A nested native callback still latches interruption in its own VM entry. fn compile_unpublished_form(mut vm VM, mut machine Machine, source reader.NodeId, mut result BoundedCompilation) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } result.function = machine.compile_source_form_inner(mut vm, source) or { result.error = err return } } fn bounded_enter_program(mut vm VM, mut machine Machine) !ActiveEntry { mut entry := ActiveEntry{} mut result := BoundedResult{} prepare_bounded_program(mut vm, mut machine, mut entry, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } return entry } // Build the activation before publishing its machine in the active registry. // A denied entry leaves any enclosing evaluation and its parameters in place. fn prepare_bounded_program(mut vm VM, mut machine Machine, mut entry ActiveEntry, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } machine.env = machine.activation_for(machine.function, unsafe { nil }, false) entry = vm.enter_context(&machine) } fn bounded_enter_call(mut vm VM, mut m Machine, procedure Value, args []Value) !ActiveEntry { if args.len >= max_int / int(sizeof(Value)) { return failure('too many procedure arguments') } mut entry := ActiveEntry{} mut result := BoundedResult{} prepare_bounded_call(mut vm, mut m, procedure, args, mut entry, mut result) if kind := result.exhausted { return bounded_allocation_request(kind) } return entry } // Prepare the argument stack before publishing the new active machine. Nested // hosts can catch a denied entry and continue executing in the enclosing VM. fn prepare_bounded_call(mut vm VM, mut m Machine, procedure Value, args []Value, mut entry ActiveEntry, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } if args.len > 1 && m.stack.cap < args.len + 1 { // Keep the old warm buffer until the replacement allocation succeeds. mut grown := []Value{cap: args.len + 1} unsafe { m.stack.free() } m.stack = unsafe { grown } } entry = vm.enter_host_call(mut m, procedure, args) } fn bounded_primitive(p Primitive, args []Value, name string) !Value { // These builtins operate on their arguments. Mutators validate before // storing and build replacement storage before publishing it. mut out := BoundedResult{} build_unpublished_value(p, args, name, mut out) if kind := out.exhausted { return bounded_allocation_request(kind) } if err := out.error { return err } return out.value } fn build_unpublished_value(p Primitive, args []Value, name string, mut out BoundedResult) { defer { if message := recover() { out.exhausted = arena.allocation_failure() or { panic(message) } } } if args.len == 2 && int(p) <= int(Primitive.greater_equal) && args[0].tag == .integer && args[1].tag == .integer { out.value = binary_integer(p, args[0], args[1]) or { out.error = err return } return } if p == .make_list { out.value = bounded_make_list(args, name) or { out.error = err return } return } out.value = primitive_call(p, args, name) or { out.error = err return } } // Reserve the complete spine before linking any pairs. A large denied // make-list then leaves room in the VM for its next evaluation. fn bounded_make_list(args []Value, name string) !Value { check_arity(name, args.len, 1, 2)! count := allocation_count(args[0], int(sizeof(Pair)))! if count == 0 { return nil_value() } fill := if args.len == 2 { args[1] } else { unspecified() } mut pairs := []Pair{len: count} mut tail := nil_value() for i := count - 1; i >= 0; i-- { unsafe { mut cell := &pairs[i] cell.car = fill cell.cdr = tail tail = payload_value(.pair, cell) } } return tail } struct BoundedBindingWrite { mut: syntax PreparedSyntaxLocation exhausted ?arena.AllocationFailure } // V3's map insertion publishes the dense key and its sparse metadata only // after allocations succeed. Stage one global value and undo its append if // key cloning or map growth fails. Existing indices are stable across growth. fn bounded_publish_binding(mut vm VM, space &Namespace, name string, value Value) !int { before := vm.globals.len mut result := BoundedBindingWrite{} slot := store_bounded_binding(mut vm, space, name, value, before, mut result) if kind := result.exhausted { if vm.globals.len > before { container_store_validated(mut vm.globals, before, Value{}) unsafe { vm.globals.len = before } } if result.syntax.created { // This was the last insertion in the location table. Removing its // dense tail neither allocates a tombstone nor moves another key. if isnil(space) { mut owner := vm.owner owner.syntax_locations.delete(name) } else { mut target := unsafe { &Namespace(space) } target.syntax_locations.delete(name) } } return bounded_allocation_request(kind) } if !isnil(result.syntax.location) { vm.commit_syntax_location(space, result.syntax, .global, slot) } return slot } fn store_bounded_binding(mut vm VM, space &Namespace, name string, value Value, slot int, mut out BoundedBindingWrite) int { defer { if message := recover() { out.exhausted = arena.allocation_failure() or { panic(message) } } } if vm.owner.syntax_tracking { out.syntax = vm.prepare_syntax_location(space, name, name) } mut bindings := if isnil(space) { vm.bindings } else { unsafe { &space.bindings } } if index := (*bindings)[name] { vm.replace_global(index, value) return index } vm.globals << value (*bindings)[name] = slot vm.binding_epoch++ return slot } fn bounded_binding_name(name string, options reader.ReadOptions) !string { mut result := BoundedResult{} key := read_unpublished_binding_name(name, options, mut result) if reason := result.exhausted { return bounded_allocation_request(reason) } if err := result.error { return err } return key } fn read_unpublished_binding_name(name string, options reader.ReadOptions, mut result BoundedResult) string { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } return binding_name(name, options) or { result.error = err return '' } }