v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-script.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_SCRIPT_H_
6#define INCLUDE_V8_SCRIPT_H_
7
8#include <stddef.h>
9#include <stdint.h>
10
11#include <memory>
12#include <tuple>
13#include <vector>
14
15#include "v8-callbacks.h" // NOLINT(build/include_directory)
16#include "v8-data.h" // NOLINT(build/include_directory)
17#include "v8-local-handle.h" // NOLINT(build/include_directory)
18#include "v8-maybe.h" // NOLINT(build/include_directory)
19#include "v8-memory-span.h" // NOLINT(build/include_directory)
20#include "v8-message.h" // NOLINT(build/include_directory)
21#include "v8config.h" // NOLINT(build/include_directory)
22
23namespace v8 {
24
25class Function;
26class Message;
27class Object;
28class PrimitiveArray;
29class Script;
30
31namespace internal {
32class BackgroundDeserializeTask;
33struct ScriptStreamingData;
34} // namespace internal
35
43 public:
49
55};
56
61 public:
66
67 int GetId() const;
69
78
83 int GetLineNumber(int code_pos = 0);
84
89 int GetColumnNumber(int code_pos = 0);
90
91 static const int kNoScriptId = 0;
92};
93
98 public:
107};
108
113 public:
114 int GetLineNumber() { return line_number_; }
115 int GetColumnNumber() { return column_number_; }
116
117 Location(int line_number, int column_number)
118 : line_number_(line_number), column_number_(column_number) {}
119
120 private:
121 int line_number_;
122 int column_number_;
123};
124
126 public:
131
136 int GetSourceOffset() const;
137
152
153 V8_DEPRECATE_SOON("Use GetImportAttributes instead")
154 Local<FixedArray> GetImportAssertions() const {
155 return GetImportAttributes();
156 }
157
158 V8_INLINE static ModuleRequest* Cast(Data* data);
159
160 private:
161 static void CheckCast(Data* obj);
162};
163
167class V8_EXPORT Module : public Data {
168 public:
176 enum Status {
182 kErrored
183 };
184
189
194
199
205
209 int GetIdentityHash() const;
210
212 Local<Context> context, Local<String> specifier,
213 Local<FixedArray> import_assertions, Local<Module> referrer);
214
223 Local<Context> context, ResolveModuleCallback callback);
224
236
243
251
257 int ScriptId() const;
258
265 bool IsGraphAsync() const;
266
270 bool IsSourceTextModule() const;
271
275 bool IsSyntheticModule() const;
276
277 /*
278 * Callback defined in the embedder. This is responsible for setting
279 * the module's exported values with calls to SetSyntheticModuleExport().
280 * The callback must return a resolved Promise to indicate success (where no
281 * exception was thrown) and return an empy MaybeLocal to indicate falure
282 * (where an exception was thrown).
283 */
286
295 Isolate* isolate, Local<String> module_name,
296 const MemorySpan<const Local<String>>& export_names,
297 SyntheticModuleEvaluationSteps evaluation_steps);
298
307 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
308
316 std::pair<LocalVector<Module>, LocalVector<Message>>
318
319 V8_INLINE static Module* Cast(Data* data);
320
321 private:
322 static void CheckCast(Data* obj);
323};
324
329class V8_EXPORT Script : public Data {
330 public:
335 Local<Context> context, Local<String> source,
336 ScriptOrigin* origin = nullptr);
337
345 Local<Data> host_defined_options);
346
351
357
362 std::vector<int> GetProducedCompileHints() const;
363};
364
366
371 public:
373
382 enum BufferPolicy { BufferNotOwned, BufferOwned };
383
385 : data(nullptr),
386 length(0),
387 rejected(false),
388 buffer_policy(BufferNotOwned) {}
389
390 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
391 // data and guarantees that it stays alive until the CachedData object is
392 // destroyed. If the policy is BufferOwned, the given data will be deleted
393 // (with delete[]) when the CachedData object is destroyed.
394 CachedData(const uint8_t* data, int length,
395 BufferPolicy buffer_policy = BufferNotOwned);
397
399 // Don't change order/existing values of this enum since it keys into the
400 // `code_cache_reject_reason` histogram. Append-only!
402 kMagicNumberMismatch = 1,
403 kVersionMismatch = 2,
404 kSourceMismatch = 3,
405 kFlagsMismatch = 5,
406 kChecksumMismatch = 6,
407 kInvalidHeader = 7,
408 kLengthMismatch = 8,
409 kReadOnlySnapshotChecksumMismatch = 9,
410
411 // This should always point at the last real enum value.
412 kLast = kReadOnlySnapshotChecksumMismatch
413 };
414
415 // Check if the CachedData can be loaded in the given isolate.
417
418 // TODO(marja): Async compilation; add constructors which take a callback
419 // which will be called when V8 no longer needs the data.
420 const uint8_t* data;
424
425 // Prevent copying.
426 CachedData(const CachedData&) = delete;
427 CachedData& operator=(const CachedData&) = delete;
428 };
429
431 // V8 did not attempt to find this script in its in-memory cache.
432 kNotAttempted,
433
434 // V8 found a previously compiled copy of this script in its in-memory
435 // cache. Any data generated by a streaming compilation or background
436 // deserialization was abandoned.
437 kHit,
438
439 // V8 didn't have any previously compiled data for this script.
440 kMiss,
441
442 // V8 had some previously compiled data for an identical script, but the
443 // data was incomplete.
444 kPartial,
445 };
446
447 // Details about what happened during a compilation.
449 InMemoryCacheResult in_memory_cache_result =
450 InMemoryCacheResult::kNotAttempted;
451
452 static constexpr int64_t kTimeNotMeasured = -1;
453 int64_t foreground_time_in_microseconds = kTimeNotMeasured;
454 int64_t background_time_in_microseconds = kTimeNotMeasured;
455 };
456
460 class Source {
461 public:
462 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
463 // The caller *must* ensure that the cached data is from a trusted source.
464 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
465 CachedData* cached_data = nullptr,
466 ConsumeCodeCacheTask* consume_cache_task = nullptr);
467 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
468 V8_INLINE explicit Source(
469 Local<String> source_string, CachedData* cached_data = nullptr,
470 ConsumeCodeCacheTask* consume_cache_task = nullptr);
471 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
472 CompileHintCallback callback, void* callback_data);
473 V8_INLINE ~Source() = default;
474
475 // Ownership of the CachedData or its buffers is *not* transferred to the
476 // caller. The CachedData object is alive as long as the Source object is
477 // alive.
478 V8_INLINE const CachedData* GetCachedData() const;
479
480 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
481
482 V8_INLINE const CompilationDetails& GetCompilationDetails() const;
483
484 private:
485 friend class ScriptCompiler;
486
487 Local<String> source_string;
488
489 // Origin information
490 Local<Value> resource_name;
491 int resource_line_offset = -1;
492 int resource_column_offset = -1;
493 ScriptOriginOptions resource_options;
494 Local<Value> source_map_url;
495 Local<Data> host_defined_options;
496
497 // Cached data from previous compilation (if a kConsume*Cache flag is
498 // set), or hold newly generated cache data (kProduce*Cache flags) are
499 // set when calling a compile method.
500 std::unique_ptr<CachedData> cached_data;
501 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
502
503 // For requesting compile hints from the embedder.
504 CompileHintCallback compile_hint_callback = nullptr;
505 void* compile_hint_callback_data = nullptr;
506
507 // V8 writes this data and never reads it. It exists only to be informative
508 // to the embedder.
509 CompilationDetails compilation_details;
510 };
511
517 public:
518 virtual ~ExternalSourceStream() = default;
519
541 virtual size_t GetMoreData(const uint8_t** src) = 0;
542 };
543
551 public:
552 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
553
554 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
555 Encoding encoding);
557
558 internal::ScriptStreamingData* impl() const { return impl_.get(); }
559
560 // Prevent copying.
563
564 CompilationDetails& compilation_details() { return compilation_details_; }
565
566 private:
567 std::unique_ptr<internal::ScriptStreamingData> impl_;
568
569 // V8 writes this data and never reads it. It exists only to be informative
570 // to the embedder.
571 CompilationDetails compilation_details_;
572 };
573
579 public:
580 void Run();
581
582 private:
583 friend class ScriptCompiler;
584
585 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
586 : data_(data) {}
587
588 internal::ScriptStreamingData* data_;
589 };
590
597 public:
599
600 void Run();
601
615 void SourceTextAvailable(Isolate* isolate, Local<String> source_text,
616 const ScriptOrigin& origin);
617
624
631
632 private:
633 friend class ScriptCompiler;
634
635 explicit ConsumeCodeCacheTask(
636 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
637
638 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
639 };
640
642 kNoCompileOptions = 0,
646 kConsumeCompileHints
647 };
648
653 kNoCacheNoReason = 0,
667 kNoCacheBecauseDeferredProduceCodeCache
668 };
669
685 Isolate* isolate, Source* source,
686 CompileOptions options = kNoCompileOptions,
687 NoCacheReason no_cache_reason = kNoCacheNoReason);
688
701 Local<Context> context, Source* source,
702 CompileOptions options = kNoCompileOptions,
703 NoCacheReason no_cache_reason = kNoCacheNoReason);
704
717 Isolate* isolate, StreamedSource* source,
718 ScriptType type = ScriptType::kClassic,
719 CompileOptions options = kNoCompileOptions,
720 CompileHintCallback compile_hint_callback = nullptr,
721 void* compile_hint_callback_data = nullptr);
722
724 Isolate* isolate, std::unique_ptr<CachedData> source);
725
734 Local<Context> context, StreamedSource* source,
735 Local<String> full_source_string, const ScriptOrigin& origin);
736
755 static uint32_t CachedDataVersionTag();
756
765 Isolate* isolate, Source* source,
766 CompileOptions options = kNoCompileOptions,
767 NoCacheReason no_cache_reason = kNoCacheNoReason);
768
777 Local<Context> context, StreamedSource* v8_source,
778 Local<String> full_source_string, const ScriptOrigin& origin);
779
790 V8_DEPRECATED("Use CompileFunction")
791 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
792 Local<Context> context, Source* source, size_t arguments_count,
793 Local<String> arguments[], size_t context_extension_count,
794 Local<Object> context_extensions[],
795 CompileOptions options = kNoCompileOptions,
796 NoCacheReason no_cache_reason = kNoCacheNoReason,
797 Local<ScriptOrModule>* script_or_module_out = nullptr);
798
800 Local<Context> context, Source* source, size_t arguments_count = 0,
801 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
802 Local<Object> context_extensions[] = nullptr,
803 CompileOptions options = kNoCompileOptions,
804 NoCacheReason no_cache_reason = kNoCacheNoReason);
805
811 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script);
812
818 static CachedData* CreateCodeCache(
819 Local<UnboundModuleScript> unbound_module_script);
820
827 static CachedData* CreateCodeCacheForFunction(Local<Function> function);
828
829 private:
830 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
831 Isolate* isolate, Source* source, CompileOptions options,
832 NoCacheReason no_cache_reason);
833
834 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
835 Local<Context> context, Source* source, size_t arguments_count,
836 Local<String> arguments[], size_t context_extension_count,
837 Local<Object> context_extensions[], CompileOptions options,
838 NoCacheReason no_cache_reason,
839 Local<ScriptOrModule>* script_or_module_out);
840};
841
843 CachedData* data,
844 ConsumeCodeCacheTask* consume_cache_task)
845 : source_string(string),
846 resource_name(origin.ResourceName()),
847 resource_line_offset(origin.LineOffset()),
848 resource_column_offset(origin.ColumnOffset()),
849 resource_options(origin.Options()),
850 source_map_url(origin.SourceMapUrl()),
851 host_defined_options(origin.GetHostDefinedOptions()),
852 cached_data(data),
853 consume_cache_task(consume_cache_task) {}
854
855ScriptCompiler::Source::Source(Local<String> string, CachedData* data,
856 ConsumeCodeCacheTask* consume_cache_task)
857 : source_string(string),
858 cached_data(data),
859 consume_cache_task(consume_cache_task) {}
860
862 CompileHintCallback callback,
863 void* callback_data)
864 : source_string(string),
865 resource_name(origin.ResourceName()),
866 resource_line_offset(origin.LineOffset()),
867 resource_column_offset(origin.ColumnOffset()),
868 resource_options(origin.Options()),
869 source_map_url(origin.SourceMapUrl()),
870 host_defined_options(origin.GetHostDefinedOptions()),
871 compile_hint_callback(callback),
872 compile_hint_callback_data(callback_data) {}
873
875 const {
876 return cached_data.get();
877}
878
880 return resource_options;
881}
882
885 return compilation_details;
886}
887
889#ifdef V8_ENABLE_CHECKS
890 CheckCast(data);
891#endif
892 return reinterpret_cast<ModuleRequest*>(data);
893}
894
896#ifdef V8_ENABLE_CHECKS
897 CheckCast(data);
898#endif
899 return reinterpret_cast<Module*>(data);
900}
901
902} // namespace v8
903
904#endif // INCLUDE_V8_SCRIPT_H_
int GetColumnNumber()
Definition v8-script.h:115
int GetLineNumber()
Definition v8-script.h:114
Location(int line_number, int column_number)
Definition v8-script.h:117
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Evaluate(Local< Context > context)
std::pair< LocalVector< Module >, LocalVector< Message > > GetStalledTopLevelAwaitMessages(Isolate *isolate)
Status GetStatus() const
Local< UnboundModuleScript > GetUnboundModuleScript()
@ kUninstantiated
Definition v8-script.h:177
static Local< Module > CreateSyntheticModule(Isolate *isolate, Local< String > module_name, const MemorySpan< const Local< String > > &export_names, SyntheticModuleEvaluationSteps evaluation_steps)
Local< FixedArray > GetModuleRequests() const
Local< Value > GetModuleNamespace()
Local< Value > GetException() const
V8_WARN_UNUSED_RESULT Maybe< bool > InstantiateModule(Local< Context > context, ResolveModuleCallback callback)
bool IsSyntheticModule() const
static V8_INLINE Module * Cast(Data *data)
Definition v8-script.h:895
Location SourceOffsetToLocation(int offset) const
int GetIdentityHash() const
int ScriptId() const
bool IsSourceTextModule() const
V8_WARN_UNUSED_RESULT Maybe< bool > SetSyntheticModuleExport(Isolate *isolate, Local< String > export_name, Local< Value > export_value)
bool IsGraphAsync() const
static V8_INLINE ModuleRequest * Cast(Data *data)
Definition v8-script.h:888
int GetSourceOffset() const
Local< FixedArray > GetImportAttributes() const
Local< String > GetSpecifier() const
void SourceTextAvailable(Isolate *isolate, Local< String > source_text, const ScriptOrigin &origin)
virtual size_t GetMoreData(const uint8_t **src)=0
V8_INLINE ~Source()=default
V8_INLINE const CompilationDetails & GetCompilationDetails() const
Definition v8-script.h:884
V8_INLINE const CachedData * GetCachedData() const
Definition v8-script.h:874
V8_INLINE const ScriptOriginOptions & GetResourceOptions() const
Definition v8-script.h:879
V8_INLINE Source(Local< String > source_string, const ScriptOrigin &origin, CachedData *cached_data=nullptr, ConsumeCodeCacheTask *consume_cache_task=nullptr)
Definition v8-script.h:842
CompilationDetails & compilation_details()
Definition v8-script.h:564
internal::ScriptStreamingData * impl() const
Definition v8-script.h:558
StreamedSource & operator=(const StreamedSource &)=delete
StreamedSource(const StreamedSource &)=delete
StreamedSource(std::unique_ptr< ExternalSourceStream > source_stream, Encoding encoding)
static V8_WARN_UNUSED_RESULT MaybeLocal< Module > CompileModule(Local< Context > context, StreamedSource *v8_source, Local< String > full_source_string, const ScriptOrigin &origin)
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, StreamedSource *source, Local< String > full_source_string, const ScriptOrigin &origin)
static ConsumeCodeCacheTask * StartConsumingCodeCache(Isolate *isolate, std::unique_ptr< CachedData > source)
@ kNoCacheBecauseResourceWithNoCacheHandler
Definition v8-script.h:666
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static V8_WARN_UNUSED_RESULT MaybeLocal< UnboundScript > CompileUnboundScript(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static uint32_t CachedDataVersionTag()
static V8_WARN_UNUSED_RESULT MaybeLocal< Module > CompileModule(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static ScriptStreamingTask * StartStreaming(Isolate *isolate, StreamedSource *source, ScriptType type=ScriptType::kClassic, CompileOptions options=kNoCompileOptions, CompileHintCallback compile_hint_callback=nullptr, void *compile_hint_callback_data=nullptr)
Local< UnboundScript > GetUnboundScript()
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Local< String > source, ScriptOrigin *origin=nullptr)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Run(Local< Context > context)
std::vector< int > GetProducedCompileHints() const
Local< Value > GetResourceName()
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Run(Local< Context > context, Local< Data > host_defined_options)
Local< Value > GetResourceName()
Local< Data > HostDefinedOptions()
Local< Value > GetSourceMappingURL()
Local< Value > GetSourceURL()
int GetColumnNumber(int code_pos=0)
Local< Script > BindToCurrentContext()
Local< Value > GetSourceMappingURL()
Local< Value > GetSourceURL()
int GetLineNumber(int code_pos=0)
int GetId() const
Local< Value > GetScriptName()
ScriptType
Definition v8-script.h:365
bool(*)(int, void *) CompileHintCallback
CachedData(const CachedData &)=delete
CachedData & operator=(const CachedData &)=delete
CachedData(const uint8_t *data, int length, BufferPolicy buffer_policy=BufferNotOwned)
CompatibilityCheckResult CompatibilityCheck(Isolate *isolate)
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_DEPRECATE_SOON(message)
Definition v8config.h:571
#define V8_DEPRECATED(message)
Definition v8config.h:563
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:628