v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
script-inl.h
Go to the documentation of this file.
1// Copyright 2017 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 V8_OBJECTS_SCRIPT_INL_H_
6#define V8_OBJECTS_SCRIPT_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
11#include "src/objects/managed.h"
12#include "src/objects/objects.h"
14#include "src/objects/smi-inl.h"
16
17// Has to be the last include (doesn't have include guards):
19
20namespace v8 {
21namespace internal {
22
23#include "torque-generated/src/objects/script-tq-inl.inc"
24
26
28
29#if V8_ENABLE_WEBASSEMBLY
30ACCESSORS_CHECKED(Script, wasm_breakpoint_infos, Tagged<FixedArray>,
31 kEvalFromSharedOrWrappedArgumentsOffset,
32 this->type() == Type::kWasm)
33ACCESSORS_CHECKED(Script, wasm_managed_native_module, Tagged<Object>,
34 kEvalFromPositionOffset, this->type() == Type::kWasm)
35ACCESSORS_CHECKED(Script, wasm_weak_instance_list, Tagged<WeakArrayList>,
36 kInfosOffset, this->type() == Type::kWasm)
37#define CHECK_SCRIPT_NOT_WASM this->type() != Type::kWasm
38#else
39#define CHECK_SCRIPT_NOT_WASM true
40#endif // V8_ENABLE_WEBASSEMBLY
41
42Script::Type Script::type() const {
44 return static_cast<Type>(value.value());
45}
46void Script::set_type(Type value) {
48 *this, Smi::FromInt(static_cast<int>(value)));
49}
50
51ACCESSORS_CHECKED(Script, eval_from_shared_or_wrapped_arguments, Tagged<Object>,
52 kEvalFromSharedOrWrappedArgumentsOffset,
56#undef CHECK_SCRIPT_NOT_WASM
57
58ACCESSORS(Script, compiled_lazy_function_positions, Tagged<Object>,
59 kCompiledLazyFunctionPositionsOffset)
60
61bool Script::is_wrapped() const {
62 return IsFixedArray(eval_from_shared_or_wrapped_arguments());
63}
64
66 return IsSharedFunctionInfo(eval_from_shared_or_wrapped_arguments());
67}
68
69void Script::set_eval_from_shared(Tagged<SharedFunctionInfo> shared,
70 WriteBarrierMode mode) {
72 set_eval_from_shared_or_wrapped_arguments(shared, mode);
73}
74
75Tagged<SharedFunctionInfo> Script::eval_from_shared() const {
77 return Cast<SharedFunctionInfo>(eval_from_shared_or_wrapped_arguments());
78}
79
80void Script::set_wrapped_arguments(Tagged<FixedArray> value,
81 WriteBarrierMode mode) {
83 set_eval_from_shared_or_wrapped_arguments(value, mode);
84}
85
86Tagged<FixedArray> Script::wrapped_arguments() const {
88 return Cast<FixedArray>(eval_from_shared_or_wrapped_arguments());
89}
90
92#if V8_ENABLE_WEBASSEMBLY
93 if (type() == Type::kWasm) {
94 return GetReadOnlyRoots().empty_weak_fixed_array();
95 }
96#endif // V8_ENABLE_WEBASSEMBLY
98}
99
100void Script::set_infos(Tagged<WeakFixedArray> value, WriteBarrierMode mode) {
101#if V8_ENABLE_WEBASSEMBLY
102 DCHECK_NE(Type::kWasm, type());
103#endif // V8_ENABLE_WEBASSEMBLY
105 CONDITIONAL_WRITE_BARRIER(*this, kInfosOffset, value, mode);
106}
107
108#if V8_ENABLE_WEBASSEMBLY
109bool Script::has_wasm_breakpoint_infos() const {
110 return type() == Type::kWasm && wasm_breakpoint_infos()->length() > 0;
111}
112
113wasm::NativeModule* Script::wasm_native_module() const {
114 return Cast<Managed<wasm::NativeModule>>(wasm_managed_native_module())->raw();
115}
116
117bool Script::break_on_entry() const { return BreakOnEntryBit::decode(flags()); }
118
119void Script::set_break_on_entry(bool value) {
120 set_flags(BreakOnEntryBit::update(flags(), value));
121}
122#endif // V8_ENABLE_WEBASSEMBLY
123
124uint32_t Script::flags() const {
125 // Use a relaxed load since background compile threads read the
126 // {compilation_type()} while the foreground thread might update e.g. the
127 // {origin_options}.
128 return TaggedField<Smi>::Relaxed_Load(*this, kFlagsOffset).value();
129}
130
131void Script::set_flags(uint32_t new_flags) {
132 DCHECK(is_int31(new_flags));
133 TaggedField<Smi>::Relaxed_Store(*this, kFlagsOffset, Smi::FromInt(new_flags));
134}
135
137 return CompilationTypeBit::decode(flags());
138}
140 set_flags(CompilationTypeBit::update(flags(), type));
141}
143 return CompilationStateBit::decode(flags());
144}
146 set_flags(CompilationStateBit::update(flags(), state));
147}
148
150 return ProduceCompileHintsBit::decode(flags());
151}
152
153void Script::set_produce_compile_hints(bool produce_compile_hints) {
154 set_flags(ProduceCompileHintsBit::update(flags(), produce_compile_hints));
155}
156
157bool Script::deserialized() const { return DeserializedBit::decode(flags()); }
158
159void Script::set_deserialized(bool value) {
160 set_flags(DeserializedBit::update(flags(), value));
161}
162
163bool Script::is_repl_mode() const { return IsReplModeBit::decode(flags()); }
164
165void Script::set_is_repl_mode(bool value) {
166 set_flags(IsReplModeBit::update(flags(), value));
167}
168
170 return ScriptOriginOptions(OriginOptionsBits::decode(flags()));
171}
173 DCHECK(!(origin_options.Flags() & ~((1 << OriginOptionsBits::kSize) - 1)));
174 set_flags(OriginOptionsBits::update(flags(), origin_options.Flags()));
175}
176
178 Tagged<Object> src = this->source();
179 if (!IsString(src)) return true;
180 Tagged<String> src_str = Cast<String>(src);
181 if (!StringShape(src_str).IsExternal()) return true;
182 if (src_str->IsOneByteRepresentation()) {
183 return Cast<ExternalOneByteString>(src)->resource() != nullptr;
184 } else if (src_str->IsTwoByteRepresentation()) {
185 return Cast<ExternalTwoByteString>(src)->resource() != nullptr;
186 }
187 return true;
188}
189
190bool Script::has_line_ends() const { return line_ends() != Smi::zero(); }
191
193#if V8_ENABLE_WEBASSEMBLY
194 return type() != Script::Type::kWasm;
195#else
196 return true;
197#endif // V8_ENABLE_WEBASSEMBLY
198}
199
200// static
202 if (script->has_line_ends()) return;
203 Script::InitLineEndsInternal(isolate, script);
204}
205// static
207 if (script->has_line_ends()) return;
208 Script::InitLineEndsInternal(isolate, script);
209}
210
212 return IsString(source_url()) && Cast<String>(source_url())->length() != 0;
213}
214
216 return IsString(source_mapping_url()) &&
217 Cast<String>(source_mapping_url())->length() != 0;
218}
219
221 // TODO(v8:12051): A more robust detection, e.g. with a dedicated sentinel
222 // value.
223 return IsUndefined(source(), isolate) ||
224 Cast<String>(source())->length() == 0;
225}
226
229 Tagged<Script> origin_script = *this;
230 while (origin_script->has_eval_from_shared()) {
231 Tagged<HeapObject> maybe_script =
232 origin_script->eval_from_shared()->script();
233 CHECK(IsScript(maybe_script));
234 origin_script = Cast<Script>(maybe_script);
235 }
236 return origin_script;
237}
238
239} // namespace internal
240} // namespace v8
241
243
244#endif // V8_OBJECTS_SCRIPT_INL_H_
void set_origin_options(ScriptOriginOptions origin_options)
Definition script-inl.h:172
bool CanHaveLineEnds() const
Definition script-inl.h:192
CompilationState compilation_state()
Definition script-inl.h:142
void set_is_repl_mode(bool value)
Definition script-inl.h:165
void set_deserialized(bool value)
Definition script-inl.h:159
static void InitLineEnds(Isolate *isolate, DirectHandle< Script > script)
Definition script-inl.h:201
bool produce_compile_hints() const
Definition script-inl.h:149
CompilationType compilation_type() const
Definition script-inl.h:136
void set_produce_compile_hints(bool produce_compile_hints)
Definition script-inl.h:153
bool IsMaybeUnfinalized(Isolate *isolate) const
Definition script-inl.h:220
bool has_line_ends() const
Definition script-inl.h:190
bool HasSourceURLComment() const
Definition script-inl.h:211
bool is_wrapped() const
static void V8_PRESERVE_MOST InitLineEndsInternal(IsolateT *isolate, DirectHandle< Script > script)
bool deserialized() const
Definition script-inl.h:157
void set_flags(uint32_t new_flags)
Definition script-inl.h:131
v8::ScriptOriginOptions origin_options()
Definition script-inl.h:169
uint32_t flags() const
Definition script-inl.h:124
void set_compilation_type(CompilationType type)
Definition script-inl.h:139
bool HasSourceMappingURLComment() const
Definition script-inl.h:215
void set_compilation_state(CompilationState state)
Definition script-inl.h:145
bool has_eval_from_shared() const
bool is_repl_mode() const
Definition script-inl.h:163
Tagged< Script > GetEvalOrigin()
Definition script-inl.h:227
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static constexpr Tagged< Smi > zero()
Definition smi.h:99
static PtrType load(Tagged< HeapObject > host, int offset=0)
static void Relaxed_Store(Tagged< HeapObject > host, PtrType value)
static PtrType Relaxed_Load(Tagged< HeapObject > host, int offset=0)
static void store(Tagged< HeapObject > host, PtrType value)
InstructionOperand source
ReadOnlyRoots GetReadOnlyRoots()
Definition roots-inl.h:86
Tagged(T object) -> Tagged< T >
kStaticElementsTemplateOffset kInstancePropertiesTemplateOffset Tagged< FixedArray >
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define SMI_ACCESSORS_CHECKED(holder, name, offset, condition)
#define ACCESSORS(holder, name, type, offset)
#define TQ_OBJECT_CONSTRUCTORS_IMPL(Type)
#define CONDITIONAL_WRITE_BARRIER(object, offset, value, mode)
#define ACCESSORS_CHECKED(holder, name, type, offset, condition)
#define NEVER_READ_ONLY_SPACE_IMPL(Type)
#define DEF_GETTER(Camel, Lower, Bit)
#define CHECK_SCRIPT_NOT_WASM
Definition script-inl.h:39
#define CHECK(condition)
Definition logging.h:124
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
wasm::ValueType type