forked from goby-lang/goby
/
object.go
136 lines (108 loc) · 3.2 KB
/
object.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package vm
import (
"fmt"
"github.com/goby-lang/goby/compiler/bytecode"
"strconv"
)
// Object represents all objects in Goby, including Array, Integer or even Method and Error.
type Object interface {
Class() *RClass
Value() interface{}
SingletonClass() *RClass
SetSingletonClass(*RClass)
findMethod(string) Object
toString() string
toJSON(t *thread) string
id() int
instanceVariableGet(string) (Object, bool)
instanceVariableSet(string, Object) Object
isTruthy() bool
}
// baseObj ==============================================================
type baseObj struct {
class *RClass
singletonClass *RClass
InstanceVariables *environment
}
// Polymorphic helper functions -----------------------------------------
// Class will return object's class
func (b *baseObj) Class() *RClass {
if b.class == nil {
panic(fmt.Sprint("Object doesn't have class."))
}
return b.class
}
// SingletonClass returns object's singleton class
func (b *baseObj) SingletonClass() *RClass {
return b.singletonClass
}
// SetSingletonClass sets object's singleton class
func (b *baseObj) SetSingletonClass(c *RClass) {
b.singletonClass = c
}
func (b *baseObj) instanceVariableGet(name string) (Object, bool) {
v, ok := b.InstanceVariables.get(name)
if !ok {
return NULL, false
}
return v, true
}
func (b *baseObj) instanceVariableSet(name string, value Object) Object {
b.InstanceVariables.set(name, value)
return value
}
func (b *baseObj) findMethod(methodName string) (method Object) {
if b.SingletonClass() != nil {
method = b.SingletonClass().lookupMethod(methodName)
}
if method == nil {
method = b.Class().lookupMethod(methodName)
}
return
}
func (b *baseObj) id() int {
r, e := strconv.ParseInt(fmt.Sprintf("%p", b), 0, 64)
if e != nil {
panic(e.Error())
}
return int(r)
}
func (b *baseObj) isTruthy() bool {
return true
}
// Pointer ==============================================================
// Pointer is used to point to an object. Variables should hold pointer instead of holding a object directly.
type Pointer struct {
Target Object
isNamespace bool
}
func (p *Pointer) returnClass() *RClass {
return p.Target.(*RClass)
}
// RObject ==============================================================
// RObject represents any non built-in class's instance.
type RObject struct {
*baseObj
InitializeMethod *MethodObject
}
// Polymorphic helper functions -----------------------------------------
// toString returns the object's name as the string format
func (ro *RObject) toString() string {
return "<Instance of: " + ro.class.Name + ">"
}
// toJSON just delegates to toString
func (ro *RObject) toJSON(t *thread) string {
customToJSONMethod := ro.findMethod("to_json").(*MethodObject)
if customToJSONMethod != nil {
t.stack.push(&Pointer{Target: ro})
callObj := newCallObject(ro, customToJSONMethod, t.sp, 0, &bytecode.ArgSet{}, nil, customToJSONMethod.instructionSet.instructions[0].sourceLine)
t.evalMethodObject(callObj, customToJSONMethod.instructionSet.instructions[0].sourceLine)
result := t.stack.pop().Target
return result.toString()
}
return ro.toString()
}
// Value returns object's string format
func (ro *RObject) Value() interface{} {
return ro.toString()
}