/
javascript.go
326 lines (249 loc) · 6.76 KB
/
javascript.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2021, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package scripts
import (
"fmt"
"strings"
"sync"
"github.com/e154/smart-home/common/apperr"
"github.com/pkg/errors"
"github.com/dop251/goja"
. "github.com/e154/smart-home/common"
"github.com/e154/smart-home/system/scripts/eventloop"
)
// Javascript ...
type Javascript struct {
engine *Engine
compiler string
vm *goja.Runtime
loop *eventloop.EventLoop
program *goja.Program
lockPrograms sync.Mutex
programs map[string]*goja.Program
}
// NewJavascript ...
func NewJavascript(engine *Engine) *Javascript {
return &Javascript{
engine: engine,
programs: make(map[string]*goja.Program),
}
}
// Init ...
func (j *Javascript) Init() (err error) {
j.vm = goja.New()
j.vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
//j.vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
j.loop = eventloop.NewEventLoop(j.vm)
j.bind()
if j.engine.model.Compiled == "" {
return
}
if j.program, err = goja.Compile("", j.engine.model.Compiled, false); err != nil {
log.Error(err.Error())
}
return
}
// Close ...
func (j *Javascript) Close() {
}
// Compile ...
func (j *Javascript) Compile() (err error) {
if err = j.GetCompiler(); err != nil {
return
}
switch j.engine.model.Lang {
case ScriptLangTs:
var result goja.Value
result, err = j.tsCompile()
if err != nil {
return
}
j.engine.model.Compiled = result.String()
case ScriptLangCoffee:
var result goja.Value
result, err = j.coffeeCompile()
if err != nil {
return
}
j.engine.model.Compiled = result.String()
case ScriptLangJavascript:
j.engine.model.Compiled = j.engine.model.Source
}
j.program, err = goja.Compile("", j.engine.model.Compiled, false)
return
}
// GetCompiler ...
func (j *Javascript) GetCompiler() error {
switch j.engine.model.Lang {
case ScriptLangTs:
data, err := Asset("scripts/typescriptServices.js")
if err != nil {
log.Error(err.Error())
return err
}
j.compiler = string(data)
case ScriptLangCoffee:
data, err := Asset("scripts/coffee-script.js")
if err != nil {
log.Error(err.Error())
return err
}
j.compiler = string(data)
default:
}
return nil
}
func (j *Javascript) tsCompile() (result goja.Value, err error) {
if _, err = j.EvalString(j.compiler); err != nil {
return
}
const options = `{ target: ts.ScriptTarget.ES5, newLine: 1 }`
// prepare script to inline
doc := strings.Join(strings.Split(j.engine.model.Source, "\n"), `\n`)
doc = strings.Replace(doc, `"`, `\"`, -1)
var SRC = fmt.Sprintf(`ts.transpile("%s", %s);`, doc, options)
// compile from typescript to native script
var program *goja.Program
if program, err = goja.Compile("", SRC, false); err != nil {
return
}
result, err = j.vm.RunProgram(program)
return
}
func (j *Javascript) coffeeCompile() (result goja.Value, err error) {
if _, err = j.EvalString(j.compiler); err != nil {
return
}
// prepare script to inline
doc := strings.Join(strings.Split(j.engine.model.Source, "\n"), `\n`)
doc = strings.Replace(doc, `"`, `\"`, -1)
var SRC = fmt.Sprintf(`CoffeeScript.compile("%s", {"bare":true})`, doc)
// compile from coffee to native script
var program *goja.Program
if program, err = goja.Compile("", SRC, false); err != nil {
return
}
result, err = j.vm.RunProgram(program)
return
}
// Do ...
func (j *Javascript) Do() (result string, err error) {
result, err = j.unsafeRun(j.program)
return
}
// AssertFunction ...
func (j *Javascript) AssertFunction(f string, args ...interface{}) (result string, err error) {
defer func() {
if r := recover(); r != nil {
log.Warn("Recovered")
}
}()
if assertFunc, ok := goja.AssertFunction(j.vm.Get(f)); ok {
var value goja.Value
var gojaArgs []goja.Value
for _, arg := range args {
gojaArgs = append(gojaArgs, j.vm.ToValue(arg))
}
if value, err = assertFunc(goja.Undefined(), gojaArgs...); err != nil {
return
}
result = value.String()
}
return
}
// PushStruct ...
func (j *Javascript) PushStruct(name string, s interface{}) {
_ = j.vm.Set(name, s)
}
// PushFunction ...
func (j *Javascript) PushFunction(name string, s interface{}) {
_ = j.vm.Set(name, s)
}
// EvalString ...
func (j *Javascript) EvalString(src string) (result string, err error) {
var program *goja.Program
if program, err = goja.Compile("", src, false); err != nil {
return
}
result, err = j.unsafeRun(program)
return
}
func (j *Javascript) bind() {
//
// print()
// console()
// hex2arr()
// marshal(obj)
// unmarshal(json)
//
_ = j.vm.Set("print", log.Info)
_, _ = j.vm.RunString(`
console = {log:print,warn:print,error:print,info:print},
hex2arr = function (hexString) {
var result = [];
while (hexString.length >= 2) {
result.push(parseInt(hexString.substring(0, 2), 16));
hexString = hexString.substring(2, hexString.length);
}
return result;
};
unmarshal = function(j) { return JSON.parse(j); }
marshal = function(obj) { return JSON.stringify(obj); }
`)
for name, structure := range j.engine.structures.heap {
_ = j.vm.Set(name, structure)
}
for name, structure := range j.engine.functions.heap {
_ = j.vm.Set(name, structure)
}
}
// CreateProgram ...
func (j *Javascript) CreateProgram(name, source string) (err error) {
j.lockPrograms.Lock()
j.programs[name], err = goja.Compile("", source, false)
j.lockPrograms.Unlock()
return
}
// RunProgram ...
func (j *Javascript) RunProgram(name string) (result string, err error) {
j.lockPrograms.Lock()
defer j.lockPrograms.Unlock()
program, ok := j.programs[name]
if !ok {
err = errors.Wrap(apperr.ErrNotFound, fmt.Sprintf("name \"%s\"", name))
return
}
result, err = j.unsafeRun(program)
return
}
func (j *Javascript) unsafeRun(program *goja.Program) (result string, err error) {
var value goja.Value
wg := sync.WaitGroup{}
wg.Add(1)
j.loop.Run(func(vm *goja.Runtime) {
value, err = vm.RunProgram(program)
wg.Done()
})
wg.Wait()
if err != nil {
err = errors.Wrap(err, "unsafeRun")
return
}
result = value.String()
return
}