-
Notifications
You must be signed in to change notification settings - Fork 171
/
method.go
109 lines (84 loc) · 2.7 KB
/
method.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
package vm
import (
"bytes"
"fmt"
"github.com/goby-lang/goby/compiler/bytecode"
"github.com/goby-lang/goby/vm/classes"
)
// MethodObject represents methods defined using goby.
type MethodObject struct {
*baseObj
Name string
instructionSet *instructionSet
argc int
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func (vm *VM) initMethodClass() *RClass {
return vm.initializeClass(classes.MethodClass)
}
// Polymorphic helper functions -----------------------------------------
func (m *MethodObject) toString() string {
var out bytes.Buffer
out.WriteString(fmt.Sprintf("<Method: %s (%d params)\n>", m.Name, m.argc))
out.WriteString(m.instructionSet.inspect())
return out.String()
}
func (m *MethodObject) toJSON(t *Thread) string {
return m.toString()
}
// Value returns method object's string format
func (m *MethodObject) Value() interface{} {
return m.toString()
}
func (m *MethodObject) paramTypes() []int {
return m.instructionSet.paramTypes.Types()
}
func (m *MethodObject) isSplatArgIncluded() bool {
for _, argType := range m.paramTypes() {
if argType == bytecode.SplatArg {
return true
}
}
return false
}
func (m *MethodObject) isKeywordArgIncluded() bool {
for _, argType := range m.paramTypes() {
if argType == bytecode.OptionalKeywordArg || argType == bytecode.RequiredKeywordArg {
return true
}
}
return false
}
// BuiltinMethodObject =================================================
// BuiltinMethodObject represents methods defined in go.
type BuiltinMethodObject struct {
*baseObj
Name string
Fn builtinMethodBody
}
// Method is a callable function
type Method = func(receiver Object, line int, t *Thread, args []Object) Object
// ExternalBuiltinMethod is a function that builds a BuiltinMethodObject from an external function
func ExternalBuiltinMethod(name string, m Method) *BuiltinMethodObject {
return &BuiltinMethodObject{
Name: name,
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, c *normalCallFrame) Object {
return m(receiver, sourceLine, t, args)
},
}
}
type builtinMethodBody func(Object, int, *Thread, []Object, *normalCallFrame) Object
// Polymorphic helper functions -----------------------------------------
// toString returns the object's name as the string format
func (bim *BuiltinMethodObject) toString() string {
return "<BuiltinMethod: " + bim.Name + ">"
}
// toJSON just delegates to `toString`
func (bim *BuiltinMethodObject) toJSON(t *Thread) string {
return bim.toString()
}
// Value returns builtin method object's function
func (bim *BuiltinMethodObject) Value() interface{} {
return bim.Fn
}