forked from grafana/k6
/
compiler.go
153 lines (133 loc) · 4.03 KB
/
compiler.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
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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2017 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
//go:generate rice embed-go
package compiler
import (
"sync"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/loadimpact/k6/lib"
)
var (
DefaultOpts = map[string]interface{}{
"presets": []string{"latest"},
"ast": false,
"sourceMaps": false,
"babelrc": false,
"compact": false,
"retainLines": true,
"highlightCode": false,
}
once sync.Once // nolint:gochecknoglobals
globalBabel *babel // nolint:gochecknoglobals
)
// A Compiler compiles JavaScript source code (ES5.1 or ES6) into a goja.Program
type Compiler struct{}
// New returns a new Compiler
func New() *Compiler {
return &Compiler{}
}
// Transform the given code into ES5
func (c *Compiler) Transform(src, filename string) (code string, srcmap *SourceMap, err error) {
var b *babel
if b, err = newBabel(); err != nil {
return
}
return b.Transform(src, filename)
}
// Compile the program in the given CompatibilityMode, optionally running pre and post code.
func (c *Compiler) Compile(src, filename, pre, post string,
strict bool, compatMode lib.CompatibilityMode) (*goja.Program, string, error) {
code := pre + src + post
ast, err := parser.ParseFile(nil, filename, code, 0)
if err != nil {
if compatMode == lib.CompatibilityModeExtended {
code, _, err = c.Transform(src, filename)
if err != nil {
return nil, code, err
}
return c.Compile(code, filename, pre, post, strict, compatMode)
}
return nil, code, err
}
pgm, err := goja.CompileAST(ast, strict)
return pgm, code, err
}
type babel struct {
vm *goja.Runtime
this goja.Value
transform goja.Callable
mutex sync.Mutex // TODO: cache goja.CompileAST() in an init() function?
}
func newBabel() (*babel, error) {
var err error
once.Do(func() {
conf := rice.Config{
LocateOrder: []rice.LocateMethod{rice.LocateEmbedded},
}
babelSrc := conf.MustFindBox("lib").MustString("babel.min.js")
vm := goja.New()
if _, err = vm.RunString(babelSrc); err != nil {
return
}
this := vm.Get("Babel")
bObj := this.ToObject(vm)
globalBabel = &babel{vm: vm, this: this}
if err = vm.ExportTo(bObj.Get("transform"), &globalBabel.transform); err != nil {
return
}
})
return globalBabel, err
}
// Transform the given code into ES5, while synchronizing to ensure only a single
// bundle instance / Goja VM is in use at a time.
func (b *babel) Transform(src, filename string) (string, *SourceMap, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
opts := make(map[string]interface{})
for k, v := range DefaultOpts {
opts[k] = v
}
opts["filename"] = filename
startTime := time.Now()
v, err := b.transform(b.this, b.vm.ToValue(src), b.vm.ToValue(opts))
if err != nil {
return "", nil, err
}
logrus.WithField("t", time.Since(startTime)).Debug("Babel: Transformed")
vO := v.ToObject(b.vm)
var code string
if err = b.vm.ExportTo(vO.Get("code"), &code); err != nil {
return code, nil, err
}
var rawMap map[string]interface{}
if err = b.vm.ExportTo(vO.Get("map"), &rawMap); err != nil {
return code, nil, err
}
var srcMap SourceMap
if err = mapstructure.Decode(rawMap, &srcMap); err != nil {
return code, &srcMap, err
}
return code, &srcMap, err
}