vile / scheme / goops_role.v
117 lines · 110 sloc · 3.99 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Ordinary instances keep the physical owner inline. A role record takes
4// over that word on the first transition and carries the same owner.
5@[inline]
6fn (o &GoopsInstance) role() &GoopsRole {
7 return unsafe { &GoopsRole(o.owner_or_role) }
8}
9
10@[inline]
11fn (o &GoopsInstance) physical_owner() voidptr {
12 return if o.has_role { o.role().allocation_owner } else { o.owner_or_role }
13}
14
15// Allocate the other body only when an identity first crosses the method/object
16// boundary. A method table can retain its stable method pointer across either
17// transition. Public values always refer to the original identity.
18@[heap]
19struct GoopsRole {
20 public Value
21 object &GoopsInstance
22 method &GoopsMethod
23 allocation_owner voidptr
24mut:
25 kind Kind
26}
27
28@[inline]
29fn (v Value) goops_kind() Kind {
30 if v.tag == .goops_instance {
31 o := unsafe { &GoopsInstance(v.pointer()) }
32 if o.has_role { return o.role().kind }
33 } else if v.tag == .goops_method {
34 m := unsafe { &GoopsMethod(v.pointer()) }
35 if !isnil(m.role) { return m.role.kind }
36 }
37 return v.tag
38}
39
40fn (v Value) ensure_goops_role(owner &Owner, class &GoopsClass) &GoopsRole {
41 mut object := unsafe { &GoopsInstance(nil) }
42 mut method := unsafe { &GoopsMethod(nil) }
43 if v.tag == .goops_instance {
44 object = unsafe { &GoopsInstance(v.pointer()) }
45 if object.has_role { return object.role() }
46 method = &GoopsMethod{ owner: owner, allocation_owner: object.physical_owner() }
47 } else {
48 method = unsafe { &GoopsMethod(v.pointer()) }
49 if !isnil(method.role) { return method.role }
50 object = &GoopsInstance{ class: class }
51 }
52 role := &GoopsRole{ public: v, object: object, method: method, kind: v.tag, allocation_owner: method.allocation_owner }
53 object.owner_or_role = voidptr(role)
54 object.has_role = true
55 method.role = role
56 return role
57}
58
59fn (vm &VM) goops_exchange_roles(object Value, copy Value, old_class &GoopsClass) !Value {
60 a := object.goops_layout()
61 b := copy.goops_layout()
62 vm.goops_require_mutation(a.allocation_owner)!
63 vm.goops_require_mutation(b.allocation_owner)!
64 ordinary := object.goops_kind() == .goops_instance
65 instance := if ordinary { object.goops_instance_pointer() } else { copy.goops_instance_pointer() }
66 method := if ordinary { copy.goops_method_pointer() } else { object.goops_method_pointer() }
67
68 // A raw field pointer can point inside either public header, including
69 // after an earlier ordinary exchange. Detach it before changing roles.
70 mut fields := unsafe { &Value(nil) }
71 bytes := instance.field_count * int(sizeof(Value))
72 if bytes != 0 {
73 fields = unsafe { &Value(vcalloc(bytes)) }
74 unsafe { vmemcpy(fields, instance.fields, bytes) }
75 }
76 mut left := object.ensure_goops_role(vm.owner, old_class)
77 mut right := copy.ensure_goops_role(vm.owner, old_class)
78 mut from := if ordinary { left } else { right }
79 mut to := if ordinary { right } else { left }
80 owner := a.allocation_owner
81 unsafe {
82 *to.object = GoopsInstance{
83 has_role: true
84 owner_or_role: voidptr(to)
85 class: if ordinary { old_class } else { instance.class }
86 layout_version: instance.layout_version
87 field_count: instance.field_count
88 fields: fields
89 migrating: if ordinary { b.migrating } else { a.migrating }
90 }
91 *from.method = GoopsMethod{
92 ...*method
93 role: from
94 type_class: if ordinary { method.type_class } else { old_class }
95 migrating: if ordinary { a.migrating } else { b.migrating }
96 migration_copy: false
97 registered: from.method.registered
98 installed: false
99 specializer_tickets: []
100 generation: from.method.generation + 1
101 }
102 // Keep already-built effective procedures valid for their recorded
103 // classes. Fresh applicability sees the new role and reports an error.
104 *to.method = GoopsMethod{
105 owner: vm.owner
106 allocation_owner: owner
107 role: to
108 registered: method.registered
109 generation: method.generation
110 }
111 if bytes != 0 { vmemset(instance.fields, 0, bytes) }
112 *from.object = GoopsInstance{ class: vm.goops.object, has_role: true, owner_or_role: voidptr(from) }
113 }
114 from.kind = .goops_method
115 to.kind = .goops_instance
116 return copy
117}
118