forked from goby-lang/goby
/
go_object.go
131 lines (108 loc) · 3.25 KB
/
go_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
package vm
import (
"fmt"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
"github.com/st0012/metago"
)
// GoObject ...
type GoObject struct {
*baseObj
data interface{}
}
// Class methods --------------------------------------------------------
func builtinGoObjectClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{}
}
// Instance methods -----------------------------------------------------
func builtinGoObjectInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "go_func",
Fn: func(receiver Object, sourceLine int) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *normalCallFrame) Object {
s, ok := args[0].(*StringObject)
if !ok {
return t.vm.initErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[0].Class().Name)
}
funcName := s.value
r := receiver.(*GoObject)
funcArgs, err := convertToGoFuncArgs(args[1:])
if err != nil {
t.vm.initErrorObject(errors.TypeError, sourceLine, err.Error())
}
result := metago.CallFunc(r.data, funcName, funcArgs...)
return t.vm.initObjectFromGoType(result)
}
},
},
}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func (vm *VM) initGoObject(d interface{}) *GoObject {
return &GoObject{data: d, baseObj: &baseObj{class: vm.topLevelClass(classes.GoObjectClass)}}
}
func (vm *VM) initGoClass() *RClass {
sc := vm.initializeClass(classes.GoObjectClass, false)
sc.setBuiltinMethods(builtinGoObjectClassMethods(), true)
sc.setBuiltinMethods(builtinGoObjectInstanceMethods(), false)
vm.objectClass.setClassConstant(sc)
return sc
}
// Polymorphic helper functions -----------------------------------------
// Value returns the object
func (s *GoObject) Value() interface{} {
return s.data
}
// toString returns the object's name as the string format
func (s *GoObject) toString() string {
return fmt.Sprintf("<GoObject: %p>", s)
}
// toJSON just delegates to toString
func (s *GoObject) toJSON(t *thread) string {
return s.toString()
}
// Other helper functions -----------------------------------------------
func convertToGoFuncArgs(args []Object) ([]interface{}, error) {
funcArgs := []interface{}{}
for _, arg := range args {
switch v := arg.(type) {
case *IntegerObject:
switch v.flag {
case f64:
funcArgs = append(funcArgs, float64(v.value))
continue
case f32:
funcArgs = append(funcArgs, float32(v.value))
continue
case ui64:
funcArgs = append(funcArgs, uint64(v.value))
continue
case ui32:
funcArgs = append(funcArgs, uint32(v.value))
continue
case ui16:
funcArgs = append(funcArgs, uint16(v.value))
continue
case ui8:
funcArgs = append(funcArgs, uint8(v.value))
continue
case i64:
funcArgs = append(funcArgs, int64(v.value))
continue
case i32:
funcArgs = append(funcArgs, int32(v.value))
continue
case i16:
funcArgs = append(funcArgs, int16(v.value))
continue
case i8:
funcArgs = append(funcArgs, int8(v.value))
continue
}
}
funcArgs = append(funcArgs, arg.Value())
}
return funcArgs, nil
}