vile / scheme / goops_construction.v
128 lines · 121 sloc · 4.93 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Constructor options are validated once. GOOPS uses the first occurrence of
4// each keyword; retaining the whole list also preserves extension metadata.
5fn goops_check_initargs(args []Value, name string) ! {
6 if args.len % 2 != 0 { return failure('${name} initargs need keyword/value pairs') }
7 for i := 0; i < args.len; i += 2 {
8 if args[i].tag != .keyword { return failure('${name} initarg must be a keyword') }
9 }
10}
11
12fn goops_find_initarg(args []Value, key string) ?Value {
13 for i := 0; i < args.len; i += 2 {
14 if args[i].text() == key { return args[i + 1] }
15 }
16 return none
17}
18
19fn goops_initarg(args []Value, key string, fallback Value) Value {
20 return goops_find_initarg(args, key) or { fallback }
21}
22
23fn (vm &VM) goops_check_init_thunk(thunk Value) ! {
24 if !thunk.is_true() { return }
25 if !thunk.is_callable() { return failure('slot init-thunk must be a procedure or #f') }
26 vm.check_callable_owner(thunk)!
27 if thunk.tag == .procedure {
28 p := thunk.procedure_pointer()
29 if p.mode in [.closure, .closure_star] && p.function.params != 0 {
30 return failure('slot init-thunk must accept zero arguments')
31 }
32 if p.mode == .case_lambda { _ = p.select_case_clause(0)! }
33 }
34}
35
36fn (vm &VM) goops_make_slot(args []Value) !Value {
37 options := args[1..]
38 goops_check_initargs(options, 'slot')!
39 name := goops_initarg(options, 'name', vm.goops.unbound)
40 allocation := goops_find_initarg(options, 'allocation') or { keyword('instance')! }
41 init_keyword := goops_initarg(options, 'init-keyword', boolean(false))
42 init_value := goops_initarg(options, 'init-value', vm.goops.unbound)
43 init_form := goops_initarg(options, 'init-form', vm.goops.unbound)
44 thunk := goops_initarg(options, 'init-thunk', boolean(false))
45 vm.goops_check_init_thunk(thunk)!
46 slot := &GoopsSlot{
47 name: if name.tag == .symbol { name.text() } else { '<anonymous>' }
48 metadata: &GoopsSlotMetadata{
49 name: name
50 allocation: allocation
51 init_keyword: init_keyword
52 options: list(options)
53 init_form: init_form
54 has_init_form: !vm.goops_unbound(init_form)
55 init_thunk: thunk
56 }
57 init_value: init_value
58 has_init_value: !vm.goops_unbound(init_value)
59 init_thunk: thunk
60 has_init_thunk: thunk.is_true()
61 init_keyword: if init_keyword.tag == .keyword { init_keyword.text() } else { '' }
62 has_init_keyword: init_keyword.tag == .keyword
63 getter: goops_initarg(options, 'getter', boolean(false))
64 setter: goops_initarg(options, 'setter', boolean(false))
65 accessor: goops_initarg(options, 'accessor', boolean(false))
66 }
67 return payload_value(.goops_slot, &GoopsSlotDefinition{
68 owner: vm.owner
69 class: vm.goops.slot_class
70 slot: slot
71 options: slot.metadata.options
72 })
73}
74
75// Materialize layout state from a prebuilt definition without changing the
76// source object. In particular, a shared cell belongs to the new class, and a
77// raw <slot> with only init-value has no instance initializer thunk.
78fn (vm &VM) goops_materialize_slot(value Value, owner voidptr) !GoopsSlot {
79 original := vm.goops_retained_slot(owner, value)!.slot
80 if original.metadata.name.tag != .symbol { return failure('class slot name must be a symbol') }
81 allocation := original.metadata.allocation
82 if allocation.tag != .keyword { return failure('slot allocation must be a keyword') }
83 mut slot := GoopsSlot{ ...*original, shared: unsafe { nil }, instance_index: -1, class_allocated: false, each_subclass: false, virtual: false }
84 match allocation.text() {
85 'instance' {}
86 'class' { slot.class_allocated = true }
87 'each-subclass' {
88 slot.class_allocated = true
89 slot.each_subclass = true
90 }
91 'virtual' { slot.virtual = true }
92 else { return failure('unsupported slot allocation ${allocation.text()}') }
93 }
94 slot.init_thunk = original.metadata.init_thunk
95 slot.has_init_thunk = slot.init_thunk.is_true()
96 if !slot.class_allocated && !slot.has_init_thunk { slot.has_init_value = false }
97 if slot.virtual {
98 options := value.goops_slot_pointer().options
99 count := options.list_length()!
100 if count % 2 != 0 { return failure('slot options need keyword/value pairs') }
101 mut cursor := options
102 mut has_getter := false
103 mut has_setter := false
104 for cursor.tag == .pair {
105 pair := cursor.pair_pointer()
106 argument := pair.cdr.pair_pointer()
107 if pair.car.tag != .keyword { return failure('slot option must be a keyword') }
108 if pair.car.text() == 'slot-ref' && !has_getter {
109 slot.virtual_getter = argument.car
110 has_getter = true
111 } else if pair.car.text() == 'slot-set!' && !has_setter {
112 slot.virtual_setter = argument.car
113 has_setter = true
114 }
115 cursor = argument.cdr
116 }
117 }
118 return slot
119}
120
121fn goops_make_class_object(mut vm VM, args []Value) !Value {
122 options := args[1..]
123 goops_check_initargs(options, 'class')!
124 name := goops_find_initarg(options, 'name') or { text_value(.symbol, '???') }
125 return vm.goops_construct_class(goops_initarg(options, 'dsupers', nil_value()),
126 goops_initarg(options, 'slots', nil_value()),
127 name, false)
128}
129