module scheme import reader import arena // Resume with the hook's original result after migrating a returned method. const goops_applicability_migration_resume_code = [ Instruction{ op: .local, b: 6, origin: reader.no_node } Instruction{ op: .goops_applicability_apply, origin: reader.no_node } ]! const goops_applicability_migration_resume = Function{ code: unsafe { nil } params: 7 slot_count: 7 instructions: unsafe { static_bridge_instructions(&goops_applicability_migration_resume_code[0], goops_applicability_migration_resume_code.len) } } struct GoopsApplicabilityEntry { types []&GoopsClass stamp []Value procedure Value epoch u64 } struct GoopsBorrowedApplicability { generic &GoopsGeneric entry GoopsApplicabilityEntry } fn (entry GoopsApplicabilityEntry) release() { mut types := unsafe { entry.types } mut stamp := unsafe { entry.stamp } unsafe { types.free() stamp.free() } } fn (mut m Machine) cleanup_goops_applicability_cache() { for cached in m.goops_applicability_cache { cached.entry.release() } unsafe { m.goops_applicability_cache.free() } m.goops_applicability_cache = []GoopsBorrowedApplicability{} } // Like Guile's normal dispatcher, exact bypasses the public hook. // Native metadata queries are intrinsic; the public hook can log class-name, // method-specializers, etc. without recursively dispatching its own queries. fn (vm &VM) goops_uses_applicability(generic &GoopsGeneric) bool { return vm.goops.protocols_ready && !generic.intrinsic_applicability && voidptr(generic.type_class) != voidptr(vm.goops.generic_class) } // Exact generics with ordinary method bodies enter directly. A user factory // or a specialized method class can have observable construction effects even // when its method is less specific than the first applicable method. fn (vm &VM) goops_needs_effective_methods(methods []&GoopsMethod) bool { for method in methods { if !isnil(method.role) || (!isnil(method.type_class) && voidptr(method.type_class) != voidptr(vm.goops.method_class)) || method.make_procedure.is_callable() { return true } } return false } fn (vm &VM) goops_is_applicability_hook(generic &GoopsGeneric, hook Value) bool { mut target := hook for target.tag == .procedure && target.procedure_pointer().mode == .with_setter { target = target.procedure_pointer().env.slots[0] } if target.tag != .procedure { return false } if target.procedure_pointer().mode !in [.goops_generic, .goops_accessor, .goops_generic_setter, .goops_make] { return false } data := vm.checked_goops_generic(target) or { return false } return voidptr(data) == voidptr(generic) } fn goops_applicability_stamp_valid(stamp []Value) bool { mut index := 0 for index < stamp.len { value := stamp[index] match value.tag { .goops_generic_data { if index + 2 >= stamp.len { return false } generic := value.goops_generic_data_pointer() if i64(generic.generation) != stamp[index + 1].whole() || voidptr(generic.type_class) != stamp[index + 2].pointer() { return false } index += 3 } .goops_method { if index + 1 >= stamp.len || i64(value.goops_method_pointer().generation) != stamp[index + 1].whole() { return false } index += 2 } .goops_slot { if index + 1 >= stamp.len || i64(value.goops_slot_pointer().accessor_generation) != stamp[index + 1].whole() { return false } index += 2 } .goops_class { if index + 2 >= stamp.len { return false } class := value.goops_class_pointer() if i64(class.version) != stamp[index + 1].whole() || i64(class.dispatch_generation) != stamp[index + 2].whole() { return false } index += 3 } else { return false } } } return true } fn (vm &VM) goops_applicability_types_match(types []&GoopsClass, args []Value) !bool { if types.len != args.len { return false } for index, value in args { if !isnil(types[index]) && voidptr(vm.goops_class_for(value)!) != voidptr(types[index]) { return false } } return true } fn (mut m Machine) goops_applicability_view(vm &VM, generic &GoopsGeneric, args []Value) !Value { for entry in generic.applicability_entries { if entry.epoch == vm.goops.applicability_epoch && goops_applicability_stamp_valid(entry.stamp) && vm.goops_applicability_types_match(entry.types, args)! { return entry.procedure } } if m.goops_cache_owner == v_arena_current() { for cached in m.goops_applicability_cache { entry := cached.entry if voidptr(cached.generic) == voidptr(generic) && entry.epoch == vm.goops.applicability_epoch && goops_applicability_stamp_valid(entry.stamp) && vm.goops_applicability_types_match(entry.types, args)! { return entry.procedure } } } return Value{} } fn goops_stamp_dependency(dependency &GoopsGeneric, mut stamp []Value) { stamp << payload_value(.goops_generic_data, dependency) stamp << integer(i64(dependency.generation)) stamp << goops_class_value(dependency.type_class) goops_stamp_class(dependency.type_class, mut stamp) } fn (vm &VM) goops_stamp_generic(generic &GoopsGeneric, mut stamp []Value) !int { if generic.has_only_local_methods() { goops_stamp_dependency(generic, mut stamp) return vm.goops_stamp_methods(generic.methods, mut stamp) } collected := vm.goops_collect_methods(generic)! defer { collected.release() } for dependency in collected.dependencies { goops_stamp_dependency(dependency, mut stamp) } return vm.goops_stamp_methods(collected.values, mut stamp) } fn (vm &VM) goops_stamp_methods(methods []&GoopsMethod, mut stamp []Value) int { mut count := 0 for method in methods { stamp << payload_value(.goops_method, method) stamp << integer(i64(method.generation)) if !isnil(method.type_class) { goops_stamp_class(method.type_class, mut stamp) } if method.specializers.len > count { count = method.specializers.len } // Vile permits a typed rest specializer. Every remaining argument // then participates in dispatch, even though the public type vector // describes the fixed specializers only. if method.rest && !isnil(method.rest_class) && voidptr(method.rest_class) != voidptr(vm.goops.top) { count = max_int } } return count } fn goops_stamp_class(class &GoopsClass, mut stamp []Value) { stamp << goops_class_value(class) stamp << integer(i64(class.version)) stamp << integer(i64(class.dispatch_generation)) } // These are immutable, private vectors in the bridge's lexical environment. // A continuation never captures headers belonging to an invalidatable cache. const goops_applicability_bridge_code = [ Instruction{ op: .local, b: 2, origin: reader.no_node } Instruction{ op: .local, b: 0, origin: reader.no_node } Instruction{ op: .local, b: 1, origin: reader.no_node } Instruction{ op: .call, a: 2, origin: reader.no_node } Instruction{ op: .goops_applicability_apply, origin: reader.no_node } ]! const goops_applicability_bridge = Function{ code: unsafe { nil } params: 6 slot_count: 6 instructions: unsafe { static_bridge_instructions(&goops_applicability_bridge_code[0], goops_applicability_bridge_code.len) } } fn (mut m Machine) prepare_goops_applicability(vm &VM, generic &GoopsGeneric, hook Value, base int) ! { vm.check_callable_owner(hook)! args := list(m.arguments(base + 1)) mut stamp := []Value{} mut types := []Value{cap: m.stack.len - base - 1} mut transferred := false defer { if !transferred { unsafe { stamp.free() types.free() } } } count := vm.goops_stamp_generic(generic, mut stamp)! for root in [hook, vm.globals[vm.goops.effective_binding], vm.globals[vm.goops.specialized_binding]]! { mut target := root for target.tag == .procedure && target.procedure_pointer().mode == .with_setter { target = target.procedure_pointer().env.slots[0] } if target.tag == .procedure && target.procedure_pointer().mode in [.goops_generic, .goops_accessor, .goops_generic_setter, .goops_make] { vm.goops_stamp_generic(vm.checked_goops_generic(target)!, mut stamp)! } } goops_stamp_class(generic.type_class, mut stamp) goops_stamp_class(vm.goops_class_for(args)!, mut stamp) for index, value in m.arguments(base + 1) { if index >= count { types << boolean(false) continue } class := vm.goops_class_for(value)! types << goops_class_value(class) goops_stamp_class(class, mut stamp) } stamp_value := payload_value(.vector, &Vector{ items: stamp }) types_value := payload_value(.vector, &Vector{ items: types }) transferred = true m.truncate(base) m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'applicability hook', function: &goops_applicability_bridge })) m.push(generic.public_value) m.push(args) m.push(hook) m.push(stamp_value) m.push(types_value) m.push(integer(i64(vm.goops.applicability_epoch))) } fn (vm &VM) goops_applicability_retained_by(owner voidptr, stamp []Value) bool { mut index := 0 for index < stamp.len { value := stamp[index] match value.tag { .goops_generic_data { if !vm.goops_retains_owner(owner, value.goops_generic_data_pointer().allocation_owner) { return false } index += 3 } .goops_method { if !vm.goops_retains_owner(owner, value.goops_method_pointer().allocation_owner) { return false } index += 2 } .goops_slot { if !vm.goops_retains_owner(owner, value.goops_slot_pointer().allocation_owner) { return false } index += 2 } .goops_class { if !vm.goops_retains_owner(owner, value.goops_class_pointer().allocation_owner) { return false } index += 3 } else { return false } } } return true } fn goops_same_applicability_types(a []&GoopsClass, b []&GoopsClass) bool { if a.len != b.len { return false } for index, class in a { if voidptr(class) != voidptr(b[index]) { return false } } return true } fn (mut m Machine) publish_goops_applicability(vm &VM, generic &GoopsGeneric, types []Value, stamp []Value, procedure Value) { current := v_arena_current() if current != generic.allocation_owner && current != m.goops_cache_owner { return } if !vm.goops_applicability_retained_by(current, stamp) { return } if _ := arena.lifetime_token(procedure.pointer()) { current_owns := !isnil(current) && v_arena_owns(current, procedure.pointer()) vm_owns := !isnil(vm.allocation_owner) && v_arena_owns(vm.allocation_owner, procedure.pointer()) if !current_owns && !vm_owns { return } } mut classes := []&GoopsClass{cap: types.len} for value in types { classes << if value.tag == .goops_class { value.goops_class_pointer() } else { unsafe { &GoopsClass(nil) } } } entry := GoopsApplicabilityEntry{ types: classes, stamp: stamp.clone(), procedure: procedure, epoch: vm.goops.applicability_epoch } if current == generic.allocation_owner { mut target := unsafe { generic } for index, previous in target.applicability_entries { if goops_same_applicability_types(previous.types, classes) { previous.release() container_store_validated(mut target.applicability_entries, index, entry) return } } target.applicability_entries << entry } else { for index, previous in m.goops_applicability_cache { if voidptr(previous.generic) == voidptr(generic) && goops_same_applicability_types(previous.entry.types, classes) { previous.entry.release() container_store_validated(mut m.goops_applicability_cache, index, GoopsBorrowedApplicability{ generic: generic, entry: entry }) return } } m.goops_applicability_cache << GoopsBorrowedApplicability{ generic: generic, entry: entry } } return } fn (mut m Machine) finish_goops_applicability(mut vm VM) ! { result := m.pop_value() generic_value := m.env.slots[0] generic := vm.checked_goops_generic(generic_value)! args := m.env.slots[1] if !result.is_true() || result.tag == .nil { m.push(vm.goops.retry) m.push(generic_value) m.push(args) m.apply(mut vm, 2, true)! return } // A hook may return methods from another generic. Stamp those methods // before the factory runs, including when a captured factory resumes. method_count := result.list_length()! if vm.goops.has_redefinitions { mut remaining := result for _ in 0 .. method_count { pair := remaining.pair_pointer() if pair.car.goops_kind() == .goops_method && goops_needs_migration(pair.car) { base := m.stack.len m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'applicability migration', function: &goops_applicability_migration_resume })) for i in 0 .. 6 { m.push(m.env.slots[i]) } m.push(result) m.prepare_goops_migration(vm, pair.car, base)! m.apply(mut vm, 3, true)! return } remaining = pair.cdr } } mut stamp := m.env.slots[3].vector_pointer().items.clone() private_types := m.env.slots[4] types := private_types.vector_pointer().items mut cursor := result for _ in 0 .. method_count { pair := cursor.pair_pointer() method := vm.checked_goops_method(pair.car)! stamp << payload_value(.goops_method, method) stamp << integer(i64(method.generation)) if !isnil(method.type_class) { goops_stamp_class(method.type_class, mut stamp) } if method.slot_definition.tag == .goops_slot && types.len > 0 && types[0].tag == .goops_class { // A generated getter/setter binds the effective slot in the actual // class. Both the method's declared slot and that target can change. direct := method.slot_definition.goops_slot_pointer() stamp << method.slot_definition stamp << integer(i64(direct.accessor_generation)) class := types[0].goops_class_pointer() index := goops_slot_index(class, direct.slot.name) if index >= 0 && !eqv(class.definitions[index], method.slot_definition) { target := class.definitions[index] stamp << target stamp << integer(i64(target.goops_slot_pointer().accessor_generation)) } } cursor = pair.cdr } all := vm.goops_all_methods(generic)! mut specialized := 0 for method in all.values { if method.specializers.len > specialized { specialized = method.specializers.len } } all.release() mut public_types := types.clone() for i := specialized; i < types.len; i++ { public_types[i] = boolean(false) } build := if voidptr(generic.type_class) == voidptr(vm.goops.generic_class) { vm.goops.standard_effective } else { vm.globals[vm.goops.effective_binding] } m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'effective method', function: &goops_effective_bridge })) m.push(build) m.push(generic_value) m.push(result) m.push(payload_value(.vector, &Vector{ items: public_types })) m.push(args) m.push(payload_value(.vector, &Vector{ items: stamp })) m.push(private_types) m.push(m.env.slots[5]) m.apply(mut vm, 8, true)! } const goops_effective_bridge_code = [ Instruction{ op: .local, b: 0, origin: reader.no_node } Instruction{ op: .local, b: 1, origin: reader.no_node } Instruction{ op: .local, b: 2, origin: reader.no_node } Instruction{ op: .local, b: 3, origin: reader.no_node } Instruction{ op: .call, a: 3, origin: reader.no_node } Instruction{ op: .goops_effective_apply, origin: reader.no_node } ]! const goops_effective_bridge = Function{ code: unsafe { nil } params: 8 slot_count: 8 instructions: unsafe { static_bridge_instructions(&goops_effective_bridge_code[0], goops_effective_bridge_code.len) } } fn (mut m Machine) finish_goops_effective(mut vm VM) ! { procedure := m.pop_value() vm.check_callable_owner(procedure)! generic := vm.checked_goops_generic(m.env.slots[1])! args := m.env.slots[4] count := args.list_length()! stamp := m.env.slots[5].vector_pointer().items types := m.env.slots[6].vector_pointer().items mut cacheable := i64(vm.goops.applicability_epoch) == m.env.slots[7].whole() && goops_applicability_stamp_valid(stamp) && count == types.len m.push(procedure) mut cursor := args for i in 0 .. count { pair := cursor.pair_pointer() if cacheable && types[i].tag == .goops_class && voidptr(vm.goops_class_for(pair.car)!) != types[i].pointer() { cacheable = false } m.push(pair.car) cursor = pair.cdr } if cacheable { m.publish_goops_applicability(vm, generic, types, stamp, procedure) } m.apply(mut vm, count, true)! }