Skip to content

Commit 0ea603d

Browse files
committed
Support using deno as a library
1 parent b6c0ad1 commit 0ea603d

16 files changed

+58
-39
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ TS_FILES = \
1818
v8worker2.d.ts
1919

2020
GO_FILES = \
21+
cmd/main.go \
2122
assets.go \
2223
deno_dir.go \
2324
deno_dir_test.go \
@@ -31,13 +32,14 @@ GO_FILES = \
3132
timers.go \
3233
util.go
3334

35+
3436
deno: msg.pb.go $(GO_FILES)
35-
go build -o deno
37+
go build -o deno ./cmd
3638

3739
assets.go: dist/main.js
3840
cp node_modules/typescript/lib/lib.*d.ts dist/
3941
cp deno.d.ts dist/
40-
go-bindata -pkg main -o assets.go dist/
42+
go-bindata -pkg deno -o assets.go dist/
4143

4244
msg.pb.go: msg.proto
4345
protoc --go_out=. msg.proto

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ A JavaScript runtime using V8 6.8 and Go.
4040

4141
* Aims to be browser compatible.
4242

43+
* Can be used as a library to easily build your own JavaScript runtime.
44+
https://github.com/ry/deno/blob/master/cmd/main.go
45+
4346

4447
## Status
4548

cmd/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 Ryan Dahl <[email protected]>
2+
// All rights reserved. MIT License.
3+
package main
4+
5+
import (
6+
"github.com/ry/deno"
7+
)
8+
9+
func main() {
10+
deno.Init()
11+
deno.Eval("deno_main.js", "denoMain()")
12+
deno.Loop()
13+
}

deno_dir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"crypto/md5"

deno_dir_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"io/ioutil"

dispatch.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"github.com/golang/protobuf/proto"
7-
"github.com/ry/v8worker2"
87
"sync"
98
)
109

@@ -20,19 +19,10 @@ var stats struct {
2019
v8workerBytesRecv int
2120
}
2221

23-
// There is a single global worker for this process.
24-
// This file should be the only part of deno that directly access it, so that
25-
// all interaction with V8 can go through a single point.
26-
var worker *v8worker2.Worker
27-
2822
var channels = make(map[string][]Subscriber)
2923

3024
type Subscriber func(payload []byte) []byte
3125

32-
func createWorker() {
33-
worker = v8worker2.New(recv)
34-
}
35-
3626
func recv(buf []byte) (response []byte) {
3727
stats.v8workerRecv++
3828
stats.v8workerBytesRecv += len(buf)

echo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
// For testing
66
func InitEcho() {

fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"github.com/golang/protobuf/proto"

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"bytes"

main.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018 Ryan Dahl <[email protected]>
22
// All rights reserved. MIT License.
3-
package main
3+
package deno
44

55
import (
66
"flag"
@@ -34,8 +34,16 @@ func FlagsParse() []string {
3434
return args
3535
}
3636

37-
func main() {
38-
args := FlagsParse()
37+
// There is a single global worker for this process.
38+
// This file should be the only part of deno that directly access it, so that
39+
// all interaction with V8 can go through a single point.
40+
var worker *v8worker2.Worker
41+
var workerArgs []string
42+
var main_js string
43+
var main_map string
44+
45+
func Init() {
46+
workerArgs = FlagsParse()
3947

4048
// Maybe start Golang CPU profiler.
4149
// Use --prof for profiling JS.
@@ -49,33 +57,36 @@ func main() {
4957
}
5058

5159
createDirs()
52-
createWorker()
53-
5460
InitOS()
5561
InitEcho()
5662
InitTimers()
5763
InitFetch()
5864

59-
main_js := stringAsset("main.js")
65+
worker = v8worker2.New(recv)
66+
67+
main_js = stringAsset("main.js")
6068
err := worker.Load("/main.js", main_js)
6169
exitOnError(err)
62-
main_map := stringAsset("main.map")
63-
64-
cwd, err := os.Getwd()
65-
check(err)
70+
main_map = stringAsset("main.map")
71+
}
6672

67-
err = worker.Load("deno_main.js", "denoMain()")
73+
// It's up to library users to call
74+
// deno.Eval("deno_main.js", "denoMain()")
75+
func Eval(filename string, code string) {
76+
err := worker.Load(filename, code)
6877
exitOnError(err)
78+
}
6979

70-
var command = Msg_START // TODO use proto3
80+
func Loop() {
81+
cwd, err := os.Getwd()
82+
check(err)
7183
PubMsg("start", &Msg{
72-
Command: command,
84+
Command: Msg_START,
7385
StartCwd: cwd,
74-
StartArgv: args,
86+
StartArgv: workerArgs,
7587
StartDebugFlag: *flagDebug,
7688
StartMainJs: main_js,
7789
StartMainMap: main_map,
7890
})
79-
8091
DispatchLoop()
8192
}

0 commit comments

Comments
 (0)