-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyfunction.go
44 lines (37 loc) · 1.05 KB
/
pyfunction.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
package vm
type PyFunction struct {
code *PyCode
globals map[string]PyObject
cellvars map[string]PyObject
Dict map[string]func(this *PyFunction, args []PyObject) PyObject
}
func NewPyFunction(code *PyCode, globals map[string]PyObject, env PyObject) *PyFunction {
dict := make(map[string]func(this *PyFunction, args []PyObject) PyObject)
dict["__call__"] = func(this *PyFunction, args []PyObject) PyObject {
frame := NewPyFrame(code, args, this.globals, code.getConstants(), this.cellvars)
result := frame.execute()
return result
}
// Just for closure
cellvars := make(map[string]PyObject)
if env != nil {
for i, item := range env.(*PyTuple).data {
cellvars[code.getFreevars()[i]] = item
}
}
return &PyFunction{
code: code,
cellvars: cellvars,
globals: globals,
Dict: dict,
}
}
func (this *PyFunction) getType() *PyType {
return PyTypes[PyFunctionType]
}
func (this *PyFunction) toString() string {
return "function(" + this.callName() + ")"
}
func (this *PyFunction) callName() string {
return this.code.getName()
}