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/script.h" 9 10 #include "src/objects/shared-function-info.h" 11 #include "src/objects/string-inl.h" 12 13 // Has to be the last include (doesn't have include guards): 14 #include "src/objects/object-macros.h" 15 16 namespace v8 { 17 namespace internal { 18 19 CAST_ACCESSOR(Script) 20 21 ACCESSORS(Script, source, Object, kSourceOffset) 22 ACCESSORS(Script, name, Object, kNameOffset) 23 SMI_ACCESSORS(Script, id, kIdOffset) 24 SMI_ACCESSORS(Script, line_offset, kLineOffsetOffset) 25 SMI_ACCESSORS(Script, column_offset, kColumnOffsetOffset) 26 ACCESSORS(Script, context_data, Object, kContextOffset) 27 SMI_ACCESSORS(Script, type, kTypeOffset) 28 ACCESSORS(Script, line_ends, Object, kLineEndsOffset) 29 ACCESSORS_CHECKED(Script, eval_from_shared_or_wrapped_arguments, Object, 30 kEvalFromSharedOrWrappedArgumentsOffset, 31 this->type() != TYPE_WASM) 32 SMI_ACCESSORS_CHECKED(Script, eval_from_position, kEvalFromPositionOffset, 33 this->type() != TYPE_WASM) 34 ACCESSORS(Script, shared_function_infos, WeakFixedArray, 35 kSharedFunctionInfosOffset) 36 SMI_ACCESSORS(Script, flags, kFlagsOffset) 37 ACCESSORS(Script, source_url, Object, kSourceUrlOffset) 38 ACCESSORS(Script, source_mapping_url, Object, kSourceMappingUrlOffset) 39 ACCESSORS(Script, host_defined_options, FixedArray, kHostDefinedOptionsOffset) 40 ACCESSORS_CHECKED(Script, wasm_module_object, Object, 41 kEvalFromSharedOrWrappedArgumentsOffset, 42 this->type() == TYPE_WASM) 43 44 bool Script::is_wrapped() const { 45 return eval_from_shared_or_wrapped_arguments()->IsFixedArray(); 46 } 47 48 bool Script::has_eval_from_shared() const { 49 return eval_from_shared_or_wrapped_arguments()->IsSharedFunctionInfo(); 50 } 51 52 void Script::set_eval_from_shared(SharedFunctionInfo* shared, 53 WriteBarrierMode mode) { 54 DCHECK(!is_wrapped()); 55 set_eval_from_shared_or_wrapped_arguments(shared, mode); 56 } 57 58 SharedFunctionInfo* Script::eval_from_shared() const { 59 DCHECK(has_eval_from_shared()); 60 return SharedFunctionInfo::cast(eval_from_shared_or_wrapped_arguments()); 61 } 62 63 void Script::set_wrapped_arguments(FixedArray* value, WriteBarrierMode mode) { 64 DCHECK(!has_eval_from_shared()); 65 set_eval_from_shared_or_wrapped_arguments(value, mode); 66 } 67 68 FixedArray* Script::wrapped_arguments() const { 69 DCHECK(is_wrapped()); 70 return FixedArray::cast(eval_from_shared_or_wrapped_arguments()); 71 } 72 73 Script::CompilationType Script::compilation_type() { 74 return BooleanBit::get(flags(), kCompilationTypeBit) ? COMPILATION_TYPE_EVAL 75 : COMPILATION_TYPE_HOST; 76 } 77 void Script::set_compilation_type(CompilationType type) { 78 set_flags(BooleanBit::set(flags(), kCompilationTypeBit, 79 type == COMPILATION_TYPE_EVAL)); 80 } 81 Script::CompilationState Script::compilation_state() { 82 return BooleanBit::get(flags(), kCompilationStateBit) 83 ? COMPILATION_STATE_COMPILED 84 : COMPILATION_STATE_INITIAL; 85 } 86 void Script::set_compilation_state(CompilationState state) { 87 set_flags(BooleanBit::set(flags(), kCompilationStateBit, 88 state == COMPILATION_STATE_COMPILED)); 89 } 90 ScriptOriginOptions Script::origin_options() { 91 return ScriptOriginOptions((flags() & kOriginOptionsMask) >> 92 kOriginOptionsShift); 93 } 94 void Script::set_origin_options(ScriptOriginOptions origin_options) { 95 DCHECK(!(origin_options.Flags() & ~((1 << kOriginOptionsSize) - 1))); 96 set_flags((flags() & ~kOriginOptionsMask) | 97 (origin_options.Flags() << kOriginOptionsShift)); 98 } 99 100 bool Script::HasValidSource() { 101 Object* src = this->source(); 102 if (!src->IsString()) return true; 103 String* src_str = String::cast(src); 104 if (!StringShape(src_str).IsExternal()) return true; 105 if (src_str->IsOneByteRepresentation()) { 106 return ExternalOneByteString::cast(src)->resource() != nullptr; 107 } else if (src_str->IsTwoByteRepresentation()) { 108 return ExternalTwoByteString::cast(src)->resource() != nullptr; 109 } 110 return true; 111 } 112 113 } // namespace internal 114 } // namespace v8 115 116 #include "src/objects/object-macros-undef.h" 117 118 #endif // V8_OBJECTS_SCRIPT_INL_H_ 119