| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import arena |
| 5 | |
| 6 | // Resume with the hook's original result after migrating a returned method. |
| 7 | const goops_applicability_migration_resume_code = [ |
| 8 | Instruction{ op: .local, b: 6, origin: reader.no_node } |
| 9 | Instruction{ op: .goops_applicability_apply, origin: reader.no_node } |
| 10 | ]! |
| 11 | |
| 12 | const goops_applicability_migration_resume = Function{ |
| 13 | code: unsafe { nil } |
| 14 | params: 7 |
| 15 | slot_count: 7 |
| 16 | instructions: unsafe { static_bridge_instructions(&goops_applicability_migration_resume_code[0], goops_applicability_migration_resume_code.len) } |
| 17 | } |
| 18 | |
| 19 | struct GoopsApplicabilityEntry { |
| 20 | types []&GoopsClass |
| 21 | stamp []Value |
| 22 | procedure Value |
| 23 | epoch u64 |
| 24 | } |
| 25 | |
| 26 | struct GoopsBorrowedApplicability { |
| 27 | generic &GoopsGeneric |
| 28 | entry GoopsApplicabilityEntry |
| 29 | } |
| 30 | |
| 31 | fn (entry GoopsApplicabilityEntry) release() { |
| 32 | mut types := unsafe { entry.types } |
| 33 | mut stamp := unsafe { entry.stamp } |
| 34 | unsafe { |
| 35 | types.free() |
| 36 | stamp.free() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn (mut m Machine) cleanup_goops_applicability_cache() { |
| 41 | for cached in m.goops_applicability_cache { cached.entry.release() } |
| 42 | unsafe { m.goops_applicability_cache.free() } |
| 43 | m.goops_applicability_cache = []GoopsBorrowedApplicability{} |
| 44 | } |
| 45 | |
| 46 | // Like Guile's normal dispatcher, exact <generic> bypasses the public hook. |
| 47 | // Native metadata queries are intrinsic; the public hook can log class-name, |
| 48 | // method-specializers, etc. without recursively dispatching its own queries. |
| 49 | fn (vm &VM) goops_uses_applicability(generic &GoopsGeneric) bool { |
| 50 | return vm.goops.protocols_ready && !generic.intrinsic_applicability |
| 51 | && voidptr(generic.type_class) != voidptr(vm.goops.generic_class) |
| 52 | } |
| 53 | |
| 54 | // Exact generics with ordinary method bodies enter directly. A user factory |
| 55 | // or a specialized method class can have observable construction effects even |
| 56 | // when its method is less specific than the first applicable method. |
| 57 | fn (vm &VM) goops_needs_effective_methods(methods []&GoopsMethod) bool { |
| 58 | for method in methods { |
| 59 | if !isnil(method.role) || (!isnil(method.type_class) && voidptr(method.type_class) != voidptr(vm.goops.method_class)) |
| 60 | || method.make_procedure.is_callable() { |
| 61 | return true |
| 62 | } |
| 63 | } |
| 64 | return false |
| 65 | } |
| 66 | |
| 67 | fn (vm &VM) goops_is_applicability_hook(generic &GoopsGeneric, hook Value) bool { |
| 68 | mut target := hook |
| 69 | for target.tag == .procedure && target.procedure_pointer().mode == .with_setter { |
| 70 | target = target.procedure_pointer().env.slots[0] |
| 71 | } |
| 72 | if target.tag != .procedure { return false } |
| 73 | if target.procedure_pointer().mode !in [.goops_generic, .goops_accessor, .goops_generic_setter, |
| 74 | .goops_make] { |
| 75 | return false |
| 76 | } |
| 77 | data := vm.checked_goops_generic(target) or { return false } |
| 78 | return voidptr(data) == voidptr(generic) |
| 79 | } |
| 80 | |
| 81 | fn goops_applicability_stamp_valid(stamp []Value) bool { |
| 82 | mut index := 0 |
| 83 | for index < stamp.len { |
| 84 | value := stamp[index] |
| 85 | match value.tag { |
| 86 | .goops_generic_data { |
| 87 | if index + 2 >= stamp.len { return false } |
| 88 | generic := value.goops_generic_data_pointer() |
| 89 | if i64(generic.generation) != stamp[index + 1].whole() |
| 90 | || voidptr(generic.type_class) != stamp[index + 2].pointer() { |
| 91 | return false |
| 92 | } |
| 93 | index += 3 |
| 94 | } |
| 95 | .goops_method { |
| 96 | if index + 1 >= stamp.len || i64(value.goops_method_pointer().generation) != stamp[index + 1].whole() { |
| 97 | return false |
| 98 | } |
| 99 | index += 2 |
| 100 | } |
| 101 | .goops_slot { |
| 102 | if index + 1 >= stamp.len || i64(value.goops_slot_pointer().accessor_generation) != stamp[index + 1].whole() { return false } |
| 103 | index += 2 |
| 104 | } |
| 105 | .goops_class { |
| 106 | if index + 2 >= stamp.len { return false } |
| 107 | class := value.goops_class_pointer() |
| 108 | if i64(class.version) != stamp[index + 1].whole() || i64(class.dispatch_generation) != stamp[index + 2].whole() { |
| 109 | return false |
| 110 | } |
| 111 | index += 3 |
| 112 | } |
| 113 | else { return false } |
| 114 | } |
| 115 | } |
| 116 | return true |
| 117 | } |
| 118 | |
| 119 | fn (vm &VM) goops_applicability_types_match(types []&GoopsClass, args []Value) !bool { |
| 120 | if types.len != args.len { return false } |
| 121 | for index, value in args { |
| 122 | if !isnil(types[index]) && voidptr(vm.goops_class_for(value)!) != voidptr(types[index]) { return false } |
| 123 | } |
| 124 | return true |
| 125 | } |
| 126 | |
| 127 | fn (mut m Machine) goops_applicability_view(vm &VM, generic &GoopsGeneric, args []Value) !Value { |
| 128 | for entry in generic.applicability_entries { |
| 129 | if entry.epoch == vm.goops.applicability_epoch |
| 130 | && goops_applicability_stamp_valid(entry.stamp) |
| 131 | && vm.goops_applicability_types_match(entry.types, args)! { |
| 132 | return entry.procedure |
| 133 | } |
| 134 | } |
| 135 | if m.goops_cache_owner == v_arena_current() { |
| 136 | for cached in m.goops_applicability_cache { |
| 137 | entry := cached.entry |
| 138 | if voidptr(cached.generic) == voidptr(generic) |
| 139 | && entry.epoch == vm.goops.applicability_epoch |
| 140 | && goops_applicability_stamp_valid(entry.stamp) |
| 141 | && vm.goops_applicability_types_match(entry.types, args)! { |
| 142 | return entry.procedure |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return Value{} |
| 147 | } |
| 148 | |
| 149 | fn goops_stamp_dependency(dependency &GoopsGeneric, mut stamp []Value) { |
| 150 | stamp << payload_value(.goops_generic_data, dependency) |
| 151 | stamp << integer(i64(dependency.generation)) |
| 152 | stamp << goops_class_value(dependency.type_class) |
| 153 | goops_stamp_class(dependency.type_class, mut stamp) |
| 154 | } |
| 155 | |
| 156 | fn (vm &VM) goops_stamp_generic(generic &GoopsGeneric, mut stamp []Value) !int { |
| 157 | if generic.has_only_local_methods() { |
| 158 | goops_stamp_dependency(generic, mut stamp) |
| 159 | return vm.goops_stamp_methods(generic.methods, mut stamp) |
| 160 | } |
| 161 | collected := vm.goops_collect_methods(generic)! |
| 162 | defer { collected.release() } |
| 163 | for dependency in collected.dependencies { goops_stamp_dependency(dependency, mut stamp) } |
| 164 | return vm.goops_stamp_methods(collected.values, mut stamp) |
| 165 | } |
| 166 | |
| 167 | fn (vm &VM) goops_stamp_methods(methods []&GoopsMethod, mut stamp []Value) int { |
| 168 | mut count := 0 |
| 169 | for method in methods { |
| 170 | stamp << payload_value(.goops_method, method) |
| 171 | stamp << integer(i64(method.generation)) |
| 172 | if !isnil(method.type_class) { goops_stamp_class(method.type_class, mut stamp) } |
| 173 | if method.specializers.len > count { count = method.specializers.len } |
| 174 | // Vile permits a typed rest specializer. Every remaining argument |
| 175 | // then participates in dispatch, even though the public type vector |
| 176 | // describes the fixed specializers only. |
| 177 | if method.rest && !isnil(method.rest_class) && voidptr(method.rest_class) != voidptr(vm.goops.top) { |
| 178 | count = max_int |
| 179 | } |
| 180 | } |
| 181 | return count |
| 182 | } |
| 183 | |
| 184 | fn goops_stamp_class(class &GoopsClass, mut stamp []Value) { |
| 185 | stamp << goops_class_value(class) |
| 186 | stamp << integer(i64(class.version)) |
| 187 | stamp << integer(i64(class.dispatch_generation)) |
| 188 | } |
| 189 | |
| 190 | // These are immutable, private vectors in the bridge's lexical environment. |
| 191 | // A continuation never captures headers belonging to an invalidatable cache. |
| 192 | const goops_applicability_bridge_code = [ |
| 193 | Instruction{ op: .local, b: 2, origin: reader.no_node } |
| 194 | Instruction{ op: .local, b: 0, origin: reader.no_node } |
| 195 | Instruction{ op: .local, b: 1, origin: reader.no_node } |
| 196 | Instruction{ op: .call, a: 2, origin: reader.no_node } |
| 197 | Instruction{ op: .goops_applicability_apply, origin: reader.no_node } |
| 198 | ]! |
| 199 | |
| 200 | const goops_applicability_bridge = Function{ |
| 201 | code: unsafe { nil } |
| 202 | params: 6 |
| 203 | slot_count: 6 |
| 204 | instructions: unsafe { static_bridge_instructions(&goops_applicability_bridge_code[0], goops_applicability_bridge_code.len) } |
| 205 | } |
| 206 | |
| 207 | fn (mut m Machine) prepare_goops_applicability(vm &VM, generic &GoopsGeneric, hook Value, base int) ! { |
| 208 | vm.check_callable_owner(hook)! |
| 209 | args := list(m.arguments(base + 1)) |
| 210 | mut stamp := []Value{} |
| 211 | mut types := []Value{cap: m.stack.len - base - 1} |
| 212 | mut transferred := false |
| 213 | defer { |
| 214 | if !transferred { |
| 215 | unsafe { |
| 216 | stamp.free() |
| 217 | types.free() |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | count := vm.goops_stamp_generic(generic, mut stamp)! |
| 222 | for root in [hook, vm.globals[vm.goops.effective_binding], vm.globals[vm.goops.specialized_binding]]! { |
| 223 | mut target := root |
| 224 | for target.tag == .procedure && target.procedure_pointer().mode == .with_setter { |
| 225 | target = target.procedure_pointer().env.slots[0] |
| 226 | } |
| 227 | if target.tag == .procedure && target.procedure_pointer().mode in [.goops_generic, .goops_accessor, |
| 228 | .goops_generic_setter, .goops_make] { |
| 229 | vm.goops_stamp_generic(vm.checked_goops_generic(target)!, mut stamp)! |
| 230 | } |
| 231 | } |
| 232 | goops_stamp_class(generic.type_class, mut stamp) |
| 233 | goops_stamp_class(vm.goops_class_for(args)!, mut stamp) |
| 234 | for index, value in m.arguments(base + 1) { |
| 235 | if index >= count { |
| 236 | types << boolean(false) |
| 237 | continue |
| 238 | } |
| 239 | class := vm.goops_class_for(value)! |
| 240 | types << goops_class_value(class) |
| 241 | goops_stamp_class(class, mut stamp) |
| 242 | } |
| 243 | stamp_value := payload_value(.vector, &Vector{ items: stamp }) |
| 244 | types_value := payload_value(.vector, &Vector{ items: types }) |
| 245 | transferred = true |
| 246 | m.truncate(base) |
| 247 | m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'applicability hook', function: &goops_applicability_bridge })) |
| 248 | m.push(generic.public_value) |
| 249 | m.push(args) |
| 250 | m.push(hook) |
| 251 | m.push(stamp_value) |
| 252 | m.push(types_value) |
| 253 | m.push(integer(i64(vm.goops.applicability_epoch))) |
| 254 | } |
| 255 | |
| 256 | fn (vm &VM) goops_applicability_retained_by(owner voidptr, stamp []Value) bool { |
| 257 | mut index := 0 |
| 258 | for index < stamp.len { |
| 259 | value := stamp[index] |
| 260 | match value.tag { |
| 261 | .goops_generic_data { |
| 262 | if !vm.goops_retains_owner(owner, value.goops_generic_data_pointer().allocation_owner) { |
| 263 | return false |
| 264 | } |
| 265 | index += 3 |
| 266 | } |
| 267 | .goops_method { |
| 268 | if !vm.goops_retains_owner(owner, value.goops_method_pointer().allocation_owner) { |
| 269 | return false |
| 270 | } |
| 271 | index += 2 |
| 272 | } |
| 273 | .goops_slot { |
| 274 | if !vm.goops_retains_owner(owner, value.goops_slot_pointer().allocation_owner) { return false } |
| 275 | index += 2 |
| 276 | } |
| 277 | .goops_class { |
| 278 | if !vm.goops_retains_owner(owner, value.goops_class_pointer().allocation_owner) { |
| 279 | return false |
| 280 | } |
| 281 | index += 3 |
| 282 | } |
| 283 | else { return false } |
| 284 | } |
| 285 | } |
| 286 | return true |
| 287 | } |
| 288 | |
| 289 | fn goops_same_applicability_types(a []&GoopsClass, b []&GoopsClass) bool { |
| 290 | if a.len != b.len { return false } |
| 291 | for index, class in a { if voidptr(class) != voidptr(b[index]) { return false } } |
| 292 | return true |
| 293 | } |
| 294 | |
| 295 | fn (mut m Machine) publish_goops_applicability(vm &VM, generic &GoopsGeneric, types []Value, stamp []Value, procedure Value) { |
| 296 | current := v_arena_current() |
| 297 | if current != generic.allocation_owner && current != m.goops_cache_owner { return } |
| 298 | if !vm.goops_applicability_retained_by(current, stamp) { return } |
| 299 | if _ := arena.lifetime_token(procedure.pointer()) { |
| 300 | current_owns := !isnil(current) && v_arena_owns(current, procedure.pointer()) |
| 301 | vm_owns := !isnil(vm.allocation_owner) && v_arena_owns(vm.allocation_owner, procedure.pointer()) |
| 302 | if !current_owns && !vm_owns { return } |
| 303 | } |
| 304 | mut classes := []&GoopsClass{cap: types.len} |
| 305 | for value in types { |
| 306 | classes << if value.tag == .goops_class { value.goops_class_pointer() } else { unsafe { &GoopsClass(nil) } } |
| 307 | } |
| 308 | entry := GoopsApplicabilityEntry{ types: classes, stamp: stamp.clone(), procedure: procedure, epoch: vm.goops.applicability_epoch } |
| 309 | if current == generic.allocation_owner { |
| 310 | mut target := unsafe { generic } |
| 311 | for index, previous in target.applicability_entries { |
| 312 | if goops_same_applicability_types(previous.types, classes) { |
| 313 | previous.release() |
| 314 | container_store_validated(mut target.applicability_entries, index, entry) |
| 315 | return |
| 316 | } |
| 317 | } |
| 318 | target.applicability_entries << entry |
| 319 | } else { |
| 320 | for index, previous in m.goops_applicability_cache { |
| 321 | if voidptr(previous.generic) == voidptr(generic) && goops_same_applicability_types(previous.entry.types, classes) { |
| 322 | previous.entry.release() |
| 323 | container_store_validated(mut m.goops_applicability_cache, index, GoopsBorrowedApplicability{ generic: generic, entry: entry }) |
| 324 | return |
| 325 | } |
| 326 | } |
| 327 | m.goops_applicability_cache << GoopsBorrowedApplicability{ generic: generic, entry: entry } |
| 328 | } |
| 329 | return |
| 330 | } |
| 331 | |
| 332 | fn (mut m Machine) finish_goops_applicability(mut vm VM) ! { |
| 333 | result := m.pop_value() |
| 334 | generic_value := m.env.slots[0] |
| 335 | generic := vm.checked_goops_generic(generic_value)! |
| 336 | args := m.env.slots[1] |
| 337 | if !result.is_true() || result.tag == .nil { |
| 338 | m.push(vm.goops.retry) |
| 339 | m.push(generic_value) |
| 340 | m.push(args) |
| 341 | m.apply(mut vm, 2, true)! |
| 342 | return |
| 343 | } |
| 344 | // A hook may return methods from another generic. Stamp those methods |
| 345 | // before the factory runs, including when a captured factory resumes. |
| 346 | method_count := result.list_length()! |
| 347 | if vm.goops.has_redefinitions { |
| 348 | mut remaining := result |
| 349 | for _ in 0 .. method_count { |
| 350 | pair := remaining.pair_pointer() |
| 351 | if pair.car.goops_kind() == .goops_method && goops_needs_migration(pair.car) { |
| 352 | base := m.stack.len |
| 353 | m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'applicability migration', function: &goops_applicability_migration_resume })) |
| 354 | for i in 0 .. 6 { m.push(m.env.slots[i]) } |
| 355 | m.push(result) |
| 356 | m.prepare_goops_migration(vm, pair.car, base)! |
| 357 | m.apply(mut vm, 3, true)! |
| 358 | return |
| 359 | } |
| 360 | remaining = pair.cdr |
| 361 | } |
| 362 | } |
| 363 | mut stamp := m.env.slots[3].vector_pointer().items.clone() |
| 364 | private_types := m.env.slots[4] |
| 365 | types := private_types.vector_pointer().items |
| 366 | mut cursor := result |
| 367 | for _ in 0 .. method_count { |
| 368 | pair := cursor.pair_pointer() |
| 369 | method := vm.checked_goops_method(pair.car)! |
| 370 | stamp << payload_value(.goops_method, method) |
| 371 | stamp << integer(i64(method.generation)) |
| 372 | if !isnil(method.type_class) { goops_stamp_class(method.type_class, mut stamp) } |
| 373 | if method.slot_definition.tag == .goops_slot && types.len > 0 && types[0].tag == .goops_class { |
| 374 | // A generated getter/setter binds the effective slot in the actual |
| 375 | // class. Both the method's declared slot and that target can change. |
| 376 | direct := method.slot_definition.goops_slot_pointer() |
| 377 | stamp << method.slot_definition |
| 378 | stamp << integer(i64(direct.accessor_generation)) |
| 379 | class := types[0].goops_class_pointer() |
| 380 | index := goops_slot_index(class, direct.slot.name) |
| 381 | if index >= 0 && !eqv(class.definitions[index], method.slot_definition) { |
| 382 | target := class.definitions[index] |
| 383 | stamp << target |
| 384 | stamp << integer(i64(target.goops_slot_pointer().accessor_generation)) |
| 385 | } |
| 386 | } |
| 387 | cursor = pair.cdr |
| 388 | } |
| 389 | all := vm.goops_all_methods(generic)! |
| 390 | mut specialized := 0 |
| 391 | for method in all.values { |
| 392 | if method.specializers.len > specialized { specialized = method.specializers.len } |
| 393 | } |
| 394 | all.release() |
| 395 | mut public_types := types.clone() |
| 396 | for i := specialized; i < types.len; i++ { public_types[i] = boolean(false) } |
| 397 | build := if voidptr(generic.type_class) == voidptr(vm.goops.generic_class) { |
| 398 | vm.goops.standard_effective |
| 399 | } else { |
| 400 | vm.globals[vm.goops.effective_binding] |
| 401 | } |
| 402 | m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'effective method', function: &goops_effective_bridge })) |
| 403 | m.push(build) |
| 404 | m.push(generic_value) |
| 405 | m.push(result) |
| 406 | m.push(payload_value(.vector, &Vector{ items: public_types })) |
| 407 | m.push(args) |
| 408 | m.push(payload_value(.vector, &Vector{ items: stamp })) |
| 409 | m.push(private_types) |
| 410 | m.push(m.env.slots[5]) |
| 411 | m.apply(mut vm, 8, true)! |
| 412 | } |
| 413 | |
| 414 | const goops_effective_bridge_code = [ |
| 415 | Instruction{ op: .local, b: 0, origin: reader.no_node } |
| 416 | Instruction{ op: .local, b: 1, origin: reader.no_node } |
| 417 | Instruction{ op: .local, b: 2, origin: reader.no_node } |
| 418 | Instruction{ op: .local, b: 3, origin: reader.no_node } |
| 419 | Instruction{ op: .call, a: 3, origin: reader.no_node } |
| 420 | Instruction{ op: .goops_effective_apply, origin: reader.no_node } |
| 421 | ]! |
| 422 | |
| 423 | const goops_effective_bridge = Function{ |
| 424 | code: unsafe { nil } |
| 425 | params: 8 |
| 426 | slot_count: 8 |
| 427 | instructions: unsafe { static_bridge_instructions(&goops_effective_bridge_code[0], goops_effective_bridge_code.len) } |
| 428 | } |
| 429 | |
| 430 | fn (mut m Machine) finish_goops_effective(mut vm VM) ! { |
| 431 | procedure := m.pop_value() |
| 432 | vm.check_callable_owner(procedure)! |
| 433 | generic := vm.checked_goops_generic(m.env.slots[1])! |
| 434 | args := m.env.slots[4] |
| 435 | count := args.list_length()! |
| 436 | stamp := m.env.slots[5].vector_pointer().items |
| 437 | types := m.env.slots[6].vector_pointer().items |
| 438 | mut cacheable := i64(vm.goops.applicability_epoch) == m.env.slots[7].whole() |
| 439 | && goops_applicability_stamp_valid(stamp) && count == types.len |
| 440 | m.push(procedure) |
| 441 | mut cursor := args |
| 442 | for i in 0 .. count { |
| 443 | pair := cursor.pair_pointer() |
| 444 | if cacheable && types[i].tag == .goops_class && voidptr(vm.goops_class_for(pair.car)!) != types[i].pointer() { |
| 445 | cacheable = false |
| 446 | } |
| 447 | m.push(pair.car) |
| 448 | cursor = pair.cdr |
| 449 | } |
| 450 | if cacheable { m.publish_goops_applicability(vm, generic, types, stamp, procedure) } |
| 451 | m.apply(mut vm, count, true)! |
| 452 | } |
| 453 | |