backport PRs 4777 and 5343 to 0.12.x. new approach is based on them

https://github.com/nodejs/node/pull/4777
https://github.com/nodejs/node/pull/5343
This commit is contained in:
Igor Klopov 2016-09-13 22:43:14 +03:00
parent 54f97db99c
commit ff9e98a804
3 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,151 @@
From d1cacb814f6d42395184beaaba906ba930e711eb Mon Sep 17 00:00:00 2001
From: Fedor Indutny <fedor@indutny.com>
Date: Wed, 20 Jan 2016 19:34:19 -0500
Subject: vm: introduce `cachedData`/`produceCachedData`
Introduce `cachedData`/`produceCachedData` options for `v8.Script`.
Could be used to consume/produce V8's code cache for speeding up
compilation of known code.
PR-URL: https://github.com/nodejs/node/pull/4777
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
diff --git a/src/node_contextify.cc b/src/node_contextify.cc
index 2e8fd2c..c16be55 100644
--- a/src/node_contextify.cc
+++ b/src/node_contextify.cc
@@ -18,10 +18,11 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node.h"
+#include "node_buffer.h"
#include "node_internals.h"
#include "node_watchdog.h"
#include "base-object.h"
#include "base-object-inl.h"
#include "env.h"
@@ -55,10 +56,11 @@ using v8::PropertyCallbackInfo;
using v8::Script;
using v8::ScriptCompiler;
using v8::ScriptOrigin;
using v8::String;
using v8::TryCatch;
+using v8::Uint8Array;
using v8::UnboundScript;
using v8::V8;
using v8::Value;
using v8::WeakCallbackData;
@@ -484,28 +486,60 @@ class ContextifyScript : public BaseObject {
TryCatch try_catch;
Local<String> code = args[0]->ToString();
Local<String> filename = GetFilenameArg(args, 1);
bool display_errors = GetDisplayErrorsArg(args, 1);
+ Local<Uint8Array> cached_data_buf = GetCachedData(args, 1);
+ bool produce_cached_data = GetProduceCachedData(args, 1);
if (try_catch.HasCaught()) {
try_catch.ReThrow();
return;
}
+ ScriptCompiler::CachedData* cached_data = nullptr;
+ if (!cached_data_buf.IsEmpty()) {
+ cached_data = new ScriptCompiler::CachedData(
+ static_cast<uint8_t*>(cached_data_buf->GetIndexedPropertiesExternalArrayData()),
+ cached_data_buf->GetIndexedPropertiesExternalArrayDataLength());
+ }
+
ScriptOrigin origin(filename);
- ScriptCompiler::Source source(code, origin);
- Local<UnboundScript> v8_script =
- ScriptCompiler::CompileUnbound(env->isolate(), &source);
+ ScriptCompiler::Source source(code, origin, cached_data);
+ ScriptCompiler::CompileOptions compile_options =
+ ScriptCompiler::kNoCompileOptions;
+
+ if (source.GetCachedData() != nullptr)
+ compile_options = ScriptCompiler::kConsumeCodeCache;
+ else if (produce_cached_data)
+ compile_options = ScriptCompiler::kProduceCodeCache;
+
+ Local<UnboundScript> v8_script = ScriptCompiler::CompileUnbound(
+ env->isolate(),
+ &source,
+ compile_options);
if (v8_script.IsEmpty()) {
if (display_errors) {
AppendExceptionLine(env, try_catch.Exception(), try_catch.Message());
}
try_catch.ReThrow();
return;
}
contextify_script->script_.Reset(env->isolate(), v8_script);
+
+ if (compile_options == ScriptCompiler::kConsumeCodeCache) {
+ // no 'rejected' field in cachedData
+ } else if (compile_options == ScriptCompiler::kProduceCodeCache) {
+ const ScriptCompiler::CachedData* cached_data = source.GetCachedData();
+ Local<Object> buf = Buffer::New(
+ env,
+ reinterpret_cast<const char*>(cached_data->data),
+ cached_data->length);
+ Local<String> cached_data_string = FIXED_ONE_BYTE_STRING(
+ args.GetIsolate(), "cachedData");
+ args.This()->Set(cached_data_string, buf);
+ }
}
static bool InstanceOf(Environment* env, const Local<Value>& value) {
return !value.IsEmpty() &&
@@ -656,10 +690,46 @@ class ContextifyScript : public BaseObject {
return value->IsUndefined() ? defaultFilename : value->ToString();
}
+ static Local<Uint8Array> GetCachedData(
+ const FunctionCallbackInfo<Value>& args,
+ const int i) {
+ if (!args[i]->IsObject()) {
+ return Local<Uint8Array>();
+ }
+ Local<String> key = FIXED_ONE_BYTE_STRING(args.GetIsolate(), "cachedData");
+ Local<Value> value = args[i].As<Object>()->Get(key);
+ if (value->IsUndefined()) {
+ return Local<Uint8Array>();
+ }
+
+ if (!value->IsUint8Array()) {
+ Environment::ThrowTypeError(
+ args.GetIsolate(),
+ "options.cachedData must be a Buffer instance");
+ return Local<Uint8Array>();
+ }
+
+ return value.As<Uint8Array>();
+ }
+
+
+ static bool GetProduceCachedData(
+ const FunctionCallbackInfo<Value>& args,
+ const int i) {
+ if (!args[i]->IsObject()) {
+ return false;
+ }
+ Local<String> key = FIXED_ONE_BYTE_STRING(args.GetIsolate(), "produceCachedData");
+ Local<Value> value = args[i].As<Object>()->Get(key);
+
+ return value->IsTrue();
+ }
+
+
static bool EvalMachine(Environment* env,
const int64_t timeout,
const bool display_errors,
const FunctionCallbackInfo<Value>& args,
TryCatch& try_catch) {

View File

@ -0,0 +1,51 @@
From 6c8378b15bd9ca378df6e14d5b0d7032caefd774 Mon Sep 17 00:00:00 2001
From: Jiho Choi <jray319@gmail.com>
Date: Sat, 20 Feb 2016 20:44:06 -0600
Subject: vm: fix `produceCachedData`
Fix segmentation faults when compiling the same code with
`produceCachedData` option. V8 ignores the option when the code is in
its compilation cache and does not return cached data. Added
`cachedDataProduced` property to `v8.Script` to denote whether the
cached data is produced successfully.
PR-URL: https://github.com/nodejs/node/pull/5343
Reviewed-By: Fedor Indutny <fedor@indutny.com>
diff --git a/src/node_contextify.cc b/src/node_contextify.cc
index c16be55..c0a138a 100644
--- a/src/node_contextify.cc
+++ b/src/node_contextify.cc
@@ -528,17 +528,25 @@ class ContextifyScript : public BaseObject {
if (compile_options == ScriptCompiler::kConsumeCodeCache) {
// no 'rejected' field in cachedData
} else if (compile_options == ScriptCompiler::kProduceCodeCache) {
const ScriptCompiler::CachedData* cached_data = source.GetCachedData();
- Local<Object> buf = Buffer::New(
- env,
- reinterpret_cast<const char*>(cached_data->data),
- cached_data->length);
- Local<String> cached_data_string = FIXED_ONE_BYTE_STRING(
- args.GetIsolate(), "cachedData");
- args.This()->Set(cached_data_string, buf);
+ bool cached_data_produced = cached_data != nullptr;
+ if (cached_data_produced) {
+ Local<Object> buf = Buffer::New(
+ env,
+ reinterpret_cast<const char*>(cached_data->data),
+ cached_data->length);
+ Local<String> cached_data_string = FIXED_ONE_BYTE_STRING(
+ args.GetIsolate(), "cachedData");
+ args.This()->Set(cached_data_string, buf);
+ }
+ Local<String> cached_data_produced_string = FIXED_ONE_BYTE_STRING(
+ args.GetIsolate(), "cachedDataProduced");
+ args.This()->Set(
+ cached_data_produced_string,
+ Boolean::New(env->isolate(), cached_data_produced));
}
}
static bool InstanceOf(Environment* env, const Local<Value>& value) {

View File

@ -14,6 +14,8 @@
"backport.R24824.patch",
"backport.R25039.patch",
"backport.R25444.patch",
"backport.PR4777.for.N0.patch",
"backport.PR5343.for.N0.patch",
"node.v0.12.15.patch"
],
"v4.5.0": [