diff --git a/main.go b/main.go index f8bdf08993..d299a48abb 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "crypto/md5" "encoding/hex" + "flag" + "fmt" "github.com/golang/protobuf/proto" "github.com/ry/v8worker2" "io" @@ -17,6 +19,10 @@ import ( "time" ) +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 DenoDir string var CompileDir string var SrcDir string @@ -44,12 +50,14 @@ func IsRemote(filename string) bool { // Fetches a remoteUrl but also caches it to the localFilename. func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) { + //println("FetchRemoteSource", remoteUrl) Assert(strings.HasPrefix(localFilename, SrcDir), localFilename) var sourceReader io.Reader file, err := os.Open(localFilename) - if os.IsNotExist(err) { + if *flagReload || os.IsNotExist(err) { // Fetch from HTTP. + println("Downloading", remoteUrl) res, err := http.Get(remoteUrl) if err != nil { return nil, err @@ -100,6 +108,7 @@ func ResolveModule(moduleSpecifier string, containingFile string) ( const assetPrefix string = "/$asset$/" func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) { + Assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty") res := &Msg{} var sourceCodeBuf []byte var err error @@ -117,6 +126,9 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [ return } + //println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier, + // "containingFile", containingFile, "filename", filename) + if IsRemote(moduleName) { sourceCodeBuf, err = FetchRemoteSource(moduleName, filename) } else if strings.HasPrefix(moduleName, assetPrefix) { @@ -249,7 +261,14 @@ func recv(buf []byte) []byte { } func main() { - args := v8worker2.SetFlags(os.Args) + flag.Parse() + args := flag.Args() + if *flagV8Options { + args = append(args, "--help") + fmt.Println(args) + } + args = v8worker2.SetFlags(args) + createDirs() worker := v8worker2.New(recv) loadAsset(worker, "dist/main.js") @@ -262,8 +281,9 @@ func main() { out, err := proto.Marshal(&Msg{ Payload: &Msg_Start{ Start: &StartMsg{ - Cwd: cwd, - Argv: args, + Cwd: cwd, + Argv: args, + DebugFlag: *flagDebug, }, }, }) diff --git a/main.ts b/main.ts index 9fe002d7e3..ac0b4d3292 100644 --- a/main.ts +++ b/main.ts @@ -2,10 +2,16 @@ import { main as pb } from "./msg.pb"; import "./util"; import * as runtime from "./runtime"; import * as timers from "./timers"; +import * as util from "./util"; -function start(cwd: string, argv: string[]): void { - // TODO parse arguments. - const inputFn = argv[1]; +// To control internal logging output +// Set with the -debug command-line flag. +export let debug = false; + +function start(cwd: string, argv: string[], debugFlag: boolean): void { + debug = debugFlag; + util.log("start", { cwd, argv, debugFlag }); + const inputFn = argv[0]; const mod = runtime.resolveModule(inputFn, cwd + "/"); mod.compileAndRun(); } @@ -14,7 +20,7 @@ V8Worker2.recv((ab: ArrayBuffer) => { const msg = pb.Msg.decode(new Uint8Array(ab)); switch (msg.payload) { case "start": - start(msg.start.cwd, msg.start.argv); + start(msg.start.cwd, msg.start.argv, msg.start.debugFlag); break; case "timerReady": timers.timerReady(msg.timerReady.id, msg.timerReady.done); diff --git a/modules_test.go b/modules_test.go new file mode 100644 index 0000000000..ac94b5a3c5 --- /dev/null +++ b/modules_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "path" + "testing" +) + +func AssertEqual(t *testing.T, actual string, expected string) { + if actual != expected { + t.Fatalf("not equal <<%s>> <<%s>>", actual, expected) + } +} + +func TestResolveModule(t *testing.T) { + moduleName, filename, err := ResolveModule( + "http://localhost:4545/testdata/subdir/print_hello.ts", + "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "http://localhost:4545/testdata/subdir/print_hello.ts") + AssertEqual(t, filename, + path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts")) + + moduleName, filename, err = ResolveModule( + "./subdir/print_hello.ts", + "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") + AssertEqual(t, filename, + "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") + + // In the case where the containingFile is a directory (indicated with a + // trailing slash) + moduleName, filename, err = ResolveModule( + "testdata/001_hello.js", + "/Users/rld/go/src/github.com/ry/deno/") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") + AssertEqual(t, filename, + "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") +} diff --git a/msg.proto b/msg.proto index 8524be2877..dba8cab942 100644 --- a/msg.proto +++ b/msg.proto @@ -17,6 +17,7 @@ message Msg { message StartMsg { string cwd = 1; repeated string argv = 2; + bool debug_flag = 3; } message SourceCodeFetchMsg { diff --git a/runtime.ts b/runtime.ts index 01dbf76ac2..7d6633ca9c 100644 --- a/runtime.ts +++ b/runtime.ts @@ -99,8 +99,10 @@ export function resolveModule( moduleSpecifier: string, containingFile: string ): FileModule { + util.assert(moduleSpecifier != null && moduleSpecifier.length > 0); // We ask golang to sourceCodeFetch. It will load the sourceCode and if // there is any outputCode cached, it will return that as well. + util.log("resolveModule", { moduleSpecifier, containingFile }); const { filename, sourceCode, outputCode } = os.sourceCodeFetch( moduleSpecifier, containingFile diff --git a/types.ts b/types.ts new file mode 100644 index 0000000000..8631d2c843 --- /dev/null +++ b/types.ts @@ -0,0 +1,8 @@ +export type TypedArray = Uint8Array | Float32Array | Int32Array; + +export interface ModuleInfo { + moduleName?: string; + filename?: string; + sourceCode?: string; + outputCode?: string; +} diff --git a/util.go b/util.go new file mode 100644 index 0000000000..851ee34753 --- /dev/null +++ b/util.go @@ -0,0 +1,7 @@ +package main + +func Assert(cond bool, msg string) { + if !cond { + panic(msg) + } +} diff --git a/util.ts b/util.ts index 42163fb417..0b4b9adbcb 100644 --- a/util.ts +++ b/util.ts @@ -13,8 +13,7 @@ _global["window"] = _global; // Create a window object. const print = V8Worker2.print; -// To control internal logging output -const debug = false; +import { debug } from "./main"; // Internal logging for deno. Use the "debug" variable above to control // output.