bootstrapper.h 5.75 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

10 11
namespace v8 {
namespace internal {
12

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

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

  void Iterate(ObjectVisitor* v) {
27
    v->VisitPointer(bit_cast<Object**, FixedArray**>(&cache_));
28 29 30 31
  }

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

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

 private:
59 60 61
  Script::Type type_;
  FixedArray* cache_;
  DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
62 63
};

64
enum ContextType { FULL_CONTEXT, THIN_CONTEXT, DEBUG_CONTEXT };
65

66 67
// The Boostrapper is the public interface for creating a JavaScript global
// context.
68
class Bootstrapper final {
69
 public:
70
  static void InitializeOncePerProcess();
71
  static void TearDownExtensions();
72

73
  // Requires: Heap::SetUp has been called.
74 75
  void Initialize(bool create_heap_objects);
  void TearDown();
76 77 78

  // Creates a JavaScript Global Context with initial object graph.
  // The returned value is a global handle casted to V8Environment*.
79
  Handle<Context> CreateEnvironment(
80
      MaybeHandle<JSGlobalProxy> maybe_global_proxy,
81
      v8::Local<v8::ObjectTemplate> global_object_template,
82 83 84 85
      v8::ExtensionConfiguration* extensions,
      ContextType context_type = FULL_CONTEXT);

  bool CreateCodeStubContext(Isolate* isolate);
86

87
  // Detach the environment from its outer global object.
88
  void DetachGlobal(Handle<Context> env);
89

90
  // Traverses the pointers for memory management.
91
  void Iterate(ObjectVisitor* v);
92

93
  // Accessor for the native scripts source code.
94 95
  template <class Source>
  Handle<String> SourceLookup(int index);
96

97
  // Tells whether bootstrapping is active.
98
  bool IsActive() const { return nesting_ != 0; }
99

100
  // Support for thread preemption.
101
  static int ArchiveSpacePerThread();
102 103 104
  char* ArchiveState(char* to);
  char* RestoreState(char* from);
  void FreeThreadResources();
105

106
  // Used for new context creation.
107
  bool InstallExtensions(Handle<Context> native_context,
108 109 110 111
                         v8::ExtensionConfiguration* extensions);

  SourceCodeCache* extensions_cache() { return &extensions_cache_; }

112
  static bool CompileNative(Isolate* isolate, Vector<const char> name,
yangguo's avatar
yangguo committed
113 114
                            Handle<String> source, int argc,
                            Handle<Object> argv[]);
115 116 117
  static bool CompileBuiltin(Isolate* isolate, int index);
  static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
  static bool CompileExtraBuiltin(Isolate* isolate, int index);
118
  static bool CompileExperimentalExtraBuiltin(Isolate* isolate, int index);
119 120 121
  static bool CompileCodeStubBuiltin(Isolate* isolate, int index);
  static bool InstallCodeStubNatives(Isolate* isolate);

122 123 124
  static void ExportFromRuntime(Isolate* isolate, Handle<JSObject> container);
  static void ExportExperimentalFromRuntime(Isolate* isolate,
                                            Handle<JSObject> container);
125

126
 private:
127
  Isolate* isolate_;
128 129 130 131 132 133 134 135
  typedef int NestingCounterType;
  NestingCounterType nesting_;
  SourceCodeCache extensions_cache_;

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

136
  explicit Bootstrapper(Isolate* isolate);
137

138 139 140 141 142 143
  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_;

144 145 146 147
  DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
};


148
class BootstrapperActive final BASE_EMBEDDED {
149
 public:
150 151 152
  explicit BootstrapperActive(Bootstrapper* bootstrapper)
      : bootstrapper_(bootstrapper) {
    ++bootstrapper_->nesting_;
153 154 155
  }

  ~BootstrapperActive() {
156
    --bootstrapper_->nesting_;
157 158 159
  }

 private:
160 161
  Bootstrapper* bootstrapper_;

162
  DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
163 164
};

165

166
class NativesExternalStringResource final
167
    : public v8::String::ExternalOneByteStringResource {
168
 public:
169 170
  NativesExternalStringResource(const char* source, size_t length)
      : data_(source), length_(length) {}
171 172
  const char* data() const override { return data_; }
  size_t length() const override { return length_; }
173 174 175 176 177 178

 private:
  const char* data_;
  size_t length_;
};

179 180
}  // namespace internal
}  // namespace v8
181 182

#endif  // V8_BOOTSTRAPPER_H_