module scheme import arena struct GoopsCoreOverride { slot int value Value } struct GoopsBootstrapInterrupted { Error } fn (_request &GoopsBootstrapInterrupted) msg() string { return 'GOOPS initialization was interrupted' } const goops_bootstrap_interrupted = GoopsBootstrapInterrupted{} fn (mut vm VM) install_deferred_goops() { vm.goops = &GoopsRuntime{ deferred: true deferred_start: vm.globals.len primitive_setter: vm.global_lookup('setter') or { panic(err) } primitive_abs: vm.global_lookup('abs') or { panic(err) } } for name in goops_binding_names { vm.bind_global(name, Value{ tag: .uninitialized }) } container_store(mut vm.globals, vm.goops.deferred_start, integer(exact_min)) container_store(mut vm.globals, vm.goops.deferred_start + 1, integer(exact_max)) } @[inline] fn (vm &VM) ensure_goops_slot(slot int) { if isnil(vm.goops) || !vm.goops.deferred || vm.goops.starting { return } if slot >= vm.goops.deferred_start && slot < vm.goops.deferred_start + goops_binding_names.len { vm.ensure_goops() } else if vm.globals[slot].tag == .uninitialized { // Only a cold imported placeholder needs the reverse lookup. Ordinary // reads and assignments do not scan the report-location table. for standard, imported in vm.owner.report_slots { if imported == slot && standard >= vm.goops.deferred_start && standard < vm.goops.deferred_start + goops_binding_names.len { vm.ensure_goops() return } } } } // A same-location standard import can remain deferred. A renamed import that // replaces a core/GOOPS location must bootstrap before changing its root name, // just as eager construction ran before user imports. Reimports take the // existing O(1) binding-identity path; reverse lookup is only for a changed alias. fn (mut vm VM) prepare_deferred_root_import(name string, binding SyntaxBinding) { if isnil(vm.goops) || !vm.goops.deferred || vm.goops.starting { return } index := (*vm.bindings)[name] or { return } if same_syntax_binding(vm.namespace_binding(unsafe { nil }, name), binding) { return } mut standard := index if standard >= vm.standard_count { standard = -1 for original, imported in vm.owner.report_slots { if imported == index { standard = original; break } } if standard < 0 { return } } if binding.kind == .global { if report_slot(binding.namespace, binding.global_slot) && voidptr(binding.namespace.report_owner) == voidptr(vm.owner) && -binding.global_slot - 2 == standard { return } if imported := vm.owner.report_slots[standard] { if binding.global_slot == imported { return } } } vm.ensure_goops() } @[inline] fn (vm &VM) read_global(slot int) Value { value := vm.globals[slot] if value.tag == .uninitialized { vm.ensure_goops_slot(slot) return vm.globals[slot] } return value } fn (source &VM) ensure_goops() { if isnil(source.goops) || !source.goops.deferred || source.goops.starting { return } if source.goops.bootstrap_failed { panic(goops_bootstrap_interrupted.msg()) } mut vm := unsafe { source } // First use can occur beneath a borrowed arena or after the VM's arena // was popped. The existing captured allocator owns all persistent metadata. unsafe { vm.goops.allocator.run[VM](mut vm, initialize_deferred_goops) } // Keep an interrupted VM quarantined even if allocator restoration failed. vm.goops.bootstrap_failed = false } fn initialize_deferred_goops(mut vm VM) { vm.goops.starting = true vm.goops.bootstrap_failed = true defer { vm.goops.starting = false mut files := vm.owner.bounded_files // A publication/compiler boundary can catch the allocation panic before // the handle sees it. Bootstrap has already changed persistent metadata. if vm.goops.deferred && !isnil(files) && files.interrupted == none { if reason := arena.allocation_failure() { files.interrupted = reason } } } standard_count := vm.standard_count global_count := vm.globals.len vm.standard_count = 0 defer { vm.standard_count = standard_count } // Self-imported builtin names may already be protected at the root. The // fixed trusted definitions must initialize those locations before the // original import protections resume; they never import user libraries. imports := unsafe { vm.imported } vm.imported = map[string]bool{} defer { unsafe { vm.imported.free() } vm.imported = unsafe { imports } } // Eager construction ran before any user rebindings or control scope. // Reproduce that environment privately; the trusted bootstrap invokes no // user callbacks, then restores every current user value unchanged. mut overrides := []GoopsCoreOverride{cap: vm.standard_original.len} defer { for item in overrides { container_store(mut vm.globals, item.slot, item.value) } unsafe { overrides.free() } } for slot, pristine in vm.standard_original { if slot >= vm.goops.deferred_start { continue } overrides << GoopsCoreOverride{slot, vm.globals[slot]} container_store(mut vm.globals, slot, pristine) } previous_native := vm.native_execution_scope vm.native_execution_scope = unsafe { nil } defer { vm.native_execution_scope = previous_native vm.invalidate_native_bindings() } mut control := &EvaluationControl(unsafe { nil }) if !isnil(vm.system) { control = vm.system.evaluation vm.system.evaluation = unsafe { nil } } defer { if !isnil(vm.system) { vm.system.evaluation = control } } vm.install_goops_bootstrap() vm.install_goops_protocols() // Root names may already point at their immutable report-import aliases. // Fill both canonical locations and any prefixed/renamed report aliases; // do not leave a placeholder behind or duplicate the procedure identity. for offset, name in goops_binding_names { standard := vm.goops.deferred_start + offset value := vm.globals[(*vm.bindings)[name]] container_store(mut vm.globals, standard, value) if imported := vm.owner.report_slots[standard] { container_store(mut vm.globals, imported, value) } } if vm.globals.len != global_count { panic('GOOPS binding catalogue must match bootstrap definitions') } vm.goops.deferred = false }