-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstance.go
More file actions
107 lines (90 loc) · 2.23 KB
/
instance.go
File metadata and controls
107 lines (90 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2023, Peter Ohler, All rights reserved.
package slip
import (
"sort"
"strings"
)
type Instance interface {
Object
Receiver
// Class of the instance.
Class() Class
// IsA returns true if the instance's class is the specified class or a
// sub-class of the specified class.
IsA(class string) bool
// Init the instance slots from the provided args list. If the scope is
// not nil then send :init is called.
Init(scope *Scope, args List, depth int)
// SetSynchronized sets the instance to be thread safe (mutex protected
// slots) or not.
SetSynchronized(on bool)
// Synchronized returns true if the instance is thread safe.
Synchronized() bool
// SlotNames returns a list of the slots names for the instance.
SlotNames() []string
// SlotValue return the value of an instance variable.
SlotValue(name Symbol) (Object, bool)
// SetSlotValue sets the value of an instance variable and return true if
// the name slot exists and was set.
SetSlotValue(sym Symbol, value Object) (has bool)
// GetMethod returns the method if it exists.
GetMethod(name string) *Method
// MethodNames returns a sorted list of the methods of the class.
MethodNames() List
// ID returns unique ID for the instance.
ID() uint64
// Dup returns a duplicate of the instance.
Dup() Instance
}
// InstanceLoadForm created a load form for an instance.
func InstanceLoadForm(obj Instance) (form List) {
makeInstSym := Symbol("make-instance")
for _, h := range obj.Hierarchy() {
if strings.EqualFold(string(h), "condition") {
makeInstSym = Symbol("make-condition")
break
}
}
names := obj.SlotNames()
sort.Strings(names)
form = List{
Symbol("let"),
List{
List{
Symbol("inst"),
List{
makeInstSym,
List{
Symbol("quote"),
Symbol(obj.Class().Name()),
},
},
},
},
}
for _, name := range names {
if name == "self" {
continue
}
iv, _ := obj.SlotValue(Symbol(name))
if iv == Unbound {
continue
}
form = append(form,
List{
Symbol("setf"),
List{
Symbol("slot-value"),
Symbol("inst"),
List{
Symbol("quote"),
Symbol(name),
},
},
iv, // TBD handle more complex values
},
)
}
form = append(form, Symbol("inst"))
return
}