v8 11.3.244 (node 20.3.0)
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 <vector>
13
14#include "v8-callbacks.h" // NOLINT(build/include_directory)
15#include "v8-data.h" // NOLINT(build/include_directory)
16#include "v8-local-handle.h" // NOLINT(build/include_directory)
17#include "v8-maybe.h" // NOLINT(build/include_directory)
18#include "v8-message.h" // NOLINT(build/include_directory)
19#include "v8config.h" // NOLINT(build/include_directory)
20
21namespace v8 {
22
23class Function;
24class Message;
25class Object;
26class PrimitiveArray;
27class Script;
28
29namespace internal {
30class BackgroundDeserializeTask;
31struct ScriptStreamingData;
32} // namespace internal
33
41 public:
47
53};
54
59 public:
64
65 int GetId() const;
67
76
81 int GetLineNumber(int code_pos = 0);
82
87 int GetColumnNumber(int code_pos = 0);
88
89 static const int kNoScriptId = 0;
90};
91
96 public:
105};
106
111 public:
112 int GetLineNumber() { return line_number_; }
113 int GetColumnNumber() { return column_number_; }
114
115 Location(int line_number, int column_number)
116 : line_number_(line_number), column_number_(column_number) {}
117
118 private:
119 int line_number_;
120 int column_number_;
121};
122
124 public:
129
134 int GetSourceOffset() const;
135
151
152 V8_INLINE static ModuleRequest* Cast(Data* data);
153
154 private:
155 static void CheckCast(Data* obj);
156};
157
161class V8_EXPORT Module : public Data {
162 public:
170 enum Status {
176 kErrored
177 };
178
183
188
193
199
203 int GetIdentityHash() const;
204
206 Local<Context> context, Local<String> specifier,
207 Local<FixedArray> import_assertions, Local<Module> referrer);
208
217 Local<Context> context, ResolveModuleCallback callback);
218
230
237
245
251 int ScriptId() const;
252
259 bool IsGraphAsync() const;
260
264 bool IsSourceTextModule() const;
265
269 bool IsSyntheticModule() const;
270
271 /*
272 * Callback defined in the embedder. This is responsible for setting
273 * the module's exported values with calls to SetSyntheticModuleExport().
274 * The callback must return a resolved Promise to indicate success (where no
275 * exception was thrown) and return an empy MaybeLocal to indicate falure
276 * (where an exception was thrown).
277 */
280
289 Isolate* isolate, Local<String> module_name,
290 const std::vector<Local<String>>& export_names,
291 SyntheticModuleEvaluationSteps evaluation_steps);
292
301 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
302
310 std::vector<std::tuple<Local<Module>, Local<Message>>>
312
313 V8_INLINE static Module* Cast(Data* data);
314
315 private:
316 static void CheckCast(Data* obj);
317};
318
324 public:
329 Local<Context> context, Local<String> source,
330 ScriptOrigin* origin = nullptr);
331
339 Local<Data> host_defined_options);
340
345
351
356 std::vector<int> GetProducedCompileHints() const;
357};
358
360
365 public:
367
376 enum BufferPolicy { BufferNotOwned, BufferOwned };
377
379 : data(nullptr),
380 length(0),
381 rejected(false),
382 buffer_policy(BufferNotOwned) {}
383
384 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
385 // data and guarantees that it stays alive until the CachedData object is
386 // destroyed. If the policy is BufferOwned, the given data will be deleted
387 // (with delete[]) when the CachedData object is destroyed.
388 CachedData(const uint8_t* data, int length,
389 BufferPolicy buffer_policy = BufferNotOwned);
391 // TODO(marja): Async compilation; add constructors which take a callback
392 // which will be called when V8 no longer needs the data.
393 const uint8_t* data;
397
398 // Prevent copying.
399 CachedData(const CachedData&) = delete;
400 CachedData& operator=(const CachedData&) = delete;
401 };
402
406 class Source {
407 public:
408 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
409 // The caller *must* ensure that the cached data is from a trusted source.
410 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
411 CachedData* cached_data = nullptr,
412 ConsumeCodeCacheTask* consume_cache_task = nullptr);
413 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
414 V8_INLINE explicit Source(
415 Local<String> source_string, CachedData* cached_data = nullptr,
416 ConsumeCodeCacheTask* consume_cache_task = nullptr);
417 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
418 CompileHintCallback callback, void* callback_data);
419 V8_INLINE ~Source() = default;
420
421 // Ownership of the CachedData or its buffers is *not* transferred to the
422 // caller. The CachedData object is alive as long as the Source object is
423 // alive.
424 V8_INLINE const CachedData* GetCachedData() const;
425
426 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
427
428 private:
429 friend class ScriptCompiler;
430
431 Local<String> source_string;
432
433 // Origin information
434 Local<Value> resource_name;
435 int resource_line_offset;
436 int resource_column_offset;
437 ScriptOriginOptions resource_options;
438 Local<Value> source_map_url;
439 Local<Data> host_defined_options;
440
441 // Cached data from previous compilation (if a kConsume*Cache flag is
442 // set), or hold newly generated cache data (kProduce*Cache flags) are
443 // set when calling a compile method.
444 std::unique_ptr<CachedData> cached_data;
445 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
446
447 // For requesting compile hints from the embedder.
448 CompileHintCallback compile_hint_callback = nullptr;
449 void* compile_hint_callback_data = nullptr;
450 };
451
457 public:
458 virtual ~ExternalSourceStream() = default;
459
481 virtual size_t GetMoreData(const uint8_t** src) = 0;
482 };
483
491 public:
492 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
493
494 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
495 Encoding encoding);
497
498 internal::ScriptStreamingData* impl() const { return impl_.get(); }
499
500 // Prevent copying.
503
504 private:
505 std::unique_ptr<internal::ScriptStreamingData> impl_;
506 };
507
513 public:
514 void Run();
515
516 private:
517 friend class ScriptCompiler;
518
519 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
520 : data_(data) {}
521
522 internal::ScriptStreamingData* data_;
523 };
524
531 public:
533
534 void Run();
535
549 void SourceTextAvailable(Isolate* isolate, Local<String> source_text,
550 const ScriptOrigin& origin);
551
558
565
566 private:
567 friend class ScriptCompiler;
568
569 explicit ConsumeCodeCacheTask(
570 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
571
572 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
573 };
574
576 kNoCompileOptions = 0,
580 kConsumeCompileHints
581 };
582
587 kNoCacheNoReason = 0,
601 kNoCacheBecauseDeferredProduceCodeCache
602 };
603
619 Isolate* isolate, Source* source,
620 CompileOptions options = kNoCompileOptions,
621 NoCacheReason no_cache_reason = kNoCacheNoReason);
622
635 Local<Context> context, Source* source,
636 CompileOptions options = kNoCompileOptions,
637 NoCacheReason no_cache_reason = kNoCacheNoReason);
638
651 Isolate* isolate, StreamedSource* source,
652 ScriptType type = ScriptType::kClassic,
653 CompileOptions options = kNoCompileOptions);
654
656 Isolate* isolate, std::unique_ptr<CachedData> source);
657
666 Local<Context> context, StreamedSource* source,
667 Local<String> full_source_string, const ScriptOrigin& origin);
668
687 static uint32_t CachedDataVersionTag();
688
697 Isolate* isolate, Source* source,
698 CompileOptions options = kNoCompileOptions,
699 NoCacheReason no_cache_reason = kNoCacheNoReason);
700
709 Local<Context> context, StreamedSource* v8_source,
710 Local<String> full_source_string, const ScriptOrigin& origin);
711
722 V8_DEPRECATED("Use CompileFunction")
723 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
724 Local<Context> context, Source* source, size_t arguments_count,
725 Local<String> arguments[], size_t context_extension_count,
726 Local<Object> context_extensions[],
727 CompileOptions options = kNoCompileOptions,
728 NoCacheReason no_cache_reason = kNoCacheNoReason,
729 Local<ScriptOrModule>* script_or_module_out = nullptr);
730
732 Local<Context> context, Source* source, size_t arguments_count = 0,
733 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
734 Local<Object> context_extensions[] = nullptr,
735 CompileOptions options = kNoCompileOptions,
736 NoCacheReason no_cache_reason = kNoCacheNoReason);
737
743 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script);
744
750 static CachedData* CreateCodeCache(
751 Local<UnboundModuleScript> unbound_module_script);
752
759 static CachedData* CreateCodeCacheForFunction(Local<Function> function);
760
761 private:
762 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
763 Isolate* isolate, Source* source, CompileOptions options,
764 NoCacheReason no_cache_reason);
765
766 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
767 Local<Context> context, Source* source, size_t arguments_count,
768 Local<String> arguments[], size_t context_extension_count,
769 Local<Object> context_extensions[], CompileOptions options,
770 NoCacheReason no_cache_reason,
771 Local<ScriptOrModule>* script_or_module_out);
772};
773
775 CachedData* data,
776 ConsumeCodeCacheTask* consume_cache_task)
777 : source_string(string),
778 resource_name(origin.ResourceName()),
779 resource_line_offset(origin.LineOffset()),
780 resource_column_offset(origin.ColumnOffset()),
781 resource_options(origin.Options()),
782 source_map_url(origin.SourceMapUrl()),
783 host_defined_options(origin.GetHostDefinedOptions()),
784 cached_data(data),
785 consume_cache_task(consume_cache_task) {}
786
787ScriptCompiler::Source::Source(Local<String> string, CachedData* data,
788 ConsumeCodeCacheTask* consume_cache_task)
789 : source_string(string),
790 cached_data(data),
791 consume_cache_task(consume_cache_task) {}
792
794 CompileHintCallback callback,
795 void* callback_data)
796 : source_string(string),
797 resource_name(origin.ResourceName()),
798 resource_line_offset(origin.LineOffset()),
799 resource_column_offset(origin.ColumnOffset()),
800 resource_options(origin.Options()),
801 source_map_url(origin.SourceMapUrl()),
802 host_defined_options(origin.GetHostDefinedOptions()),
803 compile_hint_callback(callback),
804 compile_hint_callback_data(callback_data) {}
805
807 const {
808 return cached_data.get();
809}
810
812 return resource_options;
813}
814
816#ifdef V8_ENABLE_CHECKS
817 CheckCast(data);
818#endif
819 return reinterpret_cast<ModuleRequest*>(data);
820}
821
823#ifdef V8_ENABLE_CHECKS
824 CheckCast(data);
825#endif
826 return reinterpret_cast<Module*>(data);
827}
828
829} // namespace v8
830
831#endif // INCLUDE_V8_SCRIPT_H_
int GetColumnNumber()
Definition v8-script.h:113
int GetLineNumber()
Definition v8-script.h:112
Location(int line_number, int column_number)
Definition v8-script.h:115
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Evaluate(Local< Context > context)
Status GetStatus() const
Local< UnboundModuleScript > GetUnboundModuleScript()
@ kUninstantiated
Definition v8-script.h:171
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:822
Location SourceOffsetToLocation(int offset) const
int GetIdentityHash() const
std::vector< std::tuple< Local< Module >, Local< Message > > > GetStalledTopLevelAwaitMessage(Isolate *isolate)
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 Local< Module > CreateSyntheticModule(Isolate *isolate, Local< String > module_name, const std::vector< Local< String > > &export_names, SyntheticModuleEvaluationSteps evaluation_steps)
static V8_INLINE ModuleRequest * Cast(Data *data)
Definition v8-script.h:815
Local< FixedArray > GetImportAssertions() const
int GetSourceOffset() 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 CachedData * GetCachedData() const
Definition v8-script.h:806
V8_INLINE const ScriptOriginOptions & GetResourceOptions() const
Definition v8-script.h:811
V8_INLINE Source(Local< String > source_string, const ScriptOrigin &origin, CachedData *cached_data=nullptr, ConsumeCodeCacheTask *consume_cache_task=nullptr)
Definition v8-script.h:774
internal::ScriptStreamingData * impl() const
Definition v8-script.h:498
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:600
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)
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:359
bool(*)(int, void *) CompileHintCallback
CachedData(const CachedData &)=delete
CachedData & operator=(const CachedData &)=delete
CachedData(const uint8_t *data, int length, BufferPolicy buffer_policy=BufferNotOwned)
#define V8_EXPORT
Definition v8config.h:719
#define V8_INLINE
Definition v8config.h:460
#define V8_DEPRECATED(message)
Definition v8config.h:544
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:609