Skip to content

Commit

Permalink
Support using deno as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 29, 2018
1 parent b6c0ad1 commit 0ea603d
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 39 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TS_FILES = \
v8worker2.d.ts

GO_FILES = \
cmd/main.go \
assets.go \
deno_dir.go \
deno_dir_test.go \
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main

import (
"github.com/ry/deno"
)

func main() {
deno.Init()
deno.Eval("deno_main.js", "denoMain()")
deno.Loop()
}
2 changes: 1 addition & 1 deletion deno_dir.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"crypto/md5"
Expand Down
2 changes: 1 addition & 1 deletion deno_dir_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"io/ioutil"
Expand Down
12 changes: 1 addition & 11 deletions dispatch.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"github.com/golang/protobuf/proto"
"github.com/ry/v8worker2"
"sync"
)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion echo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

// For testing
func InitEcho() {
Expand Down
2 changes: 1 addition & 1 deletion fetch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"github.com/golang/protobuf/proto"
Expand Down
2 changes: 1 addition & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"bytes"
Expand Down
41 changes: 26 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"flag"
Expand Down Expand Up @@ -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.
Expand All @@ -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()
}
3 changes: 1 addition & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ let startCalled = false;
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
mod.compileAndRun();
});
}

};
1 change: 1 addition & 0 deletions msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// All rights reserved. MIT License.
syntax = "proto3";
package main;
option go_package = "deno";

message BaseMsg {
string channel = 1;
Expand Down
2 changes: 1 addition & 1 deletion os.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"github.com/golang/protobuf/proto"
Expand Down
2 changes: 1 addition & 1 deletion os_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"path"
Expand Down
2 changes: 1 addition & 1 deletion timers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"github.com/golang/protobuf/proto"
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
package main
package deno

import (
"fmt"
Expand Down

0 comments on commit 0ea603d

Please sign in to comment.