@@ -13,6 +13,7 @@ namespace node {
13
13
14
14
using v8::AccessType;
15
15
using v8::Array;
16
+ using v8::ArrayBuffer;
16
17
using v8::Boolean ;
17
18
using v8::Context;
18
19
using v8::Debug;
@@ -40,6 +41,7 @@ using v8::ScriptCompiler;
40
41
using v8::ScriptOrigin;
41
42
using v8::String;
42
43
using v8::TryCatch;
44
+ using v8::Uint8Array;
43
45
using v8::UnboundScript;
44
46
using v8::V8;
45
47
using v8::Value;
@@ -496,15 +498,35 @@ class ContextifyScript : public BaseObject {
496
498
Local<Integer> lineOffset = GetLineOffsetArg (args, 1 );
497
499
Local<Integer> columnOffset = GetColumnOffsetArg (args, 1 );
498
500
bool display_errors = GetDisplayErrorsArg (args, 1 );
501
+ MaybeLocal<Uint8Array> cached_data_buf = GetCachedData (env, args, 1 );
502
+ bool produce_cached_data = GetProduceCachedData (env, args, 1 );
499
503
if (try_catch.HasCaught ()) {
500
504
try_catch.ReThrow ();
501
505
return ;
502
506
}
503
507
508
+ ScriptCompiler::CachedData* cached_data = nullptr ;
509
+ if (!cached_data_buf.IsEmpty ()) {
510
+ ArrayBuffer::Contents contents =
511
+ cached_data_buf.ToLocalChecked ()->Buffer ()->GetContents ();
512
+ cached_data = new ScriptCompiler::CachedData (
513
+ static_cast <uint8_t *>(contents.Data ()), contents.ByteLength ());
514
+ }
515
+
504
516
ScriptOrigin origin (filename, lineOffset, columnOffset);
505
- ScriptCompiler::Source source (code, origin);
506
- Local<UnboundScript> v8_script =
507
- ScriptCompiler::CompileUnbound (env->isolate (), &source);
517
+ ScriptCompiler::Source source (code, origin, cached_data);
518
+ ScriptCompiler::CompileOptions compile_options =
519
+ ScriptCompiler::kNoCompileOptions ;
520
+
521
+ if (source.GetCachedData () != nullptr )
522
+ compile_options = ScriptCompiler::kConsumeCodeCache ;
523
+ else if (produce_cached_data)
524
+ compile_options = ScriptCompiler::kProduceCodeCache ;
525
+
526
+ Local<UnboundScript> v8_script = ScriptCompiler::CompileUnbound (
527
+ env->isolate (),
528
+ &source,
529
+ compile_options);
508
530
509
531
if (v8_script.IsEmpty ()) {
510
532
if (display_errors) {
@@ -514,6 +536,19 @@ class ContextifyScript : public BaseObject {
514
536
return ;
515
537
}
516
538
contextify_script->script_ .Reset (env->isolate (), v8_script);
539
+
540
+ if (compile_options == ScriptCompiler::kConsumeCodeCache ) {
541
+ args.This ()->Set (
542
+ env->cached_data_rejected_string (),
543
+ Boolean::New (env->isolate (), source.GetCachedData ()->rejected ));
544
+ } else if (compile_options == ScriptCompiler::kProduceCodeCache ) {
545
+ const ScriptCompiler::CachedData* cached_data = source.GetCachedData ();
546
+ MaybeLocal<Object> buf = Buffer::Copy (
547
+ env,
548
+ reinterpret_cast <const char *>(cached_data->data ),
549
+ cached_data->length );
550
+ args.This ()->Set (env->cached_data_string (), buf.ToLocalChecked ());
551
+ }
517
552
}
518
553
519
554
@@ -666,6 +701,43 @@ class ContextifyScript : public BaseObject {
666
701
}
667
702
668
703
704
+ static MaybeLocal<Uint8Array> GetCachedData (
705
+ Environment* env,
706
+ const FunctionCallbackInfo<Value>& args,
707
+ const int i) {
708
+ if (!args[i]->IsObject ()) {
709
+ return MaybeLocal<Uint8Array>();
710
+ }
711
+ Local<Value> value = args[i].As <Object>()->Get (env->cached_data_string ());
712
+ if (value->IsUndefined ()) {
713
+ return MaybeLocal<Uint8Array>();
714
+ }
715
+
716
+ if (!value->IsUint8Array ()) {
717
+ Environment::ThrowTypeError (
718
+ args.GetIsolate (),
719
+ " options.cachedData must be a Buffer instance" );
720
+ return MaybeLocal<Uint8Array>();
721
+ }
722
+
723
+ return value.As <Uint8Array>();
724
+ }
725
+
726
+
727
+ static bool GetProduceCachedData (
728
+ Environment* env,
729
+ const FunctionCallbackInfo<Value>& args,
730
+ const int i) {
731
+ if (!args[i]->IsObject ()) {
732
+ return false ;
733
+ }
734
+ Local<Value> value =
735
+ args[i].As <Object>()->Get (env->produce_cached_data_string ());
736
+
737
+ return value->IsTrue ();
738
+ }
739
+
740
+
669
741
static Local<Integer> GetLineOffsetArg (
670
742
const FunctionCallbackInfo<Value>& args,
671
743
const int i) {
0 commit comments