1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00
denoland-deno/main.go

141 lines
3.1 KiB
Go
Raw Normal View History

2018-05-28 21:50:44 -04:00
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
2018-05-29 04:28:32 -04:00
package deno
2018-05-13 23:32:01 -04:00
import (
2018-05-19 05:38:51 -04:00
"flag"
2018-05-29 06:30:16 -04:00
"fmt"
2018-05-14 00:31:48 -04:00
"os"
2018-05-22 11:42:56 -04:00
"runtime/pprof"
2018-06-09 14:51:17 -04:00
"github.com/ry/v8worker2"
2018-05-13 23:32:01 -04:00
)
2018-05-19 05:38:51 -04:00
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.")
2018-06-09 14:51:17 -04:00
var flagCPUProf = flag.String("cpuprof", "", "Write golang cpu profile to file.")
var flagMemProf = flag.String("memprof", "", "Write golang memory profile to file.")
2018-05-19 05:38:51 -04:00
2018-05-29 05:27:41 -04:00
var flagAllowRead = flag.Bool("allow-read", true,
"Allow program to read file system.")
var flagAllowWrite = flag.Bool("allow-write", false,
"Allow program to write to the fs.")
var flagAllowNet = flag.Bool("allow-net", false,
"Allow program to make network connection.")
2018-05-29 05:27:41 -04:00
2018-06-09 14:51:17 -04:00
var memProfile *os.File
2018-05-29 05:27:41 -04:00
var Perms struct {
FsRead bool
FsWrite bool
Net bool
2018-05-29 05:27:41 -04:00
}
func setPerms() {
Perms.FsRead = *flagAllowRead
Perms.FsWrite = *flagAllowWrite
Perms.Net = *flagAllowNet
2018-05-29 05:27:41 -04:00
}
2018-05-21 17:33:33 -04:00
func stringAsset(path string) string {
data, err := Asset("dist/" + path)
check(err)
return string(data)
}
2018-05-24 10:25:38 -04:00
func FlagsParse() []string {
2018-05-19 05:38:51 -04:00
flag.Parse()
args := flag.Args()
2018-05-29 05:27:41 -04:00
setPerms()
2018-05-19 05:38:51 -04:00
if *flagV8Options {
args = append(args, "--help")
}
// Adding this causes testdata/007_stack_trace.ts to fail without a
// stacktrace.
// args = append(args, "--abort-on-uncaught-exception")
2018-05-19 05:38:51 -04:00
args = v8worker2.SetFlags(args)
2018-05-24 10:25:38 -04:00
return args
}
2018-05-29 04:28:32 -04:00
// There is a single global worker for this process.
// This file should be the only part of deno that directly access it, so that
// all interaction with V8 can go through a single point.
var worker *v8worker2.Worker
var workerArgs []string
var main_js string
var main_map string
func Init() {
workerArgs = FlagsParse()
2018-05-23 13:23:29 -04:00
2018-05-29 06:30:16 -04:00
if len(workerArgs) == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s file.ts\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
2018-06-09 14:51:17 -04:00
// Maybe start Golang profilers.
2018-05-22 11:42:56 -04:00
// Use --prof for profiling JS.
2018-06-09 14:51:17 -04:00
StartProfiling()
2018-05-22 11:42:56 -04:00
createDirs()
2018-05-21 23:00:36 -04:00
InitOS()
2018-05-23 16:45:01 -04:00
InitEcho()
2018-05-21 23:00:36 -04:00
InitTimers()
2018-05-27 03:46:18 -04:00
InitFetch()
2018-05-21 17:33:33 -04:00
2018-05-29 04:28:32 -04:00
worker = v8worker2.New(recv)
main_js = stringAsset("main.js")
2018-05-23 11:27:56 -04:00
err := worker.Load("/main.js", main_js)
exitOnError(err)
2018-05-29 04:28:32 -04:00
main_map = stringAsset("main.map")
}
2018-06-09 14:51:17 -04:00
func StartProfiling() {
if *flagCPUProf != "" {
cpuProfile, err := os.Create(*flagCPUProf)
check(err)
check(pprof.StartCPUProfile(cpuProfile))
}
if *flagMemProf != "" {
var err error
memProfile, err = os.Create(*flagMemProf)
check(err)
check(pprof.WriteHeapProfile(memProfile))
}
}
func stopProfiling() {
if *flagCPUProf != "" {
pprof.StopCPUProfile()
}
if *flagMemProf != "" {
check(memProfile.Close())
}
}
2018-05-29 04:28:32 -04:00
// It's up to library users to call
// deno.Eval("deno_main.js", "denoMain()")
func Eval(filename string, code string) {
err := worker.Load(filename, code)
2018-05-29 03:43:54 -04:00
exitOnError(err)
2018-05-29 04:28:32 -04:00
}
2018-05-29 03:43:54 -04:00
2018-05-29 04:28:32 -04:00
func Loop() {
cwd, err := os.Getwd()
check(err)
2018-05-23 17:23:50 -04:00
PubMsg("start", &Msg{
2018-05-29 04:28:32 -04:00
Command: Msg_START,
2018-05-25 15:30:09 -04:00
StartCwd: cwd,
2018-05-29 04:28:32 -04:00
StartArgv: workerArgs,
2018-05-25 15:30:09 -04:00
StartDebugFlag: *flagDebug,
StartMainJs: main_js,
StartMainMap: main_map,
2018-05-14 13:02:47 -04:00
})
2018-05-21 22:07:40 -04:00
DispatchLoop()
2018-06-09 14:51:17 -04:00
stopProfiling()
2018-05-13 23:32:01 -04:00
}