Skip to content

Commit

Permalink
Snapshot creation and various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jviksne committed Apr 12, 2020
1 parent fbe706f commit 55c9c69
Show file tree
Hide file tree
Showing 17 changed files with 696 additions and 155 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ include
libv8
v8/build/.gclient
v8/build/.gclient_entries
V8CBRIDGE.dll
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
# - V8_VERSION=6.2.414.42.1
# - V8_VERSION=6.0.286.54.3

go_import_path: github.com/augustoroman/v8
go_import_path: github.com/jviksne/v8go

# Need to download & compile v8 libraries before running the tests.
install: ./travis-install-linux.sh
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ FROM ${V8_SOURCE_IMAGE}:${V8_VERSION} as v8
# ------------ Build go v8 library and run tests ----------------------------
FROM golang as builder
# Copy the v8 code from the local disk, similar to:
# RUN go get github.com/augustoroman/v8 ||:
# RUN go get github.com/jviksne/v8go ||:
# but this allows using any local modifications.
ARG GO_V8_DIR=/go/src/github.com/augustoroman/v8/
ARG GO_V8_DIR=/go/src/github.com/jviksne/v8go/
ADD *.go *.h *.cc $GO_V8_DIR
ADD cmd $GO_V8_DIR/cmd/
ADD v8console $GO_V8_DIR/v8console/
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ln -s $(pwd)/data/vendor/v8/include $GOPATH/src/github.com/augustoroman/v8/inclu
ln -s $(pwd)/data/vendor/v8/out/x64.release $GOPATH/src/github.com/augustoroman/v8/libv8

# Run the tests to make sure everything works
cd $GOPATH/src/github.com/augustoroman/v8
cd $GOPATH/src/github.com/jviksne/v8go
go test
```

Expand Down Expand Up @@ -94,8 +94,8 @@ You need to build v8 statically and place it in a location cgo knows about. This
1. Build the bindings

```
go get github.com/augustoroman/v8
export V8_GO=$GOPATH/src/github.com/augustoroman/v8
go get github.com/jviksne/v8go
export V8_GO=$GOPATH/src/github.com/jviksne/v8go
export V8_BUILD=$V8_GO/v8/build #or wherever you like
mkdir -p $V8_BUILD
cd $V8_BUILD
Expand Down
40 changes: 34 additions & 6 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package v8
import "testing"

func BenchmarkGetValue(b *testing.B) {
ctx := NewIsolate().NewContext()

_, err := ctx.Eval(`var hello = "test"`, "bench.js")
iso, err := NewIsolate()
if err != nil {
b.Fatal(err)
}

ctx := iso.NewContext()

_, err = ctx.Eval(`var hello = "test"`, "bench.js")
if err != nil {
b.Fatal(err)
}
Expand All @@ -21,7 +27,13 @@ func BenchmarkGetValue(b *testing.B) {
}

func BenchmarkGetNumberValue(b *testing.B) {
ctx := NewIsolate().NewContext()

iso, err := NewIsolate()
if err != nil {
b.Fatal(err)
}

ctx := iso.NewContext()
val, err := ctx.Eval(`(157)`, "bench.js")
if err != nil {
b.Fatal(err)
Expand All @@ -38,7 +50,13 @@ func BenchmarkGetNumberValue(b *testing.B) {
}

func BenchmarkContextCreate(b *testing.B) {
ctx := NewIsolate().NewContext()

iso, err := NewIsolate()
if err != nil {
b.Fatal(err)
}

ctx := iso.NewContext()

b.ResetTimer()
for n := 0; n < b.N; n++ {
Expand All @@ -49,7 +67,11 @@ func BenchmarkContextCreate(b *testing.B) {
}

func BenchmarkEval(b *testing.B) {
iso := NewIsolate()
iso, err := NewIsolate()
if err != nil {
b.Fatal(err)
}

ctx := iso.NewContext()

script := `"hello"`
Expand All @@ -63,7 +85,13 @@ func BenchmarkEval(b *testing.B) {
}

func BenchmarkCallback(b *testing.B) {
ctx := NewIsolate().NewContext()
iso, err := NewIsolate()
if err != nil {
b.Fatal(err)
}

ctx := iso.NewContext()

ctx.Global().Set("cb", ctx.Bind("cb", func(in CallbackArgs) (*Value, error) {
return nil, nil
}))
Expand Down
4 changes: 2 additions & 2 deletions cmd/v8-runjs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"io/ioutil"
"os"

"github.com/augustoroman/v8"
"github.com/augustoroman/v8/v8console"
v8 "github.com/jviksne/v8go"
"github.com/jviksne/v8go/v8console"
"github.com/peterh/liner"
)

Expand Down
2 changes: 1 addition & 1 deletion docker-v8-lib/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This dockerfile will build v8 as static libraries suitable for being linked
# to the github.com/augustoroman/v8 go library bindings.
# to the github.com/jviksne/v8go go library bindings.
#
# The V8_VERSION arg is required and specifies which v8 git version to build.
# The recommended incantation to build this is:
Expand Down
2 changes: 1 addition & 1 deletion example_bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strconv"

"github.com/augustoroman/v8"
v8 "github.com/jviksne/v8go"
)

// AddAllNumbers is the callback function that we'll make accessible the JS VM.
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v8_test
import (
"fmt"

"github.com/augustoroman/v8"
v8 "github.com/jviksne/v8go"
)

func Example() {
Expand Down
33 changes: 24 additions & 9 deletions v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"sync/atomic"
)

// Callback is the signature for callback functions that are registered with a
Expand Down Expand Up @@ -111,7 +111,7 @@ func Init(icuDataFile string) {
}

func IsInit() bool {
if (atomic.LoadInt32(&isInit) == 1) {
if atomic.LoadInt32(&isInit) == 1 {
return true
}
return false
Expand Down Expand Up @@ -153,18 +153,33 @@ func RestoreSnapshotFromExport(data []byte) *Snapshot {
return newSnapshot(str)
}

/*
// CreateSnapshot creates a new Snapshot after running the supplied JS code.
// Because Snapshots cannot have refences to external code (no Go callbacks),
// all of the initialization code must be pure JS and supplied at once as the
// arg to this function.
func CreateSnapshot(js string) *Snapshot {
v8InitOnce.Do(func() { v8Init() })
js_ptr := C.CString(js)
defer C.free(unsafe.Pointer(js_ptr))
return newSnapshot(C.v8_CreateSnapshotDataBlob(js_ptr))
// isol can be null.
func CreateSnapshot(js string, includeCompiledFnCode bool, iso *Isolate) (*Snapshot, error) {
//v8InitOnce.Do(func() { v8Init() })
if !IsInit() {
return nil, fmt.Errorf("V8 not init")
}

jsPtr := C.CString(js)
defer C.free(unsafe.Pointer(jsPtr))

var isoPtr C.IsolatePtr

if iso != nil {
isoPtr = iso.ptr
}

includeCompiledFnCodeInt := 0
if includeCompiledFnCode {
includeCompiledFnCodeInt = 1
}

return newSnapshot(C.v8_CreateSnapshotDataBlob(jsPtr, C.int(includeCompiledFnCodeInt), isoPtr)), nil
}
*/

// Isolate represents a single-threaded V8 engine instance. It can run multiple
// independent Contexts and V8 values can be freely shared between the Contexts,
Expand Down
Loading

0 comments on commit 55c9c69

Please sign in to comment.