• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/objects/managed.h"
9 #include "src/objects/script.h"
10 #include "src/objects/shared-function-info.h"
11 #include "src/objects/smi-inl.h"
12 #include "src/objects/string-inl.h"
13 
14 // Has to be the last include (doesn't have include guards):
15 #include "src/objects/object-macros.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 #include "torque-generated/src/objects/script-tq-inl.inc"
21 
22 TQ_OBJECT_CONSTRUCTORS_IMPL(Script)
23 
NEVER_READ_ONLY_SPACE_IMPL(Script)24 NEVER_READ_ONLY_SPACE_IMPL(Script)
25 
26 SMI_ACCESSORS(Script, type, kScriptTypeOffset)
27 ACCESSORS_CHECKED(Script, eval_from_shared_or_wrapped_arguments, Object,
28                   kEvalFromSharedOrWrappedArgumentsOffset,
29                   this->type() != TYPE_WASM)
30 SMI_ACCESSORS_CHECKED(Script, eval_from_position, kEvalFromPositionOffset,
31                       this->type() != TYPE_WASM)
32 ACCESSORS_CHECKED(Script, wasm_breakpoint_infos, FixedArray,
33                   kEvalFromSharedOrWrappedArgumentsOffset,
34                   this->type() == TYPE_WASM)
35 ACCESSORS_CHECKED(Script, wasm_managed_native_module, Object,
36                   kEvalFromPositionOffset, this->type() == TYPE_WASM)
37 ACCESSORS_CHECKED(Script, wasm_weak_instance_list, WeakArrayList,
38                   kSharedFunctionInfosOffset, this->type() == TYPE_WASM)
39 
40 bool Script::is_wrapped() const {
41   return eval_from_shared_or_wrapped_arguments().IsFixedArray();
42 }
43 
has_eval_from_shared()44 bool Script::has_eval_from_shared() const {
45   return eval_from_shared_or_wrapped_arguments().IsSharedFunctionInfo();
46 }
47 
set_eval_from_shared(SharedFunctionInfo shared,WriteBarrierMode mode)48 void Script::set_eval_from_shared(SharedFunctionInfo shared,
49                                   WriteBarrierMode mode) {
50   DCHECK(!is_wrapped());
51   set_eval_from_shared_or_wrapped_arguments(shared, mode);
52 }
53 
eval_from_shared()54 SharedFunctionInfo Script::eval_from_shared() const {
55   DCHECK(has_eval_from_shared());
56   return SharedFunctionInfo::cast(eval_from_shared_or_wrapped_arguments());
57 }
58 
set_wrapped_arguments(FixedArray value,WriteBarrierMode mode)59 void Script::set_wrapped_arguments(FixedArray value, WriteBarrierMode mode) {
60   DCHECK(!has_eval_from_shared());
61   set_eval_from_shared_or_wrapped_arguments(value, mode);
62 }
63 
wrapped_arguments()64 FixedArray Script::wrapped_arguments() const {
65   DCHECK(is_wrapped());
66   return FixedArray::cast(eval_from_shared_or_wrapped_arguments());
67 }
68 
DEF_GETTER(Script,shared_function_infos,WeakFixedArray)69 DEF_GETTER(Script, shared_function_infos, WeakFixedArray) {
70   return type() == TYPE_WASM
71              ? ReadOnlyRoots(GetHeap()).empty_weak_fixed_array()
72              : TaggedField<WeakFixedArray, kSharedFunctionInfosOffset>::load(
73                    *this);
74 }
75 
set_shared_function_infos(WeakFixedArray value,WriteBarrierMode mode)76 void Script::set_shared_function_infos(WeakFixedArray value,
77                                        WriteBarrierMode mode) {
78   DCHECK_NE(TYPE_WASM, type());
79   TaggedField<WeakFixedArray, kSharedFunctionInfosOffset>::store(*this, value);
80   CONDITIONAL_WRITE_BARRIER(*this, kSharedFunctionInfosOffset, value, mode);
81 }
82 
has_wasm_breakpoint_infos()83 bool Script::has_wasm_breakpoint_infos() const {
84   return type() == TYPE_WASM && wasm_breakpoint_infos().length() > 0;
85 }
86 
wasm_native_module()87 wasm::NativeModule* Script::wasm_native_module() const {
88   return Managed<wasm::NativeModule>::cast(wasm_managed_native_module()).raw();
89 }
90 
compilation_type()91 Script::CompilationType Script::compilation_type() {
92   return CompilationTypeBit::decode(flags());
93 }
set_compilation_type(CompilationType type)94 void Script::set_compilation_type(CompilationType type) {
95   set_flags(CompilationTypeBit::update(flags(), type));
96 }
compilation_state()97 Script::CompilationState Script::compilation_state() {
98   return CompilationStateBit::decode(flags());
99 }
set_compilation_state(CompilationState state)100 void Script::set_compilation_state(CompilationState state) {
101   set_flags(CompilationStateBit::update(flags(), state));
102 }
103 
is_repl_mode()104 bool Script::is_repl_mode() const { return IsReplModeBit::decode(flags()); }
105 
set_is_repl_mode(bool value)106 void Script::set_is_repl_mode(bool value) {
107   set_flags(IsReplModeBit::update(flags(), value));
108 }
109 
origin_options()110 ScriptOriginOptions Script::origin_options() {
111   return ScriptOriginOptions(OriginOptionsBits::decode(flags()));
112 }
set_origin_options(ScriptOriginOptions origin_options)113 void Script::set_origin_options(ScriptOriginOptions origin_options) {
114   DCHECK(!(origin_options.Flags() & ~((1 << OriginOptionsBits::kSize) - 1)));
115   set_flags(OriginOptionsBits::update(flags(), origin_options.Flags()));
116 }
117 
HasValidSource()118 bool Script::HasValidSource() {
119   Object src = this->source();
120   if (!src.IsString()) return true;
121   String src_str = String::cast(src);
122   if (!StringShape(src_str).IsExternal()) return true;
123   if (src_str.IsOneByteRepresentation()) {
124     return ExternalOneByteString::cast(src).resource() != nullptr;
125   } else if (src_str.IsTwoByteRepresentation()) {
126     return ExternalTwoByteString::cast(src).resource() != nullptr;
127   }
128   return true;
129 }
130 
131 }  // namespace internal
132 }  // namespace v8
133 
134 #include "src/objects/object-macros-undef.h"
135 
136 #endif  // V8_OBJECTS_SCRIPT_INL_H_
137