-
Notifications
You must be signed in to change notification settings - Fork 22
/
call.go
112 lines (99 loc) · 3.54 KB
/
call.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
//
// Copyright © 2018 Aljabr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package eval
import (
"context"
"fmt"
"reflect"
// goruntime "runtime"
"github.com/kocircuit/kocircuit/lang/circuit/eval"
"github.com/kocircuit/kocircuit/lang/circuit/model"
"github.com/kocircuit/kocircuit/lang/go/eval/symbol"
"github.com/kocircuit/kocircuit/lang/go/gate"
"github.com/kocircuit/kocircuit/lang/go/kit/tree"
"github.com/kocircuit/kocircuit/lang/go/runtime"
)
// EvalCallMacro is a macro that calls a Gate.
type EvalCallMacro struct {
Gate gate.Gate `ko:"name=gate"`
}
func (m *EvalCallMacro) Splay() tree.Tree { return tree.Quote{String_: m.Help()} }
func (m *EvalCallMacro) Label() string { return "call" }
func (m *EvalCallMacro) MacroID() string { return m.Help() }
func (m *EvalCallMacro) MacroSheathString() *string { return nil }
// Help returns the a short help message of the macro
func (m *EvalCallMacro) Help() string {
if helpMethod, found := m.Gate.Receiver.MethodByName("Help"); found && helpMethod.Type.NumIn() == 1 {
instance := reflect.New(m.Gate.Receiver).Elem()
result := helpMethod.Func.Call([]reflect.Value{instance})
if len(result) == 1 {
return result[0].String()
}
}
return fmt.Sprintf("%q.%s", m.Gate.GoPkgPath(), m.Gate.GoName())
}
// Doc returns the usage documentation of the macro
func (m *EvalCallMacro) Doc() string {
if docMethod, found := m.Gate.Receiver.MethodByName("Doc"); found && docMethod.Type.NumIn() == 1 {
instance := reflect.New(m.Gate.Receiver).Elem()
result := docMethod.Func.Call([]reflect.Value{instance})
if len(result) == 1 {
return result[0].String()
}
}
return fmt.Sprintf("Run: go doc %s.%s", m.Gate.GoPkgPath(), m.Gate.GoName())
}
var (
goErrorType = reflect.TypeOf((*error)(nil)).Elem()
)
func (call *EvalCallMacro) Invoke(span *model.Span, arg eval.Arg) (returns eval.Return, effect eval.Effect, err error) {
ss := arg.(*symbol.StructSymbol)
var receiver reflect.Value
if receiver, err = symbol.Integrate(span, ss, call.Gate.Receiver); err != nil {
return nil, nil, err
} else {
defer func() {
if r := recover(); r != nil {
// buf := make([]byte, 2e5) // 200K
// stack := string(buf[:goruntime.Stack(buf, true)])
returns, effect, err = nil, nil, span.Errorf(nil, "calling gate panic: %v", r)
}
}()
ctx := NewEvalRuntimeContext(span)
result := receiver.MethodByName("Play").Call([]reflect.Value{reflect.ValueOf(ctx)})
// test error response
if len(result) > 1 {
errVal := result[len(result)-1]
if errVal.Kind() == reflect.Interface && !errVal.IsNil() {
if err := errVal.Convert(goErrorType).Interface().(error); err != nil {
return nil, nil, err
}
}
}
// lift result to declared return value
m, _ := call.Gate.Receiver.MethodByName("Play")
result[0] = result[0].Convert(m.Type.Out(0))
return symbol.Deconstruct(span, result[0]), nil, nil
}
}
func NewEvalRuntimeContext(span *model.Span) *runtime.Context {
return &runtime.Context{
Parent: nil,
Source: span.CommentLine(),
Context: context.Background(),
Kill: nil,
}
}