Skip to content

Commit

Permalink
fix: workaround for crashes loading v8 snapshot (#101) (#115)
Browse files Browse the repository at this point in the history
* fix: workaround for crashes loading v8 snapshot (#101)

* Build latest stable node on travis
  • Loading branch information
mrkurt authored Jul 18, 2018
1 parent 5508b60 commit 57d851f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
39 changes: 17 additions & 22 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
language: node_js
compiler: clang
matrix:
allow_failures:
- osx_image: xcode9.4
include:
- os: linux
node_js: "8"
addons:
addons: &linux_addons
apt:
sources:
- ubuntu-toolchain-r-test
Expand All @@ -16,28 +13,26 @@ matrix:
- autoconf
- libtool
- nasm
env:
- MATRIX_EVAL="export CXX=g++-5"
- os: linux
node_js: "10.3"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-5
- g++-5
- autoconf
- libtool
- nasm
env:
- MATRIX_EVAL="export CXX=g++-5"
env: &linux_env
- MATRIX_EVAL="export CXX=g++-5"
node_js: 8
- ox: linux
addons: *linux_addons
env: *linux_env
node_js: 10.3
- ox: linux
addons: *linux_addons
env: *linux_env
node_js: node
- os: osx
osx_image: xcode9.4
node_js: 8
- os: osx
osx_image: xcode9.4
node_js: "8"
node_js: 10.3
- os: osx
osx_image: xcode9.4
node_js: "10.3"
node_js: node

before_install:
- set -e
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@
"url-search-params": "^0.10.0"
},
"dependencies": {
"@types/semver": "^5.5.0",
"@types/sharp": "^0.17.6",
"ansi-colors": "^1.1.0",
"axios": "^0.18.0",
"better-sqlite3": "^4.1.4",
"chokidar": "^2.0.4",
"cli-table3": "^0.5.0",
"commandpost": "^1.3.0",
"console-log-level": "^1.4.0",
"better-sqlite3": "^4.1.4",
"fs-extra": "^5.0.0",
"glob": "^7.1.2",
"glossy": "0.x.x",
Expand All @@ -102,7 +103,8 @@
"promise.prototype.finally": "^3.1.0",
"promptly": "^3.0.3",
"segfault-handler": "^1.0.1",
"sharp": "^0.20.1",
"semver": "^5.5.0",
"sharp": "^0.20.5",
"source-map": "^0.6.1",
"tar-fs": "^1.16.0",
"uglifyjs-webpack-plugin": "^1.2.2",
Expand All @@ -129,4 +131,4 @@
],
"all": true
}
}
}
12 changes: 10 additions & 2 deletions src/local_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class LocalRuntime implements Runtime {
public bridge: Bridge,
options: LocalRuntimeOptions = {}
) {
if (!v8Env.snapshot)
throw new Error("base snapshot is not ready, maybe you need to compile v8env?")
//if (!v8Env.snapshot)
// throw new Error("base snapshot is not ready, maybe you need to compile v8env?")

console.log("new runtime, app:", app.name, app.sourceHash)

Expand Down Expand Up @@ -79,6 +79,13 @@ export class LocalRuntime implements Runtime {
if (current)
current.release()
const context = this.isolate.createContextSync({ inspector: !!this.options.inspect })

if (!v8Env.snapshot) {
const start = Date.now()
const script = this.isolate.compileScriptSync(v8Env.source, { filename: "bundle.js" })
script.runSync(context)
console.log("v8env loaded in", Date.now() - start, "ms")
}
const g = context.global
g.setSync("global", g.derefInto())
g.setSync('_log', new ivm.Reference(function (lvl: string, ...args: any[]) {
Expand Down Expand Up @@ -107,6 +114,7 @@ export class LocalRuntime implements Runtime {
this.app = app
if (this.lastSourceHash != "") // we had not setup the context
this.context = this.resetContext(this.context)

await this.runApp(app)
this.lastSourceHash = app.sourceHash
}
Expand Down
34 changes: 23 additions & 11 deletions src/v8env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path'
import * as fs from 'fs'
import * as semver from 'semver'

import * as webpack from 'webpack'

Expand All @@ -12,7 +13,7 @@ import * as ivm from 'isolated-vm';
let v8EnvHash = "";
let v8EnvCode = "";
let v8EnvSourceMap = "";
let v8EnvSnapshot: ivm.ExternalCopy<ArrayBuffer>;
let v8EnvSnapshot: ivm.ExternalCopy<ArrayBuffer> | undefined;

const v8dist = path.join(__dirname, '..', 'dist', 'v8env.js')
const v8distSnapshot = path.join(__dirname, '..', 'dist', 'v8env.bin')
Expand All @@ -23,17 +24,28 @@ if (fs.existsSync(v8dist)) {
v8EnvHash = createHash("sha1").update(v8EnvCode).digest("hex")
}

if (fs.existsSync(v8distSnapshot)) {
v8EnvSnapshot = new ivm.ExternalCopy(<ArrayBuffer>fs.readFileSync(v8distSnapshot).buffer)
} else if (v8EnvCode) {
v8EnvSnapshot = ivm.Isolate.createSnapshot([{
code: v8EnvCode,
filename: 'dist/v8env.js'
}])
const v8SnapshotsEnabled = semver.lt(process.version, '10.4.0')
if (!v8SnapshotsEnabled) {
console.warn("can't use v8 snapshots with this version of node, boot will be slower", process.version)
}
else {
console.log("v8 snapshots enabled")

if (fs.existsSync(v8distSnapshot)) {
console.log("loading snapshot:", v8distSnapshot)
v8EnvSnapshot = new ivm.ExternalCopy(<ArrayBuffer>fs.readFileSync(v8distSnapshot).buffer)
} else if (v8EnvCode) {
console.log("building snapshot")
v8EnvSnapshot = ivm.Isolate.createSnapshot([{
code: v8EnvCode,
filename: 'dist/v8env.js'
}])
}


if (fs.existsSync(v8mapDist)) {
v8EnvSourceMap = fs.readFileSync(v8mapDist).toString()
if (fs.existsSync(v8mapDist)) {
v8EnvSourceMap = fs.readFileSync(v8mapDist).toString()
}
}

export class V8Environment extends EventEmitter {
Expand All @@ -46,7 +58,7 @@ export class V8Environment extends EventEmitter {
}

get isReady() {
return !!v8Env && !!v8EnvSnapshot
return !!v8Env && (!!v8EnvSnapshot || !v8SnapshotsEnabled)
}

get source() {
Expand Down

0 comments on commit 57d851f

Please sign in to comment.