vile / scheme / goops_method_call.v
131 lines · 121 sloc · 4.53 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5// Factories are ordinary evaluator calls: a captured continuation resumes
6// here before the resulting procedure receives the original argument list.
7// This program has no VM or arena-owned data and is safe to share.
8const goops_method_factory_bridge_code = [
9 Instruction{ op: .local, b: 0, origin: reader.no_node }
10 Instruction{ op: .local, b: 1, origin: reader.no_node }
11 Instruction{ op: .call, a: 1, origin: reader.no_node }
12 Instruction{ op: .goops_factory_apply, origin: reader.no_node }
13]!
14
15const goops_method_factory_bridge = Function{
16 code: unsafe { nil }
17 params: 3
18 slot_count: 3
19 instructions: unsafe { static_bridge_instructions(&goops_method_factory_bridge_code[0], goops_method_factory_bridge_code.len) }
20}
21
22@[inline]
23fn (method &GoopsMethod) goops_public_procedure() Value {
24 return if method.public_procedure.tag == .unspecified {
25 method.procedure
26 } else {
27 method.public_procedure
28 }
29}
30
31// Compiler-created factories expose Guile's public method protocol while
32// ordinary dispatch can still enter their raw closure without a bridge.
33fn (method &GoopsMethod) goops_direct_factory() bool {
34 factory := method.make_procedure
35 return factory.tag == .procedure && factory.procedure_pointer().mode == .goops_method_factory
36 && eqv(factory.procedure_pointer().env.slots[0], method.procedure)
37}
38
39fn (mut m Machine) prepare_goops_factory(vm &VM, method &GoopsMethod, next Value, base int) {
40 args := list(m.arguments(base + 1))
41 m.truncate(base)
42 m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'method factory', function: &goops_method_factory_bridge }))
43 m.push(method.make_procedure)
44 m.push(next)
45 m.push(args)
46}
47
48fn (mut m Machine) finish_goops_factory(mut vm VM) ! {
49 procedure := m.pop_value()
50 vm.check_callable_owner(procedure)!
51 args := m.env.slots[2]
52 count := args.list_length()!
53 m.push(procedure)
54 mut cursor := args
55 for _ in 0 .. count {
56 pair := cursor.pair_pointer()
57 m.push(pair.car)
58 cursor = pair.cdr
59 }
60 m.apply(mut vm, count, true)!
61}
62
63// A lexical next-method reads the method's current formal bindings. Capturing
64// the activation also preserves optional defaults, mutation and escaped next
65// closures. Keyword tails without a rest binding retain their original packet;
66// keyword default values are not implicitly supplied to the next method.
67fn (mut m Machine) bind_goops_next(vm &VM, mut scope Environment, function &Function, base int, argc int, optional_end int) ! {
68 if function.goops_next_slot < 0 { return failure('invalid next-method target') }
69 keywords := if !isnil(function.star) && function.star.has_keys && function.star.rest_slot < 0 {
70 m.stack[base + 1 + optional_end .. base + 1 + argc]
71 } else { []Value{} }
72 files := vm.owner.bounded_files
73 next := if vm.uses_bounded_operations() {
74 files.next_method(vm, scope, function, m.goops_pending_next, keywords)!
75 } else {
76 new_goops_next(vm, scope, function, m.goops_pending_next, keywords)
77 }
78 // Complete all private allocation before marking the activation or publishing
79 // its callable. The argument view above is borrowed only during construction.
80 escape_environment(scope)
81 scope.slots[function.goops_next_slot] = next
82 m.goops_pending_next = Value{}
83}
84
85fn new_goops_next(vm &VM, scope &Environment, function &Function, next Value, keywords []Value) Value {
86 mut captured := value_storage(1 + keywords.len)
87 container_store_validated(mut captured, 0, next)
88 if keywords.len > 0 {
89 unsafe { vmemcpy(&Value(captured.data) + 1, keywords.data, keywords.len * int(sizeof(Value))) }
90 }
91 return procedure_value(&Procedure{
92 owner: vm.owner
93 mode: .goops_forward_next
94 name: 'next-method'
95 function: function
96 env: &Environment{ parent: scope, slots: captured, escaped: true }
97 })
98}
99
100fn (mut m Machine) forward_goops_next(p &Procedure, base int) !int {
101 scope := p.env.parent
102 function := p.function
103 count := if isnil(function.star) {
104 function.params
105 } else {
106 function.star.required + function.star.optional
107 }
108 rest_slot := if isnil(function.star) {
109 if function.rest { function.params } else { -1 }
110 } else {
111 function.star.rest_slot
112 }
113 mut rest := nil_value()
114 mut extra := p.env.slots.len - 1
115 if rest_slot >= 0 {
116 rest = scope.slots[rest_slot]
117 extra = rest.list_length()!
118 }
119 for index in 0 .. count { m.push(scope.slots[index]) }
120 if rest_slot >= 0 {
121 for _ in 0 .. extra {
122 pair := rest.pair_pointer()
123 m.push(pair.car)
124 rest = pair.cdr
125 }
126 } else {
127 for index in 1 .. p.env.slots.len { m.push(p.env.slots[index]) }
128 }
129 container_store_validated(mut m.stack, base, p.env.slots[0])
130 return count + extra
131}
132