bootstrapper.h 5.54 KB
Newer Older
1 2 3
// Copyright 2014 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.
4 5 6 7

#ifndef V8_BOOTSTRAPPER_H_
#define V8_BOOTSTRAPPER_H_

8
#include "src/factory.h"
9
#include "src/objects/shared-function-info.h"
10
#include "src/snapshot/natives.h"
11
#include "src/visitors.h"
12

13 14
namespace v8 {
namespace internal {
15

16
// A SourceCodeCache uses a FixedArray to store pairs of
17
// (OneByteString*, JSFunction*), mapping names of native code files
18
// (array.js, etc.) to precompiled functions. Instead of mapping
19 20
// names to functions it might make sense to let the JS2C tool
// generate an index for each native JS file.
21
class SourceCodeCache final BASE_EMBEDDED {
22
 public:
23
  explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
24

25 26
  void Initialize(Isolate* isolate, bool create_heap_objects) {
    cache_ = create_heap_objects ? isolate->heap()->empty_fixed_array() : NULL;
27 28
  }

29 30 31
  void Iterate(RootVisitor* v) {
    v->VisitRootPointer(Root::kExtensions,
                        bit_cast<Object**, FixedArray**>(&cache_));
32 33 34 35
  }

  bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
    for (int i = 0; i < cache_->length(); i+=2) {
36
      SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i));
37
      if (str->IsUtf8EqualTo(name)) {
38 39 40 41 42 43 44 45 46
        *handle = Handle<SharedFunctionInfo>(
            SharedFunctionInfo::cast(cache_->get(i + 1)));
        return true;
      }
    }
    return false;
  }

  void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
47 48 49
    Isolate* isolate = shared->GetIsolate();
    Factory* factory = isolate->factory();
    HandleScope scope(isolate);
50
    int length = cache_->length();
51
    Handle<FixedArray> new_array = factory->NewFixedArray(length + 2, TENURED);
52 53
    cache_->CopyTo(0, *new_array, 0, cache_->length());
    cache_ = *new_array;
54
    Handle<String> str =
55 56 57
        factory
            ->NewStringFromOneByte(Vector<const uint8_t>::cast(name), TENURED)
            .ToHandleChecked();
58
    DCHECK(!str.is_null());
59 60
    cache_->set(length, *str);
    cache_->set(length + 1, *shared);
61
    Script::cast(shared->script())->set_type(type_);
62
  }
63 64

 private:
65 66 67
  Script::Type type_;
  FixedArray* cache_;
  DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
68 69
};

70
enum GlobalContextType { FULL_CONTEXT, DEBUG_CONTEXT };
71

72 73
// The Boostrapper is the public interface for creating a JavaScript global
// context.
74
class Bootstrapper final {
75
 public:
76
  static void InitializeOncePerProcess();
77
  static void TearDownExtensions();
78

79
  // Requires: Heap::SetUp has been called.
80 81
  void Initialize(bool create_heap_objects);
  void TearDown();
82 83 84

  // Creates a JavaScript Global Context with initial object graph.
  // The returned value is a global handle casted to V8Environment*.
85
  Handle<Context> CreateEnvironment(
86
      MaybeHandle<JSGlobalProxy> maybe_global_proxy,
87
      v8::Local<v8::ObjectTemplate> global_object_template,
88
      v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
89
      v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer,
90
      GlobalContextType context_type = FULL_CONTEXT);
91

92 93 94 95
  Handle<JSGlobalProxy> NewRemoteContext(
      MaybeHandle<JSGlobalProxy> maybe_global_proxy,
      v8::Local<v8::ObjectTemplate> global_object_template);

96
  // Detach the environment from its outer global object.
97
  void DetachGlobal(Handle<Context> env);
98

99
  // Traverses the pointers for memory management.
100
  void Iterate(RootVisitor* v);
101

102
  // Accessor for the native scripts source code.
103
  Handle<String> GetNativeSource(NativeType type, int index);
104

105
  // Tells whether bootstrapping is active.
106
  bool IsActive() const { return nesting_ != 0; }
107

108
  // Support for thread preemption.
109
  static int ArchiveSpacePerThread();
110 111 112
  char* ArchiveState(char* to);
  char* RestoreState(char* from);
  void FreeThreadResources();
113

114
  // Used for new context creation.
115
  bool InstallExtensions(Handle<Context> native_context,
116 117 118 119
                         v8::ExtensionConfiguration* extensions);

  SourceCodeCache* extensions_cache() { return &extensions_cache_; }

120
  static bool CompileNative(Isolate* isolate, Vector<const char> name,
yangguo's avatar
yangguo committed
121
                            Handle<String> source, int argc,
122
                            Handle<Object> argv[], NativesFlag natives_flag);
123 124
  static bool CompileBuiltin(Isolate* isolate, int index);
  static bool CompileExtraBuiltin(Isolate* isolate, int index);
125
  static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
126

127
  static void ExportFromRuntime(Isolate* isolate, Handle<JSObject> container);
128

129
 private:
130
  Isolate* isolate_;
131 132 133 134 135 136 137 138
  typedef int NestingCounterType;
  NestingCounterType nesting_;
  SourceCodeCache extensions_cache_;

  friend class BootstrapperActive;
  friend class Isolate;
  friend class NativesExternalStringResource;

139
  explicit Bootstrapper(Isolate* isolate);
140

141 142 143 144 145
  static v8::Extension* free_buffer_extension_;
  static v8::Extension* gc_extension_;
  static v8::Extension* externalize_string_extension_;
  static v8::Extension* statistics_extension_;
  static v8::Extension* trigger_failure_extension_;
146
  static v8::Extension* ignition_statistics_extension_;
147

148 149 150 151
  DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
};


152
class BootstrapperActive final BASE_EMBEDDED {
153
 public:
154 155 156
  explicit BootstrapperActive(Bootstrapper* bootstrapper)
      : bootstrapper_(bootstrapper) {
    ++bootstrapper_->nesting_;
157 158 159
  }

  ~BootstrapperActive() {
160
    --bootstrapper_->nesting_;
161 162 163
  }

 private:
164 165
  Bootstrapper* bootstrapper_;

166
  DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
167 168
};

169 170
}  // namespace internal
}  // namespace v8
171 172

#endif  // V8_BOOTSTRAPPER_H_