From f3ce13dc84f931d7f8e7ba11334c3f7abc2ba6ef Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Sun, 4 Nov 2018 01:46:43 +0530 Subject: [PATCH 1/4] vm: add createCacheForFunction --- src/node_contextify.cc | 21 +++++++++++++++++++++ src/node_contextify.h | 2 ++ 2 files changed, 23 insertions(+) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3fbc616fda0284..b1c9e2cb6cda70 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -212,6 +212,7 @@ void ContextifyContext::Init(Environment* env, Local target) { env->SetMethod(target, "makeContext", MakeContext); env->SetMethod(target, "isContext", IsContext); env->SetMethod(target, "compileFunction", CompileFunction); + env->SetMethod(target, "createCacheForFunction", CreateCacheForFunction); } @@ -1108,6 +1109,26 @@ void ContextifyContext::CompileFunction( args.GetReturnValue().Set(fun); } +void ContextifyContext::CreateCacheForFunction( + const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + + // Argument 1: target function + Local function = args[0].As(); + + const std::unique_ptr cache( + ScriptCompiler::CreateCodeCacheForFunction(function)); + + if (cache == nullptr) { + args.GetReturnValue().SetUndefined(); + } else { + args.GetReturnValue().Set( + Buffer::Copy( + env, reinterpret_cast(cache->data), cache->length) + .ToLocalChecked()); + } +} + void Initialize(Local target, Local unused, diff --git a/src/node_contextify.h b/src/node_contextify.h index 6d6dc12b4b8d5d..10466b644aab36 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -61,6 +61,8 @@ class ContextifyContext { static void IsContext(const v8::FunctionCallbackInfo& args); static void CompileFunction( const v8::FunctionCallbackInfo& args); + static void CreateCacheForFunction( + const v8::FunctionCallbackInfo& args); static void WeakCallback( const v8::WeakCallbackInfo& data); static void PropertyGetterCallback( From db807b5e4ef5c297c06ca9fe8b93ffadc92aa392 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Mon, 5 Nov 2018 00:49:00 +0530 Subject: [PATCH 2/4] fixup! vm: add createCacheForFunction --- src/node_contextify.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index b1c9e2cb6cda70..60c4743f2943b7 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1119,9 +1119,7 @@ void ContextifyContext::CreateCacheForFunction( const std::unique_ptr cache( ScriptCompiler::CreateCodeCacheForFunction(function)); - if (cache == nullptr) { - args.GetReturnValue().SetUndefined(); - } else { + if (cache != nullptr) { args.GetReturnValue().Set( Buffer::Copy( env, reinterpret_cast(cache->data), cache->length) From fc4697933b4ce5eb442d1b5ae9e258fa0db80284 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Mon, 5 Nov 2018 01:00:26 +0530 Subject: [PATCH 3/4] vm: remove caching logic from CompileFunction Remove caching logic from the CompileFunction binding from the ContextifyScript class, moving the caching logic to the JS-layer for vm.compileFunction (using the CreateCacheForFunction binding). --- lib/vm.js | 15 +++++++++++-- src/node_contextify.cc | 48 +++++++++++------------------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/lib/vm.js b/lib/vm.js index 1bb948fa550531..dfda7c33c6ed0f 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -25,7 +25,8 @@ const { ContextifyScript, makeContext, isContext: _isContext, - compileFunction: _compileFunction + compileFunction: _compileFunction, + createCacheForFunction } = internalBinding('contextify'); const { callbackMap } = internalBinding('module_wrap'); const { @@ -390,7 +391,7 @@ function compileFunction(code, params, options = {}) { } }); - return _compileFunction( + const fn = _compileFunction( code, filename, lineOffset, @@ -401,6 +402,16 @@ function compileFunction(code, params, options = {}) { contextExtensions, params ); + + if (produceCachedData) { + const cache = createCacheForFunction(fn); + fn.cachedDataProduced = cache !== undefined; + if (fn.cachedDataProduced) { + fn.cachedData = cache; + } + } + + return fn; } diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 60c4743f2943b7..a29c3185404883 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1001,35 +1001,31 @@ void ContextifyContext::CompileFunction( cached_data_buf = args[4].As(); } - // Argument 6: produce cache data - CHECK(args[5]->IsBoolean()); - bool produce_cached_data = args[5]->IsTrue(); - - // Argument 7: parsing context (optional) + // Argument 6: parsing context (optional) Local parsing_context; - if (!args[6]->IsUndefined()) { - CHECK(args[6]->IsObject()); + if (!args[5]->IsUndefined()) { + CHECK(args[5]->IsObject()); ContextifyContext* sandbox = ContextifyContext::ContextFromContextifiedSandbox( - env, args[6].As()); + env, args[5].As()); CHECK_NOT_NULL(sandbox); parsing_context = sandbox->context(); } else { parsing_context = context; } - // Argument 8: context extensions (optional) + // Argument 7: context extensions (optional) Local context_extensions_buf; - if (!args[7]->IsUndefined()) { - CHECK(args[7]->IsArray()); - context_extensions_buf = args[7].As(); + if (!args[6]->IsUndefined()) { + CHECK(args[6]->IsArray()); + context_extensions_buf = args[6].As(); } - // Argument 9: params for the function (optional) + // Argument 8: params for the function (optional) Local params_buf; - if (!args[8]->IsUndefined()) { - CHECK(args[8]->IsArray()); - params_buf = args[8].As(); + if (!args[7]->IsUndefined()) { + CHECK(args[7]->IsArray()); + params_buf = args[7].As(); } // Read cache from cached data buffer @@ -1086,26 +1082,6 @@ void ContextifyContext::CompileFunction( return; } - if (produce_cached_data) { - const std::unique_ptr cached_data( - ScriptCompiler::CreateCodeCacheForFunction(fun)); - bool cached_data_produced = cached_data != nullptr; - if (cached_data_produced) { - MaybeLocal buf = Buffer::Copy( - env, - reinterpret_cast(cached_data->data), - cached_data->length); - if (fun->Set( - parsing_context, - env->cached_data_string(), - buf.ToLocalChecked()).IsNothing()) return; - } - if (fun->Set( - parsing_context, - env->cached_data_produced_string(), - Boolean::New(isolate, cached_data_produced)).IsNothing()) return; - } - args.GetReturnValue().Set(fun); } From ae5534aaa4673b9f689c378bbabd5712518033e3 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Mon, 5 Nov 2018 01:07:27 +0530 Subject: [PATCH 4/4] fixup! vm: remove caching logic from CompileFunction --- lib/vm.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/vm.js b/lib/vm.js index dfda7c33c6ed0f..b5f5e4f8326159 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -397,7 +397,6 @@ function compileFunction(code, params, options = {}) { lineOffset, columnOffset, cachedData, - produceCachedData, parsingContext, contextExtensions, params