This repository was archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (78 loc) · 1.88 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/olebedev/go-duktape.v2"
"io/ioutil"
"os"
"path/filepath"
"unsafe"
)
type Command struct {
Command string `json:"command"`
Key string `json:"key"`
Type string `json":type"`
}
func loadModule(moduleId string) ([]byte, error) {
sourceFile := moduleId
if filepath.Ext(moduleId) != ".js" {
if _, err := os.Stat(moduleId + ".js"); err == nil {
sourceFile = moduleId + ".js"
}
}
if src, err := ioutil.ReadFile(sourceFile); err == nil {
return src, nil
}
return nil, errors.New("Could not find module")
}
func main() {
ctx := duktape.New()
ctx.PushGlobalObject()
processIdx := ctx.PushObject()
envIdx := ctx.PushObject()
ctx.PushString("development")
ctx.PutPropString(envIdx, "NODE_ENV")
ctx.PutPropString(processIdx, "env")
ctx.PutPropString(-2, "process")
var getCommandRef unsafe.Pointer
goIdx := ctx.PushObject()
ctx.PushGoFunction(func(c *duktape.Context) int {
command := Command{}
jsonBlob := c.SafeToString(-1)
json.Unmarshal([]byte(jsonBlob), &command)
switch command.Command {
case "configure":
c.PushHeapptr(getCommandRef)
if res := c.Pcall(0); res != 0 {
fmt.Println(c.SafeToString(-1))
}
c.Pop()
}
return 0
})
ctx.PutPropString(goIdx, "sendCommand")
ctx.PushGoFunction(func(c *duktape.Context) int {
getCommandRef = c.GetHeapptr(0)
c.PutGlobalString("_get_command_ref")
return 0
})
ctx.PutPropString(goIdx, "getCommandFunc")
ctx.PutPropString(-2, "Go")
ctx.Pop()
ctx.PushGlobalGoFunction("loadModule", func(c *duktape.Context) int {
moduleId := c.SafeToString(-1)
if src, err := loadModule(moduleId); err == nil {
ctx.PushString(string(src))
} else {
fmt.Println(err)
}
return 1
})
if err := ctx.PevalFile("bootstrap.js"); err != nil {
fmt.Println(err)
}
if err := ctx.PevalFile("example.js"); err != nil {
fmt.Println(err)
}
}