diff --git a/lib/vm.js b/lib/vm.js index 1bb948fa550531..b5f5e4f8326159 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,17 +391,26 @@ function compileFunction(code, params, options = {}) { } }); - return _compileFunction( + const fn = _compileFunction( code, filename, lineOffset, columnOffset, cachedData, - produceCachedData, parsingContext, 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 3fbc616fda0284..a29c3185404883 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); } @@ -1000,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 @@ -1085,29 +1082,27 @@ 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); } +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().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(