script-inl.h 4.58 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_SCRIPT_INL_H_
#define V8_OBJECTS_SCRIPT_INL_H_

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 15 16 17 18 19

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

20
TQ_OBJECT_CONSTRUCTORS_IMPL(Script)
21 22 23

NEVER_READ_ONLY_SPACE_IMPL(Script)

24
SMI_ACCESSORS(Script, type, kScriptTypeOffset)
25 26
ACCESSORS_CHECKED(Script, eval_from_shared_or_wrapped_arguments, Object,
                  kEvalFromSharedOrWrappedArgumentsOffset,
27 28 29
                  this->type() != TYPE_WASM)
SMI_ACCESSORS_CHECKED(Script, eval_from_position, kEvalFromPositionOffset,
                      this->type() != TYPE_WASM)
30
ACCESSORS_CHECKED(Script, wasm_breakpoint_infos, FixedArray,
31
                  kEvalFromSharedOrWrappedArgumentsOffset,
32
                  this->type() == TYPE_WASM)
33 34
ACCESSORS_CHECKED(Script, wasm_managed_native_module, Object,
                  kEvalFromPositionOffset, this->type() == TYPE_WASM)
35 36
ACCESSORS_CHECKED(Script, wasm_weak_instance_list, WeakArrayList,
                  kSharedFunctionInfosOffset, this->type() == TYPE_WASM)
37

38
bool Script::is_wrapped() const {
39
  return eval_from_shared_or_wrapped_arguments().IsFixedArray();
40 41 42
}

bool Script::has_eval_from_shared() const {
43
  return eval_from_shared_or_wrapped_arguments().IsSharedFunctionInfo();
44 45
}

46
void Script::set_eval_from_shared(SharedFunctionInfo shared,
47 48 49 50 51
                                  WriteBarrierMode mode) {
  DCHECK(!is_wrapped());
  set_eval_from_shared_or_wrapped_arguments(shared, mode);
}

52
SharedFunctionInfo Script::eval_from_shared() const {
53 54 55 56
  DCHECK(has_eval_from_shared());
  return SharedFunctionInfo::cast(eval_from_shared_or_wrapped_arguments());
}

57
void Script::set_wrapped_arguments(FixedArray value, WriteBarrierMode mode) {
58 59 60 61
  DCHECK(!has_eval_from_shared());
  set_eval_from_shared_or_wrapped_arguments(value, mode);
}

62
FixedArray Script::wrapped_arguments() const {
63 64 65 66
  DCHECK(is_wrapped());
  return FixedArray::cast(eval_from_shared_or_wrapped_arguments());
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80
DEF_GETTER(Script, shared_function_infos, WeakFixedArray) {
  return type() == TYPE_WASM
             ? ReadOnlyRoots(GetHeap()).empty_weak_fixed_array()
             : TaggedField<WeakFixedArray, kSharedFunctionInfosOffset>::load(
                   *this);
}

void Script::set_shared_function_infos(WeakFixedArray value,
                                       WriteBarrierMode mode) {
  DCHECK_NE(TYPE_WASM, type());
  TaggedField<WeakFixedArray, kSharedFunctionInfosOffset>::store(*this, value);
  CONDITIONAL_WRITE_BARRIER(*this, kSharedFunctionInfosOffset, value, mode);
}

81 82 83 84
bool Script::has_wasm_breakpoint_infos() const {
  return type() == TYPE_WASM && wasm_breakpoint_infos().length() > 0;
}

85 86 87 88
wasm::NativeModule* Script::wasm_native_module() const {
  return Managed<wasm::NativeModule>::cast(wasm_managed_native_module()).raw();
}

89
Script::CompilationType Script::compilation_type() {
90
  return CompilationTypeBit::decode(flags());
91 92
}
void Script::set_compilation_type(CompilationType type) {
93
  set_flags(CompilationTypeBit::update(flags(), type));
94 95
}
Script::CompilationState Script::compilation_state() {
96
  return CompilationStateBit::decode(flags());
97 98
}
void Script::set_compilation_state(CompilationState state) {
99
  set_flags(CompilationStateBit::update(flags(), state));
100
}
Simon Zünd's avatar
Simon Zünd committed
101

102
bool Script::is_repl_mode() const { return IsReplModeBit::decode(flags()); }
Simon Zünd's avatar
Simon Zünd committed
103 104

void Script::set_is_repl_mode(bool value) {
105
  set_flags(IsReplModeBit::update(flags(), value));
Simon Zünd's avatar
Simon Zünd committed
106 107
}

108
ScriptOriginOptions Script::origin_options() {
109
  return ScriptOriginOptions(OriginOptionsBits::decode(flags()));
110 111
}
void Script::set_origin_options(ScriptOriginOptions origin_options) {
112 113
  DCHECK(!(origin_options.Flags() & ~((1 << OriginOptionsBits::kSize) - 1)));
  set_flags(OriginOptionsBits::update(flags(), origin_options.Flags()));
114 115 116
}

bool Script::HasValidSource() {
117
  Object src = this->source();
118
  if (!src.IsString()) return true;
119
  String src_str = String::cast(src);
120
  if (!StringShape(src_str).IsExternal()) return true;
121 122 123 124
  if (src_str.IsOneByteRepresentation()) {
    return ExternalOneByteString::cast(src).resource() != nullptr;
  } else if (src_str.IsTwoByteRepresentation()) {
    return ExternalTwoByteString::cast(src).resource() != nullptr;
125 126 127 128 129 130 131 132 133 134
  }
  return true;
}

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_SCRIPT_INL_H_