mirror of
https://github.com/denoland/deno.git
synced 2025-02-12 16:59:32 -05:00
Support using deno as a library
This commit is contained in:
parent
b6c0ad15fa
commit
0ea603d96e
16 changed files with 58 additions and 39 deletions
6
Makefile
6
Makefile
|
@ -18,6 +18,7 @@ TS_FILES = \
|
||||||
v8worker2.d.ts
|
v8worker2.d.ts
|
||||||
|
|
||||||
GO_FILES = \
|
GO_FILES = \
|
||||||
|
cmd/main.go \
|
||||||
assets.go \
|
assets.go \
|
||||||
deno_dir.go \
|
deno_dir.go \
|
||||||
deno_dir_test.go \
|
deno_dir_test.go \
|
||||||
|
@ -31,13 +32,14 @@ GO_FILES = \
|
||||||
timers.go \
|
timers.go \
|
||||||
util.go
|
util.go
|
||||||
|
|
||||||
|
|
||||||
deno: msg.pb.go $(GO_FILES)
|
deno: msg.pb.go $(GO_FILES)
|
||||||
go build -o deno
|
go build -o deno ./cmd
|
||||||
|
|
||||||
assets.go: dist/main.js
|
assets.go: dist/main.js
|
||||||
cp node_modules/typescript/lib/lib.*d.ts dist/
|
cp node_modules/typescript/lib/lib.*d.ts dist/
|
||||||
cp deno.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
|
msg.pb.go: msg.proto
|
||||||
protoc --go_out=. msg.proto
|
protoc --go_out=. msg.proto
|
||||||
|
|
|
@ -40,6 +40,9 @@ A JavaScript runtime using V8 6.8 and Go.
|
||||||
|
|
||||||
* Aims to be browser compatible.
|
* 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
|
## Status
|
||||||
|
|
||||||
|
|
13
cmd/main.go
Normal file
13
cmd/main.go
Normal file
|
@ -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()
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
12
dispatch.go
12
dispatch.go
|
@ -1,10 +1,9 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/ry/v8worker2"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,19 +19,10 @@ var stats struct {
|
||||||
v8workerBytesRecv int
|
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)
|
var channels = make(map[string][]Subscriber)
|
||||||
|
|
||||||
type Subscriber func(payload []byte) []byte
|
type Subscriber func(payload []byte) []byte
|
||||||
|
|
||||||
func createWorker() {
|
|
||||||
worker = v8worker2.New(recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func recv(buf []byte) (response []byte) {
|
func recv(buf []byte) (response []byte) {
|
||||||
stats.v8workerRecv++
|
stats.v8workerRecv++
|
||||||
stats.v8workerBytesRecv += len(buf)
|
stats.v8workerBytesRecv += len(buf)
|
||||||
|
|
2
echo.go
2
echo.go
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
// For testing
|
// For testing
|
||||||
func InitEcho() {
|
func InitEcho() {
|
||||||
|
|
2
fetch.go
2
fetch.go
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
41
main.go
41
main.go
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
@ -34,8 +34,16 @@ func FlagsParse() []string {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
// There is a single global worker for this process.
|
||||||
args := FlagsParse()
|
// 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.
|
// Maybe start Golang CPU profiler.
|
||||||
// Use --prof for profiling JS.
|
// Use --prof for profiling JS.
|
||||||
|
@ -49,33 +57,36 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
createDirs()
|
createDirs()
|
||||||
createWorker()
|
|
||||||
|
|
||||||
InitOS()
|
InitOS()
|
||||||
InitEcho()
|
InitEcho()
|
||||||
InitTimers()
|
InitTimers()
|
||||||
InitFetch()
|
InitFetch()
|
||||||
|
|
||||||
main_js := stringAsset("main.js")
|
worker = v8worker2.New(recv)
|
||||||
|
|
||||||
|
main_js = stringAsset("main.js")
|
||||||
err := worker.Load("/main.js", main_js)
|
err := worker.Load("/main.js", main_js)
|
||||||
exitOnError(err)
|
exitOnError(err)
|
||||||
main_map := stringAsset("main.map")
|
main_map = stringAsset("main.map")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Loop() {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
err = worker.Load("deno_main.js", "denoMain()")
|
|
||||||
exitOnError(err)
|
|
||||||
|
|
||||||
var command = Msg_START // TODO use proto3
|
|
||||||
PubMsg("start", &Msg{
|
PubMsg("start", &Msg{
|
||||||
Command: command,
|
Command: Msg_START,
|
||||||
StartCwd: cwd,
|
StartCwd: cwd,
|
||||||
StartArgv: args,
|
StartArgv: workerArgs,
|
||||||
StartDebugFlag: *flagDebug,
|
StartDebugFlag: *flagDebug,
|
||||||
StartMainJs: main_js,
|
StartMainJs: main_js,
|
||||||
StartMainMap: main_map,
|
StartMainMap: main_map,
|
||||||
})
|
})
|
||||||
|
|
||||||
DispatchLoop()
|
DispatchLoop()
|
||||||
}
|
}
|
||||||
|
|
3
main.ts
3
main.ts
|
@ -47,5 +47,4 @@ let startCalled = false;
|
||||||
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
|
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
|
||||||
mod.compileAndRun();
|
mod.compileAndRun();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
package main;
|
package main;
|
||||||
|
option go_package = "deno";
|
||||||
|
|
||||||
message BaseMsg {
|
message BaseMsg {
|
||||||
string channel = 1;
|
string channel = 1;
|
||||||
|
|
2
os.go
2
os.go
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
2
util.go
2
util.go
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||||
// All rights reserved. MIT License.
|
// All rights reserved. MIT License.
|
||||||
package main
|
package deno
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
Loading…
Add table
Reference in a new issue