module scheme import reader // Factories are ordinary evaluator calls: a captured continuation resumes // here before the resulting procedure receives the original argument list. // This program has no VM or arena-owned data and is safe to share. const goops_method_factory_bridge_code = [ Instruction{ op: .local, b: 0, origin: reader.no_node } Instruction{ op: .local, b: 1, origin: reader.no_node } Instruction{ op: .call, a: 1, origin: reader.no_node } Instruction{ op: .goops_factory_apply, origin: reader.no_node } ]! const goops_method_factory_bridge = Function{ code: unsafe { nil } params: 3 slot_count: 3 instructions: unsafe { static_bridge_instructions(&goops_method_factory_bridge_code[0], goops_method_factory_bridge_code.len) } } @[inline] fn (method &GoopsMethod) goops_public_procedure() Value { return if method.public_procedure.tag == .unspecified { method.procedure } else { method.public_procedure } } // Compiler-created factories expose Guile's public method protocol while // ordinary dispatch can still enter their raw closure without a bridge. fn (method &GoopsMethod) goops_direct_factory() bool { factory := method.make_procedure return factory.tag == .procedure && factory.procedure_pointer().mode == .goops_method_factory && eqv(factory.procedure_pointer().env.slots[0], method.procedure) } fn (mut m Machine) prepare_goops_factory(vm &VM, method &GoopsMethod, next Value, base int) { args := list(m.arguments(base + 1)) m.truncate(base) m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'method factory', function: &goops_method_factory_bridge })) m.push(method.make_procedure) m.push(next) m.push(args) } fn (mut m Machine) finish_goops_factory(mut vm VM) ! { procedure := m.pop_value() vm.check_callable_owner(procedure)! args := m.env.slots[2] count := args.list_length()! m.push(procedure) mut cursor := args for _ in 0 .. count { pair := cursor.pair_pointer() m.push(pair.car) cursor = pair.cdr } m.apply(mut vm, count, true)! } // A lexical next-method reads the method's current formal bindings. Capturing // the activation also preserves optional defaults, mutation and escaped next // closures. Keyword tails without a rest binding retain their original packet; // keyword default values are not implicitly supplied to the next method. fn (mut m Machine) bind_goops_next(vm &VM, mut scope Environment, function &Function, base int, argc int, optional_end int) ! { if function.goops_next_slot < 0 { return failure('invalid next-method target') } keywords := if !isnil(function.star) && function.star.has_keys && function.star.rest_slot < 0 { m.stack[base + 1 + optional_end .. base + 1 + argc] } else { []Value{} } files := vm.owner.bounded_files next := if vm.uses_bounded_operations() { files.next_method(vm, scope, function, m.goops_pending_next, keywords)! } else { new_goops_next(vm, scope, function, m.goops_pending_next, keywords) } // Complete all private allocation before marking the activation or publishing // its callable. The argument view above is borrowed only during construction. escape_environment(scope) scope.slots[function.goops_next_slot] = next m.goops_pending_next = Value{} } fn new_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value) Value { mut captured := value_storage(1 + keywords.len) container_store_validated(mut captured, 0, next) if keywords.len > 0 { unsafe { vmemcpy(&Value(captured.data) + 1, keywords.data, keywords.len * int(sizeof(Value))) } } return procedure_value(&Procedure{ owner: vm.owner mode: .goops_forward_next name: 'next-method' function: function env: &Environment{ parent: scope, slots: captured, escaped: true } }) } fn (mut m Machine) forward_goops_next(p &Procedure, base int) !int { scope := p.env.parent function := p.function count := if isnil(function.star) { function.params } else { function.star.required + function.star.optional } rest_slot := if isnil(function.star) { if function.rest { function.params } else { -1 } } else { function.star.rest_slot } mut rest := nil_value() mut extra := p.env.slots.len - 1 if rest_slot >= 0 { rest = scope.slots[rest_slot] extra = rest.list_length()! } for index in 0 .. count { m.push(scope.slots[index]) } if rest_slot >= 0 { for _ in 0 .. extra { pair := rest.pair_pointer() m.push(pair.car) rest = pair.cdr } } else { for index in 1 .. p.env.slots.len { m.push(p.env.slots[index]) } } container_store_validated(mut m.stack, base, p.env.slots[0]) return count + extra }