-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_runtime.go
More file actions
75 lines (69 loc) · 1.86 KB
/
Copy pathmodule_runtime.go
File metadata and controls
75 lines (69 loc) · 1.86 KB
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
package ember
import (
"context"
"fmt"
"strings"
)
func (r *Runtime) runModuleWithContextGlobalsController(ctx context.Context, key moduleKey, globals map[string]Value, controller *executionController, inherited []ScriptFrame) ([]Value, error) {
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return nil, err
}
if value, ok := r.loaded[key]; ok {
return []Value{value}, nil
}
if r.active[key] {
return nil, fmt.Errorf("module runtime: active-loading cycle %s", runtimeModuleCyclePath(r.stack, key))
}
proto, ok := r.program.protos[key]
if !ok {
return nil, fmt.Errorf("module runtime: missing proto for %s", key.String())
}
if err := controller.chargeModuleInitialization(); err != nil {
return nil, err
}
r.active[key] = true
r.stack = append(r.stack, key)
defer func() {
delete(r.active, key)
r.stack = r.stack[:len(r.stack)-1]
}()
call := r.newInvocationScope(ctx, key, globals, controller)
results, err := executeProtoWithInvocationScope(ctx, proto, call, executeOptions{
controller: controller,
inheritedScriptFrames: inherited,
})
if err != nil {
return nil, err
}
r.loaded[key] = firstRuntimeResult(results)
return results, nil
}
func runtimeModuleCyclePath(stack []moduleKey, key moduleKey) string {
start := 0
for i, active := range stack {
if active == key {
start = i
break
}
}
path := make([]string, 0, len(stack)-start+1)
for _, active := range stack[start:] {
path = append(path, active.String())
}
path = append(path, key.String())
return strings.Join(path, " -> ")
}
func removeRuntimeModuleStackKey(stack []moduleKey, key moduleKey) []moduleKey {
for index := len(stack) - 1; index >= 0; index-- {
if stack[index] != key {
continue
}
copy(stack[index:], stack[index+1:])
stack[len(stack)-1] = moduleKey{}
return stack[:len(stack)-1]
}
return stack
}