summaryrefslogtreecommitdiff
path: root/main.go
blob: bb2e32205d675e2532151b27cbd7f3d5f980be35 (plain)
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
package main

import (
	"flag"
	"github.com/ry/v8worker2"
	"os"
	"runtime/pprof"
)

var flagReload = flag.Bool("reload", false, "Reload cached remote source code.")
var flagV8Options = flag.Bool("v8-options", false, "Print V8 command line options.")
var flagDebug = flag.Bool("debug", false, "Enable debug output.")
var flagGoProf = flag.String("goprof", "", "Write golang cpu profile to file.")

func stringAsset(path string) string {
	data, err := Asset("dist/" + path)
	check(err)
	return string(data)
}

func FlagsParse() []string {
	flag.Parse()
	args := flag.Args()
	if *flagV8Options {
		args = append(args, "--help")
	}
	args = v8worker2.SetFlags(args)

	return args
}

func main() {
	args := FlagsParse()

	// Maybe start Golang CPU profiler.
	// Use --prof for profiling JS.
	if *flagGoProf != "" {
		f, err := os.Create(*flagGoProf)
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	createDirs()
	createWorker()

	InitOS()
	InitEcho()
	InitTimers()

	main_js := stringAsset("main.js")
	err := worker.Load("/main.js", main_js)
	exitOnError(err)
	main_map := stringAsset("main.map")

	cwd, err := os.Getwd()
	check(err)

	var command = Msg_START // TODO use proto3
	PubMsg("start", &Msg{
		Command:        command,
		StartCwd:       cwd,
		StartArgv:      args,
		StartDebugFlag: *flagDebug,
		StartMainJs:    main_js,
		StartMainMap:   main_map,
	})

	DispatchLoop()
}