module-inl.h 5.27 KB
Newer Older
1 2 3 4 5 6 7 8
// 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_MODULE_INL_H_
#define V8_OBJECTS_MODULE_INL_H_

#include "src/objects/module.h"
9
#include "src/objects/objects-inl.h"  // Needed for write barriers
10
#include "src/objects/scope-info.h"
11 12
#include "src/objects/source-text-module-inl.h"
#include "src/objects/source-text-module.h"
13
#include "src/objects/string-inl.h"
14
#include "src/objects/synthetic-module.h"
15 16 17 18 19 20 21

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

namespace v8 {
namespace internal {

22 23
#include "torque-generated/src/objects/module-tq-inl.inc"

24
OBJECT_CONSTRUCTORS_IMPL(Module, HeapObject)
25
TQ_OBJECT_CONSTRUCTORS_IMPL(JSModuleNamespace)
26

27
NEVER_READ_ONLY_SPACE_IMPL(Module)
28
NEVER_READ_ONLY_SPACE_IMPL(ModuleRequest)
29
NEVER_READ_ONLY_SPACE_IMPL(SourceTextModule)
30
NEVER_READ_ONLY_SPACE_IMPL(SyntheticModule)
31

32 33 34
CAST_ACCESSOR(Module)
ACCESSORS(Module, exports, ObjectHashTable, kExportsOffset)
ACCESSORS(Module, module_namespace, HeapObject, kModuleNamespaceOffset)
35 36 37 38
ACCESSORS(Module, exception, Object, kExceptionOffset)
SMI_ACCESSORS(Module, status, kStatusOffset)
SMI_ACCESSORS(Module, hash, kHashOffset)

39 40 41
BOOL_ACCESSORS(SourceTextModule, flags, async, AsyncBit::kShift)
BOOL_ACCESSORS(SourceTextModule, flags, async_evaluating,
               AsyncEvaluatingBit::kShift)
42 43 44 45
ACCESSORS(SourceTextModule, async_parent_modules, ArrayList,
          kAsyncParentModulesOffset)
ACCESSORS(SourceTextModule, top_level_capability, HeapObject,
          kTopLevelCapabilityOffset)
46

47 48 49 50 51 52
struct Module::Hash {
  V8_INLINE size_t operator()(Module const& module) const {
    return module.hash();
  }
};

53
SourceTextModuleInfo SourceTextModule::info() const {
54
  return status() == kErrored
55
             ? SourceTextModuleInfo::cast(code())
56
             : GetSharedFunctionInfo().scope_info().ModuleDescriptorInfo();
57 58
}

59 60
OBJECT_CONSTRUCTORS_IMPL(SourceTextModuleInfo, FixedArray)
CAST_ACCESSOR(SourceTextModuleInfo)
61

62
FixedArray SourceTextModuleInfo::module_requests() const {
63 64 65
  return FixedArray::cast(get(kModuleRequestsIndex));
}

66
FixedArray SourceTextModuleInfo::special_exports() const {
67 68 69
  return FixedArray::cast(get(kSpecialExportsIndex));
}

70
FixedArray SourceTextModuleInfo::regular_exports() const {
71 72 73
  return FixedArray::cast(get(kRegularExportsIndex));
}

74
FixedArray SourceTextModuleInfo::regular_imports() const {
75 76 77
  return FixedArray::cast(get(kRegularImportsIndex));
}

78
FixedArray SourceTextModuleInfo::namespace_imports() const {
79 80 81
  return FixedArray::cast(get(kNamespaceImportsIndex));
}

82
FixedArray SourceTextModuleInfo::module_request_positions() const {
83 84 85 86
  return FixedArray::cast(get(kModuleRequestPositionsIndex));
}

#ifdef DEBUG
87
bool SourceTextModuleInfo::Equals(SourceTextModuleInfo other) const {
88 89 90 91 92 93
  return regular_exports() == other.regular_exports() &&
         regular_imports() == other.regular_imports() &&
         special_exports() == other.special_exports() &&
         namespace_imports() == other.namespace_imports() &&
         module_requests() == other.module_requests() &&
         module_request_positions() == other.module_request_positions();
94 95 96
}
#endif

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
struct ModuleHandleHash {
  V8_INLINE size_t operator()(Handle<Module> module) const {
    return module->hash();
  }
};

struct ModuleHandleEqual {
  V8_INLINE bool operator()(Handle<Module> lhs, Handle<Module> rhs) const {
    return *lhs == *rhs;
  }
};

class UnorderedModuleSet
    : public std::unordered_set<Handle<Module>, ModuleHandleHash,
                                ModuleHandleEqual,
                                ZoneAllocator<Handle<Module>>> {
 public:
  explicit UnorderedModuleSet(Zone* zone)
      : std::unordered_set<Handle<Module>, ModuleHandleHash, ModuleHandleEqual,
                           ZoneAllocator<Handle<Module>>>(
            2 /* bucket count */, ModuleHandleHash(), ModuleHandleEqual(),
            ZoneAllocator<Handle<Module>>(zone)) {}
};

121
void SourceTextModule::AddAsyncParentModule(Isolate* isolate,
Georg Neis's avatar
Georg Neis committed
122 123 124 125
                                            Handle<SourceTextModule> module,
                                            Handle<SourceTextModule> parent) {
  Handle<ArrayList> async_parent_modules(module->async_parent_modules(),
                                         isolate);
126
  Handle<ArrayList> new_array_list =
Georg Neis's avatar
Georg Neis committed
127 128
      ArrayList::Add(isolate, async_parent_modules, parent);
  module->set_async_parent_modules(*new_array_list);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

Handle<SourceTextModule> SourceTextModule::GetAsyncParentModule(
    Isolate* isolate, int index) {
  Handle<SourceTextModule> module(
      SourceTextModule::cast(async_parent_modules().Get(index)), isolate);
  return module;
}

int SourceTextModule::AsyncParentModuleCount() {
  return async_parent_modules().Length();
}

bool SourceTextModule::HasPendingAsyncDependencies() {
  DCHECK_GE(pending_async_dependencies(), 0);
  return pending_async_dependencies() > 0;
}

void SourceTextModule::IncrementPendingAsyncDependencies() {
  set_pending_async_dependencies(pending_async_dependencies() + 1);
}

void SourceTextModule::DecrementPendingAsyncDependencies() {
  set_pending_async_dependencies(pending_async_dependencies() - 1);
}

155 156 157 158 159 160
}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_MODULE_INL_H_