Skip to content

Commit

Permalink
Command line flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 19, 2018
1 parent aeb85ef commit 83f436e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 10 deletions.
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/ry/v8worker2"
"io"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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,
},
},
})
Expand Down
14 changes: 10 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions modules_test.go
Original file line number Diff line number Diff line change
@@ -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:https://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:https://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")
}
1 change: 1 addition & 0 deletions msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ message Msg {
message StartMsg {
string cwd = 1;
repeated string argv = 2;
bool debug_flag = 3;
}

message SourceCodeFetchMsg {
Expand Down
2 changes: 2 additions & 0 deletions runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type TypedArray = Uint8Array | Float32Array | Int32Array;

export interface ModuleInfo {
moduleName?: string;
filename?: string;
sourceCode?: string;
outputCode?: string;
}
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func Assert(cond bool, msg string) {
if !cond {
panic(msg)
}
}
3 changes: 1 addition & 2 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 83f436e

Please sign in to comment.