-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (45 loc) · 1.27 KB
/
main.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
/*
* Copyright SeatGeek
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
*/
// + build js,wasm
package main
import (
"fmt"
"syscall/js"
"github.com/seatgeek/node-hcl/hcl"
)
var jsGlobal js.Value
var jsRoot js.Value
const (
bridgeJavaScriptName = "__node_hcl_wasm__"
)
func registerFn(name string, callback func(this js.Value, args []js.Value) (interface{}, error)) {
jsRoot.Set(name, js.FuncOf(registrationWrapper(callback)))
}
func registrationWrapper(fn func(this js.Value, args []js.Value) (interface{}, error)) func(this js.Value, args []js.Value) interface{} {
return func(this js.Value, args []js.Value) interface{} {
cb := args[len(args)-1]
ret, err := fn(this, args[:len(args)-1])
if err != nil {
cb.Invoke(err.Error(), js.Null())
} else {
cb.Invoke(js.Null(), ret)
}
return ret
}
}
func main() {
jsGlobal = js.Global().Get("global")
jsRoot = jsGlobal.Get(bridgeJavaScriptName)
c := make(chan struct{}, 0)
registerFn("merge", func(this js.Value, args []js.Value) (interface{}, error) {
if len(args) < 2 {
return nil, fmt.Errorf("Not enough arguments, expected (2)")
}
aHclString := args[0].String()
bHclString := args[1].String()
return hcl.Merge(aHclString, bHclString)
})
<-c
}