vile / scheme / goops_lazy.v
167 lines · 156 sloc · 5.96 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5struct GoopsCoreOverride {
6 slot int
7 value Value
8}
9
10struct GoopsBootstrapInterrupted {
11 Error
12}
13
14fn (_request &GoopsBootstrapInterrupted) msg() string {
15 return 'GOOPS initialization was interrupted'
16}
17
18const goops_bootstrap_interrupted = GoopsBootstrapInterrupted{}
19
20fn (mut vm VM) install_deferred_goops() {
21 vm.goops = &GoopsRuntime{
22 deferred: true
23 deferred_start: vm.globals.len
24 primitive_setter: vm.global_lookup('setter') or { panic(err) }
25 primitive_abs: vm.global_lookup('abs') or { panic(err) }
26 }
27 for name in goops_binding_names {
28 vm.bind_global(name, Value{ tag: .uninitialized })
29 }
30 container_store(mut vm.globals, vm.goops.deferred_start, integer(exact_min))
31 container_store(mut vm.globals, vm.goops.deferred_start + 1, integer(exact_max))
32}
33
34@[inline]
35fn (vm &VM) ensure_goops_slot(slot int) {
36 if isnil(vm.goops) || !vm.goops.deferred || vm.goops.starting { return }
37 if slot >= vm.goops.deferred_start && slot < vm.goops.deferred_start + goops_binding_names.len {
38 vm.ensure_goops()
39 } else if vm.globals[slot].tag == .uninitialized {
40 // Only a cold imported placeholder needs the reverse lookup. Ordinary
41 // reads and assignments do not scan the report-location table.
42 for standard, imported in vm.owner.report_slots {
43 if imported == slot && standard >= vm.goops.deferred_start
44 && standard < vm.goops.deferred_start + goops_binding_names.len {
45 vm.ensure_goops()
46 return
47 }
48 }
49 }
50}
51
52// A same-location standard import can remain deferred. A renamed import that
53// replaces a core/GOOPS location must bootstrap before changing its root name,
54// just as eager construction ran before user imports. Reimports take the
55// existing O(1) binding-identity path; reverse lookup is only for a changed alias.
56fn (mut vm VM) prepare_deferred_root_import(name string, binding SyntaxBinding) {
57 if isnil(vm.goops) || !vm.goops.deferred || vm.goops.starting { return }
58 index := (*vm.bindings)[name] or { return }
59 if same_syntax_binding(vm.namespace_binding(unsafe { nil }, name), binding) { return }
60 mut standard := index
61 if standard >= vm.standard_count {
62 standard = -1
63 for original, imported in vm.owner.report_slots {
64 if imported == index { standard = original; break }
65 }
66 if standard < 0 { return }
67 }
68 if binding.kind == .global {
69 if report_slot(binding.namespace, binding.global_slot)
70 && voidptr(binding.namespace.report_owner) == voidptr(vm.owner)
71 && -binding.global_slot - 2 == standard { return }
72 if imported := vm.owner.report_slots[standard] {
73 if binding.global_slot == imported { return }
74 }
75 }
76 vm.ensure_goops()
77}
78
79@[inline]
80fn (vm &VM) read_global(slot int) Value {
81 value := vm.globals[slot]
82 if value.tag == .uninitialized {
83 vm.ensure_goops_slot(slot)
84 return vm.globals[slot]
85 }
86 return value
87}
88
89fn (source &VM) ensure_goops() {
90 if isnil(source.goops) || !source.goops.deferred || source.goops.starting { return }
91 if source.goops.bootstrap_failed { panic(goops_bootstrap_interrupted.msg()) }
92 mut vm := unsafe { source }
93 // First use can occur beneath a borrowed arena or after the VM's arena
94 // was popped. The existing captured allocator owns all persistent metadata.
95 unsafe { vm.goops.allocator.run[VM](mut vm, initialize_deferred_goops) }
96 // Keep an interrupted VM quarantined even if allocator restoration failed.
97 vm.goops.bootstrap_failed = false
98}
99
100fn initialize_deferred_goops(mut vm VM) {
101 vm.goops.starting = true
102 vm.goops.bootstrap_failed = true
103 defer {
104 vm.goops.starting = false
105 mut files := vm.owner.bounded_files
106 // A publication/compiler boundary can catch the allocation panic before
107 // the handle sees it. Bootstrap has already changed persistent metadata.
108 if vm.goops.deferred && !isnil(files) && files.interrupted == none {
109 if reason := arena.allocation_failure() { files.interrupted = reason }
110 }
111 }
112 standard_count := vm.standard_count
113 global_count := vm.globals.len
114 vm.standard_count = 0
115 defer { vm.standard_count = standard_count }
116 // Self-imported builtin names may already be protected at the root. The
117 // fixed trusted definitions must initialize those locations before the
118 // original import protections resume; they never import user libraries.
119 imports := unsafe { vm.imported }
120 vm.imported = map[string]bool{}
121 defer {
122 unsafe { vm.imported.free() }
123 vm.imported = unsafe { imports }
124 }
125 // Eager construction ran before any user rebindings or control scope.
126 // Reproduce that environment privately; the trusted bootstrap invokes no
127 // user callbacks, then restores every current user value unchanged.
128 mut overrides := []GoopsCoreOverride{cap: vm.standard_original.len}
129 defer {
130 for item in overrides { container_store(mut vm.globals, item.slot, item.value) }
131 unsafe { overrides.free() }
132 }
133 for slot, pristine in vm.standard_original {
134 if slot >= vm.goops.deferred_start { continue }
135 overrides << GoopsCoreOverride{slot, vm.globals[slot]}
136 container_store(mut vm.globals, slot, pristine)
137 }
138 previous_native := vm.native_execution_scope
139 vm.native_execution_scope = unsafe { nil }
140 defer {
141 vm.native_execution_scope = previous_native
142 vm.invalidate_native_bindings()
143 }
144 mut control := &EvaluationControl(unsafe { nil })
145 if !isnil(vm.system) {
146 control = vm.system.evaluation
147 vm.system.evaluation = unsafe { nil }
148 }
149 defer { if !isnil(vm.system) { vm.system.evaluation = control } }
150 vm.install_goops_bootstrap()
151 vm.install_goops_protocols()
152 // Root names may already point at their immutable report-import aliases.
153 // Fill both canonical locations and any prefixed/renamed report aliases;
154 // do not leave a placeholder behind or duplicate the procedure identity.
155 for offset, name in goops_binding_names {
156 standard := vm.goops.deferred_start + offset
157 value := vm.globals[(*vm.bindings)[name]]
158 container_store(mut vm.globals, standard, value)
159 if imported := vm.owner.report_slots[standard] {
160 container_store(mut vm.globals, imported, value)
161 }
162 }
163 if vm.globals.len != global_count {
164 panic('GOOPS binding catalogue must match bootstrap definitions')
165 }
166 vm.goops.deferred = false
167}
168