@@ -49,6 +49,7 @@ using v8::HandleScope;
49
49
using v8::IndexedPropertyHandlerConfiguration;
50
50
using v8::Int32;
51
51
using v8::Isolate;
52
+ using v8::Just;
52
53
using v8::Local;
53
54
using v8::Maybe;
54
55
using v8::MaybeLocal;
@@ -58,6 +59,7 @@ using v8::MicrotaskQueue;
58
59
using v8::MicrotasksPolicy;
59
60
using v8::Name;
60
61
using v8::NamedPropertyHandlerConfiguration;
62
+ using v8::Nothing;
61
63
using v8::Number;
62
64
using v8::Object;
63
65
using v8::ObjectTemplate;
@@ -857,40 +859,75 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
857
859
}
858
860
contextify_script->script_ .Reset (isolate, v8_script);
859
861
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));
883
865
}
884
866
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 ;
890
883
891
884
TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
892
885
}
893
886
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
+
894
931
bool ContextifyScript::InstanceOf (Environment* env,
895
932
const Local<Value>& value) {
896
933
return !value.IsEmpty () &&
@@ -1242,28 +1279,18 @@ void ContextifyContext::CompileFunction(
1242
1279
.IsNothing ())
1243
1280
return ;
1244
1281
1282
+ std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
1245
1283
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 ;
1267
1294
}
1268
1295
1269
1296
args.GetReturnValue ().Set (result);
0 commit comments