summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--README.md3
-rw-r--r--cmd/main.go13
-rw-r--r--deno_dir.go2
-rw-r--r--deno_dir_test.go2
-rw-r--r--dispatch.go12
-rw-r--r--echo.go2
-rw-r--r--fetch.go2
-rw-r--r--integration_test.go2
-rw-r--r--main.go41
-rw-r--r--main.ts3
-rw-r--r--msg.proto1
-rw-r--r--os.go2
-rw-r--r--os_test.go2
-rw-r--r--timers.go2
-rw-r--r--util.go2
16 files changed, 58 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index e7103039e..437e8882f 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,7 @@ TS_FILES = \
v8worker2.d.ts
GO_FILES = \
+ cmd/main.go \
assets.go \
deno_dir.go \
deno_dir_test.go \
@@ -31,13 +32,14 @@ GO_FILES = \
timers.go \
util.go
+
deno: msg.pb.go $(GO_FILES)
- go build -o deno
+ go build -o deno ./cmd
assets.go: dist/main.js
cp node_modules/typescript/lib/lib.*d.ts dist/
cp deno.d.ts dist/
- go-bindata -pkg main -o assets.go dist/
+ go-bindata -pkg deno -o assets.go dist/
msg.pb.go: msg.proto
protoc --go_out=. msg.proto
diff --git a/README.md b/README.md
index 8a5d03dfc..fa34da225 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,9 @@ A JavaScript runtime using V8 6.8 and Go.
* Aims to be browser compatible.
+* Can be used as a library to easily build your own JavaScript runtime.
+ https://github.com/ry/deno/blob/master/cmd/main.go
+
## Status
diff --git a/cmd/main.go b/cmd/main.go
new file mode 100644
index 000000000..585cd1259
--- /dev/null
+++ b/cmd/main.go
@@ -0,0 +1,13 @@
+// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
+// All rights reserved. MIT License.
+package main
+
+import (
+ "github.com/ry/deno"
+)
+
+func main() {
+ deno.Init()
+ deno.Eval("deno_main.js", "denoMain()")
+ deno.Loop()
+}
diff --git a/deno_dir.go b/deno_dir.go
index 7f9dac445..166fcb827 100644
--- a/deno_dir.go
+++ b/deno_dir.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"crypto/md5"
diff --git a/deno_dir_test.go b/deno_dir_test.go
index 404d589cb..51a6fb016 100644
--- a/deno_dir_test.go
+++ b/deno_dir_test.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"io/ioutil"
diff --git a/dispatch.go b/dispatch.go
index a6308709e..a5728ad81 100644
--- a/dispatch.go
+++ b/dispatch.go
@@ -1,10 +1,9 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"github.com/golang/protobuf/proto"
- "github.com/ry/v8worker2"
"sync"
)
@@ -20,19 +19,10 @@ var stats struct {
v8workerBytesRecv int
}
-// 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 channels = make(map[string][]Subscriber)
type Subscriber func(payload []byte) []byte
-func createWorker() {
- worker = v8worker2.New(recv)
-}
-
func recv(buf []byte) (response []byte) {
stats.v8workerRecv++
stats.v8workerBytesRecv += len(buf)
diff --git a/echo.go b/echo.go
index f1036478a..29ec11138 100644
--- a/echo.go
+++ b/echo.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
// For testing
func InitEcho() {
diff --git a/fetch.go b/fetch.go
index 6f6f07c44..ecd3ec9ea 100644
--- a/fetch.go
+++ b/fetch.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"github.com/golang/protobuf/proto"
diff --git a/integration_test.go b/integration_test.go
index 28411e6da..5b96c0abc 100644
--- a/integration_test.go
+++ b/integration_test.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"bytes"
diff --git a/main.go b/main.go
index 9e5b6349c..3069c0380 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"flag"
@@ -34,8 +34,16 @@ func FlagsParse() []string {
return args
}
-func main() {
- args := FlagsParse()
+// 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()
// Maybe start Golang CPU profiler.
// Use --prof for profiling JS.
@@ -49,33 +57,36 @@ func main() {
}
createDirs()
- createWorker()
-
InitOS()
InitEcho()
InitTimers()
InitFetch()
- main_js := stringAsset("main.js")
+ worker = v8worker2.New(recv)
+
+ 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)
+ main_map = stringAsset("main.map")
+}
- err = worker.Load("deno_main.js", "denoMain()")
+// 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)
exitOnError(err)
+}
- var command = Msg_START // TODO use proto3
+func Loop() {
+ cwd, err := os.Getwd()
+ check(err)
PubMsg("start", &Msg{
- Command: command,
+ Command: Msg_START,
StartCwd: cwd,
- StartArgv: args,
+ StartArgv: workerArgs,
StartDebugFlag: *flagDebug,
StartMainJs: main_js,
StartMainMap: main_map,
})
-
DispatchLoop()
}
diff --git a/main.ts b/main.ts
index ddcbebb13..0a7b79bd3 100644
--- a/main.ts
+++ b/main.ts
@@ -47,5 +47,4 @@ let startCalled = false;
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
mod.compileAndRun();
});
-}
-
+};
diff --git a/msg.proto b/msg.proto
index 0a802749b..755d22e53 100644
--- a/msg.proto
+++ b/msg.proto
@@ -2,6 +2,7 @@
// All rights reserved. MIT License.
syntax = "proto3";
package main;
+option go_package = "deno";
message BaseMsg {
string channel = 1;
diff --git a/os.go b/os.go
index d28cf5e9c..7eebe41bd 100644
--- a/os.go
+++ b/os.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"github.com/golang/protobuf/proto"
diff --git a/os_test.go b/os_test.go
index 1f9ae69e7..e04f8c820 100644
--- a/os_test.go
+++ b/os_test.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"path"
diff --git a/timers.go b/timers.go
index ba751712d..f1525b97c 100644
--- a/timers.go
+++ b/timers.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"github.com/golang/protobuf/proto"
diff --git a/util.go b/util.go
index 873bf28b0..2662a256b 100644
--- a/util.go
+++ b/util.go
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
-package main
+package deno
import (
"fmt"