@@ -49,6 +49,7 @@ using v8::HandleScope;
4949using v8::IndexedPropertyHandlerConfiguration;
5050using v8::Int32;
5151using v8::Isolate;
52+ using v8::Just;
5253using v8::Local;
5354using v8::Maybe;
5455using v8::MaybeLocal;
@@ -58,6 +59,7 @@ using v8::MicrotaskQueue;
5859using v8::MicrotasksPolicy;
5960using v8::Name;
6061using v8::NamedPropertyHandlerConfiguration;
62+ using v8::Nothing;
6163using v8::Number;
6264using v8::Object;
6365using v8::ObjectTemplate;
@@ -857,40 +859,75 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
857859 }
858860 contextify_script->script_ .Reset (isolate, v8_script);
859861
860- Local<Context> env_context = env->context ();
861- if (compile_options == ScriptCompiler::kConsumeCodeCache ) {
862- args.This ()->Set (
863- env_context,
864- env->cached_data_rejected_string (),
865- Boolean::New (isolate, source.GetCachedData ()->rejected )).Check ();
866- } else if (produce_cached_data) {
867- std::unique_ptr<ScriptCompiler::CachedData> cached_data{
868- ScriptCompiler::CreateCodeCache (v8_script)};
869- bool cached_data_produced = cached_data != nullptr ;
870- if (cached_data_produced) {
871- MaybeLocal<Object> buf = Buffer::Copy (
872- env,
873- reinterpret_cast <const char *>(cached_data->data ),
874- cached_data->length );
875- args.This ()->Set (env_context,
876- env->cached_data_string (),
877- buf.ToLocalChecked ()).Check ();
878- }
879- args.This ()->Set (
880- env_context,
881- env->cached_data_produced_string (),
882- Boolean::New (isolate, cached_data_produced)).Check ();
862+ std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
863+ if (produce_cached_data) {
864+ new_cached_data.reset (ScriptCompiler::CreateCodeCache (v8_script));
883865 }
884866
885- args.This ()
886- ->Set (env_context,
887- env->source_map_url_string (),
888- v8_script->GetSourceMappingURL ())
889- .Check ();
867+ if (StoreCodeCacheResult (env,
868+ args.This (),
869+ compile_options,
870+ source,
871+ produce_cached_data,
872+ std::move (new_cached_data))
873+ .IsNothing ()) {
874+ return ;
875+ }
876+
877+ if (args.This ()
878+ ->Set (env->context (),
879+ env->source_map_url_string (),
880+ v8_script->GetSourceMappingURL ())
881+ .IsNothing ())
882+ return ;
890883
891884 TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
892885}
893886
887+ Maybe<bool > StoreCodeCacheResult (
888+ Environment* env,
889+ Local<Object> target,
890+ ScriptCompiler::CompileOptions compile_options,
891+ const v8::ScriptCompiler::Source& source,
892+ bool produce_cached_data,
893+ std::unique_ptr<ScriptCompiler::CachedData> new_cached_data) {
894+ Local<Context> context;
895+ if (!target->GetCreationContext ().ToLocal (&context)) {
896+ return Nothing<bool >();
897+ }
898+ if (compile_options == ScriptCompiler::kConsumeCodeCache ) {
899+ if (target
900+ ->Set (
901+ context,
902+ env->cached_data_rejected_string (),
903+ Boolean::New (env->isolate (), source.GetCachedData ()->rejected ))
904+ .IsNothing ()) {
905+ return Nothing<bool >();
906+ }
907+ }
908+ if (produce_cached_data) {
909+ bool cached_data_produced = new_cached_data != nullptr ;
910+ if (cached_data_produced) {
911+ MaybeLocal<Object> buf =
912+ Buffer::Copy (env,
913+ reinterpret_cast <const char *>(new_cached_data->data ),
914+ new_cached_data->length );
915+ if (target->Set (context, env->cached_data_string (), buf.ToLocalChecked ())
916+ .IsNothing ()) {
917+ return Nothing<bool >();
918+ }
919+ }
920+ if (target
921+ ->Set (context,
922+ env->cached_data_produced_string (),
923+ Boolean::New (env->isolate (), cached_data_produced))
924+ .IsNothing ()) {
925+ return Nothing<bool >();
926+ }
927+ }
928+ return Just (true );
929+ }
930+
894931bool ContextifyScript::InstanceOf (Environment* env,
895932 const Local<Value>& value) {
896933 return !value.IsEmpty () &&
@@ -1242,28 +1279,18 @@ void ContextifyContext::CompileFunction(
12421279 .IsNothing ())
12431280 return ;
12441281
1282+ std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
12451283 if (produce_cached_data) {
1246- const std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1247- ScriptCompiler::CreateCodeCacheForFunction (fn));
1248- bool cached_data_produced = cached_data != nullptr ;
1249- if (cached_data_produced) {
1250- MaybeLocal<Object> buf = Buffer::Copy (
1251- env,
1252- reinterpret_cast <const char *>(cached_data->data ),
1253- cached_data->length );
1254- if (result
1255- ->Set (parsing_context,
1256- env->cached_data_string (),
1257- buf.ToLocalChecked ())
1258- .IsNothing ())
1259- return ;
1260- }
1261- if (result
1262- ->Set (parsing_context,
1263- env->cached_data_produced_string (),
1264- Boolean::New (isolate, cached_data_produced))
1265- .IsNothing ())
1266- return ;
1284+ new_cached_data.reset (ScriptCompiler::CreateCodeCacheForFunction (fn));
1285+ }
1286+ if (StoreCodeCacheResult (env,
1287+ result,
1288+ options,
1289+ source,
1290+ produce_cached_data,
1291+ std::move (new_cached_data))
1292+ .IsNothing ()) {
1293+ return ;
12671294 }
12681295
12691296 args.GetReturnValue ().Set (result);
0 commit comments