File indexing completed on 2026-09-28 09:18:21
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_SCRIPT_H_
0006 #define INCLUDE_V8_SCRIPT_H_
0007
0008 #include <stddef.h>
0009 #include <stdint.h>
0010
0011 #include <memory>
0012 #include <tuple>
0013 #include <vector>
0014
0015 #include "v8-callbacks.h" // NOLINT(build/include_directory)
0016 #include "v8-data.h" // NOLINT(build/include_directory)
0017 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0018 #include "v8-maybe.h" // NOLINT(build/include_directory)
0019 #include "v8-memory-span.h" // NOLINT(build/include_directory)
0020 #include "v8-message.h" // NOLINT(build/include_directory)
0021 #include "v8config.h" // NOLINT(build/include_directory)
0022
0023 namespace v8 {
0024
0025 class Function;
0026 class Message;
0027 class Object;
0028 class PrimitiveArray;
0029 class Script;
0030
0031 namespace internal {
0032 class BackgroundDeserializeTask;
0033 struct ScriptStreamingData;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042 class V8_EXPORT ScriptOrModule {
0043 public:
0044
0045
0046
0047
0048 Local<Value> GetResourceName();
0049
0050
0051
0052
0053
0054 Local<Data> HostDefinedOptions();
0055 };
0056
0057
0058
0059
0060 class V8_EXPORT UnboundScript : public Data {
0061 public:
0062
0063
0064
0065 Local<Script> BindToCurrentContext();
0066
0067
0068
0069
0070 int ScriptId() const;
0071 V8_DEPRECATE_SOON("Use ScriptId")
0072 int GetId() const;
0073
0074 Local<Value> GetScriptName();
0075
0076
0077
0078
0079 Local<Value> GetSourceURL();
0080
0081
0082
0083 Local<Value> GetSourceMappingURL();
0084
0085
0086
0087
0088
0089 int GetLineNumber(int code_pos = 0);
0090
0091
0092
0093
0094
0095 int GetColumnNumber(int code_pos = 0);
0096
0097 static const int kNoScriptId = 0;
0098 };
0099
0100
0101
0102
0103 class V8_EXPORT UnboundModuleScript : public Data {
0104 public:
0105
0106
0107
0108 Local<Value> GetSourceURL();
0109
0110
0111
0112 Local<Value> GetSourceMappingURL();
0113
0114
0115
0116
0117 int ScriptId() const;
0118
0119 static const int kNoScriptId = 0;
0120 };
0121
0122 static_assert(UnboundModuleScript::kNoScriptId == UnboundScript::kNoScriptId);
0123
0124
0125
0126
0127 class V8_EXPORT Location {
0128 public:
0129 int GetLineNumber() { return line_number_; }
0130 int GetColumnNumber() { return column_number_; }
0131
0132 Location(int line_number, int column_number)
0133 : line_number_(line_number), column_number_(column_number) {}
0134
0135 private:
0136 int line_number_;
0137 int column_number_;
0138 };
0139
0140 class V8_EXPORT ModuleRequest : public Data {
0141 public:
0142
0143
0144
0145 Local<String> GetSpecifier() const;
0146
0147
0148
0149
0150 ModuleImportPhase GetPhase() const;
0151
0152
0153
0154
0155
0156 int GetSourceOffset() const;
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 Local<FixedArray> GetImportAttributes() const;
0172
0173 V8_DEPRECATED("Use GetImportAttributes instead")
0174 Local<FixedArray> GetImportAssertions() const {
0175 return GetImportAttributes();
0176 }
0177
0178 V8_INLINE static ModuleRequest* Cast(Data* data);
0179
0180 private:
0181 static void CheckCast(Data* obj);
0182 };
0183
0184
0185
0186
0187 class V8_EXPORT Module : public Data {
0188 public:
0189
0190
0191
0192
0193
0194
0195
0196 enum Status {
0197 kUninstantiated,
0198 kInstantiating,
0199 kInstantiated,
0200 kEvaluating,
0201 kEvaluated,
0202 kErrored
0203 };
0204
0205
0206
0207
0208
0209
0210 Local<Value> GetResourceName() const;
0211
0212
0213
0214
0215 Status GetStatus() const;
0216
0217
0218
0219
0220 Local<Value> GetException() const;
0221
0222
0223
0224
0225 Local<FixedArray> GetModuleRequests() const;
0226
0227
0228
0229
0230
0231 Location SourceOffsetToLocation(int offset) const;
0232
0233
0234
0235
0236 int GetIdentityHash() const;
0237
0238 using ResolveModuleCallback = MaybeLocal<Module> (*)(
0239 Local<Context> context, Local<String> specifier,
0240 Local<FixedArray> import_attributes, Local<Module> referrer);
0241 using ResolveSourceCallback = MaybeLocal<Object> (*)(
0242 Local<Context> context, Local<String> specifier,
0243 Local<FixedArray> import_attributes, Local<Module> referrer);
0244
0245 using ResolveModuleByIndexCallback = MaybeLocal<Module> (*)(
0246 Local<Context> context, size_t module_request_index,
0247 Local<Module> referrer);
0248 using ResolveSourceByIndexCallback = MaybeLocal<Object> (*)(
0249 Local<Context> context, size_t module_request_index,
0250 Local<Module> referrer);
0251
0252
0253
0254
0255
0256
0257
0258
0259 V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule(
0260 Local<Context> context, ResolveModuleCallback module_callback,
0261 ResolveSourceCallback source_callback = nullptr);
0262
0263
0264
0265
0266
0267
0268
0269 V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule(
0270 Local<Context> context, ResolveModuleByIndexCallback module_callback,
0271 ResolveSourceByIndexCallback source_callback = nullptr);
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Evaluate(Local<Context> context);
0284
0285
0286
0287
0288
0289
0290 Local<Value> GetModuleNamespace();
0291
0292
0293
0294
0295
0296
0297
0298 Local<UnboundModuleScript> GetUnboundModuleScript();
0299
0300
0301
0302
0303
0304
0305 int ScriptId() const;
0306
0307
0308
0309
0310
0311
0312
0313 bool IsGraphAsync() const;
0314
0315
0316
0317
0318
0319
0320 bool HasTopLevelAwait() const;
0321
0322
0323
0324
0325 bool IsSourceTextModule() const;
0326
0327
0328
0329
0330 bool IsSyntheticModule() const;
0331
0332
0333
0334
0335
0336
0337
0338
0339 using SyntheticModuleEvaluationSteps =
0340 MaybeLocal<Value> (*)(Local<Context> context, Local<Module> module);
0341
0342
0343
0344
0345
0346
0347
0348
0349 static Local<Module> CreateSyntheticModule(
0350 Isolate* isolate, Local<String> module_name,
0351 const MemorySpan<const Local<String>>& export_names,
0352 SyntheticModuleEvaluationSteps evaluation_steps);
0353
0354
0355
0356
0357
0358
0359
0360
0361 V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport(
0362 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
0363
0364
0365
0366
0367
0368
0369
0370
0371 std::pair<LocalVector<Module>, LocalVector<Message>>
0372 GetStalledTopLevelAwaitMessages(Isolate* isolate);
0373
0374 V8_INLINE static Module* Cast(Data* data);
0375
0376 private:
0377 static void CheckCast(Data* obj);
0378 };
0379
0380 class V8_EXPORT CompileHintsCollector : public Data {
0381 public:
0382
0383
0384
0385 std::vector<int> GetCompileHints(Isolate* isolate) const;
0386 };
0387
0388
0389
0390
0391
0392 class V8_EXPORT Script : public Data {
0393 public:
0394
0395
0396
0397 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0398 Local<Context> context, Local<String> source,
0399 ScriptOrigin* origin = nullptr);
0400
0401
0402
0403
0404
0405
0406 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
0407 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context,
0408 Local<Data> host_defined_options);
0409
0410
0411
0412
0413 Local<UnboundScript> GetUnboundScript();
0414
0415
0416
0417
0418 int ScriptId() const;
0419
0420
0421
0422
0423
0424 Local<Value> GetResourceName();
0425
0426
0427
0428
0429
0430 V8_DEPRECATE_SOON("Use GetCompileHintsCollector instead")
0431 std::vector<int> GetProducedCompileHints() const;
0432
0433
0434
0435
0436
0437
0438 Local<CompileHintsCollector> GetCompileHintsCollector() const;
0439 };
0440
0441 enum class ScriptType { kClassic, kModule };
0442
0443
0444
0445
0446 class V8_EXPORT ScriptCompiler {
0447 public:
0448 class ConsumeCodeCacheTask;
0449
0450
0451
0452
0453
0454
0455
0456
0457 struct V8_EXPORT CachedData {
0458 enum BufferPolicy { BufferNotOwned, BufferOwned };
0459
0460 CachedData()
0461 : data(nullptr),
0462 length(0),
0463 rejected(false),
0464 buffer_policy(BufferNotOwned) {}
0465
0466
0467
0468
0469
0470 CachedData(const uint8_t* data, int length,
0471 BufferPolicy buffer_policy = BufferNotOwned);
0472 ~CachedData();
0473
0474 enum CompatibilityCheckResult {
0475
0476
0477 kSuccess = 0,
0478 kMagicNumberMismatch = 1,
0479 kVersionMismatch = 2,
0480 kSourceMismatch = 3,
0481 kFlagsMismatch = 5,
0482 kChecksumMismatch = 6,
0483 kInvalidHeader = 7,
0484 kLengthMismatch = 8,
0485 kReadOnlySnapshotChecksumMismatch = 9,
0486
0487
0488 kLast = kReadOnlySnapshotChecksumMismatch
0489 };
0490
0491
0492 CompatibilityCheckResult CompatibilityCheck(Isolate* isolate);
0493
0494
0495
0496 const uint8_t* data;
0497 int length;
0498 bool rejected;
0499 BufferPolicy buffer_policy;
0500
0501
0502 CachedData(const CachedData&) = delete;
0503 CachedData& operator=(const CachedData&) = delete;
0504 };
0505
0506 enum class InMemoryCacheResult {
0507
0508 kNotAttempted,
0509
0510
0511
0512
0513 kHit,
0514
0515
0516 kMiss,
0517
0518
0519
0520 kPartial,
0521 };
0522
0523
0524 struct CompilationDetails {
0525 InMemoryCacheResult in_memory_cache_result =
0526 InMemoryCacheResult::kNotAttempted;
0527
0528 static constexpr int64_t kTimeNotMeasured = -1;
0529 int64_t foreground_time_in_microseconds = kTimeNotMeasured;
0530 int64_t background_time_in_microseconds = kTimeNotMeasured;
0531 };
0532
0533
0534
0535
0536 class Source {
0537 public:
0538
0539
0540 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
0541 CachedData* cached_data = nullptr,
0542 ConsumeCodeCacheTask* consume_cache_task = nullptr);
0543
0544 V8_INLINE explicit Source(
0545 Local<String> source_string, CachedData* cached_data = nullptr,
0546 ConsumeCodeCacheTask* consume_cache_task = nullptr);
0547 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
0548 CompileHintCallback callback, void* callback_data);
0549 V8_INLINE ~Source() = default;
0550
0551
0552
0553
0554 V8_INLINE const CachedData* GetCachedData() const;
0555
0556 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
0557
0558 V8_INLINE const CompilationDetails& GetCompilationDetails() const;
0559
0560 private:
0561 friend class ScriptCompiler;
0562
0563 Local<String> source_string;
0564
0565
0566 Local<Value> resource_name;
0567 int resource_line_offset = -1;
0568 int resource_column_offset = -1;
0569 ScriptOriginOptions resource_options;
0570 Local<Value> source_map_url;
0571 Local<Data> host_defined_options;
0572
0573
0574
0575
0576 std::unique_ptr<CachedData> cached_data;
0577 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
0578
0579
0580 CompileHintCallback compile_hint_callback = nullptr;
0581 void* compile_hint_callback_data = nullptr;
0582
0583
0584
0585 CompilationDetails compilation_details;
0586 };
0587
0588
0589
0590
0591
0592 class V8_EXPORT ExternalSourceStream {
0593 public:
0594 virtual ~ExternalSourceStream() = default;
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617 virtual size_t GetMoreData(const uint8_t** src) = 0;
0618 };
0619
0620
0621
0622
0623
0624
0625
0626 class V8_EXPORT StreamedSource {
0627 public:
0628 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
0629
0630 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
0631 Encoding encoding);
0632 ~StreamedSource();
0633
0634 internal::ScriptStreamingData* impl() const { return impl_.get(); }
0635
0636
0637 StreamedSource(const StreamedSource&) = delete;
0638 StreamedSource& operator=(const StreamedSource&) = delete;
0639
0640 CompilationDetails& compilation_details() { return compilation_details_; }
0641
0642 private:
0643 std::unique_ptr<internal::ScriptStreamingData> impl_;
0644
0645
0646
0647 CompilationDetails compilation_details_;
0648 };
0649
0650
0651
0652
0653
0654 class V8_EXPORT ScriptStreamingTask final {
0655 public:
0656 void Run();
0657
0658 private:
0659 friend class ScriptCompiler;
0660
0661 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
0662 : data_(data) {}
0663
0664 internal::ScriptStreamingData* data_;
0665 };
0666
0667
0668
0669
0670
0671
0672 class V8_EXPORT ConsumeCodeCacheTask final {
0673 public:
0674 ~ConsumeCodeCacheTask();
0675
0676 void Run();
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691 void SourceTextAvailable(Isolate* isolate, Local<String> source_text,
0692 const ScriptOrigin& origin);
0693
0694
0695
0696
0697
0698
0699 bool ShouldMergeWithExistingScript() const;
0700
0701
0702
0703
0704
0705
0706 void MergeWithExistingScript();
0707
0708 private:
0709 friend class ScriptCompiler;
0710
0711 explicit ConsumeCodeCacheTask(
0712 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
0713
0714 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
0715 };
0716
0717 enum CompileOptions {
0718 kNoCompileOptions = 0,
0719 kConsumeCodeCache = 1 << 0,
0720 kEagerCompile = 1 << 1,
0721 kProduceCompileHints = 1 << 2,
0722 kConsumeCompileHints = 1 << 3,
0723 kFollowCompileHintsMagicComment = 1 << 4,
0724 kFollowCompileHintsPerFunctionMagicComment = 1 << 5,
0725 };
0726
0727 static inline bool CompileOptionsIsValid(CompileOptions compile_options) {
0728
0729 if ((compile_options & kConsumeCodeCache) &&
0730 compile_options != kConsumeCodeCache) {
0731 return false;
0732 }
0733
0734 if ((compile_options & kEagerCompile) && compile_options != kEagerCompile) {
0735 return false;
0736 }
0737
0738
0739 constexpr int produce_and_consume = CompileOptions::kProduceCompileHints |
0740 CompileOptions::kConsumeCompileHints;
0741 if ((compile_options & produce_and_consume) == produce_and_consume) {
0742 return false;
0743 }
0744 return true;
0745 }
0746
0747
0748
0749
0750 enum NoCacheReason {
0751 kNoCacheNoReason = 0,
0752 kNoCacheBecauseCachingDisabled,
0753 kNoCacheBecauseNoResource,
0754 kNoCacheBecauseInlineScript,
0755 kNoCacheBecauseModule,
0756 kNoCacheBecauseStreamingSource,
0757 kNoCacheBecauseInspector,
0758 kNoCacheBecauseScriptTooSmall,
0759 kNoCacheBecauseCacheTooCold,
0760 kNoCacheBecauseV8Extension,
0761 kNoCacheBecauseExtensionModule,
0762 kNoCacheBecausePacScript,
0763 kNoCacheBecauseInDocumentWrite,
0764 kNoCacheBecauseResourceWithNoCacheHandler,
0765 kNoCacheBecauseDeferredProduceCodeCache,
0766 kNoCacheBecauseStaticCodeCache,
0767 };
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
0784 Isolate* isolate, Source* source,
0785 CompileOptions options = kNoCompileOptions,
0786 NoCacheReason no_cache_reason = kNoCacheNoReason);
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0800 Local<Context> context, Source* source,
0801 CompileOptions options = kNoCompileOptions,
0802 NoCacheReason no_cache_reason = kNoCacheNoReason);
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815 static ScriptStreamingTask* StartStreaming(
0816 Isolate* isolate, StreamedSource* source,
0817 ScriptType type = ScriptType::kClassic,
0818 CompileOptions options = kNoCompileOptions,
0819 CompileHintCallback compile_hint_callback = nullptr,
0820 void* compile_hint_callback_data = nullptr);
0821
0822 static ConsumeCodeCacheTask* StartConsumingCodeCache(
0823 Isolate* isolate, std::unique_ptr<CachedData> source);
0824 static ConsumeCodeCacheTask* StartConsumingCodeCacheOnBackground(
0825 Isolate* isolate, std::unique_ptr<CachedData> source);
0826
0827
0828
0829
0830
0831
0832
0833
0834 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0835 Local<Context> context, StreamedSource* source,
0836 Local<String> full_source_string, const ScriptOrigin& origin);
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 static uint32_t CachedDataVersionTag();
0857
0858
0859
0860
0861
0862
0863
0864
0865 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule(
0866 Isolate* isolate, Source* source,
0867 CompileOptions options = kNoCompileOptions,
0868 NoCacheReason no_cache_reason = kNoCacheNoReason);
0869
0870
0871
0872
0873
0874
0875
0876
0877 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule(
0878 Local<Context> context, StreamedSource* v8_source,
0879 Local<String> full_source_string, const ScriptOrigin& origin);
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunction(
0892 Local<Context> context, Source* source, size_t arguments_count = 0,
0893 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
0894 Local<Object> context_extensions[] = nullptr,
0895 CompileOptions options = kNoCompileOptions,
0896 NoCacheReason no_cache_reason = kNoCacheNoReason);
0897
0898
0899
0900
0901
0902
0903 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script);
0904
0905
0906
0907
0908
0909
0910 static CachedData* CreateCodeCache(
0911 Local<UnboundModuleScript> unbound_module_script);
0912
0913
0914
0915
0916
0917
0918
0919 static CachedData* CreateCodeCacheForFunction(Local<Function> function);
0920
0921 private:
0922 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
0923 Isolate* isolate, Source* source, CompileOptions options,
0924 NoCacheReason no_cache_reason);
0925
0926 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
0927 Local<Context> context, Source* source, size_t arguments_count,
0928 Local<String> arguments[], size_t context_extension_count,
0929 Local<Object> context_extensions[], CompileOptions options,
0930 NoCacheReason no_cache_reason,
0931 Local<ScriptOrModule>* script_or_module_out);
0932 };
0933
0934 ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
0935 CachedData* data,
0936 ConsumeCodeCacheTask* consume_cache_task)
0937 : source_string(string),
0938 resource_name(origin.ResourceName()),
0939 resource_line_offset(origin.LineOffset()),
0940 resource_column_offset(origin.ColumnOffset()),
0941 resource_options(origin.Options()),
0942 source_map_url(origin.SourceMapUrl()),
0943 host_defined_options(origin.GetHostDefinedOptions()),
0944 cached_data(data),
0945 consume_cache_task(consume_cache_task) {}
0946
0947 ScriptCompiler::Source::Source(Local<String> string, CachedData* data,
0948 ConsumeCodeCacheTask* consume_cache_task)
0949 : source_string(string),
0950 cached_data(data),
0951 consume_cache_task(consume_cache_task) {}
0952
0953 ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
0954 CompileHintCallback callback,
0955 void* callback_data)
0956 : source_string(string),
0957 resource_name(origin.ResourceName()),
0958 resource_line_offset(origin.LineOffset()),
0959 resource_column_offset(origin.ColumnOffset()),
0960 resource_options(origin.Options()),
0961 source_map_url(origin.SourceMapUrl()),
0962 host_defined_options(origin.GetHostDefinedOptions()),
0963 compile_hint_callback(callback),
0964 compile_hint_callback_data(callback_data) {}
0965
0966 const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
0967 const {
0968 return cached_data.get();
0969 }
0970
0971 const ScriptOriginOptions& ScriptCompiler::Source::GetResourceOptions() const {
0972 return resource_options;
0973 }
0974
0975 const ScriptCompiler::CompilationDetails&
0976 ScriptCompiler::Source::GetCompilationDetails() const {
0977 return compilation_details;
0978 }
0979
0980 ModuleRequest* ModuleRequest::Cast(Data* data) {
0981 #ifdef V8_ENABLE_CHECKS
0982 CheckCast(data);
0983 #endif
0984 return reinterpret_cast<ModuleRequest*>(data);
0985 }
0986
0987 Module* Module::Cast(Data* data) {
0988 #ifdef V8_ENABLE_CHECKS
0989 CheckCast(data);
0990 #endif
0991 return reinterpret_cast<Module*>(data);
0992 }
0993
0994 }
0995
0996 #endif