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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package compile
// This files defines functions to read and write a compile.Program to a file.
// Currently we use gob encoding because it is convenient.
//
// It is the client's responsibility to manage version skew between the
// compiler used to produce a file and the interpreter that consumes it.
// The version number is provided as a constant. Incompatible protocol
// changes should also increment the version number.
import (
"encoding/gob"
"fmt"
"io"
"github.com/google/starlark/syntax"
)
const magic = "!sky"
type gobProgram struct {
Version int
Filename string
Loads []gobIdent
Names []string
Constants []interface{}
Functions []gobFunction
Globals []gobIdent
Toplevel gobFunction
}
type gobFunction struct {
Id gobIdent // hack: name and pos
Code []byte
Pclinetab []uint16
Locals []gobIdent
Freevars []gobIdent
MaxStack int
NumParams int
HasVarargs, HasKwargs bool
}
type gobIdent struct {
Name string
Line, Col int32 // the filename is gobProgram.Filename
}
// Write writes a compiled Starlark program to out.
func (prog *Program) Write(out io.Writer) error {
out.Write([]byte(magic))
gobIdents := func(idents []Ident) []gobIdent {
res := make([]gobIdent, len(idents))
for i, id := range idents {
res[i].Name = id.Name
res[i].Line = id.Pos.Line
res[i].Col = id.Pos.Col
}
return res
}
gobFunc := func(fn *Funcode) gobFunction {
return gobFunction{
Id: gobIdent{
Name: fn.Name,
Line: fn.Pos.Line,
Col: fn.Pos.Col,
},
Code: fn.Code,
Pclinetab: fn.pclinetab,
Locals: gobIdents(fn.Locals),
Freevars: gobIdents(fn.Freevars),
MaxStack: fn.MaxStack,
NumParams: fn.NumParams,
HasVarargs: fn.HasVarargs,
HasKwargs: fn.HasKwargs,
}
}
gp := &gobProgram{
Version: Version,
Filename: prog.Toplevel.Pos.Filename(),
Loads: gobIdents(prog.Loads),
Names: prog.Names,
Constants: prog.Constants,
Functions: make([]gobFunction, len(prog.Functions)),
Globals: gobIdents(prog.Globals),
Toplevel: gobFunc(prog.Toplevel),
}
for i, f := range prog.Functions {
gp.Functions[i] = gobFunc(f)
}
return gob.NewEncoder(out).Encode(gp)
}
// ReadProgram reads a compiled Starlark program from in.
func ReadProgram(in io.Reader) (*Program, error) {
magicBuf := []byte(magic)
n, err := in.Read(magicBuf)
if err != nil {
return nil, err
}
if n != len(magic) {
return nil, fmt.Errorf("not a compiled module: no magic number")
}
if string(magicBuf) != magic {
return nil, fmt.Errorf("not a compiled module: got magic number %q, want %q",
magicBuf, magic)
}
dec := gob.NewDecoder(in)
var gp gobProgram
if err := dec.Decode(&gp); err != nil {
return nil, fmt.Errorf("decoding program: %v", err)
}
if gp.Version != Version {
return nil, fmt.Errorf("version mismatch: read %d, want %d",
gp.Version, Version)
}
file := gp.Filename // copy, to avoid keeping gp live
ungobIdents := func(idents []gobIdent) []Ident {
res := make([]Ident, len(idents))
for i, id := range idents {
res[i].Name = id.Name
res[i].Pos = syntax.MakePosition(&file, id.Line, id.Col)
}
return res
}
prog := &Program{
Loads: ungobIdents(gp.Loads),
Names: gp.Names,
Constants: gp.Constants,
Globals: ungobIdents(gp.Globals),
Functions: make([]*Funcode, len(gp.Functions)),
}
ungobFunc := func(gf *gobFunction) *Funcode {
pos := syntax.MakePosition(&file, gf.Id.Line, gf.Id.Col)
return &Funcode{
Prog: prog,
Pos: pos,
Name: gf.Id.Name,
Code: gf.Code,
pclinetab: gf.Pclinetab,
Locals: ungobIdents(gf.Locals),
Freevars: ungobIdents(gf.Freevars),
MaxStack: gf.MaxStack,
NumParams: gf.NumParams,
HasVarargs: gf.HasVarargs,
HasKwargs: gf.HasKwargs,
}
}
for i := range gp.Functions {
prog.Functions[i] = ungobFunc(&gp.Functions[i])
}
prog.Toplevel = ungobFunc(&gp.Toplevel)
return prog, nil
}
|